Linux expr Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn about the Linux expr command and how to use it for various text processing and editing tasks. The expr command is a powerful tool that allows you to perform arithmetic operations, string manipulation, and conditional expressions directly from the command line. You will explore the purpose and syntax of the expr command, learn how to perform basic arithmetic operations, and apply it for string manipulation and conditional expressions. This lab will equip you with the knowledge to effectively utilize the expr command in your daily Linux workflow.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicSystemCommandsGroup -.-> linux/test("`Condition Testing`") linux/TextProcessingGroup -.-> linux/expr("`Evaluate Expressions`") subgraph Lab Skills linux/echo -.-> lab-422671{{"`Linux expr Command with Practical Examples`"}} linux/test -.-> lab-422671{{"`Linux expr Command with Practical Examples`"}} linux/expr -.-> lab-422671{{"`Linux expr Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the expr Command

In this step, you will learn about the purpose and syntax of the expr command in Linux. The expr command is a powerful tool for performing various operations, including arithmetic calculations, string manipulation, and conditional expressions.

The basic syntax of the expr command is:

expr [option] expression

Here, expression is the operation or expression that you want to evaluate. The expr command can perform the following types of operations:

  • Arithmetic operations: +, -, *, /, %
  • String manipulation: match, substr, index, length
  • Logical and comparison operations: =, !=, \<, \>, \<=, \>=, |, &

Let's start by exploring some basic arithmetic operations using expr.

## Perform basic arithmetic operations
expr 5 + 3
expr 10 - 4
expr 6 \* 7
expr 15 / 3
expr 17 % 5

Example output:

8
6
42
5
2

In the above examples, we used the expr command to perform addition, subtraction, multiplication, division, and modulo operations. Note that for multiplication, we need to escape the * character with a backslash (\*) to prevent the shell from interpreting it as a wildcard.

Now, let's explore some string manipulation operations using expr.

## Perform string manipulation
expr "Hello" : '\(.*\)'
expr "Linux is fun" : '.*is\(.*\)'
expr "www.example.com" : '.*\(.*\)\..*'

Example output:

Hello
 fun
example

In these examples, we used the match operator to extract substrings from the input strings using regular expressions. The match operator returns the part of the string that matches the regular expression pattern.

The expr command can also be used for conditional expressions and logical operations. Here's an example:

## Perform conditional expressions
expr 5 \> 3
expr 7 \< 10
expr 4 = 4
expr 8 \!= 5

Example output:

1
1
1
1

In the above examples, the expr command returns 1 if the condition is true, and 0 if the condition is false.

Perform Basic Arithmetic Operations Using expr

In this step, you will learn how to use the expr command to perform basic arithmetic operations, such as addition, subtraction, multiplication, division, and modulo.

Let's start by performing some arithmetic operations:

## Addition
expr 12 + 5

Example output:

17
## Subtraction
expr 20 - 8

Example output:

12
## Multiplication
expr 6 \* 4

Example output:

24
## Division
expr 18 / 3

Example output:

6
## Modulo
expr 19 % 7

Example output:

5

In the above examples, we used the expr command to perform basic arithmetic operations. Remember to escape the * character with a backslash (\*) when performing multiplication to prevent the shell from interpreting it as a wildcard.

Now, let's try some more complex arithmetic expressions:

## Complex expression
expr \( 10 + 5 \) \* 3 / 2 + 1

Example output:

26

In this example, we used parentheses () to group the operations and control the order of evaluation. The expression (10 + 5) * 3 / 2 + 1 is evaluated as follows:

  1. 10 + 5 = 15
  2. 15 * 3 = 45
  3. 45 / 2 = 22.5
  4. 22.5 + 1 = 23.5, which is rounded down to 26 by the expr command.

Apply expr for String Manipulation and Conditional Expressions

In this step, you will learn how to use the expr command for string manipulation and conditional expressions.

Let's start with string manipulation:

## Extract a substring
expr "Linux is fun" : '.*is\(.*\)'

Example output:

 fun

In this example, we used the match operator with a regular expression to extract the substring that comes after the word "is" in the input string.

## Get the length of a string
expr "hello" : '.\{5\}'

Example output:

5

Here, we used the match operator with a regular expression to get the length of the string "hello".

Now, let's explore some conditional expressions:

## Comparison operations
expr 7 \< 10
expr 5 \> 3
expr 4 = 4
expr 8 \!= 5

Example output:

1
1
1
1

In these examples, we used the expr command to perform comparison operations, such as less than, greater than, equal to, and not equal to. The expr command returns 1 if the condition is true, and 0 if the condition is false.

You can also combine multiple conditions using logical operators:

## Logical operations
expr 5 -eq 5 -a 7 -gt 3
expr 4 -ne 5 -o 8 -lt 10

Example output:

1
1

In these examples, we used the -eq, -gt, -ne, and -lt operators to perform logical operations, such as AND (-a) and OR (-o).

Summary

In this lab, you learned about the purpose and syntax of the expr command in Linux. You explored how to use expr for performing basic arithmetic operations, string manipulation, and conditional expressions. You discovered that expr can be a powerful tool for various types of calculations and text processing tasks in the command line. The lab provided practical examples to demonstrate the versatility of the expr command, equipping you with the knowledge to leverage it effectively in your Linux workflows.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like