2

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

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

#!/bin/bash
ip=dig +short myip.opendns.com @resolver1.opendns.com
echo “$ip”

Any idea how to fix it? Thx

Fred J.
  • 285
  • 1
  • 6
  • 17
  • 4
    See: [How to set a variable equal to the output from a command in Bash?](http://stackoverflow.com/q/4651437/3776858) – Cyrus Oct 30 '16 at 20:36

2 Answers2

2

I have an easier oneliner for you:

curl ipecho.net/plain

If you want it in a variable:

myIp=$(curl ipecho.net/plain)
maulinglawns
  • 8,426
  • 2
  • 28
  • 36
  • 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
1

Replace

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

by

ip=$(dig +short myip.opendns.com @resolver1.opendns.com)
Cyrus
  • 12,059
  • 3
  • 29
  • 53