Table of Contents
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.
1 2 3 4 5 6 7 8 |
bool_var=true if $bool_var; then echo "The Boolean is true." else echo "The Boolean is false." fi |
1 2 3 |
The Boolean is true. |
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
.
1 2 3 4 5 6 7 8 |
bool_var=true if $bool_var; then echo "The Boolean is true." else echo "The Boolean is false." fi |
1 2 3 |
The Boolean is false. |
Use if
with [[ ]]
Command
Use the if
statement with the [[ ]]
command to check if boolean is true in Bash.
1 2 3 4 5 6 7 8 |
bool_var=true if [[ $bool_var == true ]]; then echo "The Boolean is true." else echo "The Boolean is false." fi |
1 2 3 |
The Boolean is true. |
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.
1 2 3 4 5 6 7 8 |
bool_var=false if [[ $bool_var == true ]]; then echo "The Boolean is true." else echo "The Boolean is false." fi |
1 2 3 |
The Boolean is false. |
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.
1 2 3 4 5 6 7 8 |
bool_var=1 if (( $bool_var )); then echo "The Boolean is true." else echo "The Boolean is false." fi |
1 2 3 |
The Boolean is true. |
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
.
1 2 3 4 5 6 7 8 |
bool_var=0 if (( $bool_var )); then echo "The Boolean is true." else echo "The Boolean is false." fi |
1 2 3 |
The Boolean is false. |
Use if
with -eq
Operator
Use the if
statement with the -eq
operator to check if Boolean is true
in Bash.
1 2 3 4 5 6 7 8 |
bool_var=1 if [[ $bool_var -eq 1 ]]; then echo "The Boolean is true." else echo "The Boolean is false." fi |
1 2 3 |
The Boolean is true |
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.
1 2 3 4 5 6 7 8 9 10 11 |
bool_var=true case $bool_var in true) echo "The Boolean is true." ;; false) echo "The Boolean is false." ;; esac |
1 2 3 |
The Boolean is true |
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 thecase
statement. It matched the value of$bool_var
when it was equal totrue
.false)
– Denoted the second pattern that was being validated in thecase
statement. It matched the$bool_var
variable’s value when it was equal tofalse
.
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.
1 2 3 4 5 6 7 8 9 |
mobile_missing=false if [ $mobile_missing ] then echo "Lost Mobile." else echo "Not Lost Mobile." fi |
1 2 3 |
Lost Mobile. |
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 returnstrue
. In contrast, the-z
checks if the mentioned string operand size is zero; it returnstrue
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
mobile_missing=false case $mobile_missing in true) echo "Lost Mobile." ;; false) echo "Not Lost Mobile." ;; *) echo "Invalid Input." ;; esac |
1 2 3 |
Not Lost Mobile. |
Let’s have another example where the missing_mobile
was set to an empty string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
mobile_missing="" case $mobile_missing in true) echo "Lost Mobile." ;; false) echo "Not Lost Mobile." ;; *) echo "Invalid Input." ;; esac |
1 2 3 |
Invalid Input. |
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
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
bool_var1=true bool_var2=false case "$bool_var1$bool_var2" in "00" | "falsefalse") echo "Both variables are false." ;; "01" | "falsetrue") echo "'bool_var1' is false and 'bool_var2' is true." ;; "10" | "truefalse") echo "'bool_var1' is true and 'bool_var2' is false." ;; "11" | "truetrue") echo "Both variables are true." ;; *) echo "Invalid input." ;; esac |
1 2 3 |
'bool_var1' is true and 'bool_var2' is false. |
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:
"00" | "falsefalse")
– This pattern matched when both variables were set tofalse
."01" | "falsetrue")
– It matched whenbool_var1
wasfalse
andbool_var2
wastrue
."10" | "truefalse")
– It matched whenbool_var1
wastrue
andbool_var2
wasfalse
."11" | "truetrue")
– This pattern matched when both variables were set totrue
.*)
(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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
bool_var1=0 bool_var2=1 case "$bool_var1$bool_var2" in "00" | "falsefalse") echo "Both variables are false." ;; "01" | "falsetrue") echo "'bool_var1' is false and 'bool_var2' is true." ;; "10" | "truefalse") echo "'bool_var1' is true and 'bool_var2' is false." ;; "11" | "truetrue") echo "Both variables are true." ;; *) echo "Invalid input." ;; esac |
1 2 3 |
'bool_var1' is false and 'bool_var2' is true. |
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.