Bash Math Math using expr

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

expr or Evaluate expressions evaluates an expression and writes the result on standard output

Basic arithmetics

expr 2 + 3
5

When multiplying, you need to escape the * sign

expr 2 \* 3
6

You can also use variables

a=2
expr $a + 3
5

Keep in mind that it only supports integers, so expression like this

expr 3.0 / 2

will throw an error expr: not a decimal number: '3.0'.

It supports regular expression to match patterns

expr 'Hello World' : 'Hell\(.*\)rld'
o Wo

Or find the index of the first char in the search string

This will throw expr: syntax error on Mac OS X, because it uses BSD expr which does not have the index command, while expr on Linux is generally GNU expr

expr index hello l
3

expr index 'hello' 'lo'
3


Got any Bash Question?