BASH Programming

How to Check If an Environment Variable Exists and Get Its Value – Bash

Environment variables are a fundamental aspect of the bash as they are used to store configuration settings, system parameters, and other critical values as in some cases, Bash scripts may need to check for the existence of an environment variable and retrieve its value. This article will discuss how to check the existence of an environment variable and to get its value in bash.

How to Check If an Environment Variable Exists and Get Its Value

Environment variables are crucial for storing configuration settings and other essential values in software development, here are some ways to check if the if an environment variable exists and in case if exists then print its value:

1: Using echo Command with z Flag

The easiest method to check for the existence of an environment variable is by using the echo command with the z flag. The echo command can be used to print the value of an environment variable, also to see if there is a value in the variable z flag is used and for illustration below is the example:

#!/bin/bash

if [ -z "$PATH" ]
then
    echo "Environmental variable does not exist."
else
    echo "The value of this Environmental Variable is: $PATH"
fi

 
The if statement checks if the PATH variable is empty or not using the -z option. If it is empty, the script will output a message indicating that the variable does not exist and if the variable is not empty, the else block will execute and print the value of the PATH variable using the echo command:

2: How to Check If an Environment Variable Exists and Get Its Value Using env Command

Another way to check for the existence of an environment variable and retrieve its value is using the env command as the env command prints a list of environment variables and their values, further there is an example that illustrates the use or env command to check the existence of environment variable:

#!/bin/bash
env | grep PATH > /dev/null
if [ $? -eq 0 ]
then
    echo "The value of Environment variable is: $PATH"
else
    echo "Environment variable does not exist."
fi

 
This is a bash script that checks if the environment variable PATH exists and if it does, prints its value. If the PATH variable exists, the script will print a message saying “The value of the Environmental variable is: [the value of PATH]”. If the PATH variable does not exist, the script will print a message saying “Environment variable does not exist.”

The script first runs the env command, which displays the current environment variables as the output of this command is piped (using |) to the grep command, which searches for the string “PATH”. The > /dev/null portion of the command redirects any output from grep to the null device, effectively suppressing it.

The if statement checks the exit status of the grep command using $?. If the exit status is 0 (which means that the PATH variable was found), the then block is executed, which prints the value of PATH. If the exit status is not 0, the else block is executed, which prints a message saying that the variable does not exist.

Conclusion

Environment variables are essential in computer programming, and being able to check if they exist and get their values can help you make decisions in your code. This article explains two ways to check the existence of an environment variable in bash and to get its value that is through echo command with z flag and through env command.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.