Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if environment variable is already set [duplicate]

Tags:

linux

shell

unix

I am writing a shell script, where I have to check if environment variable is set, if not set then I have to set it. Is there any way to check in shell script, whether an environment variable is already set or not ?

like image 533
Lolly Avatar asked Jul 27 '12 10:07

Lolly


People also ask

How do I check if an environment variable is set?

In the command window that opens, enter echo %VARIABLE%. Replace VARIABLE with the name of the environment variable you set earlier. For example, to check if MARI_CACHE is set, enter echo %MARI_CACHE%. If the variable is set, its value is displayed in the command window.

How do I check if an environment variable is set in Bash?

To find out if a bash variable is defined: Return true if a bash variable is unset or set to the empty string: if [ -z ${my_variable+x} ];

How do I check if an environment variable is set in Python?

getenv() method in Python returns the value of the environment variable key if it exists otherwise returns the default value. default (optional) : string denoting the default value in case key does not exists. If omitted default is set to 'None'.

How do I see environment variables in terminal?

To display the values of environment variables, use the printenv command. If you specify the Name parameter, the system only prints the value associated with the variable you requested.


2 Answers

The standard solution to conditionally assign a variable (whether in the environment or not) is:

: ${VAR=foo} 

That will set VAR to the value "foo" only if it is unset.
To set VAR to "foo" if VAR is unset or the empty string, use:

: ${VAR:=foo} 

To put VAR in the environment, follow up with:

export VAR 

You can also do export VAR=${VAR-foo} or export VAR=${VAR:=foo}, but some older shells do not support the syntax of assignment and export in the same line. Also, DRY; using the name on both sides of the = operator is unnecessary repetition. (A second line exporting the variable violates the same principal, but feels better.)

Note that it is very difficult in general to determine if a variable is in the environment. Parsing the output of env will not work. Consider:

export foo=' VAR=var-value' env | grep VAR 

Nor does it work to spawn a subshell and test:

sh -c 'echo $VAR' 

That would indicate the VAR is set in the subshell, which would be an indicator that VAR is in the environment of the current process, but it may simply be that VAR is set in the initialization of the subshell. Functionally, however, the result is the same as if VAR is in the environment. Fortunately, you do not usually care if VAR is in the environment or not. If you need it there, put it there. If you need it out, take it out.

like image 178
William Pursell Avatar answered Oct 01 '22 02:10

William Pursell


[ -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.

like image 40
Igor Chubin Avatar answered Oct 01 '22 01:10

Igor Chubin