Nov 12, 2022 3 min read

How to Evaluate Strings as Numbers in Bash

Learn how to evaluate bash strings as integers to perform successful arithmetic operations.

Table of Contents

Like everything is file in Linux, everything is string in bash.

Yes! Technically, there are no data types in Bash. Essentially, Bash variables are just character strings.

And that creates a problem when you are trying to do arithmetic operations in bash. The numbers you try to add give you weird results.

[email protected]:~$ sum=3+6
[email protected]:~$ echo $sum

To convert strings to integers in bash, wrap them in $((X)). Like this:


If the string has an identifiable number, it will be treated as a number. The example I showed above can be corrected as:

[email protected]:~$ sum=$((3+6))
[email protected]:~$ echo $sum
Anything you do inside $((...)) is considered to be arithmetic operation.

Let's see the conversion of strings into numbers in a bit more detail so that you can use arithmetic calculations in bash.

Converting string variables to numbers

Let's declare some "numbers" which are basically strings of characters.

[email protected]:~$ a=11
[email protected]:~$ b=3

You can check the assigned number:

[email protected]:~$ echo $a
[email protected]:~$ echo $b

Let's now try to add both numbers and store the value in a third variable c:

[email protected]:~$ c=$a+$b
[email protected]:~$ echo $c

As you can see on the output above, c is also being treated as a string.

To make the addition successful, you need to “convert” it through an arithmetic expansion:

[email protected]:~$ c=$(($a+$b))
[email protected]:~$ echo $c

Do note that c is still actually a string, until you implicitly use the same arithmetic expansion again for another operation that I'll discuss in a while.

[email protected]:~$ c=$((5))

The above is equivalent to:

[email protected]andbook:~$ c=5

Let's use a third variable d for another operation:

[email protected]:~$ d=10

All these implicit declarations are still strings. It is same as how I had defined a and b earlier.

Now for the second operation:

[email protected]:~$ e=$(($a+$b*$c-$d))
[email protected]:~$ echo $e

In the above expression, the product of b and c is calculated first and finally the addition and subtraction follow.

As I mentioned previously, anything you do between $((...)) is considered to be an arithmetic operation.

All this conversion works for integers. It won't work with floating points i.e. numbers with decimal points.

Try mixing actual strings and numbers

Mixing strings and integers is still safer than mixing coke and mint. It does produce a weird result, though.

[email protected]:~$ sum=$((3+hello))
[email protected]:~$ echo $sum

It will only work when there are numbers (as string). If it is pure character string, it won't be magically converted into some numbers. Instead, it is ignored as you can see in the example above.

Bash Beginner Series #3: Passing Arguments to Bash Scripts
In the third part of the Bash Beginner Series, you’ll learn to pass arguments to a bash shell script. You’ll also learn about special bash shell variables.

Alternate method: use expr

You can also use the expr tool to do the evaluation, but do note that it is not a “native” Bash procedure, as you need to have coreutils installed (by default on Ubuntu) as a separate package.

[email protected]:~$ expr $a + $b \* $c - $d

I hope this quick little tutorial helped you in evaluating bash strings as numbers. If you have questions or suggestions, feel free to leave a comment below.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Linux Handbook.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.