The goal here is to test that a variable both exists and is not empty. You can use the following to achieve that:


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"

But that test will also pass if THEVAR is set but empty.