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: shopt -s nocaseglob makes shell wildcards ignore upper and lower case

Shell
~/tips/tip-shell-nocaseglob

shopt -s nocaseglob

Match photo.JPG and photo.jpg with one pattern.

Camera files come as IMG_1234.JPG, phone files as img_1234.jpg, and a loop over *.jpg silently skips half of them.

Bash can match file names without caring about case:

shopt -s nocaseglob
for f in *.jpg; do
  echo "resizing $f"
done

Now *.jpg matches .jpg, .JPG and .Jpg. The option affects every glob after it in that shell, so in a script it covers the whole rest of the script.

I like it more than writing *.[jJ][pP][gG], which works but reads like a captcha.

Turn it off again with shopt -u nocaseglob if later parts of the script expect exact matches. On macOS the default filesystem already treats a.jpg and A.JPG as the same file, but bash matching itself still cares about case there too, so the option helps on a Mac as well.

There is a sibling, shopt -s nocasematch, for case statements and [[ ... == ... ]] tests, which this one does not touch. And zsh has its own way, with setopt NO_CASE_GLOB.

Further reading: Bash manual: The Shopt Builtin

More Shell tips zmv -n '(*).log' '$1.txt' zoxide query -ls project f=$(mktemp --suffix=.json) shopt globskipdots echo $EPOCHSECONDS All Shell tips (78) All topics

Link