Bash Boolean

Even though Bash does not have builtin data type and of course no boolean values, we can use the commands true and false for boolean values in bash scripts.

true command is “Do nothing, successfully.”

false command is “Do nothing, and fail.”

Examples

True

In the following script, we assign a value of true to variable x. We use this variable as condition in if-else statement.

Example.sh

x=true
if $x
then
    echo "Condition is true."
else
    echo "Condition is false."
fi

Output

Condition is true.

False

In the following script, let us assign value of false to x, and execute the same if-else statement.

Example.sh

x=false
if $x
then
    echo "Condition is true."
else
    echo "Condition is false."
fi

Output

Condition is false.
ADVERTISEMENT

Conclusion

In this Bash Tutorial, we have learnt how to realise boolean values in Bash using true and false commands.