Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

When do I need `expr ......` and when not? $(($aaa - $bbb)) simplyfiable?

0 views
Skip to first unread message

Wolfgang Meister

unread,
Jun 15, 2008, 2:48:31 AM6/15/08
to
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

Stephane CHAZELAS

unread,
Jun 15, 2008, 4:22:54 AM6/15/08
to
2008-06-15, 06:48(+00), Wolfgang Meister:

> 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?
[...]

You need expr in that case when writing scripts meant to be
portable to systems that don't have a POSIX shell. Mostly more
than 15 year old systems I'd say, or embedded systems that come
with a torn down version of a shell.

The Bourne shell and early versions of the Almquist shell didn't
have $((...)). In modern Unices, sh is no longer a Bourne shell
but an interpreter or the other of a standard sh language
specified by POSIX. And that language has $((...)).

The ":" operator of "expr" can still be uselful nowadays.

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.

--
Stéphane

Wayne

unread,
Jun 15, 2008, 2:38:35 PM6/15/08
to
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

Stephane CHAZELAS

unread,
Jun 15, 2008, 2:48:58 PM6/15/08
to
2008-06-15, 14:38(-04), Wayne:
[...]

>> 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.

Yes, and sed operates on lines by default, while expr operates
on a string that may contain several lines.

expr can also be used for string comparison.

expr "x$string1" "<" "x$string2"

which the POSIX "[" can't do.

>> 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} ))

[...]

The problem with that is that it strips only one 0, and for the
number "0" itself, it converts it to an empty string.

${aaa#"${aaa%%[!0]*}"}

removes every leading 0, but doesn't solve the other problem and
becomes hard to read. You'd probably want to use a function to
do the 0 stripping.

strip_zeros() {
eval "$1=\${$1#\"\${$1%%[!0]*}\"}; $1=\${$1:-0}"
}
strip_zeros aaa

for instance.

--
Stéphane

Jignesh

unread,
Jul 12, 2008, 2:50:57 PM7/12/08
to
On Jun 15, 11:38 pm, Wayne <nos...@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

0 new messages