Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need help deciphering what is being said here:

export IP=`/sbin/ifconfig | grep 'inet addr:'|grep -v ''|
    cut -d: -f2 | awk '{print $1}'`

I know it is to set IP as an environment variable, but other than that, what does everything else mean? Is there a manual on this?

Thank you.

share|improve this question
I have just a suggestion to make. This command works perfectly, but it uses lots of pipes and commands (though it keeps it simple). You could use the power of awk to do this (and narrow it down to one pipe): /sbin/ifconfig | awk '/inet addr:/ { if ($2 !~ /:127./) { split($2, ip, ":") ; print ip[2] } }' – Torian Sep 8 '11 at 3:54
up vote 15 down vote accepted

It's going to list all of the non-loopback IP addresses on the system. Assuming you only have one non-loopback IP address on this box, it's going to export the IP address of the system to the environment variable "IP".

Start by running the pipeline one "section" at a time. Meaning, first run


to see what it does. Then run ...

/sbin/ifconfig | grep 'inet addr:'

and keep going from there.

share|improve this answer
Good idea for a new person to step through the commands like this. +1 – n0pe Sep 8 '11 at 2:49

There's a lot going on. First:

export IP=`...`

means to run the command between backquotes (not single quotes, be careful) and use it's output as the value of variable named "IP". "export" means that any child processes will inherit the variable named "IP" and its value.

The command is a pipeline, stdout of each feeding into stdin of the next. /sbin/ifconfig produces a list of information about network interfaces. All of the lines of output of ifconfig feed into grep 'inet addr:'. Grep just passes lines of text through that at least partially match the first argument, a pattern called a regular expression.

The command grep -v '' only passes lines of text that don't match. is the "loop back" IP address. It's well-known, we don't want it to get into the value of variable named "IP".

The command cut -d: -f2 prints the second field of each line in its input. Fields are delimited by ':' (semicolons). The awk '{print $1}' command just prints the first whitespace delimited column.

As TheDude writes, it puts all non-loopback IP addresses as strings into the value of the shell environment variable IP.

share|improve this answer

Just a note: Since ifconfig typically displays more than one connection (in addition to the loopback connection), this code creates a single string variable with each IP address separated from the next by a newline. Mine looks like this:

bigbird@sananda:~/pq$ export IP=`/sbin/ifconfig | grep 'inet addr:'|\
grep -v ''| cut -d: -f2 | awk '{print $1}'`
bigbird@sananda:~/pq$ echo "[${IP}]"

To actually use this for something, it would probably have to parsed further (splitting the string on newlines).

One way to handle this would be to put the IP address strings into array elements to start with:

export IP=($(/sbin/ifconfig | grep 'inet addr:'|grep -v ''| cut -d: -f2 |\
awk  '{printf "%s ", $1}'))

for (( I=0; I<${#IP[@]}; I++ ))
  echo "${I} [${IP[${I}]}]"

bigbird@sananda:~/bin$ mytest
0 []
1 []
2 []

The added parens () turn the output into array elements. The print has been changed to printf to add a blank (the default bash separator character) after each IP and to eliminate the newlines. The back-tick notation:


for turning the output of a command into a string has also been replaced with newer syntax $(...) .

The for loop is included just to show the results and one way to access them.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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