If you are behind a router you can't use ifconfig to find the public IP, but the problem can be solved easily beacause the IP can be obtained from remote servers such as
You only have to choose that with quicker response time. You could even create your own webpage using PHP and
This way you can avoid third party services.
At this point, in the linux shell or bash script you can use curl or a more common wget that's preinstalled on basically every distro of the planet. E.g.
wget -qO- icanhazip.com
This can be useful in many ways… e.g., to make a bash script for dynamic DNS update.
See also Bash: DtDns IP update client