
This directory of Linux commands is from Linux in a Nutshell, 5th Edition. Click on any of the 687 commands below to get a description and list of available options. All links in the command summaries point to the online version of the book on Safari Bookshelf. Buy it now, or read it online on Safari Bookshelf. exprexpr arg1 operator arg2 [ operator arg3 ... ] Evaluate arguments as expressions and print the results. Arguments and operators must be separated by spaces. In most cases, an argument is an integer, typed literally or represented by a shell variable. There are three types of operators: arithmetic, relational, and logical, as well as keyword expressions. Exit status for expr is 0 (expression is nonzero and nonnull), 1 (expression is 0 or null), or 2 (expression is invalid). Arithmetic operatorsUse these to produce mathematical expressions whose results are printed:
Addition and subtraction are evaluated last, unless they are grouped inside parentheses. The symbols *, (, and ) have meaning to the shell, so they must be escaped (preceded by a backslash or enclosed in single quotes). Relational operatorsUse these to compare two arguments. Arguments can also be words, in which case comparisons are defined by the locale. If the comparison statement is true, the result is 1; if false, the result is 0. Symbols > and < must be escaped.
Logical operatorsUse these to compare two arguments. Depending on the values, the result can be arg1 (or some portion of it), arg2, or 0. Symbols  and & must be escaped.
Keywords
ExamplesDivision happens first; result is 10: expr 5 + 10 / 2 Addition happens first; result is 7 (truncated from 7.5): expr \( 5 + 10 \) / 2 Add 1 to variable i. This is how variables are incremented in shell scripts: i=`expr $i + 1` Print 1 (true) if variable a is the string "hello": expr $a = hello Print 1 (true) if b plus 5 equals 10 or more: expr $b + 5 \>= 10 Find the 5th, 6th, and 7th letters of the word character: expr substr character 5 3 In the examples that follow, variable p is the string "version.100". This command prints the number of characters in p:
expr $p : '.*' Match all characters and print them:
expr $p : '\(.*\)' Print the number of lowercase letters at the beginning of p:
expr $p : '[az] *' Match the lowercase letters at the beginning of p:
expr $p : '\([az] *\)' Truncate $x if it contains five or more characters; if not, just print $x. (Logical OR uses the second argument when the first one is 0 or null, i.e., when the match fails.) expr $x : '\(.....\)' \ $x In a shell script, rename files to their first five letters: mv $x `expr $x : '\(.....\)' \ $x` (To avoid overwriting files with similar names, use mv i.) 
