O'Reilly logo

Linux and Unix Shell Programming by David Tansley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

17.5. Using expr

The expr command is used mostly for integer values, but can be used for strings as well. Its general format is:

expr argument operator argument

Expr is a handy command line calculator as well:

						$ expr 10 + 10
20

$ expr 900 + 600
1500

$ expr 30 / 3
10

$ expr 30 / 3 /2
5

When using the multiply sign, you will have to escape it with a backslash, because the shell might misinterpret the asterisk ‘*’:

						$ expr 30 \* 3
90
					

17.5.1. Incrementing counters

Expr does the incrementing of values when using loops. First the loop is initialized to zero. Then one is added to the variable loop. The use of the quotes means command substitution, which basically means take the output from the command (expr) and put it in the variable loop.

							$ LOOP=0 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required