Index

expr

basic math
expr [number1] + [number2]
expr [number1] - [number2]
expr [number1] '*' [number2]
expr [number1] / [number2]
expr [number1] % [number2]
comparing numbers
expr [number1] = [number2]
expr [number1] '!=' [number2]
expr [number1] '<' [number2]
expr [number1] '<=' [number2]
expr [number1] '>' [number2]
expr [number1] '>=' [number2]
boolean logic
expr [boolean] '&' [boolean]
expr [boolean] '|' [boolean]
examples (0=false, ''=false, everything else=true)
expr 0 '&' 0    # 0
expr 1 '&' 0    # 0
expr 0 '&' 1    # 0
expr 1 '&' 1    # 1

expr 0 '|' 0    # 0
expr 1 '|' 0    # 1
expr 0 '|' 1    # 1
expr 1 '|' 1    # 1
combining expressions; prints 7
expr 1 + 2 '*' 3
overriding presedence; prints 9
expr '(' 1 + 2 ')' '*' 3

expr regex

syntax

lower case letter
[a-z]
upper case letter
[A-Z]
any case letter
[a-zA-Z]
not a letter
[^a-zA-Z]
any character
.
end of line
$
zero or one letters
[a-z]\?
any number of letters
[a-z]*
one or more letters
[a-z]\+
parentheses
()
capturing a word
\([a-z]\+\)

usage

test string against regex
expr "abc" : '[a-c]\+'
capturing group (returns first matching capturing group), prints 'a'
expr 'ab' : '\(.\)b'
second group matching example: prints 'abc'
expr 'abc' '|' '\([0-9]\+\)\?\([a-z]\+\)'

expr alternative

echo "2 * 5 + 4" | bc
documentation
https://www.geeksforgeeks.org/bc-command-linux-examples/