> ## Content Index
> Fetch the complete content index at: https://linuxhandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Check if Variable is Empty in Bash
- URL: https://linuxhandbook.com/check-variable-empty-bash/
- Published: 2023-06-20T14:36:04.000Z
- Updated: 2023-06-20T14:36:04.000Z
- Description: Learn various ways to check if a variable is empty in bash.
- Author: Sagar Sharma
- Tags: Bash Tips

There are several reasons why one would want to check whether the variable is empty, such as checking for input validation.

And in this tutorial, I will show you 4 ways to check how to check if the variable is empty in bash:

- By checking the length of the variable
- Using non-empty check
- By comparing a variable to an empty string
- Using double square brackets `[[]]`

So let's start with the first one.

## 1\. By checking the length of the variable 

In this method, I will be using the `-z` flag with the `[]` operator to check for the empty variable. 

First, use the following command to create and open an empty file:

```
nano length.sh
```

Now, paste the following lines of code into the file:

```bash
#!/bin/bash

# Set the variable
variable=""

# Check if the variable is empty
if [ -z "$variable" ]; then
    echo "Variable is empty."
else
    echo "Variable is not empty."
fi
```

[Save changes and exit from the nano](https://linuxhandbook.com/nano-save-exit/) text editor.

Finally, [use the chmod command to make the file executable](https://linuxhandbook.com/chmod-command/) as shown:

```
chmod +x length.sh
```

Now, you can execute the script, which should get you the output by saying the **Variable is empty:**

![By checking the length of the variable ](https://linuxhandbook.com/content/images/2023/06/Check-if-variable-is-empty-in-Bash-By-checking-the-length-of-the-variable-.png)

## 2\. Using a non-empty check

In this method, I will be using the `-n` flag to check whether the variable is empty or not. 

Here's the simple script which will tell me whether the variable is empty or non-empty:

```
#!/bin/bash
variable=""
if [ -n "$variable" ]; then
    echo "Variable is not empty."
else
    echo "Variable is empty."
fi

```

As you can see, it uses a simple if-else statement it two echo statements. 

And if you execute the above script, you should expect the following result:

![Check if variable is empty in Bash Using a non-empty check](https://linuxhandbook.com/content/images/2023/06/Check-if-variable-is-empty-in-Bash-Using-a-non-empty-check.png)

## 3\. By comparing a variable to an empty string

As the name suggests, in this method, I will be comparing the variable to an empty string using the `[]` operator.

Here's the simple script:

```
#!/bin/bash

if [ "$variable" = "" ]; then
    echo "Variable is empty."
else
    echo "Variable is not empty."
fi
```

If you notice, there's a condition inside `[]` which simply checks whether the `$variable` is empty or not.

And on the basis of that, it gets the output.

If you use the shown script, you should expect the following output:

![Check if variable is empty in Bash By comparing a variable to an empty string](https://linuxhandbook.com/content/images/2023/06/Check-if-variable-is-empty-in-Bash-By-comparing-a-variable-to-an-empty-string.png)

## 4\. Using double square brackets

Using double square brackets, you get more flexibility like adding regular expressions, etc. 

And here, I will be using the `-z` flag which I used previously to check the length of the variable, and here too, it will have the same effect.

Here's the script:

```
#!/bin/bash
variable=""
if [[ -z $variable ]]; then
    echo "Variable is empty."
else
    echo "Variable is not empty."
fi

```

Once you execute the above script without any changes, you'd get the following results:

![Check if variable is empty in Bash using double square brackets](https://linuxhandbook.com/content/images/2023/06/Check-if-variable-is-empty-in-Bash-using-double-square-brackets.png)

Pretty easy. Isn't it?

You may also like to learn about [special variables in Bash](https://linuxhandbook.com/bash-special-variables/).

[Special Variables in Bash Shell \[With Script Examples\]The bash shell has some special variables that have specific usages and purposes. Learn more about them here.![](https://linuxhandbook.com/content/images/size/w256h256/2021/08/Linux-Handbook-New-Logo.png)Linux HandbookSagar Sharma![](https://linuxhandbook.com/content/images/2023/04/special-variables-in-bash-shell-scripting.png)](https://linuxhandbook.com/bash-special-variables/)

## New to Bash? Start here

If you are new to Bash scripting, we have a good starting point for you.

[Bash Tutorial Series for Beginners: Start Learning Bash ScriptingThe 10 chapter series will teach the bash scripting essentials.![](https://linuxhandbook.com/content/images/size/w256h256/2021/08/Linux-Handbook-New-Logo.png)Linux HandbookAhmed Alkabary![](https://linuxhandbook.com/content/images/2020/08/bash-beginner-series.jpg)](https://linuxhandbook.com/tag/bash-beginner/)

Let me know if you notice any issues with what I discussed here.