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 [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:
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:
$ 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:
$ 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 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:
b=70
To see if a and b are equivalent, do the following:
echo $c
To see if a if a is less than b:
echo $c
To see if a is not equal to 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 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:
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.