Expr Command in Linux/Unix With Examples | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

expr evaluates integer or string expressions, including pattern matching regular expressions. Each symbol (operator, value, etc.) in the expression must be given as a separate parameter. 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

  • for integers: addition, subtraction, multiplication, division and modulus
  • for strings: match a regular expression; in some versions: find a set of characters in a string (“index”), find substring (“substr”), length of string (“length”)
  • for either: comparison (equal, not equal, less than, etc.)
~/codeFactory$ expr --help
Usage: expr EXPRESSION
  or:  expr OPTION

      --help     display this help and exit
      --version  output version information and exit

Print the value of EXPRESSION to standard output.  A blank line below
separates increasing precedence groups.  EXPRESSION may be:

  ARG1 | ARG2       ARG1 if it is neither null nor 0, otherwise ARG2

  ARG1 & ARG2       ARG1 if neither argument is null or 0, otherwise 0

  ARG1 < ARG2       ARG1 is less than ARG2
  ARG1 <= ARG2      ARG1 is less than or equal to ARG2
  ARG1 = ARG2       ARG1 is equal to ARG2
  ARG1 != ARG2      ARG1 is unequal to ARG2
  ARG1 >= ARG2      ARG1 is greater than or equal to ARG2
  ARG1 > ARG2       ARG1 is greater than ARG2

  ARG1 + ARG2       arithmetic sum of ARG1 and ARG2
  ARG1 - ARG2       arithmetic difference of ARG1 and ARG2

  ARG1 * ARG2       arithmetic product of ARG1 and ARG2
  ARG1 / ARG2       arithmetic quotient of ARG1 divided by ARG2
  ARG1 % ARG2       arithmetic remainder of ARG1 divided by ARG2

  STRING : REGEXP   anchored pattern match of REGEXP in STRING

  match STRING REGEXP        same as STRING : REGEXP
  substr STRING POS LENGTH   substring of STRING, POS counted from 1
  index STRING CHARS         index in STRING where any CHARS is found, or 0
  length STRING              length of STRING
  + TOKEN                    interpret TOKEN as a string, even if it is a
                               keyword like 'match' or an operator like '/'

  ( EXPRESSION )             value of EXPRESSION

Beware that many operators need to be escaped or quoted for shells.
Comparisons are arithmetic if both ARGs are numbers, else lexicographical.
Pattern matches return the string matched between \( and \) or null; if
\( and \) are not used, they return the number of characters matched or 0.

Exit status is 0 if EXPRESSION is neither null nor 0, 1 if EXPRESSION is null
or 0, 2 if EXPRESSION is syntactically invalid, and 3 if an error occurred.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report expr translation bugs to <https://translationproject.org/team/>
Full documentation at: <https://www.gnu.org/software/coreutils/expr>
or available locally via: info '(coreutils) expr invocation'

1. Using expr for basic arithmetic operations

for multiplication '*' will not work, we need to use '\*'
~/codeFactory$ expr 4 * 5
expr: syntax error: unexpected argument

~/codeFactory$ expr 4 \* 5
20

~/codeFactory$ expr 4 / 2
2

~/codeFactory$ expr 3 - 4
-1

~/codeFactory$ expr 3 + 4
7

2. Performing operations on variables inside a shell script

~/codeFactory$ cat test5.sh 
echo "Enter X and Y numbers"
echo "Enter X"
read x
echo "Enter Y"
read y
sum=`expr $x + $y`
echo "Sum: $sum"

~/codeFactory$ sh test5.sh 
Enter X and Y numbers
Enter X
10
Enter Y
20
Sum: 30

Note: expr is an external program used by Bourne shell. It uses expr external program with the help of backtick. The backtick(`) is actually called command substitution.

3. Comparing two expressions

~/codeFactory$ cat test5.sh 
x=10
y=20

res=`expr $x = $y`
echo $res

res=`expr $x \< $y`
echo $res

res=`expr $x \!= $y`
echo $res


~/codeFactory$ sh test5.sh 
0
1
1

Example: Evaluating boolean expressions

OR
~/codeFactory$ expr length "codefactory" ">" 15 "|" 4 - 3 "<" 2
1

AND
~/codeFactory$ expr length "codefactory" ">" 15 "&" 4 - 3 "<" 2
0

4. For String operations

~/codeFactory$ cat test5.sh 
str="CodeFactory"
len=`expr length $str`
echo $len

~/codeFactory$ sh test5.sh 
11

Example: Finding substring of a string

~/codeFactory$ cat test5.sh 
str="CodeFactory"
sub=`expr substr $str 5 7`
echo $sub

~/codeFactory$ sh test5.sh 
Factory

Example: Matching number of characters in two strings

~/codeFactory$ expr abcd : ab
2

~/codeFactory$ expr abcd : bc
0

~/codeFactory$ expr match abcd  ab
2

~/codeFactory$ expr match abcd bc
0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s