expr command (with examples)

expr command (with examples)

The expr command in Linux is a handy utility that allows you to evaluate expressions and manipulate strings. It provides various functions for performing mathematical calculations, working with strings, and pattern matching. In this article, we will explore eight different use cases of the expr command and provide code examples for each.

Getting the length of a specific string

One use case of the expr command is to find the length of a string. To achieve this, we can use the length function provided by expr. Here’s an example code:

expr length "string"
  • “string”: This is the input string for which we want to find the length.

Motivation: Knowing the length of a string can be useful in various scenarios, such as validating input limits or manipulating strings based on their length.

Example Output:

$ expr length "Hello World"
11

Getting the substring of a string with a specific length

In some cases, we may need to extract a substring of a specific length from a string. The expr command provides the substr function for this purpose. Here’s an example code:

expr substr "string" from length
  • “string”: This is the input string from which we want to extract a substring.
  • from: This argument specifies the starting position of the substring.
  • length: This argument specifies the length of the substring.

Motivation: Extracting substrings is a common task when dealing with text processing or manipulating strings.

Example Output:

$ expr substr "Hello World" 7 5
World

Matching a specific substring against an anchored pattern

Another useful feature of expr is the ability to match a specific substring against an anchored pattern. We can achieve this using the match function of expr. Here’s an example code:

expr match "string" 'pattern'
  • “string”: This is the input string that we want to match against the pattern.
  • ‘pattern’: This is the anchored pattern to match against.

Motivation: Pattern matching is often required for tasks like validation or extracting specific information from strings.

Example Output:

$ expr match "Hello World" 'Hello'
5

Getting the first character position from a specific set in a string

Sometimes, we may need to find the position of the first character in a string that matches a specific set of characters. The expr command provides the index function to accomplish this. Here’s an example code:

expr index "string" "chars"
  • “string”: This is the input string in which we want to search for a character.
  • “chars”: This argument specifies the set of characters to search for.

Motivation: Finding the position of a character is useful for various operations, such as replacing or manipulating specific characters within a string.

Example Output:

$ expr index "Hello World" "o"
5

Calculating a specific mathematical expression

expr is primarily known for its ability to perform mathematical calculations. It supports basic arithmetic operations like addition, subtraction, multiplication, division, and modulus. Here’s an example code:

expr expression1 +|-|*|/|% expression2
  • expression1: This is the first expression for the desired calculation.
  • +|-|*|/|%: This is the operator specifying the mathematical operation.
  • expression2: This is the second expression for the desired calculation.

Motivation: expr’s mathematical capabilities can come in handy when performing quick calculations or incorporating arithmetic operations into shell scripts.

Example Output:

$ expr 2 + 3
5

Getting the first expression if its value is non-zero and not null, otherwise getting the second one

The expr command provides a conditional expression evaluation feature. It allows us to output the value of the first expression if it is non-zero and not null. Otherwise, we can output the value of the second expression. Here’s an example code:

expr expression1 \| expression2
  • expression1: This is the first expression to evaluate.
  • expression2: This is the second expression to evaluate.

Motivation: Conditional expressions are common in scripting, and expr’s ability to evaluate them enables us to handle different situations based on specific conditions.

Example Output:

$ expr 2 \| 3
2

Getting the first expression if both expressions are non-zero and not null, otherwise getting zero

Another interesting feature of the expr command is the ability to return the value of the first expression if both expressions are non-zero and not null. Otherwise, it returns zero. Here’s an example code:

expr expression1 \& expression2
  • expression1: This is the first expression to evaluate.
  • expression2: This is the second expression to evaluate.

Motivation: This feature can be useful for evaluating complex conditions and performing actions based on the results.

Example Output:

$ expr 2 \& 3
2

In conclusion, the expr command is a powerful tool for evaluating mathematical expressions and manipulating strings in Linux. It provides various functions that come in handy for various tasks, from simple calculations to string manipulations and pattern matching. By understanding the different use cases and syntax provided in this article, you can make the most of the expr command in your Linux command-line operations.

Related Posts

How to use the command sqfscat (with examples)

How to use the command sqfscat (with examples)

The sqfscat command is used to concatenate files from a squashfs filesystem and print them to standard output (stdout).

Read More
Exploring `czkawka-cli` Command with Examples

Exploring `czkawka-cli` Command with Examples

List Duplicate or Similar Files in Specific Directories To use czkawka-cli to list duplicate or similar files in specific directories, you can execute the following command:

Read More
How to use the command ctkd (with examples)

How to use the command ctkd (with examples)

The ctkd command is a SmartCard daemon that should not be invoked manually.

Read More