brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: set -f turns off globbing until you set +f

Globbing
~/tips/tip-bash-set-f-noglob-turn-off-globbing

set -f

Switches off filename expansion in the current shell, so a * is passed on as a plain character.

Sometimes a command needs a literal * and the shell keeps replacing it with file names. Quoting is the usual answer. When there are many such arguments, you can switch the expansion off for a while.

set -f
echo *.txt
*.txt
set +f
echo *.txt
a.txt b.txt

set -f disables filename expansion in the current shell, set +f brings it back. The manual lists noglob as another name for -f, so set -o noglob and set +o noglob do the same and read better in a script. In a folder with a.txt and b.txt the first echo *.txt after set -f printed *.txt, and after set +f it printed both files again.

It is useful when you read patterns from a file or a variable and want to loop over them without bash matching them against the current folder first. Word splitting still happens, and only the file name matching is off: for p in $v with v='*.txt *.log' gave the two items *.txt and *.log.

Caveats. It is a switch for the whole shell, not for one command. If you forget the set +f, every glob after it stays literal, and that is confusing in an interactive shell. In a script that is not a problem as long as you turn it back on when the loop is done. Also, it only stops the shell from expanding. Brace expansion is a separate step: with set -f active, echo {a,b}.txt still printed a.txt b.txt.

Further reading: Bash reference manual

More Globbing tips zmv -n '(*).log' '$1.txt' echo {01..10} shopt globskipdots GLOBIGNORE='*.bak:*~' ls All Globbing tips (12) All topics

Link