2

What I want to do it use curl with bash variable to retrieve content length from URL with incrementing parameters.

for ((i=2561;i<=2563;i++))
do 
(curl -i -s -k  -X 'GET' 'http:someurl.asp?q=$i)
done

When I do like this I always get the same content length from HTTP response. Now if I take same curl command and run outside the loop, like this:

curl -i -s -k  -X 'GET' 'http:someurl.asp?q=2562

it works perfectly. I tried both for and while loops, also tried reading from file with cat, but it's always the same content length. What am I missing here?

7
  • You aren't closing the quotes so i don't know how it isn't causing an error. Also it's in single quotes so the parameter isn't expanded, also $2562 will be blank unless you pass 2562 parameters to your script. Commented Jun 30, 2016 at 13:37
  • 1
    You're quoting the url in single-quotes, the $i won't be expanded. Moreover, if you want the result to contain a $, you have to escape it, ie use "http:someurl.asp?q=\$$i". Commented Jun 30, 2016 at 13:41
  • The parentheses around curl are unnecessary. Commented Jun 30, 2016 at 13:48
  • Thanks guys. @Aaron Yes, double quotes indeed fixed that! I spend good hour trying to figure this out. P.s. that $ before number was just a copy mistake. Commented Jun 30, 2016 at 13:49
  • 2
    @123 Actually, $2562 is interpreted as ${2}562 by bash. Commented Jun 30, 2016 at 14:09

2 Answers 2

2

A corrected code would be

for ((i=2561; i<=2563; i++))
do 
    curl -i -s -k  -X GET "http:someurl.asp?q=$i"
done

You need double quotes to allow $1 to be expanded to the current value of i.

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

Comments

0

Try

curl -i -s -k  -X 'GET' "http:someurl.asp?q=$i"

Using Single quotes ie - 'http:someurl.asp?q=$i' usually means

  1. no variable expansion
  2. no command substitution
  3. you get the exact stuff you put inside ''.

Also (process) runs the process in a subshell which is unnecessary here.


Sidenote

The C style forloop sonstruct doesn't require you to precede variables with $. However when using the variables you need precede them $.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.