The expr command can perform several arithmetic and string operations. Here are some common operations:
Arithmetic Operations:
- Addition:
expr 5 + 3→ Adds two numbers. - Subtraction:
expr 5 - 3→ Subtracts the second number from the first. - Multiplication:
expr 5 \* 3→ Multiplies two numbers (note the backslash to escape the*). - Division:
expr 6 / 3→ Divides the first number by the second. - Modulus:
expr 5 % 3→ Returns the remainder of the division.
String Operations:
- Length of a string:
expr length "Hello"→ Returns the length of the string. - 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!
