expr

text processingLinux/Unix
The expr command is one of the most frequently used commands in Linux/Unix-like operating systems. expr Evaluate expressions and display the results

Quick Reference

Command Name:

expr

Category:

text processing

Platform:

Linux/Unix

Basic Usage:

expr [options] [arguments]

Common Use Cases

    Syntax

    expr EXPRESSION

    Options

    Operator Description
    ARG1 | ARG2 If ARG1 is neither null nor 0, returns ARG1; otherwise, returns ARG2
    ARG1 & ARG2 If neither ARG1 nor ARG2 is null or 0, returns ARG1; otherwise, returns 0
    ARG1 < ARG2 Returns 1 if ARG1 is less than ARG2, otherwise 0
    ARG1 <= ARG2 Returns 1 if ARG1 is less than or equal to ARG2, otherwise 0
    ARG1 = ARG2 Returns 1 if ARG1 equals ARG2, otherwise 0
    ARG1 != ARG2 Returns 1 if ARG1 is not equal to ARG2, otherwise 0
    ARG1 >= ARG2 Returns 1 if ARG1 is greater than or equal to ARG2, otherwise 0
    ARG1 > ARG2 Returns 1 if ARG1 is greater than ARG2, otherwise 0
    ARG1 + ARG2 Returns the arithmetic sum of ARG1 and ARG2
    ARG1 - ARG2 Returns the arithmetic difference of ARG1 and ARG2
    ARG1 * ARG2 Returns the arithmetic product of ARG1 and ARG2
    ARG1 / ARG2 Returns the arithmetic quotient of ARG1 divided by ARG2
    ARG1 % ARG2 Returns the arithmetic remainder of ARG1 divided by ARG2
    STRING : REGEX Performs pattern matching of REGEX against STRING
    match STRING REGEX Same as STRING : REGEX
    substr STRING POS LENGTH Returns the substring of STRING starting at position POS with length LENGTH
    index STRING CHARS Returns the position in STRING where CHARS is found, or 0 if not found
    length STRING Returns the length of STRING
    + TOKEN Interpret TOKEN as a string, even if it's a keyword like 'match' or an operator
    ( EXPRESSION ) Value of EXPRESSION

    Examples

    How to Use These Examples

    The examples below show common ways to use the expr command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    expr 5 + 3
    Evaluate the addition of 5 and 3, displaying 8.
    expr 10 - 4
    Evaluate the subtraction of 4 from 10, displaying 6.
    expr 5 \* 3
    Evaluate the multiplication of 5 and 3, displaying 15. # Advanced Examples Advanced expr 10 / 3 Perform integer division of 10 by 3, displaying 3. expr 10 % 3 Calculate the remainder when dividing 10 by 3, displaying 1. expr length "hello" Return the length of the string "hello", displaying 5. expr "hello" : "h\(.*\)o" Extract "ell" using a regular expression, displaying 3 (the length of the match). expr index "hello world" "lo" Find the position of "lo" in "hello world", displaying 4. expr $num = 5 Test if variable num equals 5, returns 1 for true, 0 for false. expr $num \> 5 Test if variable num is greater than 5, returns 1 for true, 0 for false.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The expr command evaluates expressions and displays the results. It is primarily used for arithmetic operations, string manipulation, and comparison testing within shell scripts. Important notes about using expr: 1. Space Sensitivity: expr requires spaces between operators and arguments. For example, '5+3' will not work, but '5 + 3' will. 2. Shell Escaping: Many characters used in expr expressions have special meaning in shell scripting and must be escaped with a backslash (\). This includes *, (, ), and other special characters. 3. Return Values: expr returns 0 (success) if the expression evaluates to a non-zero and non-null value, 1 if the expression evaluates to 0 or null, and 2 if the expression is invalid. 4. Integer Operations: By default, expr performs integer arithmetic only. For floating-point operations, other tools like bc are more appropriate. 5. String Operations: expr provides several string manipulation functions, including pattern matching with regular expressions, substring extraction, and string length calculation. 6. Performance: For simple operations in modern shells, built-in arithmetic (like $((...)) in bash) is generally more efficient than calling the external expr command. Expr is particularly useful in older shell scripts and in situations where maximum portability is required across different UNIX-like systems.

    Related Commands

    These commands are frequently used alongside expr or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the expr command works in different scenarios.

    $ expr
    View All Commands