Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need help deciphering what is being said here:

export IP=`/sbin/ifconfig | grep 'inet addr:'|grep -v '127.0.0.1'|
    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
6  
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
add comment

3 Answers

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

/sbin/ifconfig

to see what it does. Then run ...

/sbin/ifconfig | grep 'inet addr:'

and keep going from there.

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

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 '127.0.0.1'| cut -d: -f2 | awk '{print $1}'`
bigbird@sananda:~/pq$ echo "[${IP}]"
[192.168.126.1
192.168.114.1
192.168.1.2]
bigbird@sananda:~/pq$

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 '127.0.0.1'| cut -d: -f2 |\
awk  '{printf "%s ", $1}'))

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

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

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
add comment

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 '127.0.0.1' only passes lines of text that don't match. 127.0.0.1 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
add comment

Your Answer

 
discard

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.