Bash One-liner to get IP address for each network interface


This one should be pretty bulletproof. Use a single ‘sed’ to dump the list of all ip addresses from network interfaces showing in ifconfig.

The Input

$ ifconfig | sed -n ‘/^[A-Za-z0-9]/ {N;/dr:/{;s/.*dr://;s/ .*//;p;}}’
or even simpler with:
$ ifconfig | sed -n ‘s/.*inet addr:\([0-9.]\+\)\s.*/\1/p’
(thanks to Aleksandar)

The Output:

For reference, here’s ifconfig by itself:


Posted on June 15th, by admica in programming, simple.

One thought on “Bash One-liner to get IP address for each network interface

  1. How is this better than something simple like:
    # ifconfig | sed -n ‘s/.*inet addr:\([0-9.]\+\)\s.*/\1/p’