I'm trying to output my current IP address using a bash script. I'm trying to wrap my head around awk
and managed the following:
/sbin/ifconfig $1 | grep "inet" | awk '$1 == "inet" {gsub(/\/.$/, "", $2); print $2}'
which outputs:
127.0.0.1
192.168.178.57
I have two issues now: First of all, 127.0.0.1
is useless, how do I get rid of it?
Secondly, 192.168.178.57
is the IP address of my Wi-Fi connection. However I'd like the script to be able to grab the IP address of either Wi-Fi or Ethernet, whichever one I'm using at the moment.
A sample output from /sbin/ifconfig
can be found here.
If you don't mind installing something and/or if you already have node, try sindresorhus/internal-ip
Pro: cross-platform
Note: I know it's not bash and that it doesn't use
ifconfig
, but I got here looking for a generic tool to use anywhere, perhaps it can help others.