How to Declare and Use Boolean Variables in Bash Scripts
This guide explains how to create, test, and apply boolean-like variables in Bash, covering numeric (0/1) and string (true/false) representations, conditional checks, and practical examples for cron jobs and backup scripts on Linux/Unix systems.
Bash does not have a native boolean type, but you can represent boolean values using numeric 0 (false) and 1 (true) or the strings "true" and "false". The article shows how to declare such variables, test them, and use them in real‑world scripts.
Declaring Boolean Variables
You can assign numeric values:
failed=0 # false
jobdone=1 # trueOr use more readable string literals:
failed=false
jobdone=trueTesting Numeric Booleans
When the variable holds 0 or 1, use a numeric comparison:
if [ $failed -eq 1 ]
then
echo "Job failed"
else
echo "Job done"
fiTesting String Booleans
When using "true"/"false" strings, compare the values as strings:
failed="false"
if [ "$failed" == "true" ]
then
echo "Job failed"
else
echo "Job done"
fiYou can also use the double‑bracket syntax for string comparison:
bool=false
if [[ "$bool" = true ]]
then
echo "Done."
else
echo "Failed."
fiPractical Example: Backup Script with Failure Flag
A sample script shows how to initialize failed=0, run rsync for multiple servers, set failed=1 if any rsync command returns a non‑zero status, and then send an email alert if failed equals 1.
#!/bin/bash
source "/apps/.keychain/$HOSTNAME-sh"
source "/apps/scripts/cli_app.sh"
failed=0
D="/nfsefs/ec2mum/prodwwwroot"
log="/tmp/server.log.$$.txt"
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>"${log}" 2>&1
for s in www-0{1..8}
do
/usr/bin/rsync -az --delete backupt@${s}:/data/apps/ ${D}/${s}/
[ $? -ne 0 ] && failed=1
done
if [ $failed -eq 1 ]
then
echo "$0 script error: rsync backup failed. See attached log file." |
mail -A ${log} -s "$HOSTNAME - LXD backup failed" -r sysuser@my-corp-tld alert@somewhere-tld
push_to_mobile "$0" "Backup failed at $HOSTNAME"
fi
[ -f "${log}" ] && rm -f "${log}"Additional Boolean Checks
The article also demonstrates capturing command exit status, scanning logs for errors, and sending alerts based on combined conditions:
status=$?
alogs="$(egrep -w '^ERROR:|ERROR' $log)"
if [ $status -ne 0 ] || [ "$alogs" != "" ]; then
# send failure notification
else
# success handling
fiSummary
By treating 0/1 or "true"/"false" as boolean equivalents, Bash scripts can implement clear success/failure logic, especially for cron jobs, backups, and monitoring tasks. Refer to man bash, help test, and help if for further details.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
