Most of the postings on this thread rely on one or another third party service which may not format its response in a uniform way, may not take kindly to frequent querying or may not exist indefinitely.
For example, curl whatismyip.org may have worked in the past, but it doesn't now. Visiting whatismyip.org with a web browser produces a nice clear visible result, but it uses client-side javascript which neither curl nor wget interpret.
Most other web pages produce a clear result without client-side scripting, but usually produce text that has to be parsed to produce just the IP address to use, say, in a script.
If you have a web site, you could consider creating a PHP page containing just
<? echo $_SERVER["REMOTE_ADDR"]."\n"; ?>
curl
http://your-site/showip.php would then produce just what you need. However, there's a slight risk your site might be overwhelmed if everyone discovered and used it! To avoid that, you'd need to interrogate your router instead. This isn't quite so easy as it depends on your router's manufacturer and model, and I haven't found a good thread for that.