What Is Expr in Bash Scripting?

//

Angela Bailey

What Is Expr in Bash Scripting?

In Bash scripting, the expr command is a powerful tool that allows you to perform various arithmetic and string operations. It is especially useful when you need to manipulate and evaluate data within your scripts. In this tutorial, we will explore the different use cases of the expr command and how it can simplify your scripting tasks.

Arithmetic Operations

The expr command provides a range of arithmetic operations that can be performed on numeric values. These operations include addition, subtraction, multiplication, division, modulus, and exponentiation.

To illustrate these operations, consider the following example:

NUM1=10
NUM2=5

SUM=$(expr $NUM1 + $NUM2)
DIFF=$(expr $NUM1 - $NUM2)
PRODUCT=$(expr $NUM1 \* $NUM2)
QUOTIENT=$(expr $NUM1 / $NUM2)
MODULUS=$(expr $NUM1 % $NUM2)
POWER=$(expr $NUM1 \*\* 2)

echo "Sum: $SUM"
echo "Difference: $DIFF"
echo "Product: $PRODUCT"
echo "Quotient: $QUOTIENT"
echo "Modulus: $MODULUS"
echo "Power of 10 squared: $POWER"

This script declares two variables, NUM1 and NUM2, with values 10 and 5 respectively. Using the expr command with appropriate operators, we perform various arithmetic operations on these variables.

  • The Addition (+) operation adds the values of $NUM1 and $NUM2 and stores the result in the variable SUM.
  • The Subtraction (-) operation subtracts the value of $NUM2 from $NUM1 and stores the result in the variable DIFF.
  • The Multiplication (*) operation multiplies the values of $NUM1 and $NUM2 and stores the result in the variable PRODUCT.
  • The Division (/) operation divides the value of $NUM1 by $NUM2 and stores the result in the variable QUOTIENT.
  • The Modulus (%) operation calculates the remainder when dividing $NUM1 by $NUM2, storing it in the variable< code >MODULUS.
     

  • The Exponentiation ( ** ) operation calculates< code > $ NUM1 raised to< code > $ NUM2 power, storing it in a variable named< code > POWER.

    In this example, we use various arithmetic operations to manipulate numeric values. However, keep in mind that all variables within an expression must be enclosed in dollar signs ($) to denote their values.

    String Operations

    In addition to arithmetic operations, the expr command also supports string manipulation. It allows you to concatenate strings using a simple syntax.

    To concatenate two strings, use the following format:

    STRING1="Hello"
    STRING2="World"
    
    CONCATENATED=$(expr $STRING1 : '$STRING1$STRING2')
    
    echo "Concatenated String: $CONCATENATED"
    

    In this example, we have two variables, STRING1 and STRING2, with values “Hello” and “World” respectively. Using the expr command, we concatenate these two strings and store the result in the variable CONCATENATED.

    The syntax for string concatenation follows a specific pattern:
    $VARIABLE1 : '$VARIABLE1$VARIABLE2'

    Note that string concatenation using expr requires single quotes () to denote the expression. Additionally, using double quotes () around variables is essential to preserve whitespace.

    In Conclusion

    The expr command in Bash scripting is a versatile tool that allows you to perform arithmetic and string operations. By understanding its capabilities, you can utilize it effectively to manipulate data within your scripts.

    In this tutorial, we explored the usage of expr for arithmetic operations such as addition, subtraction, multiplication, division, modulus, and exponentiation. We also demonstrated how to concatenate strings using this command.

    Bash scripting becomes more powerful when you have a variety of tools at your disposal. The expr command is undoubtedly one such tool that enhances your scripting capabilities.

    Note: The expr command has been deprecated in favor of using arithmetic expansion or other built-in commands like (( )), $(( )), or the let command. However, it is still worth understanding expr as you may come across it in legacy scripts or older documentation.