expr—evaluates arguments as an expression.

expr expression…
expr {--help,--version}

The arguments are taken as an expression. After evaluation, the result is written on the standard output. The terms of the expression must be separated by blanks. Characters special to the shell must be escaped. Used in Bourne shell scripts for performing simple arithmetic operations.

Example A.24.
1 expr 5 + 4
2 expr 5 \* 3
3 num=0
					num=`expr $num + 1`
				

Explanation

  1. Prints the sum of 5 + 4.

  2. Prints of result of 5 \* 3. The asterisk is protected from shell expansion.

  3. After assigning 0 to variable num, the expr command adds 1 to num and the result is assigned to num.

Get Linux Shells by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.