expr

expr is a command line Unix utility which evaluates an expression and outputs the corresponding value. It first appeared in Unix v7.

Syntax: expr (expression)

expr evaluates integer or string expressions, including pattern matching regular expressions. Most of the challenge posed in writing expressions is preventing the invoking command line shell from acting on characters intended for expr to process.

The operators available

The following is an example involving boolean expressions:

expr length  "abcdef"  "<"  5  "|"  15  -  4  ">"  8

This example outputs "1". This is because length "abcdef" is 6, which is not less than 5 (so the left side of the | returns zero). But 15 minus 4 is 11 and is greater than 8, so the right side is true, which makes the or true, so 1 is the result. The program exit status is zero for this example.

For pure arithmetic, it is often more convenient to use bc. For example:

echo "3*4+14/2" | bc

since it accepts the expression as a single argument.

For portable shell programming use of the length and substr commands is not recommended.

External links


This article is issued from Wikipedia - version of the Tuesday, July 28, 2015. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.