![Forrest logo](/images/logo.png)
expr
The expr
command line tool is a utility in Unix-like operating systems that evaluates and prints the value of an expression. It is commonly used to perform basic arithmetic operations, string manipulation, and logical comparisons within shell scripts or directly from the command line. Some key features of expr
include: 1. Arithmetic operations: expr
can perform addition, subtraction, multiplication, and division on numbers. 2. String manipulation: It can extract substrings, match regular expressions, and concatenate strings. 3. Logical comparisons: expr
allows you to perform comparisons such as equality, inequality, greater than, and less than between numbers or strings. 4. Conditional expressions: It supports basic if-else style conditions, enabling executing different code paths based on the expression's evaluation. 5. Error handling: expr
handles potential errors like division by zero or invalid operands by returning appropriate error codes. Here's a simple example demonstrating the usage of expr
: $ expr 5 + 3 8 $ expr length "Hello, World!" 13 $ expr substr "This is a test" 6 2 is $ expr index "abcdefg" "g" 7 $ expr \( 2 + 3 \) \* 4 20
In addition to these basic operations, expr
can be combined with other command line utilities and control flow constructs to build more complex scripts or one-liners.
List of commands for expr:
-
expr:tldr:0561c expr: Calculate a specific mathematic expression.$ expr ${expression1} ${select} ${expression2}try on your machineexplain this command
-
expr:tldr:1f2c3 expr: Get the length of a specific string.$ expr length "${string}"try on your machineexplain this command
-
expr:tldr:2704a expr: Get the first expression if its value is non-zero and not null otherwise get the second one.$ expr ${expression1} \| ${expression2}try on your machineexplain this command
-
expr:tldr:30178 expr: Get the first char position from a specific set in a string.$ expr index "${string}" "${chars}"try on your machineexplain this command
-
expr:tldr:327a4 expr: Get the substring of a string with a specific length.$ expr substr "${string}" ${from} ${length}try on your machineexplain this command
-
expr:tldr:668e2 expr: Get the first expression if both expressions are non-zero and not null otherwise get zero.$ expr ${expression1} \& ${expression2}try on your machineexplain this command
-
expr:tldr:b34dc expr: Match a specific substring against an anchored pattern.$ expr match "${string}" '${pattern}'try on your machineexplain this command