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 ?
-
Actually, this is not a true duplicate of that question: The OP here wants to know whether a variable was set, and if not do something about it, instead of exitting a script. ${V:?} is not a valid answer in that case. `set | grep -q '^V='` is. This tests for the presence of the variable and works even if the variable was set but to the empty string. – Coroos Nov 14 '15 at 20:24
4 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.

- 204,365
- 48
- 270
- 300
-
1Could you add an explanation of how the `: ${VAR:=foo}` notation works internally? I always find it as THE solution but never HOW it actually works. – Vincent Robert Jan 13 '14 at 18:03
-
2I found some explanation on [StackOverflow](http://stackoverflow.com/questions/4437573/bash-assign-default-value) ! – Vincent Robert Jan 13 '14 at 18:09
[ -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.

- 61,765
- 13
- 122
- 144
-
-
@Senthilnathan: yes, of course, the only thing that could be rewritten to make this peacof code more portable is `grep -q`. One could write `env | grep ^VARIABLE= > /dev/null` instead. – Igor Chubin Jul 27 '12 at 11:02
-
If I export a variable, then set the variable to something else without exporting it, do I end up with a mismatch between my non-exported variable and my exported variable, or will it automagically update the exported one, too? – ArtOfWarfare Sep 11 '15 at 11:29
-
Note if you are using `set -eu` to help you avoid errors, using `test -z` on an unset variable will terminate your script. So @William Pursell's answer is better I think. – Paul A Jungwirth Sep 25 '15 at 16:34
-
This is not robust. Consider `export foo="$(printf "\nVAR=fake value")"` Now foo is in the environment and `grep ^VAR` matches, but VAR is not in the environment. – William Pursell Jan 20 '18 at 15:16
What you want to do is native in bash, it is called parameter substitution:
VARIABLE="${VARIABLE:=abc}"
If VARIABLE is not set, right hand side will be equal to abc. Note that the internal operator :=
may be replaced with :-
which tests if VARIABLE is not set or empty.

- 4,886
- 1
- 20
- 33
-
1Exactly what I was looking for. The line `export TEST_DATABASE_NAME="${TEST_DATABASE_NAME:=prodlike}"` will only use prodlike as a default if TEST_DATABASE_NAME wasn't already defined. No messy conditional blocks. All in one line of code. – l p Apr 15 '17 at 05:41
if [ -z "$VARIABLE" ]; then
VARIABLE=...
fi
This checks if the length of $VARIABLE is zero.

- 842,883
- 184
- 1,785
- 1,677