1

What I want to do is in order not to hardcode a variable as a string I want to make it a little bit more dynamic.

I want to do something like this:

 export SERVER_IP=45.100.1.100 #this works but it is hardcoding

IP = short myip.opendns.com @resolver1.opendns.com #grabs IP and should save it into a variable
export SERVER_IP = IP #exports IP to an environmental variable

How to make this actually work? Thanks.

funguy
  • 2,072
  • 7
  • 30
  • 41

1 Answers1

1

The command you meant to use is dig (presumably):

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

Now, to save the command's STDOUT in a variable, you need command substitution, $():

ip=$(dig +short myip.opendns.com @resolver1.opendns.com)

Also there must not be any whitespace around = in variable declaration in bash.

As a side note, don't use all uppercase for variable name in bash to prevent accidental overtiring of any environment variable with the same name.

heemayl
  • 39,294
  • 7
  • 70
  • 76
  • So I do something like: export server_ip=$(dig +short myip.opendns.com @resolver1.opendns.com) but then I try to run it I still get: root@vvv:/srv/www/deploy# server_ip server_ip: command not found – funguy Jan 09 '17 at 09:31
  • 1
    @funguy variable is not a command, you can expand it to the value only. Hint: `echo "$server_ip"` – heemayl Jan 09 '17 at 09:35