get the Primary IP
February 26, 2020
To get the primary IP in Linux:
ip route get 1 | awk '{print $NF;exit}'
In
awk
NF return the number of fields and by suffixing it with$
is like doingprint $7
In macOS, to list all your interfaces:
networksetup -listallhardwareports
If want to get the IP assigned to the Wi-FI:
ipconfig getifaddr $(networksetup -listallhardwareports | awk '/Hardware Port: Wi-Fi/{getline; print $2}')