In our last post “NUMBERS AND MATHS IN BASH SHELL SCRIPTING LANGUAGE“, we saw the use of ‘let’ utility to perform mathematical calculations. Here in this tutorial I will take you to another aspect of MATHS in Bash using ‘expr’ utility.


expr command-line utility

expr is an old UNIX program that made Mathematical evaluation possible in the good old days of Bournie shell which didn’t supported maths. The only thing to remember with expr is that ‘expr’ is picky about space and you need to use $ with expr utility, unlike let utility where it was not necessary and optional.

Examples of expr utility

  1. Addition using expr utility
  2. Sample Output

    Three things to notice here.
    • As already stated, expr is picky about space, hence Z=expr $X + $Y and Z=expr $X+$Y are not same. The later will result into error.
    • You must use $ (symbol) with the variables when using expr command-line tool.
    • expr statement is enclosed inside backquotes/backticks which can be found just above TAB key. Remember backquotes/backticks symbol () is not equal to a single quotes symbol(”).
  3. Subtraction using expr utility
  4. Sample Output

  5. Multiplication using expr utility
  6. # Print the output
    echo “$X * $Y = $Z”

    Sample output

    Things to notice here

    You must have noticed in the above example, that multiplication operand (*) has been escaped (\). This is to prevent the shell from interpreting it as the filename meta character.

  7. Division using expr utility
  8. Sample output

  9. Modulo using expr utility (Calculating Reminder)
  10. Sample output

  11. Calculation of mathematical expression with more than one operator using expr command-line utility
  12. Sample output

Similarly, you can calculate mathematical expression of any degree. However, there are certain limitations of expr command-line utility like you can’t calculate the exponent of a number. At least there is no direct way to calculate this. expr won’t support exponentiation like


That’s all for now. We will look into other methods of mathematical calculation in shell in the next post. Till then stay tuned and connected to JoinTux. Share your valuable feedback in the comments below. Share this post, if you liked it and enjoyed learning and exploring.


Please enter your comment!
Please enter your name here