Check If Environment Variable Is Already Set

How to check if an environment variable exists and get its value?

[ -z "${DEPLOY_ENV}" ] checks whether DEPLOY_ENV has length equal to zero. So you could run:

if [[ -z "${DEPLOY_ENV}" ]]; then
MY_SCRIPT_VARIABLE="Some default value because DEPLOY_ENV is undefined"
else
MY_SCRIPT_VARIABLE="${DEPLOY_ENV}"
fi

# or using a short-hand version

[[ -z "${DEPLOY_ENV}" ]] && MyVar='default' || MyVar="${DEPLOY_ENV}"

# or even shorter use

MyVar="${DEPLOY_ENV:-default_value}"

check if environment variable is already set

[ -z "$VARIABLE" ] && VARIABLE="abc"

if env | grep -q ^VARIABLE=
then
echo env variable is already exported
else
echo env variable was not exported, but now it is
export VARIABLE
fi

I want to stress that [ -z $VARIABLE ] is not enough, because you can have VARIABLE but it was not exported. That means that it is not an environment variable at all.

Check if a env variable is set and its value

The only thing I can think of is that you were mistaken in that you set it but did not export it. That would explain why you could echo the value, but it didn't make into os.environ in Python. Here's a demonstration:

>>> VAR1="vvv1"
>>> export VAR2="vvv2"
>>> echo $VAR1
vvv1
>>> echo $VAR2
vvv2
>>> python
Python 3.7.3 (default, Sep 16 2020, 12:18:14)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> "VAR1" in os.environ
False
>>> "VAR2" in os.environ
True

So VAR1 was only set, but VAR2 was set and exported. Both can be echoed, but only the exported one shows up in Python.

To check at the command line for if a variable is set and exported, use export and grep:

>>> export | grep VAR1
>>> export | grep VAR2
declare -x VAR2="vvv2"
>>>

It's helpful to really understand what export does. Only an exported variable will be inherited by child processes launched by the current shell process. So when you launch Python from the command line, that launches a child process. Only exported variables are copied into the child process, and so only exported variables are seen by Python.

It is this child process thing that explains why you can't run a shell script to set environment variables in your current shell. Because that script will run in a child process, it will only affect the variables in that child process. Once the script runs, that child process goes away. The main process's variable space was not affected by the script.

There is a way to run a script to set environment variables, and it can also be used to run a script that will have access to unexported variables. That is to run a script with '.' or 'source'. When you do . myscript.sh or source myscript.sh, this causes your script to be run in the current shell process. It prevents a subprocess from launching to run the script. So then the script is seeing and affecting the main shell environment.

Another small bit of trivia. I wasn't sure if there was any difference between . myscript.sh and source myscript.sh. Per this SO question, the only difference is portability. In Bash and other modern shells, there is no difference, but not all shells support both variants.

How to check that environment variable is set but also really exists?

Since we're writing shell code, environment variables are just normal variables.

if [ -f "$FILE" ]; then
echo env variable is already exported
else
echo env variable was not exported
fi

I don't see why you would have gotten an error when FILE wasn't exported with the above code, but if you were to test like this: if [ -f $FILE ] (i.e. without the quotation marks) you would run into an error when FILE isn't set.

What is a good practice to check if an environmental variable exists or not?

Use the first; it directly tries to check if something is defined in environ. Though the second form works equally well, it's lacking semantically since you get a value back if it exists and only use it for a comparison.

You're trying to see if something is present in environ, why would you get just to compare it and then toss it away?

That's exactly what getenv does:

Get an environment variable, return None if it doesn't exist. The
optional second argument can specify an alternate default.

(this also means your check could just be if getenv("FOO"))

you don't want to get it, you want to check for it's existence.

Either way, getenv is just a wrapper around environ.get but you don't see people checking for membership in mappings with:

from os import environ
if environ.get('Foo') is not None:

To summarize, use:

if "FOO" in os.environ:
pass

if you just want to check for existence, while, use getenv("FOO") if you actually want to do something with the value you might get.

How do I check if an environment variable is set in cmake

You CMake code is correct. The problem is most likely that you only set the environment variable in your shell but did not export it. Run the following before invoking cmake:

export THING


Related Topics



Leave a reply



Submit