Attempting to shorten a bash script that uses curl to fetch multiple API calls takes something like this:
curl --user $USER:$PASS https://api.example.com/foo -o 'foo.json'
curl --user $USER:$PASS https://api.example.com/bar -o 'bar.json'
curl --user $USER:$PASS https://api.example.com/baz -o 'baz.json'
And use it in this form:
curl --user $USER:$PASS https://api.example.com/{foo,bar,baz} -o '#1.json'
The issue is that curl is fetching foo, bar and baz but is not assigning the output to foo.json, bar.json and baz.json. It is literally creating #1.json and piping output to stdout. Has been tried with single, double, and no quotes, all same result.
This is being run inside a bash script, although the curl command behaves the same way when entered directly on the command line. Is this an OS X syntax issue?
Best Answer
Your problem is that the
{...}
expression is also valid shell syntax. For example, run:And you get:
So when you run:
The
{foo,bar,baz}
is getting interpreted by your shell, andcurl
actually receives the command line:Since
curl
doesn't see the{...}
expression, you don't get the magical handling for#1
. The solution is simply to enclose the URL in single quotes:The single quotes inhibit any shell expansion of the string.