What other operations can expr do?

0134

The expr command can perform several arithmetic and string operations. Here are some common operations:

Arithmetic Operations:

  1. Addition: expr 5 + 3 → Adds two numbers.
  2. Subtraction: expr 5 - 3 → Subtracts the second number from the first.
  3. Multiplication: expr 5 \* 3 → Multiplies two numbers (note the backslash to escape the *).
  4. Division: expr 6 / 3 → Divides the first number by the second.
  5. Modulus: expr 5 % 3 → Returns the remainder of the division.

String Operations:

  1. Length of a string: expr length "Hello" → Returns the length of the string.
  2. Substring extraction: expr substr "Hello" 2 3 → Extracts a substring starting at position 2 with a length of 3 (returns "ell").

Example:

$ expr 5 + 3        # Addition
8
$ expr 5 - 3        # Subtraction
2
$ expr 5 \* 3       # Multiplication
15
$ expr 6 / 3        # Division
2
$ expr 5 % 3        # Modulus
2
$ expr length "Hello"  # String length
5
$ expr substr "Hello" 2 3  # Substring
ell

Feel free to ask if you need more details or examples!

0 Comments

no data
Be the first to share your comment!