I have a curl command I would like to execute in a for loop. For example I wanted to loop 1-100 times and when curl command runs it uses iterator variable value in the curl command itself. something like
#!/bin/bash
for i in {1..10}
do
curl -s -k 'GET' -H 'header info' -b 'stuff' 'http://example.com/id=$i'
done
--notice here I want var i to be changing with every curl.
Anything helps Thanks.
Let us say you want to pass shell variable $name in the data option -d of cURL command. There are two ways to do this. First is to put the whole argument in double quotes so that it is expandable but in this case, we need to escape the double quotes by adding backslash before them.
The curl command transfers data to or from a network server, using one of the supported protocols (HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP or FILE). It is designed to work without user interaction, so it is ideal for use in a shell script.
Try this:
set -B # enable brace expansion
for i in {1..10}; do
curl -s -k 'GET' -H 'header info' -b 'stuff' 'http://example.com/id='$i
done
See: Difference between single and double quotes in Bash
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With