# Getting the current IP from the command line

I made a tiny little bash script, that prints the current IP Address to the command line. I checked the script with ShellCheck (0.4.6-1) on a local debian install and I get the following message:

SC2026: This word is outside of quotes. Did you intend to 'nest '"'single quotes'"' instead'?

What is wrong with the way I have been line-breaking the pipe, or what is proper practice?

#!/bin/bash

curl -s POST whatismyip.org\
| awk -F '>' '{print \$2}'\
| sed 's/<\/h2//g'\
| sed 's/<\/span//g'

• Interesting... I tried copying and pasting the code into ShellCheck and it reports "No issues detected!"... – Sᴀᴍ Onᴇᴌᴀ Jul 19 '17 at 20:47
• ok, sorry the link in the question was confusing. I have been using shellcheck 0.4.6-1 on a local debian install. (I'll edit my question to make that clear). But if the online version does not detect any issues, it might be rather a ShellCheck thing. – nath Jul 19 '17 at 20:58
• #923. It was fixed in June. – Zeta Jul 19 '17 at 21:05
• Thanks @Zeta, so I need to wait for an upgrade for this annoying messages to disappear, which is much better then editing all my bash-goodies :-) – nath Jul 19 '17 at 21:12
• If you're on a LTS version you probably want to install it via stack, though. – Zeta Jul 19 '17 at 21:24

Your code is fine, except for the stray method. POST at that point isn't valid, and you will try to connect to a server called POST. Just try your curl call with -Iv, you will notice two connections:

curl -Iv POST whatismyip.org


If you want to use the method, you would have to write -x POST, but to just get information, you use -x GET (which is the default when you don't transfer any data). Also, you should specify the protocol:

curl -s http://whatismyip.org


That being said there are other sites such as ipecho.net that provide a direct method:

curl -s -L http://ipecho.net/plain; echo


By the way, whatismyip.org seems to be up for sale, so you might not get your IP in another few months, or the format might change. ipecho.net has a strange whois entry too, but at least its mentioned in a highly voted answer and returns your IP without the need to extract it from the HTML.

• Much thanks for this feedback. The curl -s -L http://ipecho.net/plain; echo is not just simpler, it also is much faster! – nath Jul 19 '17 at 21:35
• @nath you're welcome. Make sure to read man curl to understand -L (follows redirection). – Zeta Jul 20 '17 at 5:00
• You can remove the flags for curl if you use curl icanhazip.com – alexyorke Jul 20 '17 at 11:55

I would use a different service that provides output in a format that is good for tools (not humans (html is for displaying for humans)).

Hit this url:

https://ifconfig.co/json


You're output looks like this:

{
"ip": "154.140.296.288",
"ip_decimal": 926648668,
"country": "United States",
"city": "Seattle",
"hostname": "154-140-296-288.Bob.com"
}


Which is obviously json which can be parsed using the jq tool.

> curl -s https://ifconfig.co/json  | jq .ip -r
154.140.296.188

• That's not a URL; did you mean https://ifconfig.co/json? – Toby Speight Aug 30 '17 at 14:41

Other answers already drilled down the issue with your approach, and also provided couple of alternative resources to query over HTTP(S).

The approaches with HTTP are not future-proof because the domain might be sold, the specific resource might be taken down or deprecated, or worse -- could be compromised altogether resulting in garbaging out the client endpoint.

You would be better off using DNS for getting your public IP, some (big) providers provide you with your public IP when queried for a certain domain on their nameserver(s).

For example, Opendns provides this feature, and you would get desired result while querying for domain myip.opendns.com on one of their resolvers (e.g. resolver1.opendns.com):

% dig +short myip.opendns.com @resolver1.opendns.com
1.2.3.4  ## Obfuscated