Bash Shell Command to Find or Get IP address

Originally published at: https://www.cyberciti.biz/faq/bash-shell-command-to-find-get-ip-address/

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:

   #!/bin/bash
   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 10.20.30.40/24. In this case /24 is 255.255.255.0.
Maybe this can help