How to get your IP from a BASH script

For when you have many network interfaces

The short version:

ip addr show $(ip route | awk '/default/ { print $5 }') | grep "inet" | head -n 1 | awk '/inet/ {print $2}' | cut -d'/' -f1
  • We use ip route and awk to determine the name of our default network interface
  • We use ip addr to get the IP details for that…