Word Splitting and Pathname Expansion
Word Splitting and Pathname Expansion
After parameter expansion and command substitution, Bash performs Word Splitting on unquoted words using the Internal Field Separator ($IFS). By default, $IFS is set to space, tab, and newline.
The Dangers of Word Splitting
filename="My Important Document.pdf"
# DANGEROUS: Word splitting breaks filename into three arguments: "My", "Important", "Document.pdf"
rm $filename
# rm: cannot remove 'My': No such file or directory
# rm: cannot remove 'Important': No such file or directory
# rm: cannot remove 'Document.pdf': No such file or directory
# SAFE: Always double-quote variables
rm "$filename"Controlling Globbing (Pathname Expansion)
Globbing matches filesystem patterns. When no match is found, Bash’s default behavior is to return the unexpanded pattern as literal text, which can cause subtle failures:
# Enable nullglob: unmatched globs expand to nothing instead of literal string
shopt -s nullglob
# Enable failglob: unmatched globs throw a hard error
shopt -s failglob
# Disable globbing entirely
set -f # or set -o noglob