- Unix Commands Reference
- Unix Commands - Home
expr Command in Linux
expr is a Linux command used to evaluate expressions and display the results on the terminal. You can use this command to perform basic arithmetic operations like addition, subtraction, multiplication, modulus or division. Apart from that, you can also use the expr command to compare values and evaluate regular expressions.
The expr command is a versatile tool that you can use in shell scripting to handle various calculations and text manipulations.
Table of Contents
Here is a comprehensive guide to the options available with the expr command −
- Syntax of expr Command in Linux
- Different Options Available for expr Command
- Examples of expr Command in Linux
Syntax of expr Command in Linux
The basic syntax to use the expr command on Linux is as follows −
expr expression
Here, expression represents the arithmetic or string operation you want to evaluate.
Different Options Available for expr Command
The primary focus of expr command is to evaluate and output the result of the given expression. Thus, it doesn’t provide any options for customizing its behavior. Instead, you can only use options like --help and --version to display help and version information about the command.
Examples of expr Command in Linux
Let’s explore a few examples of expr command in Linux system for performing different arithmetic expressions −
Addition
Consider an example, where we have two numbers 3 and 4 and want to add them using the expr command. For that purpose, you can simply use expr command followed by the expression for addition of two numbers, as given below −
expr 3 + 4
Note − Make sure to add a space between the numbers and operator to get the desired result.
Multiplication
For multiplication of two numbers using expr command, you need to escape the * character with a backslash (\). This is because the asterisk has a special meaning in the shell. For example −
expr 4 \* 2
This command will output 8, which is the result of multiplying 4 by 2.
Note − You can do the same for division and modulus as well.
String Length
You can also find the length of the string with the help of expr command. For example, let’s say we have a string "hello, world!", to find its length, we can use −
expr length "hello, world!"
Substring Extraction
To extract a substring from a string using the expr command, you must specify the starting position and the length of the substring. Here’s how you can do it −
expr substr "hello world!" 3 4
This command will output “llo,”, which is the substring starting from the 3rd character and of length 4.
Comparison
To compare two values using the expr command, you can simply use logical operators like =, <, >, and !=. If the condition is true, the command outputs 1; otherwise, it outputs 0. Here’s an example −
expr 5 \> 3
The above command will output 1, which indicates that 5 is greater than 3.
That’s how you can use the expr command on your Linux system.
Conclusion
The expr is a versatile command on Linux systems used for performing arithmetic operations, comparison, finding string length or extracting substring from a string.
In this user-friendly tutorial, we have covered the basic syntax of expr command and explored a few examples on Linux systems. These examples will help you learn the basics of performing addition, multiplication, comparison and other such tasks directly on the terminal.