How to test that an environment variable is set in bash?
The goal here is to test that a variable both exists and is not empty. You can use the following to achieve that:
${THEVAR:+x}Which will expand to “x” if THEVAR is set and not empty.
Alternatively, this test can be used:
if [ ! -v THEVAR ]; then
echo "THEVAR is not set"
fiBut that test will also pass if THEVAR is set but empty.