Check If Boolean Is True in Bash

Bash Check if boolean is true

Using if Statement

We can use an if statement with different variations to determine whether the provided Boolean variable is true. Note that the examples covered in this section will be similar. We will learn the first example in Use if with true Condition in detail, but later, we will discuss the section which will differ from the previous example(s). Let’s begin with the first example below.

Use if with true Condition

Use the if statement with the true condition to check if boolean is true in Bash.

First, we have a variable bool_var, which was assigned the value true. The data type of this variable was Boolean, meaning the variable’s value can be either true or false. Note that we don’t have to specify the variable type in Bash explicitly.

Next, we encountered an if statement. The if statement was used to decide based on the provided condition. In the above example case, we used the condition, $bool_var, to check the value of the bool_var variable.

When we utilised $bool_var within the condition to expand the value of the bool_var variable, which was true in this case, essentially, it became equivalent to writing if true; then. Now, let’s break down the if statement further.

The if keyword indicated the start of the if statement, whereas the $bool_var was the condition being evaluated. Since it expanded to true, the condition was considered true. Next, we used the then keyword to indicate the start of the if block, which needs to be executed if the condition is true.

Inside the if block, we had an echo command to print the corresponding message to let the user know if the value of the provided Boolean variable was true to false. Using the else block is optional, but better to show a relevant message if the specified condition did not meet.

In the above code fence, the else indicated the start of the else block to be executed if the specified condition was false. We also had an echo statement in the else block to display a message. Finally, the fi keyword was used to mark the if statement’s end in Bash.

Let’s take another example where the bool_var was set to false.

Use if with [[ ]] Command

Use the if statement with the [[ ]] command to check if boolean is true in Bash.

In the above example, we used the double square brackets ([[ ... ]]) that indicated a conditional expression in Bash, and it allowed for more advanced comparisons and pattern matching. In this case, we checked if the value of bool_var was equal to the true. To check equality, we used the == operator.

Let’s experiment with the above code by modifying the value of the bool_var variable and setting it to false. See the following example.

Remember, using the -eq operator for comparison in the above example is not recommended, as it is used to compare numeric values. We will learn it in a while.

Until now, we have learned two ways (the true condition and the [[...]] command), and both are case-sensitive, which means if you set the bool_var to True as bool_var=True, it will not execute the if block. Instead, the else block will be executed (if it is specified); otherwise, you will get nothing.

Can we still find if the Boolean is true by avoiding this case-sensitivity thing? Yes. Let’s learn how to do it.

Use if with (( )) Command

Use the if statement with the (( )) command to check if Boolean variable is true in Bash.

First, we initialised the bool_var variable with 1; this variable denoted a Boolean value, where 0 typically denoted false and 1 indicated true. Next, we encounter an if statement. The if statement allowed us to conditionally execute a block of code based on a specified condition.

For the condition part, we used (( ... )), which was an arithmetic expansion construct in Bash. It allowed us to perform mathematical operations and evaluations. In our case, we were using it to evaluate the value of the bool_var variable.

Since the value of bool_var was 1, the expression became (( 1 )). In arithmetic evaluation, any non-zero value is considered true, while zero is considered false. Since the value of bool_var was 1, the expression (( 1 )) was evaluated to true. As a result, the echo statement inside the if block was executed to print a message saying the Boolean is true.

Let’s have another example setting the bool_var to 0.

Use if with -eq Operator

Use the if statement with the -eq operator to check if Boolean is true in Bash.

We have already learned the [[ ]] command and the meaning of 1 while working with Boolean-type variables. Here, we used the -eq operator to evaluate the expression inside the [[ ]]. If the expression was evaluated to true, the if block would be executed; otherwise, the else block; remember that the -eq operator is used when we are supposed to do a numeric comparison.

Using case Statement

Use the case statement to check if the specified Boolean is true in Bash.

The case and if-else statements are used interchangeably. You can continue with any of them based on the project requirements. The above code performed a conditional check based on the value of the bool_var variable, which was set to true.

Then, we used the case statement to set up a conditional block where different patterns were matched against the $bool_var variable’s value. Those patterns are given below:

  • true) – Represented the first pattern, checked in the case statement. It matched the value of $bool_var when it was equal to true.
  • false) – Denoted the second pattern that was being validated in the case statement. It matched the $bool_var variable’s value when it was equal to false.

We used an echo command for both patterns to display the corresponding message on the Bash console. The ;; was used to indicate the case block’s end; they also separated the different patterns and their corresponding code blocks from one another. Finally, we used the esac keyword to mark the end of the case, closing the conditional block.

At this point, you must have a question popping into your mind why do you need to use a case statement when the goal can be achieved via if-else statements? Before going into the details, see the following example and predict its output.

We expected the else block to be executed in the above code fence because we set the mobile_missing variable to false. But it did not work as it was supposed to be. Why?

In Bash, variables don’t have types, so there is no such thing as a Boolean variable or value like true or false. In fact, all bash variables are just strings. When we test a variable/string in bash without specifying the type of test (-n or -z), it will default to a -n (non-zero length string) test.

The -n determines if the specified string operand size is non-zero; if so, it returns true. In contrast, the -z checks if the mentioned string operand size is zero; it returns true if it is zero length.

So [ $mobile_missing ] is equivalent to [ -n $mobile_missing ]. As long as $mobile_missing contains at least one character, it was evaluated as true.

The expression was always true since false (just a string) contained five characters. It doesn’t matter what string we set mobile_missing to (true, false, 0, 1); the expression would always be true because mobile_missing was the non-zero length.

This is where the case statement came into the picture. Let’s transform the above example into a case as follows:

Let’s have another example where the missing_mobile was set to an empty string.

In the above example, the default case (*)) was used, which was matched if any of the above patterns did not match.

Until now, we learned how to check a single Boolean variable to find if that is true; what if we are required to validate multiple variables?

Validate Multiple Boolean Variables in Bash

Use the case statement to validate multiple Boolean variables and check if it is true.

After defining two Boolean variables, we used the case statement to check the concatenation of bool_var1 and bool_var2 using "$bool_var1$bool_var2" expression. We used this expression to concatenate the values of bool_var1 and bool_var2 into a single string for comparison.

After that, the case statement checked the value of the concatenated string against different patterns and executed the corresponding code block for the matching pattern. Let’s analyse the different patterns below:

  1. "00" | "falsefalse") – This pattern matched when both variables were set to false.
  2. "01" | "falsetrue") – It matched when bool_var1 was false and bool_var2 was true.
  3. "10" | "truefalse") – It matched when bool_var1 was true and bool_var2 was false.
  4. "11" | "truetrue") – This pattern matched when both variables were set to true.
  5. *) (default pattern): This pattern was used as a catch-all for any other value that did not match the previous patterns.

We used the echo command to display a corresponding message for all the patterns.

We covered both input possibilities: a string (true/false) and numbered form (1/0). Whereas the | represented the logical OR operator.

Let’s specify the numbered values; see the following example:

So, we learned various ways to check whether Boolean is true. We also checked which Boolean variable is true if we had multiple variables. You can go ahead with any way that suits your project requirements better.

That’s all about Bash check if boolean is True.

Was this post helpful?

Leave a Reply

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