How to Add Numbers in Linux Command Line

How to Add Numbers in Linux Command Line

If you want to know how to add numbers in linux command line, the easiest method is shell arithmetic expansion with $(( )). It works well for whole numbers, is built into most shells, and is usually the fastest option. For decimals, use bc. This guide shows the main ways to do it, with examples and common pitfalls.

Quick answer

For most Linux terminal users, this is the simplest way to add two numbers:

echo $((5 + 3))

Output:

8

If you need decimal arithmetic, use bc instead of shell arithmetic.

Add numbers with shell arithmetic

Shell arithmetic expansion is the standard way to do simple math in bash and many other Linux shells. It is best for integers and quick calculations inside scripts or terminal commands.

Add two numbers

echo $((12 + 7))

Output:

19

Use variables

You can store numbers in variables and add them:

a=10
b=15
echo $((a + b))

Output:

25

Add more than two numbers

echo $((4 + 8 + 2 + 6))

Output:

20

Use arithmetic in scripts

This method is especially useful in shell scripts because it is compact and readable:

count=3
count=$((count + 1))
echo "$count"

That example increments a value by one, which is common in loops and counters.

Add numbers with expr

The expr command is an older method that still appears in many Linux examples. It works, but it is less convenient than $(( )).

expr 5 + 3

Output:

8

One important detail is that expr requires spaces between the numbers and the operator. Without spaces, it will fail.

expr 5+3

That does not work as arithmetic, because expr treats it as a single string.

When expr is useful

  • Working in very old shell environments
  • Reading older scripts
  • Simple compatibility with legacy systems

For most modern Linux use, $(( )) is easier and clearer.

Add decimal numbers with bc

Shell arithmetic expansion does not handle decimals properly. If you need floating-point math, use bc, which is a basic calculator language available on many Linux systems.

echo "5.5 + 2.3" | bc

Output:

7.8

By default, bc may truncate decimal places depending on the expression. If you want more precision, set the scale:

echo "scale=2; 5.5 + 2.3" | bc

Output:

7.8

For divisions, scale matters even more:

echo "scale=4; 10 / 3" | bc

Output:

3.3333

Use let for arithmetic in bash

The let builtin also performs arithmetic in bash:

let "sum = 9 + 6"
echo "$sum"

Output:

15

This works, but many users prefer $(( )) because it is more readable and widely used.

Which method should you use?

MethodBest forLimitation
$(( ))Integers, shell scripts, quick terminal mathNot suitable for decimals
exprLegacy scripts and older systemsLess convenient, requires spaces
bcDecimal numbers and precise calculationsNeeds an external command
letBash arithmetic in scriptsLess commonly used than $(( ))

Common mistakes when adding numbers in Linux command line

Common mistakes when adding numbers in Linux command line
  • Using the wrong tool for decimals, shell arithmetic only handles integers.
  • Forgetting spaces with expr, for example expr 5+3 will not work as expected.
  • Missing quotes in bc input, the expression should be passed as a string or through standard input.
  • Assuming all shells behave the same, most modern shells support arithmetic expansion, but script portability still matters.
  • Expecting bash syntax in every shell, some minimal shells have fewer features.

How to add multiple values from command output

How to add multiple values from command output

Sometimes you are not adding fixed numbers, you are adding values collected from commands or variables. In that case, capture the values first, then use arithmetic expansion.

cpu=4
ram=8
disk=20
total=$((cpu + ram + disk))
echo "$total"

Output:

32

If values come from command output, make sure they are numeric before adding them. Non-numeric text will cause errors or unexpected results.

How to print the result cleanly

The simplest way to show the result is with echo:

echo $((100 + 250))

If you want to store the result and use it later in a script, assign it to a variable:

result=$((100 + 250))
echo "Total: $result"

When to use shell arithmetic versus bc

When to use shell arithmetic versus bc

Use shell arithmetic when the values are whole numbers and you want a fast, built-in solution. Use bc when you need decimals, division with precision, or calculator-style math that shell arithmetic cannot handle well.

A simple rule helps:

  • Integers, use $(( ))
  • Decimals, use bc
  • Legacy scripts, you may still see expr

Related Linux command line task: adding a printer

Related Linux command line task adding a printer

Some readers searching for Linux command line help are also trying to manage system tasks like printer setup. If you need to add a printer in Linux command line, that is a separate administrative task and usually involves tools such as lpadmin, lpstat, or CUPS-related commands rather than math utilities.

That topic is different from number addition, but both are common terminal tasks for Linux users. If you are setting up a printer, focus on the printer name, driver, device URI, and CUPS service status rather than arithmetic commands.

Frequently Asked Questions About how to add numbers in linux command line

What is the easiest way to add numbers in Linux terminal?

The easiest method is echo $((a + b)). It is built into the shell and works well for integers.

How do I add decimal numbers in Linux command line?

Use bc. For example, echo "5.2 + 1.8" | bc calculates decimal values.

What is the difference between expr and $(( ))?

$(( )) is shell arithmetic expansion, while expr is an external command. $(( )) is usually simpler and better for modern scripts.

Can I add numbers directly in a shell script?

Yes. Arithmetic expansion is commonly used in scripts for counters, totals, and calculations.

Why does expr need spaces?

expr parses each argument separately, so spaces are required around the operator.

How do I print a number result in Linux shell?

Use echo with arithmetic expansion, such as echo $((10 + 15)).

How do I add a printer in Linux command line?

Printer setup is separate from numeric calculation. In many Linux systems, you add a printer with CUPS tools such as lpadmin after identifying the printer URI and driver.

Conclusion

For most users, $(( )) is the best answer to how to add numbers in linux command line. It is fast, built into the shell, and easy to read. Use expr only when you need older compatibility, and use bc when your calculations involve decimals or more precise math.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *