Shell Programming/expr command

From Wikibooks, open books for an open world
Jump to navigation Jump to search

expr 1 | expr 2[edit | edit source]

Expression 1 if expression 1 is non-zero, otherwise expression 2.

  expr 1 | expr 2
  1        0 -> Expression 1
  1        1 -> Expression 1
  0        0 -> Expression 2
  0        1 -> Expression 2

expr 1 & expr 2[edit | edit source]

Zero if either expression is zero, otherwise expression 1.

  expr 1 & expr 2
  0        0 -> 0
  1        0 -> 0
  0        1 -> 0
  1        1 -> Expression 1

Other operations in expr command[edit | edit source]

The expr command is useful in simple arithmetic operations using integers only.

  expr 1 + 1
  output:
  2

Similarly,

  expr 1 - 1
  expr 1 * 1
  expr 1 / 1
  expr 1 % 1

which have following results, respectively: 0; 1; 1; 0. Note that % is modulus.