As seen from this question, the following command gives the IP address of the system. ip route get 8.8.8.8 | awk '{print $NF; exit}'
I wanted to make use of this command to create an alias so that I do not have to type such a long command everytime. So, I added the following line into .bashrc
alias ipconfig="ip route get 8.8.8.8 | awk '{print $NF; exit}'"
But I am facing an issue with the awk
command when I use it with the alias (specifically, I think the $NF variable is not set. I have provided three versions of the output using the commandline and the set alias for better understanding.
dennis@dennis-HP:~$ ip route get 8.8.8.8 | awk '{print $NF; exit}'
192.168.0.X
dennis@dennis-HP:~$ ipconfig
8.8.8.8 via 192.168.0.Y dev wlx9d5d8eae8f64 src 192.168.0.X
dennis@dennis-HP:~$ ip route get 8.8.8.8
8.8.8.8 via 192.168.0.Y dev wlx9d5d8eae8f64 src 192.168.0.X
cache
How can I get it to produce the same output?