How to use the expr command in Linux system

  
                

expr is a command in the Linux system, can be used for the operation of computer strings of integer values, then what is the purpose of the expr command? The following small series will give you a detailed introduction to the use of the expr command on Linux, let's learn together.

wordlength

# expr length & ldquo; this is a test & rdquo;

14

seeking quotient digital

# expr 14 % 9

5

Grab a string from a location

# expr substr “this is a test” 3 5

Is is

number string only the first character

# expr index “testforthegame” e

2

String reappears

quote thisisatestformela

thisisatestformela

The expr command is a manual command line counter for evaluating the value of an expression variable under UNIX/LINUX, generally for integer values, or for use. In the string.

– The format is:

expr Expression (the command reads the Expression parameter, calculates its value, and then writes the result to standard output)

– Rules:

Separate each item with a space;

Put a \\ (backslash) in front of a shell-specific character;

contains spaces and other special characters Strings should be enclosed in quotation marks

Expr usage examples:

(1) Calculate string length

# expr length “this is a test”

14

(2) Grab Strings

# expr substr “this is a test” 3 5

is is

(3) Grab the position where the first character string appears

# expr index “sarasara” a

2

(4) The string reappears < Br>

# expr quote sara

sara

(5) integer arithmetic

# expr 14 % 9

5

# expr 10 + 10

20

# expr 1000 + 900

1900

# expr 30 /3 /2

5

# expr 30 \\* 3 (When using multiplication , you must use a backslash to block its specific meaning. Because the shell may misunderstand the meaning of the asterisk)

90

# expr 30 * 3

expr: Syntax error

(6) Incremental Count

Description: expr is used for incremental calculation in loops. First initialize the variable to 0, then add 1 to the loop value. The use of backquotes is replaced by commands.

》 LOOP=0

》 LOOP=`expr $LOOP + 1`

(7) Numerical Test

Description: Test a number with expr . If you try to calculate a non-integer, an error is returned.

# rr=3.4

# expr $rr + 1

expr: non-numeric argument

# rr=5

# expr $rr + 1

6

(8) Pattern Matching

Description: expr also has a pattern matching function. You can use expr to calculate the number of characters in a string by specifying a colon option. .* means that any character is repeated 0 or more times.

# VALUE=account.doc

# expr $VALUE : ‘.*’

8

String matching can be used in expr Operation, here use the schema to extract the .doc file attachment name.

$expr $VALUE : ‘\\(.*\\).doc’

account

The above is the usage of the Linux expr command, we can use the expr command Perform digital business search, true reproduction of strings, grab strings, etc., which is equivalent to the command line calculator.

Copyright © Windows knowledge All Rights Reserved