3 ms·
> Just test the string directly like [ "$var" ] && echo "var not empty" Will this work if you've activated set -u, fail on unset variables? Will the !-z varian
by Too 10y ago
> Just test the string directly like [ "$var" ] && echo "var not empty"
Will this work if you've activated set -u, fail on unset variables? Will the !-z variant do?
- brbsix 10y agoIf `set -u` is enabled, the aforementioned test will fail on unset variables. To avoid this, you can "declare" `var` at the beginning of the script, e.g. `var=''`. Also instead of `[ ! -z "$var" ]` I prefer `[ -n "$var" ]`, which is the same as `[ "$var" ]` but more explicit.