vendredi 29 mars 2019

Bash: Safe to remove all occurrences of -n inside brackets?

I recently found this "bug" in my bash script;

if [ "$var" ]; then
    echo "Condition is true"
fi

where what I meant was to check if $var is non-empty, i.e. [ -n "$var" ]. As it happens, the code seems to work perfectly fine without the -n. Similarly, I find that I can replace [ -z "$var" ] with [ ! "$var" ].

I tend to like this implicit falseyness (truthiness) based on the (non-)emptyness of a variable in other languages, and so I might adopt this pattern in bash as well. Are there any danger to doing so, i.e. edge cases where the two set of syntaxes are inequivalent?

Aucun commentaire:

Enregistrer un commentaire