This question already has an answer here:

The following bash script is expected to echo the current ip of the wan, instead of google "what is my ip".

./ipChange.txt: line 2: +short: command not found “??

ip=dig +short myip.opendns.com @resolver1.opendns.com
echo “$ip”

Any idea how to fix it? Thx

marked as duplicate by Cyrus, Jeff Schaller, techraf, don_crissti, HalosGhost Oct 30 '16 at 23:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



ip=dig +short myip.opendns.com @resolver1.opendns.com


ip=$(dig +short myip.opendns.com @resolver1.opendns.com)
  • Note for completeness: this seems to be IPv4-specific – Celada Oct 30 '16 at 20:38

I have an easier oneliner for you:

curl ipecho.net/plain

If you want it in a variable:

myIp=$(curl ipecho.net/plain)
  • 3
    I think it's better to keep the OP's lightweight DNS-based approach instead of using a relatively more expensive HTTP request. – Celada Oct 30 '16 at 20:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.