Table of Contents [hide]
Using if-else
with -z
,-v
,-n
Options
Use the if-else
statement with the -v
option to check if an environment variable is set in bash. The echo
command in the if
block will be executed if the [-v HOME]
will be true
, and it will be true
if the specified variable is set.
The
-v
option returnstrue
if the given variable is set to an empty string, while it returnsfalse
if the provided variable is not declared.
Use the if-else
statement with the -z
option to check if the length of the environment variable is 0
in bash. Length 0
represents the unset variable. The echo
command within the if
block will be executed if the [ -z "${HOME}" ]
will be true
and it will be true
if the length of the specified variable 0
.
Alternatively, we can use the -z
option with double square brackets. We don’t have to double-quote the variable using double square brackets with the -z
option.
The
-z
option returnstrue
if the variable is undeclared or initialized with an empty string.
Or, we can use the one-line solution as follows.
Use the if-else
statement with -z
and -v
options to check if the environment variable is set, unset, or initialized with an empty string.
Use the if-else
statement with the -n
option to check if the environment variable is not empty. If it is not empty, the given variable is set; otherwise, unset.
Using Parameter Expansion
If you don’t have any concerns about the difference between an unset variable or the variable with an empty value, you can use the default-value
parameter expansion as demonstrated below.
Here, we get an advantage of the parameter expansion, which used the ${parameter:-word}
syntax to substitute the default value if the HOME
environment variable is not set. Considering the above code, the ${parameter}
is the parameter’s name, which is HOME
in this case. The :-
is an expansion operator that specifies if the parameter is null or unset, the default value should be used. Finally, the word
is the default value if the parameter is null or unset.
So, if it is set, the echo "${HOME:-default_value}"
printed the value of the HOME
environment variable. On the other hand, if it is unset or null, the string default_value
will be printed instead. This approach is useful when we want a default value if the provided variable is unset or undefined.
Let’s see another example to understand more clearly.
In contrast, if you do care about the difference between an unset variable or the variable with an empty value, then drop the colon.
Using printenv
Command
Use the printenv
command to print the current value of the specified environment variable in bash.
Use the printenv
command to print the current value of all the environment variables in bash.
Further reading:
Using env
Command
Use the env
command with the grep
command to grab the specified environment variable with its current value in bash.
Use the env
command to print the current value of all the environment variables in bash.
Using echo
Command
Use the echo
command with ${VARNAME:+VALUE}
syntax to print the Variable is set
message if the specified environment variable is set in bash. It will show nothing if the given variable is undeclared or initialized with an empty string.
Use the echo
command with ${VARNAME}
syntax to print the current value of the specified environment variable in bash. It will show nothing if the given variable is not declared or initialized with an empty string.
That’s all about how to check if environment variable is set in Bash.