Linux Basics: Make Decisions with Bash Conditionals
A useful script makes choices. Bash can compare numbers, test text patterns, and select an action from named options. Learn which test fits each job using examples that only print their decisions.
These examples were tested on Linux with GNU Bash 5.2.37. Use Bash rather than sh; some syntax is Bash-specific. No packages or administrator access are needed.
Prepare a scratch directory
You need Bash, a text editor, and the mktemp, rm, and rmdir utilities. Keep using the same terminal. The guards stop the shell if setup fails. Save each named source file in this directory before running its command. Calling bash explicitly means you do not need to change executable permissions.
start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 11. Choose between numeric ranges
Save this as condition-ranges.sh:
#!/usr/bin/env bash
count=7
if (( count == 0 )); then
printf 'No jobs\n'
elif (( count < 10 )); then
printf 'Small batch: %s\n' "$count"
else
printf 'Large batch: %s\n' "$count"
fiRun it:
bash condition-ranges.shOutput:
Small batch: 7if runs the first branch whose test succeeds. In arithmetic tests, a nonzero expression is true and produces shell status zero. These values are fixed integers; validate external input before using it in arithmetic expressions.
2. Distinguish literal text from a pattern
Save this as condition-patterns.sh:
#!/usr/bin/env bash
file='report final.csv'
expected='report final.csv'
if [[ "$file" == "$expected" ]]; then
printf 'Exact name matches\n'
fi
if [[ "$file" == *.csv ]]; then
printf 'CSV pattern matches\n'
fi
if [[ "$file" == '*.csv' ]]; then
printf 'Literal wildcard matches\n'
else
printf 'Literal wildcard does not match\n'
fiRun it:
bash condition-patterns.shOutput:
Exact name matches
CSV pattern matches
Literal wildcard does not matchInside [[ ... ]], an unquoted right-hand side of == is a shell pattern. Quoting it requests literal matching. This is not a regular expression test. Leave spaces around [[, ]], and the operator. Unlike ordinary unquoted command arguments, words inside this construct are not split or expanded into matching filenames.
3. Dispatch named choices with case
Save this as condition-case.sh:
#!/usr/bin/env bash
for mode in preview check publish; do
case "$mode" in
preview|check) printf '%s: read-only route\n' "$mode" ;;
publish) printf '%s: approval needed\n' "$mode" ;;
*) printf '%s: unknown option\n' "$mode" ;;
esac
doneRun it:
bash condition-case.shOutput:
preview: read-only route
check: read-only route
publish: approval neededcase selects the first matching pattern. | groups alternatives, ;; ends a branch, and the final * provides a fallback. This example only prints labels; it does not publish or change anything. Put specific patterns before the catch-all.
Clean up
In the same terminal, remove only the example files, return to your original directory, and remove the empty scratch directory:
rm -- condition-ranges.sh condition-patterns.sh condition-case.sh
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dirReference
The GNU Bash manual: Conditional Constructs documents the syntax used here.
Acknowledgements
This post was created with assistance from AI.