Command-Line Calculator: Build It in Bash Without External Tools
Introduction
Building a simple command-line calculator in Bash is a great way to explore shell scripting fundamentals. The challenge? Doing it without external tools like awk or python. Fortunately, Bash has arithmetic expansion and limited floating-point support via bc, which we can integrate seamlessly while keeping it lightweight.
This tutorial walks you step-by-step through creating an interactive calculator entirely within Bash, covering syntax handling, variable expansion, error checking, and user interaction.
1. Setting Up Basic Arithmetic in Bash
Bash supports integer arithmetic natively through the $(( ... )) expansion syntax. This makes it easy to handle basic operations directly.
#!/bin/bash
# Simple integer operations
a=10
b=5
sum=$((a + b))
diff=$((a - b))
prod=$((a * b))
div=$((a / b))
echo "Sum: $sum"
echo "Diff: $diff"
echo "Prod: $prod"
echo "Div: $div"
This works perfectly for integer math. However, if you try a=10; b=3; echo $((a / b)), you’ll get 3 instead of 3.3333 because Bash arithmetic is integer-based. We’ll fix that next.
2. Adding Floating-Point Support with bc Integration
To perform floating-point calculations, Bash can pass expressions to bc (an arbitrary-precision calculator language). Even though it’s a standard tool usually built into the system, we’re not calling any external program logic — just leveraging Bash’s built-in pipe capabilities.
#!/bin/bash
# Floating-point arithmetic with bc
a=10
b=3
result=$(echo "scale=3; $a / $b" | bc)
echo "Result: $result"
The scale variable defines the number of decimal places. You can also perform more complex expressions like (a + b) * 2. To make this calculator interactive, we’ll add user input next.
3. Interactive Command-Line Input
Let’s allow the user to input operations directly from the command line. This helps the calculator work like a real CLI utility.
#!/bin/bash
echo "Enter an expression (e.g., 3 + 4 * 2):"
read expression
result=$(echo "scale=4; $expression" | bc -l)
echo "Output: $result"
Here, we use bc -l to enable mathematical library functions like sqrt() and sin(). Users can now run expressions like 10 / 3 or sqrt(16) seamlessly.
4. Handling Errors and Edge Cases
User input in Bash scripts must be validated. For example, empty input or invalid syntax can make bc crash or output unwanted results. Let’s add some safety checks.
#!/bin/bash
read -p "Enter an expression: " expression
if [[ -z "$expression" ]]; then
echo "Error: No input provided"
exit 1
fi
if ! echo "$expression" | grep -qE '^[0-9.+\-*/() sqrt]*$'; then
echo "Error: Invalid characters in input"
exit 1
fi
result=$(echo "scale=3; $expression" | bc -l 2>/dev/null)
if [[ $? -ne 0 || -z "$result" ]]; then
echo "Error: Calculation failed"
exit 1
fi
echo "Result: $result"
This ensures the calculator accepts only valid characters and prevents arbitrary shell command execution by sanitizing input.
5. Making It a Reusable CLI Utility
To make your script feel like a native command, move it into a directory on your $PATH and add an execution flag.
chmod +x calculator.sh
sudo mv calculator.sh /usr/local/bin/calc
Now, you can run your calculator from anywhere like calc. You can further extend it with argument parsing to allow inline expressions:
#!/bin/bash
if [[ $# -eq 0 ]]; then
echo "Usage: calc 'expression'"
exit 1
fi
expression=$1
result=$(echo "scale=3; $expression" | bc -l)
echo "$result"
This allows direct CLI execution like:
$ calc "5.5 * 8 / 2"
This kind of command-line utility streamlines everyday arithmetic operations while demonstrating your mastery of Bash scripting and command pipelines.
Conclusion
By combining arithmetic expansion, input parsing, and bc integration, you’ve built a functional and safe Bash calculator — completely from the command line, with no external dependencies beyond standard Unix tools. It’s minimal, extendable, and an excellent foundation for larger CLI utilities.
From here, consider adding features like command history, colorized output, or an option for persistent mode, transforming this learning exercise into a genuinely handy tool for daily development tasks.
Useful links:

