ip a | egrep '([0-9]{1,3}\.){3}[0-9]{1,3}'
Can somebody explain what the above command will do?
ip a | egrep '([0-9]{1,3}\.){3}[0-9]{1,3}'
Can somebody explain what the above command will do?
It first runs the ip
command with the a
argument, which, on Linux, is short-hand for ip address
, which will output several stanzas of several lines corresponding to your network devices and their possible network addresses.
That output is then sent to the egrep
command, which is asked to match (print) lines of its input that match the given regular expression. The regular expression appears intended to match a superset of IPv4 addresses.
The regular expression specifically matches:
(grouped together) - "any single digit between 0 and 9: between 1 and 3 of those, followed by a period" -- and require three items of that group, in sequence,
followed by a single digit between 0 and 9: between 1 and 3 of them.
IP addresses would match this pattern; for example: 1.234.56.7
or 1.1.1.1
, but non-IPv4 addresses would also match (if they showed up in ip a
's output), such as: 999.888.777.666
or even 1.2.3.999
.
grep -E
: https://unix.stackexchange.com/questions/17949/what-is-the-difference-between-grep-egrep-and-fgrep and the differences in basic & extended regular expressions (and more) : https://unix.stackexchange.com/questions/119905/why-does-my-regular-expression-work-in-x-but-not-in-y – Jeff Schaller Jun 25 '18 at 00:36