Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

As Linux users, we frequently work with shell scripts. One of the common requirements in scripts is to test if a particular environment variable is set or not. This helps in handling various error conditions.

In this tutorial, we’ll see the various example to check if a particular environment variable is set or not.

2. Using the if Conditional Expression

We can use the if conditional expression to check if a value is assigned to a variable or not:

$ VAR= 
$ if [ x"${VAR}" == "x" ]; then 
     echo "Value is not assigned to a variable"
  else
     echo "Value is assigned to a variable"
  fi
Value is not assigned to a variable

Let’s assign a value to a variable and verify the result:

$ VAR="sample-value"
$ if [ x"${VAR}" == "x" ]; then
     echo "Value is not assigned to a variable"
  else 
     echo "Value is assigned to a variable"
  fi
Value is assigned to a variable

3. Using the Double Square Brackets

Another way to check if a variable is set or not is by using a double square bracket:

$ VAR=
$ [[ x"${VAR}" == "x" ]] && echo "Value is not assigned to a variable" || echo "Value is assigned to a variable"
Value is not assigned to a variable

Let’s verify the output by assigning a value to a variable:

$ VAR="sample-value"
$ [[ x"${VAR}" == "x" ]] && echo "Value is not assigned to a variable" || echo "Value is assigned to a variable"
Value is assigned to a variable

Note that this method works only with Bash, Z Shell (zsh), and Korn Shell (ksh).

4. Using the Parameter Expression

We can use bash built-in parameter expressions to check if a variable is set or not:

$ VAR=
$ [[ ${VAR:-"unset"} == "unset" ]] && echo "Value is not assigned to a variable" || echo "Value is assigned to a variable"
Value is not assigned to a variable

Let’s set the value of a variable and check whether it works:

$ VAR="sample-value"
$ [[ ${VAR:-"unset"} == "unset" ]] && echo "Value is not assigned to a variable" || echo "Value is assigned to a variable"
Value is assigned to a variable

5. Using the -z Conditional Expression

In bash, there is a -z conditional expression that returns true if the length of a string is zero. We can use this property to check if a variable is set or not:

$ VAR=
$ [[ -z "${VAR}" ]] && echo "Value is not assigned to a variable" || echo "Value is assigned to a variable"
Value is not assigned to a variable

Let’s try it out:

$ VAR="sample-value"
$ [[ -z "${VAR}" ]] && echo "Value is not assigned to a variable" || echo "Value is assigned to a variable"
Value is assigned to a variable

6. Using the -n Conditional Expression

Similarly, there is -n conditional expression that returns true if the length of a string is non-zero. We can use this property to check if a variable is set or not:

$ VAR=
$ [[ ! -n "${VAR}" ]] && echo "Value is not assigned to a variable" || echo "Value is assigned to a variable"
Value is not assigned to a variable

Let’s update the value of a variable and confirm the result:

$ VAR="sample-value"
$ [[ ! -n "${VAR}" ]] && echo "Value is not assigned to a variable" || echo "Value is assigned to a variable"
Value is assigned to a variable

7. Conclusion

In this tutorial, we discussed various practical examples to check if a variable is set or not. First, we discussed if conditional expression. Then we discussed parameter expression, and finally, we discussed built-in conditional expressions of the bash. We can use these commands in day-to-day life to make shell scripts more robust.