0

I wanted to download a bunch of numbered text files by:

curl https://www.grc.com/sn/sn-{472..807}.txt > sn-{472..807}.txt

but that gave me:

bash: sn-{472..807}.txt: ambiguous redirect

so I ended up doing a:

for iEpisode in {472..807}; do
    curl https://www.grc.com/sn/sn-"$iEpisode.txt" > sn-"$iEpisode.txt"; 
    if [[ $? -eq 1 ]]; then exit; fi;
done

The question is: Is there a way of doing a simple curl with dual brace extension?

(Because it doesn't look ambiguous to me and my Google-fu led nowhere) :/

2
  • 1
    Brace expansions are independent from each other. What do you expect from echo hello > sn-{472..807}.txt? Besides curl https://www.grc.com/sn/sn-{472..807}.txt runs one curl with many arguments. One curl generates exactly one stdout stream you can redirect with >. – Kamil Maciorowski Mar 2 at 10:57
  • @KamilMaciorowski echo hello > sn-{472..807}.txt I expect sn-472.txt...sn-807.txt to contain "hello"? Anyway, @quasimodo already got it and gave me a solution... – Fabby Mar 2 at 11:14
8

That is ambiguous because you are trying to redirect to multiple files. Which would your shell choose?

In general you will need the for loop, but with Curl you can use

curl --remote-name-all https://www.grc.com/sn/sn-{472..807}.txt

That option is like -O (see the manual page),

-O, --remote-name
Write output to a local file named like the remote file we get. (Only the file part of the remote file is used, the path is cut off.)

But applies it to every argument.

Curl also understands shell-like ranges:

curl -O 'https://www.grc.com/sn/sn-[472-807].txt'
0
3

echo hello > sn-{472..807}.txt I expect sn-472.txt...sn-807.txt to contain "hello"?

That would work in Zsh, but not in Bash.

But in any case, there's still just one output stream from curl, so something like

zsh% curl https://www.grc.com/sn/sn-{472..807}.txt > sn-{472..807}.txt

would have curl output each downloaded file to the same stream, from where Zsh would then copy the whole set to each of the output files.

There's really no way for the shell to determine that you want to run the similar many times, and not just once with many arguments, unless you explicitly say that by using a loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.