Use Linux Terminal as a Calculator: Commands and Examples
Yes. You can use the Linux terminal as a calculator with tools such as bc, the shell’s arithmetic expansion, or a short Python command. For quick whole-number arithmetic, shell syntax is enough; for decimals, fractions, and scientific functions, use a calculator program such as bc.
Quick Arithmetic in the Shell
Bash and several other common shells support arithmetic expansion. Enter an expression inside $(( )) and print the result with echo:
echo $((7 + 5))
echo $((18 * 4))
echo $((20 / 3))
These examples return 12, 72, and 6. Shell arithmetic generally uses integers, so division discards the fractional part rather than rounding: 20 / 3 produces 6. This is useful for counts, indices, and other whole-number calculations, but not for accurate decimal results.
The common operators are + for addition, - for subtraction, * for multiplication, / for integer division, and % for the remainder. Parentheses set the order of operations. Quote or escape characters such as * when writing shell commands, because the shell may otherwise treat them as filename patterns.
Using bc for Decimals and More
bc is a command-line calculator available on many Linux systems. Check whether it is installed by running bc; if the shell says the command is unavailable, install it using the package manager for your distribution. The exact package command differs between distributions.
At the bc prompt, enter an expression and press Enter. Type quit to leave. You can also pass an expression directly:
echo '7.5 * 2.4' | bc
By default, bc can truncate decimal results according to its scale setting. Set scale to choose how many digits to retain after the decimal point:
echo 'scale=4; 10 / 3' | bc
This prints 3.3333. The setting controls decimal precision in calculations; it does not guarantee that every operation is rounded in the way you might expect. For a rounded display, choose a suitable scale and check whether truncation or rounding is appropriate for the task.
For a reusable session, start bc and enter several expressions on separate lines. This is handy when working through a calculation without repeatedly launching the command.
Order of Operations and Useful Expressions
As with ordinary arithmetic, parentheses make the intended order clear. For example, (12 + 8) / 5 evaluates the addition first and returns 4. Without parentheses, multiplication and division are evaluated before addition and subtraction.
In bc, use ^ for exponentiation, as in 2^8, and % for a remainder. For example, 17 % 5 returns 2. The standard library provides functions including square root, sine, cosine, and logarithms; load it with -l when starting the program:
echo 'sqrt(81)' | bc -l
That expression returns 9. Trigonometric functions in bc use radians, not degrees. To convert degrees to radians, multiply by pi and divide by 180. Since bc does not provide a built-in pi constant in the usual expression syntax, supply a suitable approximation when needed, and be aware that this limits precision.
Choosing a Terminal Calculator
Use the simplest tool that fits the calculation. These options cover most everyday needs:
- Shell arithmetic: Best for quick integer calculations and scripts that need simple counts or remainders.
bc: A practical choice for decimal arithmetic, interactive calculations, and common mathematical functions.- Python: Useful when a calculation needs variables, reusable logic, or more control over numeric types and formatting.
For example, Python can evaluate a decimal expression directly with python3 -c 'print(7.5 * 2.4)'. It is more capable than shell arithmetic, but a one-line command can become hard to read if it grows complicated. For important work, put the calculation in a short script with named variables and checks rather than relying on a dense command.
These tools are usually included with or readily available for Linux, but their availability depends on the installation. A terminal calculator is not automatically a symbolic algebra system: basic arithmetic tools evaluate expressions, but they do not necessarily simplify algebra, solve equations, or explain their reasoning.
Common Problems and Safe Input
Integer division is the most common surprise. If a result needs a decimal, use bc with an appropriate scale or use a language that handles floating-point numbers. Also check the units before calculating: multiplying a value in centimeters by one in meters without converting first can produce a mathematically valid but meaningless result.
When using a shell, quote expressions passed to another command. Characters such as *, $, and parentheses have special meanings to the shell. For example, use single quotes around an expression sent to bc:
echo '3 * (4 + 2)' | bc
Do not pass untrusted text into a shell command that evaluates it as code. Shell expansion and command substitution can do much more than arithmetic. If you are building a script that accepts user input, validate it and use a suitable arithmetic or parsing method rather than treating arbitrary input as a trusted expression.
Finally, distinguish display precision from real-world accuracy. Printing ten decimal places does not make an input measurement accurate to ten places. For measurements, use precision that reflects the source data and the purpose of the calculation.
Frequently Asked Questions
Can I use the Linux terminal as a calculator without installing anything?
Usually, yes. Shell arithmetic is built into common shells for integer calculations. Other tools, including bc and Python, may already be installed, but availability varies by distribution.
How do I do decimal division in a Linux terminal?
Use bc and set its scale. For example, echo 'scale=2; 10 / 3' | bc displays two decimal places, subject to bc’s truncation behavior.
Why does Bash show 2 for 5 divided by 2?
Shell arithmetic uses integer division in the usual arithmetic expansion, so it discards the fractional part. Use bc or another calculator for decimal results.
How do I calculate a square root in the terminal?
Use bc -l with its square-root function: echo 'sqrt(81)' | bc -l. Python also supports square roots through its math library.
Does bc use degrees or radians?
Its trigonometric functions use radians. Convert degree values to radians before using them, or choose a tool that explicitly supports degree-based functions.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.