Stop your glob patterns from failing when they match nothing
Shell Scripting / Bash Tricks
Stop your glob patterns from failing when they match nothing
🧩 The Challenge
Ever written a script to clean up log files with rm *.log, only to have the shell literally try to delete a file named “*.log” because the directory was empty? I spent half a morning once wondering why my cleanup script was throwing “file not found” errors instead of just doing its job.
💡 The Fix
You need to tell Bash to just shut up and return an empty list if nothing matches your glob, rather than passing the literal string to the next command. It keeps your scripts from hallucinating files that don’t exist.
shopt -s nullglob
⚙️ Why It Works
Setting this shell option forces Bash to replace patterns that match nothing with a null value instead of keeping the glob pattern itself in the argument list. It changes the behavior globally for the rest of your current shell or script execution.
🚀 Pro-Tip: Pair this with failglob if you want your script to die immediately when a pattern fails to match, which is way safer than letting a broken glob trigger a dangerous command.
Linux Tips & Tricks | © ngelinux.com | 7/31/2026
