From: Wolfgang Meister on
As I learned it is not always necessary to use `expr ....` (with backticks) to
calculate mathematical expressions. When I do for example the following

firstsec=345
secondsec=723
diffsec=$((secondsec - firstsec))
echo diff=$diffsec

then it works although I did not used "expr" for calculate the difference.

So when do I have to use expr and when not?


By the way: When can I write just

diffsec=$(secondsec - firstsec)

and when

diffsec=$((secondsec - firstsec))

and when

diffsec=$(($secondsec - $firstsec))

Wolfgang
From: Wayne on
Stephane CHAZELAS wrote:
>
> The ":" operator of "expr" can still be uselful nowadays.

Indeed, expr can be used with if to test if some value is
a number, something that test alone can't do. And while
sed can be used to return a testable value when matching
some string against a regular expression, it is much
easier with expr.
>
> You may still want to use expr when dealing with 0 padded
> decimal numbers. For $((...)), 010 is 8 (octal 010) while for
> expr, it's 10.

Of course you can also easily strip off leading zeros from
numbers in shell variables in POSIX shells, using ${NUM##0}:
$(( ${aaa##0} - ${bbb##0} ))

Stephane, I still say you should write a book. Your
knowledge and experience of shells is invaluable.

-Wayne
From: Jignesh on
On Jun 15, 11:38 pm, Wayne <nos...(a)all4me.invalid> wrote:

> Stephane CHAZELAS wrote:
>
> > The ":" operator of "expr" can still be uselful nowadays.
>
> Indeed, expr can be used with if to test if some value is

> Stephane, I still say you should write a book.  Your
> knowledge and experience of shells is invaluable.

>
> -Wayne

Yes, Stephane, You should :). I have read your tips on 'Advanced Bash
scripting Guide' at TLDP.

Jignesh