Linux expr commandThe expr command is used to evaluate a given expression and display its standard output. Each separated expression is considered as an argument. These expressions could be integer and string expressions, including regular expressions. If expressions are not passed properly, it will prevent the execution of the command. The expr command supports the following operators:
It will be useful if we want to perform an operation while working on the terminal, such as searching for a substring in a string, searching its index, performing arithmetic operations, and more. So, the expr command allows us to perform all these tasks from the terminal. Syntax:expr expression Options:The expr command supports the following options to make it more useful.
The above command will display the help and exit. Consider the below output:
Consider the below output: Let's understand some basic and advanced examples of the expr command. In this section, we will perform all the examples on ubuntu, but it can also work on all the Linux based systems. Example1: Perform basic arithmetic operations using expr command. To perform basic arithmetic operations, execute the expr command, and pass the integers and operators on it. Remember, every token(expressions) should be separated by a space. Consider the following examples:
Output:
Output:
Output:
Output: Example2: Find the length of a string. To find the length of a string, let's take a string 'ALPHABET.' Execute the following commands to find the length of the given string: In the above commands, the first variable stores the value of the given string, and the second variable stores the length of the string. The echo command will display the value of b. Consider the below output: Example3: Find the position of the character in a string. To find the position of character 'P' in the string 'ALPHABET,' execute the following commands: The above commands will display the position of the specified character. Consider the below output: It is displaying the position as '3', as it is the third character from the beginning. Example4: Find the substring from a string. Let's find a substring starting from the third character to sixth, from the string 'ALPHABET.' Execute the following commands: The above commands will trim the string having length 6 from the third character. Consider the below output: Example5: Perform an operation on variables using a shell script. We can also perform operations on a variable by using a shell script. For example, let's create a script for adding two numbers. Create a file 'script.sh,' and write the following script in it: The above script will take the values of variables a and b from the terminal and perform addition by using expr command. Make the script executable by using the chmod command as follows: Now, execute the script as: Consider the below output: Example6: Compare two expressions. We can compare two expressions by using the expr command. Several logical operators such as "=, <, >, !=", are used to compare the expressions. It will result in 1 if the condition is true and 0 if it is false. Let's take two numbers, 50 and 70 perform some operations on it. Declare two variables a and b as follows: Check if a is equal to b: Check if a is less than b: Check if a is not equal to b: From the above commands, the output will be 1 if the condition is true otherwise 0. Consider the below output: Example7: Match the numbers of characters in two strings. We can display the number of matching characters in two strings. To display the number of matching characters, execute the expr command, and pass the strings as follows: For example, execute the following commands: The above commands will display the number of matching characters. Consider the below output: Example8: Increment a variable. The expr command is also used to make an increment to the value of a variable. For example, declare a variable having value 20: Increase the value by 1: To increase the value by 5: In the above commands, we have stored the incremented value in a new variable 'b,' but the updated value can be stored in the same variable as well. Consider the below output: Example9: Find the modulus value. To find the modulus value, use the operator modulo (%). For example, find the 10 mod 5, execute the command as: for 20 mod 30 execute the command as: The above commands will display the mod value of the given numbers. Consider the below output: Example10: Display the true condition between the two conditions. There are two conditions for a scenario that one is true, and the other is false, and we want to display the true condition. To do so, we will use the '|' operator between two conditions, and it will display the condition1 if it is true; otherwise, it will display the condition2. Consider below commands: In the above commands, there are two conditions c and d, and we are comparing them in variable e. It will print true condition. Consider the below output: Next TopicLinux RegEx |