Table of Contents
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.
1 2 3 4 5 6 7 |
if [ -v HOME ]; then echo "Variable is set: $HOME" else echo "Variable isn't set" fi |
1 2 3 |
Variable is set: /root |
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
.
1 2 3 4 5 6 7 |
if [ -z "${HOME}" ]; then echo "Variable isn't set" else echo "Variable is set: $HOME" fi |
1 2 3 |
Variable is set: /root |
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.
1 2 3 4 5 6 7 |
if [[ -z ${HOME} ]]; then echo "Variable isn't set" else echo "Variable is set: $HOME" fi |
1 2 3 |
Variable is set: /root |
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.
1 2 3 |
[[ -z "${HOME}" ]] && echo "Variable isn't set" || echo "Variable is set: $HOME" |
1 2 3 |
Variable is set: /root |
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.
1 2 3 4 5 6 7 8 9 10 |
var=$HOME if [[ ! -v var ]]; then echo "Variable is not set" elif [[ -z "$var" ]]; then echo "Variable is set to the empty string" else echo "Variable has the value: $var" fi |
1 2 3 |
Variable has the value: /root |
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.
1 2 3 |
[[ -n "${HOME}" ]] && echo "Variable is set: $HOME" || echo "Variable isn't set" |
1 2 3 |
Variable is set: /root |
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.
1 2 3 4 5 |
echo "${HOME:-default_value}" #the above command can also be used as follows: echo "${HOME:-default}" |
1 2 3 4 |
/root /root |
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.
1 2 3 4 5 6 |
UNSET HOME echo "${HOME:-default_value}" HOME="TEST" echo "${HOME:-default_value}" |
1 2 3 4 |
default_value TEST |
In contrast, if you do care about the difference between an unset variable or the variable with an empty value, then drop the colon.
1 2 3 4 5 6 |
UNSET HOME echo "${HOME-default_value}" HOME="" echo "${HOME-default_value}" |
1 2 3 4 |
default_value #It is an empty line because HOME="" |
Using printenv
Command
Use the printenv
command to print the current value of the specified environment variable in bash.
1 2 3 |
printenv HOME |
1 2 3 |
/root |
Use the printenv
command to print the current value of all the environment variables in bash.
1 2 3 |
printenv |
1 2 3 4 5 6 7 8 9 10 |
SHELL=/bin/bash HOSTNAME=jdoodle PWD=/home _=/usr/sbin/printenv HOME=/root LANG=en_US.UTF-8 SHLVL=2 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin |
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.
1 2 3 |
env | grep HOME |
1 2 3 |
HOME=/root |
Use the env
command to print the current value of all the environment variables in bash.
1 2 3 |
env |
1 2 3 4 5 6 7 8 9 10 |
SHELL=/bin/bash HOSTNAME=jdoodle PWD=/home _=/usr/sbin/env HOME=/root LANG=en_US.UTF-8 SHLVL=2 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin |
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.
1 2 3 |
echo ${HOME:+Variable is set} |
1 2 3 |
Variable is set |
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.
1 2 3 |
echo ${HOME} |
1 2 3 |
/root |
That’s all about how to check if environment variable is set in Bash.