What are other expr operators?

082

In addition to the basic arithmetic operators, the expr command in Linux supports several other operators for string and logical operations. Here’s a concise overview of the operators you can use with expr:

Arithmetic Operators

  1. Addition: +

    expr 5 + 3  # Outputs: 8
  2. Subtraction: -

    expr 5 - 3  # Outputs: 2
  3. Multiplication: * (must be escaped or quoted)

    expr 5 \* 3  # Outputs: 15
  4. Division: /

    expr 6 / 3  # Outputs: 2
  5. Modulus: % (remainder of division)

    expr 5 % 3  # Outputs: 2

String Operators

  1. String Length: To find the length of a string, use the length function:

    expr length "Hello"  # Outputs: 5
  2. String Comparison: You can compare strings using = or !=:

    expr "Hello" = "Hello"  # Outputs: 1 (true)
    expr "Hello" != "World"  # Outputs: 1 (true)

Logical Operators

  1. Logical AND: &

    expr 1 \& 1  # Outputs: 1 (true)
  2. Logical OR: |

    expr 0 \| 1  # Outputs: 1 (true)

Comparison Operators

  1. Greater Than: >

    expr 5 \> 3  # Outputs: 1 (true)
  2. Less Than: <

    expr 5 \< 3  # Outputs: 0 (false)
  3. Greater Than or Equal To: >=

    expr 5 \>= 5  # Outputs: 1 (true)
  4. Less Than or Equal To: <=

    expr 5 \<= 5  # Outputs: 1 (true)

Summary

The expr command is versatile, allowing for various arithmetic, string, and logical operations. Remember to use spaces around operators and escape special characters like *, &, and | to avoid syntax errors.

If you're interested in practicing these operators, consider trying them out in your terminal or exploring related LabEx labs on shell scripting and command-line utilities. Let me know if you have any more questions!

0 Comments

no data
Be the first to share your comment!