How to evaluate expressions in Linux With expr

Configurare noua (How To)

Situatie

Solutie

The expr Command

expr is a tool that can perform calculations and manipulate strings based on the expressions you provide. With the expr command, you can perform various operations on integers and strings, like comparing values or finding specific patterns using regular expressions.

You can pass multiple expressions to expr as arguments, separated by spaces. It not only evaluates an expression but also shows its corresponding output on the terminal. The expr command works both in the Bash terminal and shell scripts.

The expr command is handy when manipulating data or doing calculations without leaving the terminal. However, you need to be careful with the syntax and the order of the expressions, or else the command will fail and display an error message.

The syntax of the expr command is:

expr expression

… where the expression can be a combination of arguments and operators. For example, the below-given expression evaluates the operation between arg1 and arg2 and displays the result:

expr arg1 operator arg2

The arguments can be numbers or strings, depending on the operator. The operators can be arithmetic, relational, string-related, or logical. It is the symbol that specifies the operation to be performed. For example, for integers, you can use operators like +, -, *, /, and %.

For strings, you can use regular expressions and character sets to find matches and indexes. You can also use parentheses to group expressions and backslashes to escape special characters.

expr Command Options

While expr doesn’t have traditional command-line options, it offers versatile operators for arithmetic, string manipulation, and comparison. Furthermore, you can use the –help option to show expr’s help page, which explains its syntax, features, and examples:

expr --help
Linux terminal with expr command documentation

To check the expr command version, run:

expr --version
Linux terminal that shows the version number and developer details of the expr command

This option displays the version number, source code, license, and author of expr.

Performing Arithmetic Operations With expr

To use the expr command for basic arithmetic operations, write the command expr followed by a space. Then, write the expression that you want to evaluate. This expression is a combination of integers and operators such as +, -, *, and /. Make sure to separate each token (integer or operator) in the expression by a space character.

For example, if you want to find out the sum of 15 and 12 using expr, you can write:

expr 15 + 12
Linux terminal showing the addition of two variables using expr command

Similarly, you can use the expr command to perform other arithmetic operations, such as subtraction, multiplication, and division. Let’s evaluate some expressions using the expr command:

expr 15 - 12
expr 15 \* 5
expr 10 / 2
Linux terminal showing various arithmetic operations with expr command

You need to escape the multiplication (*) operator with a backslash (\) to avoid shell expansion. Otherwise, the shell will try to match the operator (*) with the filenames in the current directory and pass them to the expr command, which will cause an error.

Linux terminal with syntax error

You can also prevent the shell from interpreting the characters by quoting the arguments and operators properly:

expr "5" "*" "3"
Linux terminal with variables and operator quotes with double quotations marks

To compare two expressions using the expr command, you can use logical operators such as =, <, >, and !=. If you see the command output one, the condition is true, otherwise, it is false, and the value returned is zero.

For example, to check if the first argument is equal to the second argument, we use the = operator:

expr 40 = 50
Linux terminal displaying checking of the two numbers equality using expr command

Now, let’s check whether the first argument is smaller than the other argument. For that, we use the < operator:

expr 40 \< 50
Linux terminal showing commands to check the smaller variable using the expr command with less than operator

You can also check whether the values are not equal. To do that, simply use the != operator between the two arguments’ values:

expr 45 \!= 55
Linux terminal showing the comparison of two values using the expr command with not equal to operator

Here, the output 1 indicates that 45 is not equal to 55.

This way, the expr command provides a simple and effective method to compare numerical values.

Like the multiplication (*) operator, the <, >, and != operators also need to be escaped with a backslash (\). Otherwise, they may be interpreted as special characters by the shell.

Finding a String’s Length With expr

To find the length of a string using the expr command, you can use the length method. This operator will return the number of characters present in the given string to the output.

FThe Linux terminal displays usage of expr command to decrement the variable value by 1

Tip solutie

Permanent

Voteaza

(1 din 2 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?