Linux Komutları

How to Work with Linux Expr command

How to Work with Linux Expr command
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:

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:

$expr 11+6

Subtract:

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

$expr 11-8

Division:

Here's the division example:

$ expr 16 / 4

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

$ expr 24 % 5

Multiplication:

Multiplication can be done as follows:

$ expr 10 * 4

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:

$ expr 10 \* 4

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:

$ expr length [ String ]

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:

a=50
b=70

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 \< $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

$ expr linuxhint: Linux

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:

$ expr --help

Version:

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

$ expr --version

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.

Mouse left-click button not working on Windows 10
If you are using a dedicated mouse with your laptop, or desktop computer but the mouse left-click button is not working on Windows 10/8/7 for some rea...
Cursor jumps or moves randomly while typing in Windows 10
If you find that your mouse cursor jumps or moves on its own, automatically, randomly while typing in Windows laptop or computer, then some of these s...
How to reverse Mouse and Touchpads scrolling direction in Windows 10
Mouse and Touchpads not only make computing easy but more efficient and less time-consuming. We cannot imagine a life without these devices, but still...