1. Overview

In Bash, values are untyped but usually treated as strings. Converting values to numbers is primarily meant for calculations, especially for creating scripts. In this tutorial, we will explore alternatives for those conversions when dealing with numbers in the Linux command-line interface.

2. Introduction to the Problem

When trying to perform a sum, we might instead be getting a concatenation. Let’s see an example:

$ a=1
$ b=2
$ c=$a+$b
$ echo $c

In the example above, we tried to sum 1 and 2, assigned to variables a and b. However, when printing the result, we see a concatenated string 1+2.

3. expr

The command expr evaluates expressions while converting values to numbers when the expression is an arithmetic calculation.

Let’s try an example with a sum operation:

$ a=1
$ b=2
$ expr $a + $b

In the example above, we evaluated the sum between 1 and 2, assigned to variables a and b, which resulted in 3expr converts all values as needed but does not support floating point values.

4. The Double-Parentheses Construct

The double-parentheses construct is part of the basic Bash features and is designed for evaluating arithmetic expansions.

Let’s perform a calculation:

$ a=1
$ b=2
$ c=$((a + b))
$ echo $c

In the example above, we assigned the result of the calculation to a variable, c. Basically, this construct works similar to the expr command but adheres to the Bash syntax and still does not support floating point values.

5. let Built-in Command

In Bash, let is a built-in. Just as the double-parentheses construct and expr, it evaluates arithmetic expressions.

Let’s see an example with let:

$ a=1
$ b=2
$ let c=a+b
$ echo $c

In the example above, we first assigned the values 1 and 2 to variables a and b. Then, we assigned the result of the sum to another variable, c.

Printing the result with echo showed us the result, 3.

6. awk

The awk command is a very powerful and versatile tool. One of its functionalities is the possibility to perform arithmetic expressions.

We can pipe the values into awk, like the following example:

$ a=1
$ b=2
$ echo $a $b | awk '{print $1 + $2}'

In the example above, we assigned the values to variables a and b, then performed the calculation. The references $1 and $2, in awk, point to the first and second values printed with echo, separated by whitespace.

Crucially, awk supports floating point values:

$ echo 1 2 | awk '{print $1 / $2 }'

Let’s explore another tool which supports more complex calculations.

7. bc

The bc tool is a calculator language designed to be used in the terminal interface. We can either input calculations into a file and process them with bc or pipe them to bc with echo or cat.

Let’s perform a calculation as an example:

$ a=1
$ b=2
$ c=3
$ d=4
$ echo $a+$b+$c+$d | bc

In the example above, we printed the string 1+2+3+4 and then piped it into bc, which converted the string to an actual calculation with the correct answer – 10.

Unlike the options above, bc is less likely to be part of a default Linux distribution.

8. Conclusion

In this article, we learned that we could also use Bash for numeric operations. However, using numerical values is not the default behavior, so we need to convert them to numbers or even convert a calculation string with a tool.

Thus, we explored how to do so with expr, the double parentheses construct, the let built-in command, awk, and bc. That enables us to, for instance, perform arithmetic calculations within Bash.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.