I have some lines like [ 17.440000] eth0: XYZCOMPANY ABC4353 802.11 Wireless Controller

[ 17.540000] eth1: XYZCOMPANY ABC4353 802.11 Wireless Controller

[ 17.640000] eth2: XYZCOMPANY ABC4353 802.11 Wireless Controller

I need to get eth0, eth1, eth2 into a variable in shell script. As you can notice all the other lines like XYZCOMPANY ABC4353 remains constant.but interface name is keep changing. I need to get those interface names into a variable.


  • Thanks. Small doubt..what happens if [ 17.640000] is present only attimes? Because the solution given below always expect ethX to be the third field right? But the time optional one and it may be absent too.. – kumar Jul 19 '10 at 13:14

This will show eth and wlan addresses with any number of digits:

var=$(dmesg | grep -Eo '(eth|wlan)[[:digit:]]+')

If you don't need wlan you can remove the alternate. I'm not sure if the eth driver uses hex, but you can change "digit" to "xdigit" if it does and you need to allow for more than 10 intefaces.

  • Firstly thanks! Is the variable a array ? I need to evaluate interfaces one by one like eth0 , eth1 etc., – kumar Jul 19 '10 at 13:59
  • I can deal with it like an array like this : for i in ${var[@]}; do echo "hi" done – kumar Jul 19 '10 at 14:08
  • @kumar: You can do it without an array: for i in $var; do echo "interface: $i"; done. To make it an array, add a set of parentheses: array=($(dmesg | grep -Eo '(eth|wlan)[[:digit:]]+')). – Paused until further notice. Jul 19 '10 at 15:43

This solution should be working too:

var=$(dmesg | grep "eth[0-9]: [A-Za-z0-9 ]* 802.11 Wireless Controller" | awk '{ print substr($3,0,5) }')


That is to have something like "eth0: eth1: eth2" in your variable

var=`awk /./'{print $3}' lala `
echo $var

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.