Check If Variable Is Empty in Bash

Check if Variable is empty in Bash

Checking If Variable Is Empty in Bash

There are multiple ways to check if a variable is empty or not. Before moving towards those methods, knowing when a variable is called empty is necessary. In Bash, a variable is called empty if:

  • A variable is declared without assigning any value to it.
  • A variable is declared and assigned with an empty string.

For the first approach, we declare the variable as variable= and use any of the following approaches while in second method, we assign an empty string to the variable as variable="" and then use any of the following methods. Since, the methods are the same for the above-mentioned approaches, let’s learn them below to check if the variable is empty in Bash.

Use the -z Option

Use the -z option to check if the specified variable is empty in Bash.

Now, assign any value as demonstrated below. Again, you can set any value: a string, integer, or float value.

In the above examples, we used the if-else block and -z option to determine if the specified variable is empty. We declared and initialized the variable in the first code snippet with an empty string represented with "". In contrast, in the second example, the variable was initialized with a string value John.

In both examples, we used the if statement with the -z option to check if the variable is empty. Here, the -z option is a unary test operator that checks if the string is empty. This operator accepts one argument that is a string to be tested; it returns true (0 exist status) if the given string is empty; otherwise, a false (non-zero exit status).

Now, you might have a question are we only allowed to use the -z option to test string type value? No, the -z option can take a string literal, a variable, or an outcome of the pipeline or command.

Use the -n Option

Use the -n option to check if the specified variable is empty in Bash.

Now, assign the variable with any value, as shown below.

These two examples are similar to the previous examples, but we used the -n operator here. Unlike the -z option, the -n option is also a unary test operator used to determine the length of the specified variable, whether it is zero or not.

If the length is non-zero, the condition is true means the given variable is not empty; otherwise, the condition is false, and the variable is empty. So in our case, since variable was initialized with the value of 10, which is a non-zero string, the script will print The variable is not empty to the console.

Use = Operator

Use = operator to check if the specified variable is empty or not.

Now, assign a string value "John" to the variable as given below.

Here, we used the comparison operator represented with = to check whether the left and right sides of the specified expression in the if statement are equal. We compared the specified variable in both code snippets with an empty string (""). The if condition would only be true if the variable is empty.

Use ! Operator

Use the ! operator to check whether the specified variable is empty.

Now, assign a value to the variable as follows:

Before learning the code examples, it would be best if you learned the code snippets in the previous section. We used the negation operator represented with !, which negates the things means it makes true to false and vice versa.

So, if the if condition is true, it will make it false, and we only need to exchange the echo statements in the if and else blocks to get the required results.

That’s all about how to check if variable is empty in Bash

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *