Tobias Timpe
Tobias Timpe

Reputation: 770

Use Variable in Command Substitution

I need some help with the following simple bash script, where the variable i does not seem to get substituted when running curl (causing an error).

(This is just a simple abstraction of the actual script)

for i in {1..3}
do
  HTML=$(curl -s 'http://example.com/index.php?id=$i')
done;

Upvotes: 9

Views: 12805

Answers (2)

amadain
amadain

Reputation: 2836

From http://tldp.org/LDP/abs/html/varsubn.html

Enclosing a referenced value in double quotes (" ... ") does not interfere with variable substitution. This is called partial quoting, sometimes referred to as "weak quoting." Using single quotes (' ... ') causes the variable name to be used literally, and no substitution will take place. This is full quoting, sometimes referred to as 'strong quoting.'

A

Upvotes: 1

nosid
nosid

Reputation: 50034

Variables are not substituted within single quotes. You have to use double quotes in this case:

for i in {1..3}; do
    HTML=$( curl -s "http://example.com/index.php?id=$i" )
done

Upvotes: 19

Related Questions