Using expr
Example of expr function usage
#!/bin/bash
# Demonstrating some of the uses of 'expr'
# =======================================
echo
# Arithmetic Operators
# ---------- ---------
echo "Arithmetic Operators"
echo
a=`expr 5 + 3`
echo "5 + 3 = $a"
a=`expr $a + 1`
echo
echo "a + 1 = $a"
echo "(incrementing a variable)"
a=`expr 5 % 3`
# modulo
echo
echo "5 mod 3 = $a"
echo
echo
# Logical Operators
# ------- ---------
# Returns 1 if true, 0 if false,
#+ opposite of normal Bash convention.
echo "Logical Operators"
echo
x=24
y=25
b=`expr $x = $y` # Test equality.
echo "b = $b" # 0 ( $x -ne $y )
echo
a=3
b=`expr $a > 10`
echo 'b=`expr $a > 10`, therefore...'
echo "If a > 10, b = 0 (false)"
echo "b = $b" # 0 ( 3 ! -gt 10 )
echo
b=`expr $a < 10`
echo "If a < 10, b = 1 (true)"
echo "b = $b" # 1 ( 3 -lt 10 )
echo
# Note escaping of operators.
b=`expr $a <= 3`
echo "If a <= 3, b = 1 (true)"
echo "b = $b" # 1 ( 3 -le 3 )
# There is also a ">=" operator (greater than or equal to).
echo
echo
# String Operators
# ------ ---------
echo "String Operators"
echo
a=1234zipper43231
echo "The string being operated upon is "$a"."
# length: length of string
b=`expr length $a`
echo "Length of "$a" is $b."
# index: position of first character in substring
# that matches a character in string
b=`expr index $a 23`
echo "Numerical position of first "2" in "$a" is "$b"."
# substr: extract substring, starting position & length specified
b=`expr substr $a 2 6`
echo "Substring of "$a", starting at position 2,
and 6 chars long is "$b"."
# The default behavior of the 'match' operations is to
#+ search for the specified match at the ***beginning*** of the string.
#
# uses Regular Expressions
b=`expr match "$a" '[0-9]*'` # Numerical count.
echo Number of digits at the beginning of "$a" is $b.
b=`expr match "$a" '([0-9]*)'` # Note that escaped parentheses
# == == + trigger substring match.
echo "The digits at the beginning of "$a" are "$b"."
echo
exit 0
Important
The : operator can substitute for match. For example, b=`expr $a : [0-9]*` is the exact equivalent of b=`expr match $a [0-9]*` in the above listing.
#!/bin/bash
echo
echo "String operations using "expr $string : " construct"
echo "==================================================="
echo
a=1234zipper5FLIPPER43231
echo "The string being operated upon is "`expr "$a" : '(.*)'`"."
# Escaped parentheses grouping operator. == ==
# ***************************
#+ Escaped parentheses
#+ match a substring
# ***************************
# If no escaped parentheses...
#+ then 'expr' converts the string operand to an integer.
echo "Length of "$a" is `expr "$a" : '.*'`." # Length of string
echo "Number of digits at the beginning of "$a" is `expr "$a" : '[0-9]*'`."
# ------------------------------------------------------------------------- #
echo
echo "The digits at the beginning of "$a" are `expr "$a" : '([0-9]*)'`."
# == ==
echo "The first 7 characters of "$a" are `expr "$a" : '(.......)'`."
# ===== == ==
# Again, escaped parentheses force a substring match.
#
echo "The last 7 characters of "$a" are `expr "$a" : '.*(.......)'`."
# ==== end of string operator ^^
# (actually means skip over one or more of any characters until specified
#+ substring)
echo
exit 0
The above script illustrates how expr uses the escaped parentheses -- ( ... ) -- grouping operator in tandem with regular expression parsing to match a substring.




