Skip to content

expr Linux Command Guide

The expr command in Linux is used for evaluating expressions and performing arithmetic operations. It can be used to perform various mathematical calculations, string manipulations, and logical operations. By utilizing operators such as addition (+), subtraction (-), multiplication (*), division (/), and modulus (%), users can quickly evaluate and manipulate numeric values. Additionally, the expr command can be used to compare values, test for equality, and perform other conditional operations. It is a versatile tool that can be utilized within shell scripts or directly from the command line to streamline tasks requiring mathematical or logical operations.

expr Syntax:

Terminal window
expr [expression]

Options:

OptionDescription
:Used to separate multiple expressions in one line.

Parameters:

ParameterDescription
expressionThe mathematical expression to be evaluated.

expr Command Samples:

Perform Basic Arithmetic Operations

Terminal window
expr 10 + 5

Calculates the sum of 10 and 5 using the expr command.

Evaluate an Arithmetic Expression

Terminal window
expr 20 \* 3

Evaluates the multiplication of 20 and 3 using the expr command.

Increment a Variable

Terminal window
count=5
expr $count + 1

Increments the value of the variable count by 1.

Check if Two Numbers are Equal

Terminal window
expr 10 = 10

Checks if the numbers 10 and 10 are equal using the expr command.

Extract a Substring Length

Terminal window
string="HelloWorld"
expr length "$string"

Calculates the length of the substring “HelloWorld” using the expr command.

Compare Two Numbers

Terminal window
expr 15 '>' 10

Compares whether 15 is greater than 10 using the expr command.

Perform Arithmetic Operations within a Shell Script

#!/bin/bash
num1=20
num2=5
result=$(expr $num1 / $num2)
echo "Result: $result"

Performs division operation between num1 and num2 within a bash shell script using the expr command.

expr FAQ:

How do I use expr in Linux?

To use the expr command in Linux, execute the following command:

Terminal window
expr 10 + 5

What is expr used for in Linux?

Expr is a command-line utility that evaluates expressions, performs text manipulation, and provides arithmetic operations in shell scripts.

How can I perform subtraction using expr in Linux?

To perform subtraction using expr in Linux, use the following command format:

Terminal window
expr 15 - 6

How do I concatenate strings using expr in Linux?

To concatenate strings using expr in Linux, use the following command format:

Terminal window
expr "Hello, " : '.*' "World"

Can expr be used to compare numbers in Linux?

Yes, expr can be used to compare numbers in Linux by utilizing the equal sign (==) for comparison as shown in the following command:

Terminal window
expr 10 == 10

How do I perform string length calculation using expr in Linux?

To calculate the length of a string using expr in Linux, use the following command format:

Terminal window
expr length "Linux"

Applications of the expr command

  1. Performing arithmetic operations
  2. Extracting substrings from strings
  3. Incrementing or decrementing values
  4. Evaluating conditions and expressions
  5. Converting infix expressions to postfix expressions