3 ms·
> to put `-u` (nounset) in your shebang Any particular reason why in the shebang instead of set -u? > The only exception I typically run across is expansion o
by Calzifer 3y ago
> to put `-u` (nounset) in your shebang
Any particular reason why in the shebang instead of set -u?
> The only exception I typically run across is expansion of arrays using "${arr[@]}" syntax
In Bash? Works for me.
Edit: another comment mentions it as well. Seem to behave better in newer versions of Bash and only problematic in <= 4.3 https://news.ycombinator.com/item?id=38397241 https://news.ycombinator.com/item?id=38397241
$ bash -uc 'unset x; echo "=> ${x[@]}"'
=>
$ bash -uc 'x=(); echo "=> ${x[@]}"'
=>
$ bash -uc 'x=(); echo "=> ${x[0]}"'
bash: x[0]: unbound variable
Zsh does not like the first example but both should support:
$ bash -uc 'unset x; echo "=> ${x[@]:-null}"'
=> null
> Also handy is `-e` (errexit),
It is unfortunately very confusion with functions. Made me like it less over the years.
- mr-wendel 3y agoUsing the shebang just helps highlight the fact that the rule is in use globally, but otherwise has no advantage to using `set -u`. The clarifications on `-u` and arrays are useful. I'm definitely used to assuming newer (... non-ancient?) versions of Bash are what is available.
- Xophmeister 3y agoUsing `set -u` is more portable. If your shebang is `/usr/bin/env bash`, which it probably should be, then you can't add additional command line arguments in Linux with older coreutils. macOS supports additional arguments, regardless, and in Linux, coreutils 8.30 added the `-S` option to `env` to get around this problem.