Bash Math Math using expr


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