Get the ip address in a shell script

Using ifconfig was not a good idea at all.
Here is a more distro-agnostic way:

ip addr show eth0 | grep "inet "| awk '{ print $2}' | awk -F '/' '{ print $1 }'


# set the language to English, to avoid translation problems with ifconfig
export LANG=C
# get IP address of eth0 network interface
ifconfig eth0 | awk '/inet addr/ {split ($2,A,":"); print A[2]}'


source

4 thoughts on “Get the ip address in a shell script

  1. Mr Xhark

    Doesn’t work with french distributions.
    Correction : ifconfig eth0 | awk ‘/inet adr/ {split ($2,A,”:”); print A[2]}’

    ‘addr’ => ‘adr’

Leave a Reply