Table of Contents
$ [ `expr 7 \> 5 ` ] || echo aaa
$ [ `expr 7 \> 5 ` ] && echo aaa aaa
$ if [ `expr 7 \> 5 ` -eq 1 ]; then echo aaa; else echo bbb; fi aaa
$ if [ `expr 7 \< 5 ` -eq 1 ]; then echo aaa; else echo bbb; fi bbb
$ if [ `expr 7 \> 5 ` ]; then echo aaa; else echo bbb; fi aaa
$ if [ `expr 7 \< 5 ` ]; then echo aaa; else echo bbb; fi aaa
$ expr \( 5 + 10 \) / 2 7
$ vv=1 $ vv=`expr $vv + 1` $ svs vv vv=2
$ expr `echo 2` '>' 1; echo $? 1 0
$ expr `echo 1` '>' 1; echo $? 0 1
$ expr `echo ' 2'` '>' 1; echo $? 1 0
— space is not a problem
$ a='aaa' $ expr "$a" = aaa 1 $ expr "$a" = aab 0
$ b=5 $ expr "$b" + 5 \>= 10 1 $ expr "$b" + 5 \>= 11 0
$ p="version.100" $ expr "$p" : '.*' 11 $ [ `expr "$p" : '.*'` -gt 0 ] && echo yes yes $ expr "$p" : '\(.*\)' version.100
$ expr "$p" : '[a-z]*' 7 $expr "$p" : '\([a-z]*\)' version
# return no more than 4 char... $ x='12345' $expr "$x" : '\(....\)' \| "$x" 1234 $ x='123' $ expr "$x" : '\(....\)' \| "$x" 123 $ expr "$x" : '\(....\)'
$ expr "-b4" : '-b\([0-9]*\)' \| 2 4
but '+' nok:
$ expr "-b4" : '-b\([0-9]+\)' \| 2 2
$
$ a=/opt/SUNWconn/chorus_4.0LA/chorus-powerpc/os/root $ expr $a : '.*/\(.*\)' '|' $a root
$ v="version1234." $ expr $v : '.*\([0-9]+\).*'
$ expr $v : '.*\([0-9][0-9]*\).*' 4 $ expr $v : '.*n\([0-9]+\).*'
$ expr $v : '.*\(n[0-9]+\).*'
$ expr $v : '.*\(n[0-9][0-9]*\).*' n1234 $ expr $v : '.*n\([0-9][0-9]*\).*' 1234
: Perform pattern matching. Its arguments are coerced to strings and the second one is considered to be a regular expression, with a `^' implicitly added at the beginning. The first argument is then matched against this regular expression. If the match succeeds and part of the string is enclosed in `\(' and `\)', that part is the value of the : expression; otherwise an integer whose value is the number of characters matched is returned. If the match fails, the : operator returns the null string if `\(' and `\)' are used, otherwise 0.
Only one `\(' and `\)' pair can be used.
The following may be used to print the non-directory part of the file name stored in variable a ( if the value does not contain `/', use $a):
expr $a : '.*/\(.*\)' '|' $a
Note the quoted shell metacharacters.
expr returns the following exit status:
0 if the expression is neither null nor 0, 1 if the expression is null or 0, 2 for invalid expressions.
$ tty /dev/pts/0
$ expr `tty` : '/dev/pts/'; echo $? 9 0
$ expr `tty` : '/dev/tty'; echo $? 0 1