2

I would like to store an interface name into a variable in linux shell script. I found a solution to do this via "grep" command and the IP-Address. My problem is that my interface doesn't have an IP-Address at this moment.

The interface name always begins with "enx" and the MAC-Address like "enxb2240be99fc3" while the MAC-Address can change every reboot.

Is there a solution to get this interface name into a shell script variable?

2
  • Please be more specific.. Give example inputs, expected outputs etc. Commented Jul 4, 2016 at 11:28
  • Ok, for example: i get 3 interface names with"ls /sys/class/net". One of those names is "enxb2240be99fc3" (MAC-Address changes every reboot). I only want this one extracted into a variable in shell script. Commented Jul 4, 2016 at 11:54

3 Answers 3

6
iname=$(ip -o link show | sed -rn '/^[0-9]+: en/{s/.: ([^:]*):.*/\1/p}')

save the interface name to variable iname

Sign up to request clarification or add additional context in comments.

Comments

2
iname=$(ls /sys/class/net | grep -i enx)
echo $iname

Comments

1

ip addr show | awk '/inet.*brd/{print $NF}'

this is also works,check it...

1 Comment

Tested here and returned all interfaces... Need to append a "| grep -i en" on this command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.