I want a script to behave differently based on whether an environment variable has been set or not. I tried a bunch of different builtins mentioned here (the manual lists, but doesn’t explain them, so I had to rely on documentation for other shells), however no matter what I try, Ion exits with the error: ion: expansion error: Variable does not exist
. I assume this is to prevent programming errors that confuse an empty variable and an unset variable. The proper way to do it in bash (according to this SO answer) is very non-obvious:
if [ -z ${var+x} ]; then echo "var is unset"; else echo "var is set to '$var'"; fi
where
${var+x}
is a parameter expansion which evaluates to nothing ifvar
is unset, and substitutes the stringx
otherwise.
What is the proper way to do it in Ion? The env namespace section in the manual seems related, but running echo ${env::VARIABLE}
still errors if $VARIABLE is not set.
–
I came up with this question while trying to find a solution to How to tell if you’re in a login shell?. My idea was to check if the $DISPLAY variable exists. This would probably be a very bad solution for that problem, but the question remains.