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

bash is not capable of comparing of strings and real numbers

4 views
Skip to first unread message

phani krishna jampala

unread,
Dec 7, 2009, 8:13:31 AM12/7/09
to bug-...@gnu.org
bash is not capable of comparing of strings ( imean interms of lessthan or greater than etc) and real numbers ( the float values). So can any one please consider and if possible can release solution in current version 4 it self will be more appreciated.

Thanks
Phani Krishna



pk

unread,
Dec 7, 2009, 5:22:38 PM12/7/09
to
phani krishna jampala wrote:

> bash is not capable of comparing of strings ( imean interms of lessthan or
> greater than etc)

It is, if you use [[ ]]

a="abcd"
b="bcde"
if [[ "$b" > "$a" ]]; then
echo "$b is greater than $a"
fi

> and real numbers ( the float values).

True, but I can't really speak as to whether this is planned or not (I think
it isn't, but of course I might be wrong).

DennisW

unread,
Dec 7, 2009, 8:08:02 PM12/7/09
to

Since printf understands floats (or acts like it does), you can use it
plus a little care and luck to do float comparisons in Bash:

$ a=.3
$ b=0.2
$ [[ $a < $b ]] && echo true || echo false
true # WRONG
$ printf -v a "%0.2f" $a
$ printf -v b "%0.2f" $b
$ [[ $a < $b ]] && echo true || echo false
false # CORRECT
echo "$a $b"
0.30 0.20

$ a=147.1
$ b=23
$ [[ $a < $b ]] && echo true || echo false
true # WRONG
$ printf -v a "%08.2f" $a
$ printf -v b "%08.2f" $b
$ [[ $a < $b ]] && echo true || echo false
false # CORRECT
echo "$a $b"
00147.10 00023.00

Greg Wooledge

unread,
Dec 8, 2009, 8:11:36 AM12/8/09
to bug-...@gnu.org
On Mon, Dec 07, 2009 at 05:08:02PM -0800, DennisW wrote:
> Since printf understands floats (or acts like it does), you can use it
> plus a little care and luck to do float comparisons in Bash:
> [...]

> $ printf -v a "%08.2f" $a
> $ printf -v b "%08.2f" $b
> $ [[ $a < $b ]] && echo true || echo false
> false # CORRECT
> echo "$a $b"
> 00147.10 00023.00

While this is certainly clever, it requires some knowledge of the
possible range of values being compared, so that one can construct a
suitable printf format specifier. Any unexpectedly large or small
input would break it.

Real floating-point comparisons require the services of an external
program (such as bc or awk) since bash has no built-in support for it.
(Or a loadable bash builtin, but very few people use those.)


DennisW

unread,
Dec 8, 2009, 12:46:36 PM12/8/09
to

That's where the "care and luck" come in. With some convoluted
gyrations, code could scale the width and precision factors. It's not
the most desirable way to do it, but as you say, it's "clever".

So how come ksh has float and Bash doesn't?

Stephane Chazelas

unread,
Dec 11, 2009, 10:49:47 AM12/11/09
to
2009-12-07, 22:22(+00), pk:

> phani krishna jampala wrote:
>
>> bash is not capable of comparing of strings ( imean interms of lessthan or
>> greater than etc)
>
> It is, if you use [[ ]]
>
> a="abcd"
> b="bcde"
> if [[ "$b" > "$a" ]]; then
> echo "$b is greater than $a"
> fi
[...]

Or the "[" (aka test) builtin:

[ "%b" \> "$a" ]

--
Stᅵphane

0 new messages