3 ms·
I often set the nullglob option in scripts, because it makes the handling of globs which don't match anything a bit more predictable: http://bash.cumulonim.biz
by twic 7y ago
I often set the nullglob option in scripts, because it makes the handling of globs which don't match anything a bit more predictable:
http://bash.cumulonim.biz/NullGlob.html http://bash.cumulonim.biz/NullGlob.html
There's a note at the end about how with nullglob set, ls on a glob with no matches does something surprising. This is a great illustration of how an empty list and the absence of a list are different. Sadly it's rather hard to make that distinction in shells!
I do wish that either shells had a more explicit syntax for globbing, or other commands didn't use the same syntax for patterns. Then confusion like this couldn't occur. An example of the former would be if you had to write:
ls $(glob *.txt)
Here, the shell would not treat * specially, but rather you would have to explicitly expand it. This would be a pain, but at least you wouldn't do it by mistake!
- johncs 7y agoI set failglob: `shopt -s failglob`. Makes the whole command fail if there's no matches. That combined with `set -e` which aborts the script in the event of any command failing makes me feel somewhat safe. Indeed I add the following two lines to every bash script I write: set -exu shopt -s failglob
- nothrabannosir 7y agoIf you like set -e, I recommend looking at set -o pipefail.
- simias 7y agoI don't know if it's the default or if it's something I've set in my config a decade or two ago but my zsh behaves like this by default (i.e. I get an error for blobs that match nothing instead of silently passing the * along). That seems much saner to me: $ find . -name *.txt zsh: no matches found: *.txt That'll teach you to quote your `find` patterns real fast...
- daveFNbuck 7y agoIt's either default or part of oh my zsh, because I've had the same experience and I don't have any custom configs.
- chubot 7y agoYes, Oil (https://oilshell.org/ https://oilshell.org/) now has nullglob on when you run bin/oil instead of bin/osh. So you get this: oil$ find . -name *.jpg find: missing argument to `-name' oil$ find . -name '*.jpg' (works) Details: I had it on when you set 'shopt -s strict:all', but I neglected to turn it on for 'oil:basic' and 'oil:all'. Those are oil-specific option groups so you don't have to remember all the option names. But I just fixed that and it will be out with the next Oil release. https://github.com/oilshell/oil/commit/ddac119254f9a7045dca7e659f823c8a9a83192e https://github.com/oilshell/oil/commit/ddac119254f9a7045dca7... If anyone wants to add more strictness options to Oil to avoid these types of mistakes, let us know! (e.g. on https://github.com/oilshell/oil https://github.com/oilshell/oil, or there's more contact info on the home page).