D.W. D.W. - 3 months ago 12x
Linux Question

Get interface name into linux shell script variable

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?

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

save the interface name to variable iname