How to Declare Bash Boolean Variable in a Shell Script

Bash scripting is an essential skill for Linux and Unix system administrators. It enables them to automate tasks and develop efficient workflows. Understanding how to declare and utilize boolean variables within a Bash script is at the core of script development. 

In this blog, we will explore the concept of boolean variables and the different ways of declaring them in a shell script.

Understanding Boolean Variables

In Bash scripting, a boolean variable is a type of variable that can have only two values: true or false. While Bash itself does not have a native boolean data type, it leverages integer values to represent boolean conditions. Conventionally, 0 is considered false, and any non-zero integer is treated as true.

For simplicity and readability, developers often use the terms true and false to represent boolean values in Bash scripts. However, it's essential to remember that Bash interprets these as integer values under the hood.

New to Linux and Scripting? Check out our beginner courses:

Let's dive into how to declare boolean variables in a shell script.

#1. Using Integer Values

In Bash, boolean variables can be represented using integer values, where 0 stands for false and any non-zero value stands for true. Let's consider a simple example:

#!/bin/bash

# Declare a boolean variable using integer values
is_enabled=1

# Check the boolean variable
if [ $is_enabled -eq 1 ]; then
	echo "Application enabled."
else
	echo "Application disabled."
fi

In this example, is_enabled is set to 1, indicating that the application is enabled. The script checks this value and prints the corresponding message.

Learn How to Run Shell Script.

#2. Using Strings

Another approach is to use strings to represent boolean values. Conventionally, "true" and "false" are common choices:

#!/bin/bash

# Declare a boolean variable using strings
application_enabled="true"

# Check the boolean variable
if [ "$application_enabled" == "true" ]; then
	echo "Application enabled."
else
	echo "Application disabled."
fi

Here, we use string comparison to determine whether the application is enabled or disabled.

#3. Using Arithmetic Evaluation

Bash allows the use of arithmetic evaluation to handle boolean values. This approach is concise and often preferred:

#!/bin/bash

# Declare a boolean variable using arithmetic evaluation
application_enabled=true

# Check the boolean variable
if $application_enabled; then
	echo "Application enabled."
else
	echo "Application disabled."
fi

The variable application_enabled is directly used in the if statement. If the variable is true, the corresponding block of code is executed.

#4. Using 'true' and 'false' Commands

The true and false commands in Bash can be used to represent boolean values directly:

#!/bin/bash

# Declare a boolean variable using 'true' and 'false'
application_enabled=true

# Check the boolean variable
if $application_enabled; then
	echo "Application enabled."
else
	echo "Application disabled."
fi

In this example, we set application_enabled to true, and the if statement checks if the application is enabled or disabled.

#5. Using Conditional Ternary Operator

Bash doesn't have a built-in ternary operator, but we can mimic its behavior using a combination of if-else statements:

#!/bin/bash

# Declare a boolean variable using a conditional ternary operator
application_enabled=true

# Ternary operator simulation
message=$([ "$application_enabled" == true ] && echo "Application enabled." || echo "Application disabled.")

# Print the message
echo $message

Here, the script assigns a message based on the boolean value, simulating a ternary operator behavior.

#6. Using Functions

Organizing your script with functions can make the script more modular. The function can return true or false as boolean values. 

#!/bin/bash

# Define a function to check the application status
check_application() {
	local application_enabled=true
	echo $application_enabled
}

# Call the function and check the boolean variable
if $(check_application); then
	echo "Application enabled."
else
	echo "Application disabled."
fi

In this example, the check_application function returns a boolean value, and the main script checks and prints the corresponding message.

Learn How to Return Value From a Bash Function.

Use Cases

Here are some common use cases for boolean variables in shell scripts:

  • We can use a boolean variable to control various configurations or settings in your script.
  • We can use it to determine whether a feature should be enabled or disabled.
  • We can use it in conjunction with conditional statements to determine which parts of the script should be executed.
  • We can use it to flag error conditions. For example, you might set an ERROR_OCCURRED variable to true if an unexpected situation arises.
  • We can use it to control loop iterations.

Bash Variable Best Practices

Here are some best practices for working with variables in Bash scripts:

  • Initialize variables with default values to prevent unexpected behavior.
  • Choose variable names that clearly indicate their purpose.
  • If a variable's value should not be changed, declare it as read-only to prevent accidental modifications.
  • By convention, reserve uppercase variable names for environment variables and system variables to prevent unintentional overwriting.
  • Before using a variable, check if it exists to avoid errors, especially when dealing with user inputs.

Conclusion

Understanding various ways to declare boolean variables in Bash gives you flexibility when writing clear and concise scripts. Whether you’re using integer values, strings, arithmetic evaluation, or other methods, choose the approach that aligns with your script's requirements and enhances readability. Experiment with these techniques to become proficient in leveraging boolean variables effectively in your bash scripts.

Want to master Bash Boolean Variables? Check out our course below.

Advanced Bash Scripting | KodeKloud