curl icanhazip.com
or
curl ipecho.net
or
$ dig +short myip.opendns.com @resolver1.opendns.com
How do I store my IP address in a shell variable?
The syntax is:
myip=”$(dig +short myip.opendns.com @resolver1.opendns.com)”
echo “My WAN/Public IP address: ${myip}”
Sample outputs:
My WAN/Public IP address: 74.86.144.194
Refer: https://www.cyberciti.biz/faq/how-to-find-my-public-ip-address-from-command-line-on-a-linux/