Stop your scripts from nuking themselves when glob patterns go empty

Shell Scripting / Bash Tricks

Stop your scripts from nuking themselves when glob patterns go empty

🧩 The Challenge

Everyone has had that one script where a wildcard like *.conf suddenly matches nothing, and the shell helpfully passes the literal string *.conf to your poor, confused application. Watching a production cleanup script try to delete a literal file named *.conf is a special kind of nightmare I never want to revisit.

💡 The Fix

You need to tell Bash to just shut up and return nothing if a pattern doesn’t match anything. Flipping one shell option makes the whole process sane again.

shopt -s nullglob

⚙️ Why It Works

Setting this option prevents the shell from leaving the unmatched pattern in the argument list, effectively turning it into an empty list instead of a dangerous literal string. Once you enable it, your loops over wildcards will finally behave the way you expect them to.

🚀 Pro-Tip: Stick this in your global bashrc or at the top of your scripts to save your future self from a massive headache.

Linux Tips & Tricks | © ngelinux.com | 11/11/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted