# The expr command in Linux – Evaluate Expressions and Perform String Operations The expr command in Linux evaluates a given expression for you and displays the output. It also lets you perform certain string operations such as finding length, substring and more. In this tutorial, we will learn how to perform these operations. First, we will look at the arithmetic operations and then move on to the string operations.

Let’s get started!

## Performing Arithmetic Operations with expr

You can perform basic arithmetic operations such as addition, subtraction, multiplication and division with the expr command. Let’s take a look at these operations one by one.

You can perform addition with the expr command using :

``` expr 8 + 4
```

Output :

```12
```

### 2. Subtraction Operations

You can perform subtraction with the expr command using :

``` expr 8 - 4
```

Output :

```4
```

### 3. Multiplication Operations

You can perform multiplication with the expr command using :

``` expr 8 \* 4
```

Note : We use the backslash to escape the multiplication character.

Output :

```32
```

### 4. Division Operations

You can perform division with the expr command using :

``` expr 8 / 4
```

Output :

```2
```

## Using expr command with variables

You can also use expr with variables. That means you can also use expr in your shell scripts. Let’s see an example.

```x= 15

y=`expr \$x + 10`

echo \$y
```

Output :

```25
```

## Performing String Operations with expr command

Now let’s learn about the string operations that you can perform using the expr command. You can perform the following operations with the expr:

### 1. Find the length of a string

To find the length of a string use:

```expr length [string]
```

Let’s see an example :

```expr length Hello
```

Output :

```5
```

### 2. Extract a substring

You can also use expr to take out a substring from a string. The syntax for that is :

```expr substr [string] [pos] [length]
```

Where [string] is the parent string, [pos] is the starting position for the substring and [length] is the length of the substring to be extracted.

Let’s see an example :

```expr substr LinuxForDevices 6 3
```

Output :

```For
```

### 3. Find starting index of a pattern

You can also use expr to get the starting index of a pattern in the string (pattern is a substring). The syntax for that is:

```expr index [string] [pattern]
```

Let’s see an example :

```expr index LinuxForDevices ux
```

Output :

```4
```

### 4. Compare two strings

You can use expr to compare two strings as well. Comparing using expr gives the number of matching characters in the two strings. The syntax for comparing two strings is :

```expr [string1] : [string2]
```

Let’s see an example:

```expr bookmark : book
```

Output :

```4
```

## Conclusion

This tutorial was about expr in Linux. We learned how this command is used for evaluating arithmetic expressions and performing string operations. To learn more about the expr command read its man page. Alternatively, use the man command for the same.