In Bash you can check if an environment variable is set with test -v FOO. It doesn't work in Dash on Debian 11 though because its test doesn't support -v:
# test -v FOO
dash: 2: test: -v: unexpected operator
Is there a portable way that works in Bash and Dash (and ideally more shells, but those are the main ones I am concerned with)?
You can use
+in the expansion of the variable, and test it:The expansion results in an empty string if
FOOis unset, orxotherwise.