Introduction
In the world of system management and shell scribing, having tools to evaluate expressions quickly and reliably is essential. The commandexpris one of those classic utilities that has been present in Unix and Linux systems for decades. Although today there are more powerful alternatives like the arithmetic expansion of Bash ($(( ))) orbc, exprremains useful in environments where compatibility with POSX shells is required or when working on very simple scripts. This post explains what it isexpr, how it is used, what types of expressions it can handle and what are its limitations, all with practical examples that you can copy and paste into your terminal.
What is expr?
The commandexprevaluate expressions and write the result on the standard output. It was originally designed to work on the Bourne shell and continues to respect the POSIX standard. You can handle entire arithmetic operations, comparisons, logical operations and some chain manipulations. Its syntax is quite direct: it passes the expression as arguments separated by spaces and returns the result as text. If the expression is invalid,exprreturns an error message and an output state other than zero.
Basic syntax
The general form is:
EXr EXPRESSION
Each operator and operating must be separated by spaces. For example, to add two numbers it is writtenexpr 5 + 3. The arithmetic operators supported are+, -, *, /and%(module). It is important to remember that the asterisk (*) has special meaning for the shell, so it must escape or intertwine:expr 5 \* 3orexpr 5 '*' 3. The comparison operators include=, !=, <, <=, >and>=. They return 1 if the condition is true and 0 if it is false. The logical operators are|(OR) and&(AND), also requiring escape or quotes.
Practical examples of arithmetic operations
- Amount:
expr 12 + 7return19 - Subtract:
expr 20 - 4return16 - Multiplication (with exhaust):
expr 6 \* 5return30 - Whole division:
expr 17 / 3return5(the rest is discarded) - Module:
expr 17 % 3return2 - Comparison:
expr 10 \> 5return1(true) - False comparison:
expr 10 < 5return0
Manipulation of chains with expr
In addition to numbers,exprcan perform basic operations on chains. These are useful when you need to extract subchains, get the length or look for patterns within a variable.
- Length of a chain:
expr length "hola mundo"return10(includes space) - Remove subchain:
expr substr "hola mundo" 1 4returnhola(starts in position 1, takes 4 characters) - Find a character:
expr index "hola mundo" "m"return6(position of the first 'm') - Compare chains:
expr "hola" = "hola"return1;expr "hola" = "adios"return0
In these examples it is necessary to use double quotes around the chain so that the shell does not interpret special spaces or characters.
Combine expr with other shell commands and structures
The true power ofexpris revealed when used within scripts or in combination with pipes and variable assignments. For example, to increase a counter inside a loopwhile:
counter = 0while [ $"value: $counter "counter =$(expr $counter + 1) done
Or to validate that an argument passed to a script is a positive number:
if [ $(expr match "$1 '' [0-9] [0-9]*') -eq 0]; then I do "Error: a positive integer is expected" exit 1fi
These patterns show howexprcan be integrated into input control and validation logic.
Limitations and considerations
Althoughexpris portable and simple, has some restrictions that it is good to know:
- He only works with integers. It does not support floating point numbers; for those it is necessary
bcorawk. - The performance is less than the arithmetic expansion of Bash (
$(( ))) because it involves the creation of an external process. - Some operators, such as the asterisk and the ampersand, require escape or quotes to avoid the interpretation of the shell.
- It does not handle complex expressions with nested parentheses intuitively; multiple calls or other tools should be used.
In modern scripts where Bash or Zsh is guaranteed, it is preferable to use syntax$(( expresión ))for arithmetic operations and[[ cadena =~ regex ]]for patterns. However,exprremains valuable when you need maximum portability between POSX shells.
Conclusion
The commandexpris a classic, simple and portable tool to evaluate arithmetic and chain expressions in the Linux shell environment. Although it has been partially replaced by more modern syntax, its presence in virtually all distributions makes it a reliable resource for scripts to be executed in minimum systems or in restricted environments. With the examples shown you can start usingexprToday for calculation, validation and text handling, and decide when it is more appropriate than other alternatives.


