4 ms·
What does this do in a directory containing no .txt files? for f in *.txt; do echo $f done Surprisingly, Bash still executes the body of the l
by rgovostes 6y ago
What does this do in a directory containing no .txt files?
for f in *.txt; do
echo $f
done
Surprisingly, Bash still executes the body of the loop once, with f containing the literal string "*.txt". This behavior is controlled by the "nullglob" option, which hardly anyone ever sets.
- krebs_liebhaber 6y agoI encountered this error a few weeks ago while trying to convert a folder of .webp files to .png. God, I love Bash.
- kevincox 6y agoI would argue that this is continent for interactive use. It means I don't have to quote as many things and usually gets a reasonable error if I did intent on matching things. But yes, for scripts you are better served by nullglob.
- qiqitori 6y agoSee https://mywiki.wooledge.org/BashPitfalls https://mywiki.wooledge.org/BashPitfalls for a large collection of similar edge cases. (Recently discussed on HN: https://news.ycombinator.com/item?id=24401085 https://news.ycombinator.com/item?id=24401085)