Unix-Style Calculator
Evaluate expressions like you would with bc, then optionally convert output to hex, octal, or binary.
+, -, *, /, %, ^, parentheses.Supported functions/constants:
sqrt, abs, sin, cos, tan, log, ln, exp, pow(a,b), pi, e.
Why use a calculator in Unix at all?
If you spend time in Linux, macOS Terminal, or any Unix shell, calculation comes up constantly: scripting, file sizes, percentages, date math, performance checks, and quick sanity tests. While GUI calculators are fine, terminal-native tools are faster in technical workflows because you can keep everything inside one command stream.
The phrase calculator in Unix usually refers to command-line methods such as bc, shell arithmetic expansion, awk, expr, and sometimes lightweight calls to Python or Perl. Each has a sweet spot, and knowing when to use each one can save serious time.
Core Unix calculator methods
1) Shell arithmetic: $(( ... ))
Best for quick integer math directly in Bash, Zsh, and similar shells.
echo $(( 25 * 4 + 10 ))
# 110
a=17
b=5
echo $(( a % b ))
# 2
- Fast and built into the shell.
- Great for loop counters and script logic.
- Primarily integer arithmetic (no real floating-point precision).
2) expr for older/simple scripts
expr is classic Unix. It works, but is less ergonomic than modern shell arithmetic.
expr 8 + 12
# 20
expr 17 % 3
# 2
Useful when maintaining legacy scripts, but for new scripting most developers prefer $(( )) or bc.
3) bc: the power tool
For decimal precision and advanced math, bc is the standard answer.
echo "scale=4; 10/3" | bc
# 3.3333
echo "scale=8; sqrt(2)" | bc -l
# 1.41421356
- Handles floating-point-like calculations via arbitrary precision decimal math.
- Supports custom
scaleand mathematical functions with-l. - Very script-friendly for finance, engineering, and data tasks.
4) awk as a calculator
awk is excellent when calculations are tied to text-processing pipelines.
awk 'BEGIN { print (37.5 * 1.08) }'
# 40.5
echo "12 30 44" | awk '{ print ($1 + $2 + $3)/3 }'
# 28.6667
When you are already parsing files or logs, awk can compute metrics in one pass.
Floating-point and precision strategy
One of the most common Unix math mistakes is assuming every tool handles decimals the same way. Shell arithmetic truncates. bc uses decimal precision with configurable scale. awk generally uses double-precision floating point.
Rule of thumb:
- Integer logic in scripts:
$(( )) - Precise decimal output:
bc - Math + text pipeline:
awk - Complex scientific stack: Python / NumPy
Base conversion in Unix (hex, octal, binary)
Base conversion is common in systems work, networking, and debugging. In shell, you can use formatted output:
printf "Hex: %X\n" 255
# Hex: FF
printf "Octal: %o\n" 255
# Octal: 377
For binary conversion, many users rely on small helper snippets (awk/python/perl) or custom shell functions. The calculator above includes direct base output options to speed that up in-browser.
Practical examples for daily use
Disk growth percentage
old=128
new=156
echo "scale=2; (($new-$old)/$old)*100" | bc
# 21.87
Estimate transfer duration
# 5 GB at 20 MB/s
echo "scale=2; (5*1024)/20" | bc
# 256.00 (seconds)
Convert epoch timestamp to date (related date math workflow)
date -d @1700000000
# Tue Nov 14 22:13:20 UTC 2023
Common errors and how to avoid them
- Forgetting scale in bc:
10/3may give truncated output unless scale is set. - Using shell arithmetic for decimals: it will truncate instead of preserving precision.
- Mixed assumptions in scripts: always document expected numeric type (int vs decimal).
- Locale issues: ensure decimal separators and environment settings are consistent.
Quick Unix calculator cheat sheet
# Integer math
echo $(( 40 / 6 ))
# Decimal math with precision
echo "scale=5; 40/6" | bc
# Advanced math
echo "scale=8; s(1.2)" | bc -l # sine in bc -l
echo "scale=8; l(10)" | bc -l # natural log in bc -l
# Inline awk calculation
awk 'BEGIN { print 125.75 * 1.2 }'
# Hex output
printf "%X\n" 4096
Final thoughts
Mastering a calculator in Unix is less about memorizing one command and more about choosing the right tool for each job. Start with shell arithmetic for simple integer operations, move to bc for precision, and use awk when computation meets text processing. Once these become muscle memory, your scripts get cleaner, faster, and more reliable.