219

I'm trying to assign the output of cURL into a variable like so:

#!/bin/sh
$IP=`curl automation.whatismyip.com/n09230945.asp`
echo $IP
sed s/IP/$IP/ nsupdate.txt | nsupdate

However, when I run the script the following happens:

./update.sh: 3: =[my ip address]: not found

How can I get the output into $IP correctly?

ghoti
  • 45,319
  • 8
  • 65
  • 104
Alex Bliskovsky
  • 5,973
  • 7
  • 32
  • 41
  • The accepted answer is correct, but there's another minor distinction between that example and what's here: if the $IP var passed to echo is not wrapped in double quotes, it will only output the last line of the captured curl output. – Christopher Hunter Dec 05 '18 at 20:57
  • Thanks @ChristopherHunter, I came here just looking for this. Why does it behave this way though? – Amey May 23 '20 at 08:46
  • 1
    @Amey I can't say exactly what the reasoning was, just that this is how echo behaves when you give it a multi-line string as an argument. – Christopher Hunter Jun 18 '20 at 00:11

2 Answers2

380

In shell, you don't put a $ in front of a variable you're assigning. You only use $IP when you're referring to the variable.

#!/bin/bash

IP=$(curl automation.whatismyip.com/n09230945.asp)

echo "$IP"

sed "s/IP/$IP/" nsupdate.txt | nsupdate
Community
  • 1
  • 1
ghoti
  • 45,319
  • 8
  • 65
  • 104
  • 3
    Is there a way to suppress the output and progress bar of `curl`? Adding `-silent` leaves `$IP` empty... – Dror Jul 14 '14 at 08:30
  • 4
    @Dror, `curl` sends its noisy output to stderr, so the progress bar should be ignored in the case of a script like this. Nevertheless, `--silent` or `-s` works just fine. If you have troubles, please [ask a question](http://stackoverflow.com/questions/ask). – ghoti Jul 14 '14 at 15:46
  • 1
    Use `curl -s` to disable the progress bar and error messages. – Searene Jul 01 '18 at 09:04
  • You can always redirect `stderr`: `IP=$(curl 2>/dev/null)` – BallpointBen Aug 10 '18 at 02:10
  • finding with the $() syntax i'm not able to use env variables e.g. instead of hard coding an email I want to use $2, $3 as variables. Any ideas on why these wont render? – Evan Burbidge Jun 20 '19 at 12:47
  • @EvanBurbidge .. it's not entirely clear what you're asking. Perhaps you could [make a question of it](https://stackoverflow.com/questions/ask) and show a reproducible example of what isn't working for you? – ghoti Jun 21 '19 at 02:37
  • My apologies I was trying to use bash variables instead of hard coding my strings it turns out I had to stop the main string wrapped in the -d '{}' in order to do this ``` AUTH=$(curl -X POST -d '{ "email": "'"$TRANSLATION_EMAIL"'", "password": "'"$TRANSLATION_PASS"'" }' -H "Content-Type: application/json" $LOGIN_URL)``` – Evan Burbidge Jun 21 '19 at 09:46
40

Same with something more complex...getting the ec2 instance region from within the instance.

INSTANCE_REGION=$(curl -s 'http://169.254.169.254/latest/dynamic/instance-identity/document' | python -c "import sys, json; print json.load(sys.stdin)['region']")

echo $INSTANCE_REGION
Tamil Selvan C
  • 19,913
  • 12
  • 49
  • 70
Kostas Demiris
  • 3,415
  • 8
  • 47
  • 85