3

Hi i am trying to use curl in shell script as shown below but i am not able to substitute the variable $line in the CURL . please suggest

while read line
do
    echo "allowing mac $line"
    curl -X POST -d '{"src-mac": "$line"}' http://localhost:8080/wm/firewall/rules/json
    curl -X POST -d '{"dst-mac": "$line"}' http://localhost:8080/wm/firewall/rules/json
done < /home/floodlight/allowedmacs

1 Answer 1

7

No variable substitution in single quotes. Switch to double around the expansion, like this:

curl -X POST -d '{"src-mac": "'"$line"'"}' http://localhost:8080/wm/firewall/rules/json
curl -X POST -d '{"dst-mac": "'"$line"'"}' http://localhost:8080/wm/firewall/rules/json

Or you could use double-quotes around the whole thing and escape the inner ones:

curl -X POST -d "{\"src-mac\": \"$line\"}" http://localhost:8080/wm/firewall/rules/json
curl -X POST -d "{\"dst-mac\": \"$line\"}" http://localhost:8080/wm/firewall/rules/json

Either way, can't be inside single quotes when you get to $line if you want it expanded.

Sign up to request clarification or add additional context in comments.

2 Comments

or swap the double/single quotes: "{'src-mac': '$line'}"
@glennjackman - nope. While a valid literal in javascript code, that's not legal JSON, which requires double quotes around strings.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.