Running ./test.sh
gives the below output:
test1 is empty
test2 is set (empty string)
test3 is set (has string though)
test4 is not set (does not exist at all)
#!/bin/bash | |
set -eu | |
TEST1="" | |
if [[ "${TEST1}" != "" ]]; then | |
echo 'test1 is NOT empty' | |
else | |
echo 'test1 is empty' | |
fi | |
TEST2="" | |
if [[ -z "${TEST2+x}" ]]; then | |
echo 'test2 is not set' | |
else | |
echo 'test2 is set (empty string)' | |
fi | |
TEST3="something" | |
if [[ -z "${TEST3+x}" ]]; then | |
echo 'test3 is not set' | |
else | |
echo 'test3 is set (has string though)' | |
fi | |
if [[ -z "${TEST4+x}" ]]; then | |
echo 'test4 is not set (does not exist at all)' | |
else | |
echo 'test4 is set' | |
fi |