Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run curl command with parameter in a loop from bash script? [duplicate]

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.

like image 643
DoodleKana Avatar asked Sep 25 '15 22:09

DoodleKana


People also ask

How do you pass a variable in a curl command in shell scripting?

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.

Can we use curl command in shell script?

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.


1 Answers

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

like image 111
Cyrus Avatar answered Sep 20 '22 21:09

Cyrus