Boolean variables (Bash)

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen

In Bash, boolean variables are typically represented using integers, where 0 stands for false and any non-zero value represents true. You can use this concept to create boolean variables and perform conditional checks:

#!/bin/bash

# Initializing boolean variables
is_true=1
is_false=0

# Conditional checks
if [ $is_true -eq 1 ]; then
  echo "is_true is true"
fi

if [ $is_false -eq 0 ]; then
  echo "is_false is false"
fi

# Changing boolean variable values
is_true=0
is_false=1

# Performing checks after changing values
if [ $is_true -eq 1 ]; then
  echo "is_true is true"
else
  echo "is_true is false"
fi

if [ $is_false -eq 0 ]; then
  echo "is_false is false"
else
  echo "is_false is true"
fi

In this example, we're using integer values (0 and 1) to represent boolean variables (is_true and is_false). We change the values and perform conditional checks using the -eq operator to compare integers.