Linux Commands: Evaluate expressions


expr-Linux Command

expr command- Evaluate expressions

Syntax: expr numeric_expr ...

Description: expr evaluates each numeric_expr command line argument as a separate numeric expression. Thus a single expression cannot contain unescaped whitespaces or needs to be placed in a quoted string (i.e. between "{" and "}"). Arithmetic is performed on signed integers (currently numbers in the range from -2,147,483,648 to 2,147,483,647). Successful calculations cause no output (to either standard out/error or environment variables). So each useful numeric_expr needs to include an assignment (or op-assignment). Each numeric_expr argument supplied is evaluated in the order given (i.e. left to right) until they all evaluate successfully (returning a true status). If evaluating a numeric_expr fails (usually due to a syntax error) then the expr command fails with "error" as the exit status and the error message is written to the environment variable "error".
OPERATORS: The precedence of each operator is shown following the description in square brackets. "0" is the highest precedence. Within a single precedence group evaluation is left-to-right except for assignment operators which are right-to-left. Parentheses have higher precedence than all operators and can be used to change the default precedence shown below.

UNARY OPERATORS

+

Does nothing to expression/number to the right.

-

negates expression/number to the right.

!

logically negate expression/number to the right.

~

Bitwise negate expression/number to the right.

BINARY ARITHMETIC OPERATORS

*

Multiply enclosing expressions [2]

/

Integer division of enclosing expressions

%

Modulus of enclosing expressions.

+

Add enclosing expressions

-

Subtract enclosing expressions.

<<

Shift left expression _left_ by number in right expression. Equivalent to: left * (2 ** right)

>>

Shift left expression _right_ by number in right expression. Equivalent to: left / (2 ** right)

&

Bitwise AND of enclosing expressions

^

Bitwise exclusive OR of enclosing expressions. [8]

|

Bitwise OR of enclosing expressions. [9]

BINARY LOGICAL OPERATORS

These logical operators yield the number 1 for a true comparison and 0 for a false comparison. For logical ANDs and ORs their left and right expressions are assumed to be false if 0 otherwise true. Both logical ANDs and ORs evaluate both their left and right expressions in all case (cf. C's short-circuit action).

<=

true when left less than or equal to right. [5]

>=

true when left greater than or equal to right. [5]

<

true when left less than right. [5]

>

true when left greater than right. [5]

==

true when left equal to right. [6]

!=

true when left not equal to right. [6]

&&

logical AND of enclosing expressions [10]

||

logical OR of enclosing expressions [11]

ASSIGNMENT OPERATORS

In the following descriptions "n" is an environment variable while "r_exp" is an expression to the right. All assignment operators have the same precedence which is lower than all other operators. N.B. Multiple assignment operators group right-to-left (i.e. same as C language).

=

Assign right expression into environment variable on left.

*=

n *= r_exp is equivalent to: n = n * r_exp

/=

n /= r_exp is equivalent to: n = n / r_exp

%=

n %= r_exp is equivalent to: n = n % r_exp

+=

n += r_exp is equivalent to: n = n + r_exp

-=

n -= r_exp is equivalent to: n = n - r_exp

<<=

n <<= r_exp is equivalent to: n = n << r_exp

>>=

n >>= r_exp is equivalent to: n = n >> r_exp

&=

n &= r_exp is equivalent to: n = n & r_exp

|=

n |= r_exp is equivalent to: n = n | r_exp
NUMBERS: All number are signed integers in the range stated in the description above. Numbers can be input in base 2 through to base 36. Base 10 is the default base. The default base can be overridden by:
a leading "0" : implies octal or hexadecimal
a number of the form _base_#_num_
Numbers prefixed with "0" are interpreted as octal. Numbers prefixed with "0x" or "0X" are interpreted as hexadecimal. For numbers using the "#" notation the _base_ must be in the range 2 through to 36 inclusive. For bases greater then 10 the letters "a" through "z" are utilized for the extra "digits". Upper and lower case letters are acceptable. Any single digit that exceeds (or is equal to) the base is consider an error. Base 10 numbers only may have a suffix. See suffix for a list of valid suffixes. Also note that since expr uses signed integers then "1G" is the largest magnitude number that can be represented with the "Gigabyte" suffix (assuming 32 bit signed integers, -2G is invalid due to the order of evaluation).
VARIABLES: The only symbolic variables allowed are K9 environment variables. Regardless of whether they are being read or written they should never appear preceded by a "$". Environment variables that didn't previous exist that appear as left argument of an assignment are created. When a non-existent environment variable is read then it is interpreted as the value 0.
EXAMPLES: Some simple examples:
expr {n = 1 + 2} # create n

echo $n
3
expr {n*=2} # 3 * 2 result back into n

echo $n
6
expr { k = n > 5 } # 6 > 5 is true so create k = 1

echo $k
1
NOTE: expr is a Husky "built-in" command. See the "Note" section in "set" to see the implications.
SEE ALSO: husky, set, suffix, and test
Example:

Related: eval, for, function, gawk, and test

Other Linux Commands:
alias , break , builtin , cal , case , cat , cd , cfdisk , chgrp , chmod , chown , chroot , cksum , cmp , comm , continue , cp , cron , crontab , csplit , cut , date , dc , dd , declare , df , diff , diff3 , dir , dircolors , dirname , dirs , du , echo , egrep , enable , env , eval , exec , expand , export , expr , factor , false , fdformat , fdisk , fgrep , find , fmt , fold , for , fsck , function , getopts , hash , hashcharacter , head , history , hostname , id , if , import , install , join , kill , less , let , ln , local , locate , logname , logout , lpc , lpr , lprm , ls , m4 , man , mbadblocks , mkdir , mkfifo , mknod , more , mount , mtools , mv , nice , nl , nohup , passwd , paste , period , popd , pr , printf , ps , pushd , pwd , quota , quotacheck , quotactl , ram , rcp , read , readonly , return , rm , rmdir , rsync , Run , sdiff , sed , select , seq , set , shift , shopt , shutdown , sleep , sort , split , su , sum , symlink , sync , tac , tail , tar , tee , Test , time , times , top , touch , tr , traceroute , true , tsort , tty , Type , ulimit , umask , uname , Unexpand , uniq , units , unset , unshar , until , useradd , usermod , users , uuencode , watch , wc , whereis , which , while , who , xargs , yes ,
Live2Support.com :
Product - Benefits - Features - How Does it Works - Pricing - SSL Certificates - Signup - Contactus - Login - Submit Resources