Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the js_composer domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/holhol2/public_html/wp-includes/functions.php on line 6121

How to Work with Linux Expr command

By holhol24 | April 27, 2021
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:

  • 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.

holhol24

I am a Linux server pro admin and enthusiast, an upcoming web developer, junior php developer. I am currently a content creator for Holhol24, I love to play with Linux and all other distributions of it. I loves working with computers and strongly believes in sharing knowledge. Follow me For more insight into what is happening in Tech.

Advertisement Share: Facebook Twitter WhatsApp

Deprecated: File Theme without comments.php is deprecated since version 3.0.0 with no alternative available. Please include a comments.php template in your theme. in /home/holhol2/public_html/wp-includes/functions.php on line 6121

Leave a Reply

Your email address will not be published. Required fields are marked *