HOWTO · Bash

How to Declare and Utilize Booleans in Bash

Learn how Bash represents Boolean-like state, how command exit statuses work, and how to use safe conditions, integer flags, logical operators, functions, and loops.

On this page

Bash has no dedicated Boolean variable type. To represent a choice, use a command’s success or failure status directly, or choose a value convention such as 1 for enabled and 0 for disabled. These are different ideas: a command status of 0 means success, while nonzero means failure. The examples below show how to use both forms without treating arbitrary variable contents as commands.

Boolean Values and Exit Status in Bash

The commands true and false report success and failure, respectively. Their status is what an if statement evaluates; they are commands, not special values stored in variables. A script can also use a documented integer flag when a value needs to be stored or passed as an argument.

Use true and false in Conditions

Use a command directly as a condition when its success or failure is the question. This lets Bash branch on the command’s exit status.

if true; then
    printf '%s\n' 'The command succeeded.'
fi

if false; then
    printf '%s\n' 'This branch is skipped.'
else
    printf '%s\n' 'The command reported failure.'
fi

Avoid writing if $value; then to test a variable. Bash would try to run the variable’s contents as a command. Compare a known value or use a test expression instead.

Represent Boolean-Like State in Variables

An integer flag is an application convention, so choose and document its mapping. This example uses 1 for enabled and 0 for disabled. Arithmetic conditions compare it numerically and do not confuse it with command status.

Run this Bash example to see the enabled flag checked numerically, passed to a function, and used to control loop output.

enabled=1

if (( enabled == 1 )); then
    printf '%s\n' 'Feature is enabled.'
else
    printf '%s\n' 'Feature is disabled.'
fi

report_feature() {
    local flag=$1
    if (( flag == 1 )); then
        printf '%s\n' 'Function received an enabled flag.'
    else
        printf '%s\n' 'Function received a disabled flag.'
    fi
}

report_feature "$enabled"

for item in alpha beta; do
    if (( enabled == 1 )); then
        printf 'Processing %s.\n' "$item"
    fi
done
Feature is enabled.
Function received an enabled flag.
Processing alpha.
Processing beta.

For file checks, prefer the appropriate test expression. Quote expansions in [ tests so a missing or whitespace-containing value does not change the test’s arguments. Use [[ ... ]] or arithmetic syntax for Bash-specific conditions.

Combine Conditions with Logical Operators

&& runs the command on its right only when the command on its left succeeds. || runs the right side when the left side fails. Both operators therefore use command status, where zero is success. Parenthesize or use separate if statements when the intended grouping would otherwise be hard to read.

is_logged_in=1
is_admin=0

if (( is_logged_in == 1 && is_admin == 1 )); then
    printf '%s\n' 'Access granted.'
else
    printf '%s\n' 'Access denied.'
fi

config_file=/etc/example.conf
if [[ -f "$config_file" ]] && printf '%s\n' 'Configuration file exists.'; then
    :
fi

The first condition combines two integer flags. The second checks a file before printing its message; [[ -f ... ]] is a Bash conditional expression, not a command that produces a Boolean variable.

Use Flags in Functions and Loops

Pass an explicitly defined flag as a quoted function argument, then compare it using the same convention. A loop can read or update that state to decide whether to continue. Keep the loop’s stopping condition clear so it cannot continue indefinitely.

keep_running=1
step=0

while (( keep_running == 1 )); do
    (( step += 1 ))
    printf 'Step %d.\n' "$step"
    if (( step >= 3 )); then
        keep_running=0
    fi
done

Arithmetic commands return a status based on the expression’s value. Here, the increment is positive, and the loop condition uses the stored flag to stop after the counter reaches three.

Common Boolean Mistakes in Bash

  • Do not assume Bash treats 0 as true and 1 as false. That mapping is a convention you define; shell command status uses zero for success and nonzero for failure.
  • Do not expand an arbitrary variable as a command to ask whether it is true. Compare its value or use a conditional expression.
  • Quote variable expansions in single-bracket tests, as in [ "$name" = "ready" ]. For numbers, use (( number == 1 )) or quote operands in a numeric test.
  • Remember that && and || respond to command success, not to words such as true and false stored in a variable.

Choose one flag convention, use it consistently in assignments and comparisons, and use command status when you need to test whether an operation succeeded.