Assign output to variable in Bash

Alex Bliskovsky picture Alex Bliskovsky · Jan 5, 2012 · Viewed 239.2k times · Source

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?

Answer

ghoti picture ghoti · Jan 5, 2012

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