In bash
, I know that there are a few ways to check whether a given variable is defined and contains a value. However, I want to check whether a given variable exists in the environment, and not as simply a local variable. For example ...
VAR1=value1 # local variable
export VAR2=value2 # environment variable
if [[ some_sort_of_test_for_env_var VAR1 ]]
then
echo VAR1 is in the environment
else
echo VAR1 is not in the environment
fi
if [[ some_sort_of_test_for_env_var VAR2 ]]
then
echo VAR2 is in the environment
else
echo VAR2 is not in the environment
fi
How can some_sort_of_test_for_env_var
be defined so that the bash code above will cause the following two lines to be printed?
VAR1 is not in the environment
VAR2 is in the environment
I know that I can define a shell function to run env
and do a grep
for the variable name, but I'm wondering if there is a more direct "bash-like" way to determine whether a given variable is in the environment and not just a local variable.
Thank you in advance.