Numbers following interpolated variables need to be in separate strings

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the bash category.

Last Updated: 2024-04-13

This code, which was supposed to list numbers from "000" to "6001" produced nothing but newlines. Why?

for i in $(seq 0 60); do echo "$i01"; done

# Prints a new line 60 times...

The issue is that in the above code bash sees $i01 and does not know where the variable name ends and the regular string begins. This confusion happens because variable names can contain numbers (e.g. jack3=foo would be valid)

The fix:

# Use the ${<variable>} syntax in the string to demarcate its end
for i in $(seq 0 60); do echo "${i}01"; done

# Or put the numbers outside the quotes of the echo statements
for i in $(seq 0 60); do echo "$i"01; done