FUNDAMENTALS A Complete Guide for Beginners
Bash does not include any distinct Boolean data type, rather Boolean logic can be utilized by defining variables with integer values such as 0 and 1 or strings such as ‘False’ and ‘True’. In Bash, 0 (zero) remarks ‘false’, and 1 (or any non-zero integer) remarks ‘true’, which represent Boolean states. In this article, I will state how you can implement the if statement for different variations in Bash to check whether a Boolean (value or Boolean-like variable) is true or false.
What is ‘if True’ or ‘if False’ Construct in Bash?
In Bash, the ‘if true’ or ’if false’ construct is the block of code that executes when a certain condition evaluates to ‘true’ or ‘false’ inside the ‘if’ statement. This explicit set of instructions within the conditional statement controls the execution of the script and determines the Boolean values of the script. The basic syntaxes of these constructs are as follows:
Basic Syntax of ‘if true’:
if true; then
#Code to execute
fi
Basic Syntax of ‘if false’:
if false; then
#Code to execute
fi
5 Ways to Check if a Boolean Value is True in Bash
To check if a Boolean value is true, Bash offers several options like the ‘test’ command, “[[ ]]” command, “(( ))” command, and “-eq” operator. Also, a direct comparison of a Boolean-like variable within an ‘if’ conditional can be performed to accomplish the Boolean check.
In the following part, I will discuss 5 ways to check whether a Boolean value is true in Bash:
1. Using “test” Command With “=” Operator
The ‘test’ command can be represented as an equivalent test operator “[ ]” which is helpful for any conditional checking in Bash. So, to check if a Boolean value is ‘true’, you can use the syntax if [ "$boolean_variable" = true ]
. Here’s an example:
#!/bin/bash
#Declaring a boolean variable as true
boolean_variable=true
#Checking if the boolean variable is true
if [ "$boolean_variable" = true ]; then
echo "The Boolean is true"
fi
The script first declares a variable boolean_variable as ‘true’, checks if the variable is true using the “=” operator within the test command “[ ]” and then displays the output using the echo command.
The above image portrays that when I inserted the ‘test’ command within the ‘if’ conditional, it satisfied the condition and so is the result ‘The Boolean is true’.
A similar process can be implemented to check if a Boolean value is false where you need to assign the Boolean-like variable as ‘false’. In that case, use the syntax: [ "$boolean_variable" = false ]
.
2. Placing Variable Directly With ‘if’ Conditional
Placing a variable directly with a conditional and expanding its value is another way to check if a Boolean value is actually true or not and it requires a very simple syntax i.e. if $boolean_variable
. Here’s a demonstration for you:
#!/bin/bash
#Declaring a boolean variable as true
boolean_variable=true
#Checking if the boolean variable is true
if $boolean_variable; then
echo "The Boolean is true"
fi
First, the script declares a variable as ‘true’. Then, it encounters an ‘if’ statement and expands the value of the boolean_variable directly to check if it is true. If it satisfies the condition, the script echoes the specific result for the ‘if true’ section.
From the above image, you can see that when I used the variable directly inside the ‘if’ conditional, it satisfied the condition and addressed that the value is ‘true’.
3. Utilizing “[[ ]]” Command With “==” Operator
The “[[ ]]” command works in the same way as “[ ]” but it’s the improved version that offers more flexibility and control over pattern matching and complicated conditional checks.
To check if a Boolean value is true, you can use this “[[ ]]” command with the string equality operator “==” following the syntax if [[ $boolean_variable == true ]]
. Here’s an example for you to get a clear hint:
#!/bin/bash
#Declaring a boolean variable as true
boolean_variable=true
#Checking if the boolean variable is true
if [[ $boolean_variable == true ]]; then
echo "The Boolean is true"
fi
The above script checks if the value of the declared variable is actually true by using the “==” operator within the “[[ ]]” command and prints the output accordingly using the echo command.
This image shows that the declared Boolean variable satisfies the true condition.
Same method can be implemented to check if a Boolean value is false where you need to assign the Boolean-like variable as ‘false’. In that case, use the syntax: if [[ $boolean_variable == false ]]
.
4. Leveraging “(( ))” Command
In Bash, “(( ))” emerges as a powerful command for arithmetic expansion operations. So, leverage this command using the syntax if (( $boolean_variable ))
to check if the Boolean value is true. Check out the following example:
#!/bin/bash
#Declaring a boolean variable as true
boolean_variable=1
#Checking if the boolean variable is true
if (( $boolean_variable )); then
echo "The Boolean is true"
fi
The above script checks whether the declared variable is actually holding a non-zero value using the “(( ))” command and if it does so, the script displays the true output using the echo command.
The above image is a clear indication of Boolean evaluation where the “(( ))” command interprets the Boolean-like variable as true for the non-zero value ‘1’.
5. Using “-eq” Operator Within “[[ ]]”
The “-eq” operator can be used for Boolean checks explicitly though it is designed for numeric equality checking. So, if you want to check if a Boolean value is true, use the syntax if [[ $boolean_variable -eq 1 ]]
. Follow the example:
#!/bin/bash
#Declaring a boolean variable as true
boolean_variable=1
#Checking if the boolean variable is true
if [[ $boolean_variable -eq 1 ]]; then
echo "The Boolean is true"
fi
The script declares boolean_variable as 1 (non-zero integer remarks ‘true’) and then performs a checking by using the “-eq” operator inside the “[[ ]]”. Lastly, it displays the output employing the echo command if the variable is equal to the assigned value.
In the above image, you can see that the Boolean-like variable becomes true when it equals the non-zero integer value ‘1’.
Similar process can be implemented to check if a Boolean value is false where you need to assign the Boolean-like variable as ‘0’. In that case, use the syntax: if [[ $boolean_variable == 0 ]]
.
2 Ways to Check if a Boolean Value is False in Bash
To check if a Boolean value is false, Bash offers various options like the NOT (!) operator, “(( ))” command, ‘test’ command, “[[ ]]” command, and “-eq” operator.
The process using the ‘test’ command, “[[ ]]” command, and “-eq” operator is already discussed in the previous section. So, I will give an overview of 2 different ways to check whether a Boolean value is false in Bash in the subsequent portion:
1. Utilizing Logical NOT (!) Operator
The NOT (!) operator always negates the value of an assigned variable in Bash.
Use the syntax if ! $is_boolean
to check if a Boolean value is false. Here’s how:
#!/bin/bash
#Declaring a boolean variable as false
is_boolean=false
#Checking if the boolean variable is false
if ! $is_boolean; then
echo "The Boolean is false."
fi
In the script, after declaring is_boolean as ‘false’, the ‘if’ conditional checks whether the variable is not true i.e. false, and if it meets the condition, the script echoes the output ‘The Boolean is false.’.
From the above image, you can see that using the NOT (!) operator, I have easily checked the falseness of a Boolean variable in Bash.
2. Using “(( ))” Command With “==” Operator
Alternatively, you can check if a Boolean value is false using the “(( ))” command with an equality checking operator “==” inside an ‘if’ statement. if (( is_boolean == 0 ))
is the syntax you need to use to do so. For instance:
#!/bin/bash
#Declaring a boolean variable as false
is_boolean=0
#Checking if the boolean variable is false
if (( is_boolean == 0 )); then
echo "The Boolean is false."
fi
In this script, the ‘if’ conditional checks whether the numeric value of the declared variable is_boolean is ‘0’. The check is done using the “(( ))” command generally used for arithmetic operations. When the condition becomes true, it means the Boolean value is equal to 0 which represents ‘false’ and so the script prints ‘The Boolean is false.’.
The above image states that the assigned Boolean-like variable is false represented numerically in the script.
Conclusion
So far, you have learned several ways to check whether a Boolean-like variable or Boolean value is true or false. Just choose the method that fits your script’s requirements and suits the data type you are dealing with. Happy coding!
People Also Ask
Can I use commands other than ‘true’ for the true condition?
Yes, Bash gives you the flexibility to replace ‘true’ with any command that consistently returns a zero exit status.
How can I prompt the user for boolean input and check it in Bash?
To prompt the user for Boolean input, use the ‘read’ command and to check it in bash, use the syntax [[ $boolean_input == true ]]
or [ "$boolean_input" = "true" ]
.
Are there dedicated boolean operators in Bash?
No, there are no dedicated Boolean operators in Bash. But numeric and string equality operators like ‘==’ or ‘-eq’ can be used for Boolean checks.
Are there any alternative constructs or commands for boolean checks in Bash?
Yes, there are alternatives for Boolean checks in Bash such as functions, ‘case’ statements, and ternary operators.
Can I use logical operators with ‘if true’ and ‘if false’ to create more complex conditions?
Yes, you can use logical operators like ‘&& (AND)’ ‘|| (OR)’ with ‘if true’ and ‘if false’ inside an ‘if’ statement to create more complex conditions and validate multiple Boolean-like variables in Bash. For example:
#!/bin/bash
#Declaring three Boolean-like variables
boolean_variable1=false
boolean_variable2=true
boolean_variable3=true
#Checking multiple Boolean values
if [ "$boolean_variable1" == false ] || [ "$boolean_variable2" == true ] && [ "$boolean_variable3" == true ]; then
echo "Satisfied complex Boolean conditions"
fi
Related Articles
- Mastering 10 Essential Options of If Statement in Bash
- Bash Test Operations in ‘If’ Statement
- Check If a Variable is Empty/Null or Not in Bash [5 Methods]
- Check If a Variable is Set or Not in Bash [4 Methods]
- Check If Environment Variable Exists in Bash [6 Methods]
- Bash Modulo Operations in “If” Statement [4 Examples]
- How to Use “OR”, “AND”, “NOT” in Bash If Statement [7 Examples]
- Evaluate Multiple Conditions in Bash “If” Statement [2 Ways]
- Using Double Square Brackets “[[ ]]” in If Statement in Bash
- 6 Ways to Check If a File Exists or Not in Bash
- How to Check If a File is Empty in Bash [6 Methods]
- 7 Ways to Check If Directory Exists or Not in Bash
- Negate an “If” Condition in Bash [4 Examples]
- Check If Bash Command Fail or Succeed [Exit If Fail]
- How to Write If Statement in One Line? [2 Easy Ways]
- Different Loops with If Statements in Bash [5 Examples]
- How to Use Flags in Bash If Condition? [With Example]
- Learn to Compare Dates in Bash [4 Examples]
<< Go Back to If Statement in Bash | Bash Conditional Statements | Bash Scripting Tutorial