Linux Basics: Calculate Small Integers in Bash
Bash can calculate counts, durations, and small integer totals without an external calculator. Learn the difference between expanding a result, validating decimal input, and running an arithmetic command.
These examples were tested on Linux with GNU Bash 5.2.37. Use Bash rather than sh. No administrator access is needed.
Prepare a scratch directory
You need Bash, a text editor, and the mktemp, rm, and rmdir utilities. Keep using the same terminal. Save each named source file in this scratch directory before running its command. Calling bash explicitly avoids changing executable permissions.
start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 11. Split a duration into hours, minutes, and seconds
Save this as arithmetic-duration.sh:
#!/usr/bin/env bash
seconds=3671
hours=$((seconds / 3600))
minutes=$((seconds % 3600 / 60))
remaining=$((seconds % 60))
printf '%d seconds = %dh %dm %ds\n' "$seconds" "$hours" "$minutes" "$remaining"Run it:
bash arithmetic-duration.shOutput:
3671 seconds = 1h 1m 11s$((...)) expands to the computed integer. Division here discards the fractional part, and % gives the remainder. Variable names can appear directly inside the expression. This conversion assumes a nonnegative duration.
2. Validate decimal digits before calculating
Save this as arithmetic-decimal.sh:
#!/usr/bin/env bash
export LC_ALL=C
value=${1-}
if [[ $# -ne 1 || ! $value =~ ^[0-9]{1,6}$ ]]; then
printf 'Expected one argument of 1 to 6 decimal digits\n' >&2
exit 2
fi
number=$((10#$value))
printf '%s + 1 = %d\n' "$value" "$((number + 1))"Run it:
bash arithmetic-decimal.sh 008
bash arithmetic-decimal.sh 12x 2>&1
printf 'Invalid input status: %d\n' "$?"Output:
008 + 1 = 9
Expected one argument of 1 to 6 decimal digits
Invalid input status: 2The regular expression accepts only a bounded, unsigned decimal string. 10# makes 008 decimal rather than an invalid octal literal. Validate before arithmetic: Bash can interpret variable contents as expressions, so arithmetic itself is not an input validator. The six-digit limit also keeps this example well within the integer range. The second invocation deliberately shows the error and its status.
3. Understand a zero result’s command status
Save this as arithmetic-status.sh:
#!/usr/bin/env bash
jobs=1
((jobs -= 1))
status=$?
printf 'Jobs: %d; arithmetic status: %d\n' "$jobs" "$status"
jobs=$((jobs + 1))
status=$?
printf 'Jobs: %d; assignment status: %d\n' "$jobs" "$status"Run it:
bash arithmetic-status.shOutput:
Jobs: 0; arithmetic status: 1
Jobs: 1; assignment status: 0The ((...)) command returns status one when its expression evaluates to zero, even though the calculation succeeded. A plain assignment using valid arithmetic expansion returns zero here. This matters with set -e: a standalone arithmetic command producing zero can stop the script. Choose an assignment for a counter update when you do not need an arithmetic test.
Keep in mind
Bash arithmetic uses fixed-width integers and does not detect overflow. It does not calculate fractional values, and division by zero is an error. Keep counts bounded and use a suitable numeric language or calculator for large values or decimal arithmetic.
Clean up
In the same terminal, remove only the files created for these examples, return to your original directory, and remove the empty scratch directory:
rm -- arithmetic-duration.sh arithmetic-decimal.sh arithmetic-status.sh
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dirReferences
- GNU Bash manual: Shell Arithmetic
- GNU Bash manual: Arithmetic Expansion
- GNU Bash manual: Conditional Constructs
Acknowledgements
This post was created with assistance from AI.