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

- Addition using expr utility
- 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(”).`

- Subtraction using expr utility
- Multiplication using expr utility
- Division using expr utility
- Modulo using expr utility (Calculating Reminder)
- Calculation of mathematical expression with more than one operator using expr command-line utility

1 2 3 4 5 6 7 8 9 10 11 12 13 |
#!/usr/bin/env bash # Define a variable X and assign it a value equal to 40 X=40 # Define a variable Y and assign it a value equal to 20 Y=20 # Define a variable Z and use expr command here to add variables X and Y Z=`expr $X + $Y` # Print the output echo "$X + $Y = $Z" |

##### Sample Output

1 |
40 + 20 = 60 |

##### Three things to notice here.

1 2 3 4 5 6 7 8 9 10 11 12 13 |
#!/usr/bin/env bash # Define a variable X and assign it a value equal to 40 X=40 # Define a variable Y and assign it a value equal to 20 Y=20 # Define a variable Z and use expr command here to Subtract variables Y from Variable X Z=`expr $X - $Y` # Print the output echo "$X - $Y = $Z" |

##### Sample Output

1 |
40 - 20 = 20 |

1 2 3 4 5 6 7 8 9 10 |
#!/usr/bin/env bash # Define a variable X and assign it a value equal to 40 X=40 # Define a variable Y and assign it a value equal to 20 Y=20 # Define a variable Z and use expr command here to Multiply variables X and Variable Y Z=`expr $X \* $Y` |

# Print the output

echo “$X * $Y = $Z”

##### Sample output

1 |
40 * 20 = 800 |

##### 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.

1 2 3 4 5 6 7 8 9 10 11 12 13 |
#!/usr/bin/env bash # Define a variable X and assign it a value equal to 40 X=40 # Define a variable Y and assign it a value equal to 20 Y=20 # Define a variable Z and use expr command here to Divide variables X by Variable Y Z=`expr $X / $Y` # Print the output echo "$X / $Y = $Z" |

##### Sample output

1 |
40 / 20 = 2 |

1 2 3 4 5 6 7 8 9 10 11 12 13 |
#!/usr/bin/env bash # Define a variable X and assign it a value equal to 40 X=40 # Define a variable Y and assign it a value equal to 3 Y=3 # Define a variable Z and use expr command here to calculate modulo Z=`expr $X % $Y` # Print the output echo "$X % $Y = $Z" |

##### Sample output

1 |
40 % 3 = 1 |

1 2 3 4 5 6 7 |
#!/usr/bin/env bash # Define a variable X and assign it a value equal to 40 X=`expr 12 + 3 - 7 \* 4 / 2` # Print the output echo "$X" |

##### Sample output

1 |
1 |

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

1 |
x=`expr 2 \** 3` |

or

1 |
x=`expr 2 ^ 3` |

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.