2
ip a | egrep '([0-9]{1,3}\.){3}[0-9]{1,3}'

Can somebody explain what the above command will do?

1
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.