Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: December 22, 2024
A shell script doesn’t have any concept of a boolean datatype. Instead, we can construct boolean variables, which allows us to store and handle true or false values as boolean values. The best practice to represent a boolean value most likely depends on the use case of a script.
In this tutorial, we’ll discuss best practices for representing a boolean value in a shell script.
Conventionally, but not always, any integer value that is 0 is regarded as false, while any non-zero integer is considered true, in the context of Bash scripts. We’ll be sticking with this convention in the examples that follow.
Although shell scripts don’t have a native boolean datatype, we can emulate boolean behavior using variables.
To illustrate, let’s use an example:
#!/bin/bash
is_active=true
# Using the boolean variable in an if condition
if [ "$is_active" == true ]; then
echo "The feature is active."
else
echo "The feature is inactive."
fi
Let’s understand the above script:
Above, the variable is_active mimics a boolean value, allowing us to implement boolean-like logic in shell scripts.
The boolean values can be presented as integers (0 or 1) when a particular condition needs to be tested. To demonstrate that, we’ll go over some examples.
For our first example, let’s look at the contents of our example.sh Bash file:
#!/bin/bash
val=0
if [ $val -eq 1 ]; then
echo "True"
else
echo "False"
fi
Let’s break down the script above:
Now, let’s execute the Bash script and see its output:
$ bash example.sh
$ True
Other than an if-else statement, the integer boolean values work best with loops.
Let’s see an example in which we execute a while loop using the integer boolean value.
Let’s say we have a variable continue that represents a boolean integer value 0 (False):
#!/bin/bash
continue=0
count=0
while [ $continue -eq 0 ]; do
count=$((count + 1))
echo "count $count Running ..."
if [ $count -ge 3 ]; then
continue=1
fi
sleep 1
done
echo "Completed..."
Now, let’s explore the script we’ve used within this example:
Now, let’s see the output of the Bash script:
count 1 Running ...
count 2 Running ...
count 3 Running ...
Completed...
Here’s what we’ve learned from the above output:
Other than integers, boolean values can be presented as strings, true or false.
Let’s see an example of using a string as a boolean value in a Bash script.
To demonstrate, we’re using the environment variable $RANDOM to identify whether the random integer value is even or odd:
#!/bin/bash
r=$RANDOM
echo "Random Number: $r"
if (( r % 2 == 0 )); then
isEven="true"
else
isEven="false"
fi
echo "Is $r Even? : $isEven"
Let’s have a look at the Bash code we’ve used within this illustration:
Now, let’s execute the file example.sh:
Random Number: 18188
Is 18188 Even? : true
In return, the script has been successfully executed, identifying the random number 18188 as an Even value.
To enhance a script’s modularity, we can use functions with the exit statements and control the execution flow of a shell script.
The exit code return 0 represents the success of a script without any errors in Unix-based systems, whereas the exit code return 1 represents the failure of a script when a particular error is found or a condition isn’t met.
Let’s have a Bash illustration for using exit statuses in the function.
To demonstrate, we’re creating a file alpha.sh within the current working directory and using it within the script via the variable fname:
#!/bin/bash
fname="alpha.sh"
testfunc() {
if [ -e "$fname" ]; then
return 0
else
return 1
fi
}
testfunc
if [ $? -eq 0 ]; then
echo "File exists"
else
echo "File doesn't exist"
fi
Now, let’s explore the Bash code below:
On running the above Bash script, this is what we can learn from the output:
File doesn’t exist
The file alpha.sh doesn’t exist in the current working directory; thus, the testfunc() returns the exit status 1.
After capturing the exit code 1 from the function, the else part of the if-else statement is executed, and prints File doesn’t exist.
In this article, we’ve seen a few ways to represent boolean values in Bash scripts. These include using integers 0 and 1 or the strings true and false as boolean values.
Lastly, the exit status codes return 0 and return 1 are used as examples to demonstrate their working in functions to enhance modularity. We can choose between the different techniques based on preference or the given use case.