expr
expr is a command on the Unix operating system and its derivatives , which evaluates an expression and outputs the result. expr processes expressions with integer values or strings as well as regular expressions .
The command is mainly used in shell scripts ; most of the expressions that can be put together with expr can also be evaluated by modern Unix shells themselves using syntax constructs that are also available in programming languages .
expr is part of the Single UNIX Specification . The GNU implementation is part of the GNU Core Utilities .
Available operands
All expressions are generally subject to the rules of propositional logic , and it can be used include the following operations:
- for integers: addition, subtraction, multiplication, division and modulus
- for character strings: evaluate regular expressions, find certain characters in a character string, determine length
- for both: comparisons (equal, not equal, less than, greater than, etc.)
- In addition, Boolean expressions can be used with the logic operators and and or .
example
The following expression outputs "1" as the result:
$ expr length "abcdef" "<" 5 "|" 15 - 4 ">" 8
In general, the expression is divided into a left and right part of the disjunction , both are evaluated separately before the disjunction is applied:
- The length of the character string
"abcdef"
is 6, i.e. greater than 5. The left part of the expression therefore results in 0. - However, since 15-4 equals 11 and this number is greater than 8, the right part equals True , i.e. 1.
Now the final disjunction can be applied, which 0 | 1
gives the result 1.
Web links
-
expr
: evaluate arguments as an expression - Open Group Base Specification -
expr(1)
: evaluate expression - OpenBSD General Commands Manual -
expr(1)
: Evaluating expressions - Debian GNU / Linux Executable programs or shell commands man page