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