help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Is it reasonable to let zero numeric value results in no


From: Peng Yu
Subject: Re: [Help-bash] Is it reasonable to let zero numeric value results in non zero exit status?
Date: Mon, 22 Oct 2018 08:16:25 -0500

That is a possible walkaround. But one drawback is x=$((x+1)) is
slower than ((x=x+1))

See the following example.

==> main1.sh <==
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

x=0
for((i=0;i<10000;++i))
do
        x=$((x+1))
        x=$((x+1))
        x=$((x+1))
        x=$((x+1))
        x=$((x+1))
        x=$((x+1))
        x=$((x+1))
        x=$((x+1))
        x=$((x+1))
        x=$((x+1))
done

==> main2.sh <==
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

x=0
for((i=0;i<10000;++i))
do
        ((x=x+1))
        ((x=x+1))
        ((x=x+1))
        ((x=x+1))
        ((x=x+1))
        ((x=x+1))
        ((x=x+1))
        ((x=x+1))
        ((x=x+1))
        ((x=x+1))
done
$ time ./main1.sh

real    0m0.473s
user    0m0.453s
sys     0m0.012s
$ time ./main2.sh

real    0m0.425s
user    0m0.411s
sys     0m0.007s

> I personally prefer using: x=$(( )) rather than ((x=)), I find it more
> natural and it doesn't exit with 1.

-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]