Bash Shell Command to Find or Get IP address

Originally published at:

How do I find out my Linux / UNIX system ip address, subnet, and related networking information from a bash shell command prompt? How can I determine my private and public IP addresses from the command line?

You can use ip and hostname, like here:

   myip=$(hostname -i)
   mask=$(ip addr | grep $myip | awk '{print $2}')
   echo "My ip:  $myip"
   echo "Mask $mask"

What I do is getting ip address by hostname -i, and then, looking that ip in the output of ip command. Awk print the second field of what found, something like In this case /24 is
Maybe this can help