How to Work with Linux Expr command

21

The “expr” is a Linux command-line utility that evaluates and displays the output of an expression. It calculates integer and string expressions, including regular expressions that fit patterns. It stops the invoking command-line shell from acting on characters “expr” that is supposed to process the source of the majority of the difficulties in writing expressions. If expressions are not passed correctly in the “expr” command, the execution order will be interrupted.

The following operators are available:

  • For integers: +, -, /, *, % are all basic operation
  • For Strings: Standard expression evaluation, string operations such as substring, length of strings, and so on.

Syntax:

The syntax of the “expr” command is mentioned below:

expr [Expression]

expr [Option]

Examples:

Let’s discuss the various examples of the “expr” command:

Addition:

Let’s say you want to add two numbers (11 and 6) and use “expr” to do so:

Subtract:

To subtract two numbers, use the “-“operator:

Division:

Here’s the division example:

If you divide two numbers and want to see the remainder, use the % operator:

Multiplication:

Multiplication can be done as follows:

However, * is a built-in shell operator, so that it won’t work. You must use the following syntax to perform like a multiplication operator:

To find length:

Let’s you have a string “hello” and want to check its length, use the commands below:

a=hello

b=`expr length $a`

echo $b

You can also use the “expr” command to perform a variety of string-related operations.

For example, to find the length, use the command as follows:

To check the index of character:

You have a string “LinuxHint” and want to see any character’s position in the string. For example, you want to check the position of character “t”, execute the below-mentioned commands:

$ a=linuxhint

$ b=`expr index $a t`

$ echo $b

Extract the substring:

If you want to trim the substring from a string ‘HelloWorld’, select a substring beginning and end. For instance, the following command fourth character is the beginning of the substring, and the ending is the tenth. Execute the commands given below:

$ a=HelloWorld

$ b=`expr substr $a 4 10`

$ echo $b

You can also use the “expr” command to get the index of a substring from a string:

$ expr substr [string] [position] [length]

$expr substr linuxhint 5 9

Compare two expressions:

Using the “expr” command, we can compare two expressions as well. To compare the expressions, use logical operators such as “=, >, !=”. If the condition is valid, it will show 1; otherwise, it will show 0 in the output.

Let’s say you have two numbers, 30 and 80, and assign them as variables a and b:

To see if a and b are equivalent, do the following:

c=`expr $a = $b`

echo $c

To see if a if a is less than b:

c=`expr $a strong>strong> $b`

echo $c

To see if a is not equal to b:

c=`expr $a !$b`

echo $c

Match the number of characters of two string:

Let’s say you have two strings and want to match the number of characters. Follow the syntax:

$ expr String 1 : String 2

$expr helloworls: helloworld

Increment a variable:

Also, the expr command is used to increase the value. Consider a variable with the value 20 as an example:

a=20

Increase the value by 1:

$ b=`expr $a + 1`

$b

Help:

To print help information, use the “–help” option with the “expr” command:

Version:

To print version, use “–version” with “expr” command:

Conclusion:

The expr command evaluates and displays the standard output of a given expression. Each distinct expression is regarded as a statement. Integer and string expressions and regular expressions are used with the “expr” command. We have covered multiple examples of expr command in this guide.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More