In an attempt to make my many programming experiments last and live longer than my hard disk might live, I wanted to create a remote copy of my git repositories.
So I programmed the following line
for i in *; do if test -d $i/.git; then (git clone --bare $i /Volumes/Backup/Git/$i.git); fi; done
Of course I had a problem, that I had not expected. Can you spot it? It is not, that I used parenthesis, where they are not needed. I kept them, because I first wanted to program (cd $i; …). But this would have been „wrong“ too. So what is it? Of course, it is using $i without quotes!
„Back in the good old days“ nobody liked spaces in files or directories. It made all programming and typing a pain. So back then, the above line was not only common, but more or less seen as „correct“. But nowadays even my Xcode projects sometimes have blanks in their filenames. So the correct version is this one:
for i in *; do if test -d "$i"/.git; then (git clone --bare "$i" /Volumes/Backup/Git/"$i".git); fi; done
Why is it „$i“ instead of ‚$i‘, `$i` or ´$i´? I assume, you know that „$i“ is quoting including substitution/evaluation of the variable i, ‚$i‘ is quoting the variable without substituting it and that `$i` will substitute $i with the shell-execution result of $i’s content after evaluation. And ´$i´? That’s no quote at all, just two special characters.
Small programs (and program errors) like this made programmers like me superstitious: Never use blanks in filenames. I still try to avoid them, but the „forces of the evil blank“ are so nice, sweet and appealing…