Bash Substitution for a String with an Underscore

This is most definitely a post to my future self for when I want to do this again and can’t remember how.

I want to download a set of files that are named like this -

01_some_file.txt
02_some_file.txt
03_some_file.txt
...etc

Normally I use a simple for loop like this -

for i in {1..10}; do
    wget "https://baseurl/$i_some_file.txt"
done

But this produces a series of wget commands like this -

wget https://baseurl/.txt
wget https://baseurl/.txt
wget https://baseurl/.txt
...etc

The problem is the first underscore in the file name, but the fix is simple -

for i in {1..10}; do
    wget "https://baseurl/"$i"_some_file.txt"
done

There, my future self doesn’t need to remember how to do it.

comments powered by Disqus

Related