expr does not seem to like parenthesis (used in mathematics to explicit operator priority):
expr 3 * (2 + 1) bash: syntax error near unexpected token `('
How to express operator priority in bash?
Another way to use
As @Stéphane Chazelas pointed out, in
For portability, use
You can use the arithmetic expansion instead.
In my personal opinion, this looks a bit nicer than using
There's no reason to be using
POSIX defines the
However, as the quoting makes it awkward and not very legible (not to the same extent as
which is a lot more legible and should be used instead.
On the non-Bourne front, there are a few shells with built-in arithmetic operator:
Use parenthesis with quotes:
The quotes prevent bash from interpreting the parenthesis as bash syntax.
Unless you're working on an antique unix system from the 1970s or 1980s, there is very little reason to use
Bash offers an additional convenience syntax when you want to perform assignments or to test whether an expression is 0 but don't care about the result. This construct also exists in ksh and zsh but not in plain sh.
In addition to integer arithmetic,
Unless you're maintaing scripts that run on 20-year-old systems, you don't need to know that