Shell Scripting
Boolean Variables
Programming Languages
Coding Tips
Script Variables

How can I declare and use Boolean variables in a shell script?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In shell scripting, Boolean variables are used to control the flow of execution in scripts, enabling conditional behavior based on whether conditions are true or false. Unlike some programming languages that have a dedicated Boolean data type (true and false), shell scripting primarily relies on exit status codes to represent Boolean variables — where 0 (zero) typically indicates success (true), and any non-zero value indicates failure (false).

Declaring Boolean Variables

In shell scripts, you don’t formally declare a data type. Variables can be treated as Booleans by considering their values in conditional expressions. The typical practice is to use commands that return exit statuses and to evaluate these statuses directly in conditional statements.

Here's how you can set and use Boolean-like variables:

bash
1#!/bin/bash
2
3# Example of setting a Boolean condition
4file_exists="false"
5if [ -e "/path/to/your/file" ]; then
6  file_exists="true"
7fi
8
9# Using the Boolean variable in a conditional statement
10if [ "$file_exists" = "true" ]; then
11  echo "File exists."
12else
13  echo "File does not exist."
14fi

In the above example, file_exists is a string that represents a Boolean condition by convention rather than by data type. The actual check for file existence is performed by the [ -e "/path/to/your/file" ] command, which sets the variable.

Working with Exit Statuses

More traditionally, shell scripts use exit statuses directly for Boolean expressions like this:

bash
1#!/bin/bash
2
3if [ -e "/path/to/your/file" ]; then
4  echo "File exists."
5else
6  echo "File does not exist."
7fi

Here, the [ -e "/path/to/your/file" ] command itself checks whether a file exists (true if it does, false otherwise), directly influencing the flow of the if-else statement.

Using Boolean Operators

Shell scripts also support Boolean operators such as && (AND) and || (OR) which allows combining multiple conditions:

bash
1#!/bin/bash
2
3# AND operation
4if [ -e "/path/to/file1" ] && [ -e "/path/to/file2" ]; then
5  echo "Both files exist."
6else
7  echo "At least one file does not exist."
8fi
9
10# OR operation
11if [ -e "/path/to/file1" ] || [ -e "/path/to/file2" ]; then
12  echo "At least one file exists."
13else
14  echo "Neither file exists."
15fi

Using the test Command

The test command evaluates the expression provided to it and exits with a status code (0 if true, non-zero if false). It is equivalent to using [ expression ].

bash
1#!/bin/bash
2
3if test -e "/path/to/your/file"; then
4  echo "File exists."
5else
6  echo "File does not exist."
7fi

Negating Conditions

You can negate a condition using !, which inverses the truthiness of an expression:

bash
1#!/bin/bash
2
3if ! [ -e "/path/to/your/file" ]; then
4  echo "File does not exist."
5else
6  echo "File exists."
7fi

Summary Table

ConceptSyntaxDescription
Direct Conditionif [ condition ]; then ... fiExecutes based on the evaluation of condition.
AND Operatorif [ cond1 ] && [ cond2 ]; then ... fiTrue if both conditions are true.
OR Operatorif \[ cond1 ] | | \[ cond2 ]; then ... fiTrue if at least one condition is true.
Negationif ! [ condition ]; then ... fiTrue if the condition is false.
Test Commandif test condition; then ... fiAn alternative to [ condition ].

Conclusion

In shell scripting, Boolean variables and expressions are fundamental for directing the flow of scripts based on conditions. Understanding how to use these alongside conditional statements, logical operators, and the test command, is crucial for effective scripting.


Course illustration
Course illustration

All Rights Reserved.