O'Reilly logo

Linux Pocket Guide, 2nd Edition by Daniel J. Barrett

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

expr — stdin  stdout  - file  -- opt  --help  --version

Synopsis

expr expression

The expr command does simple math (and other expression evaluation) on the command line:

$ expr 7 + 3
10
$ expr '(' 7 + 3 ')' '*' 14   Special shell characters are quoted
140
$ expr length ABCDEFG
7
$ expr 15 '>' 16
0                           Meaning false

Each argument must be separated by whitespace. Notice that we had to quote or escape any characters that have special meaning to the shell. Parentheses (escaped) may be used for grouping. Operators for expr include:

Operator

Numeric operation

String operation

+

Addition

 

-

Subtraction

 

*

Multiplication

 

/

Integer division

 

%

Remainder (modulo)

 

<

Less than

Earlier in dictionary.

<=

Less than or equal

Earlier in dictionary, or equal.

>

Greater than

Later in dictionary.

>=

Greater than or equal

Later in dictionary, or equal.

=

Equality

Equality.

!=

Inequality

Inequality.

|

Boolean “or”

Boolean “or”.

&

Boolean “and”

Boolean “and”.

s : regexp

 

Does the regular expression regexp match string s?

substr s p n

 

Print n characters of string s, beginning at position p. (p =1 is the first character.)

index s chars

 

Return the index of the first position in string s containing a character from string chars. Return 0 if not found. Same behavior as the C function index( ).

For Boolean expressions, the number 0 and the empty string are considered false; any other value is true. For Boolean results, 0 is false and 1 is true. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required