bug-bash
[Top][All Lists]
Advanced

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

Re: Bash is broken


From: Stephane Chazelas
Subject: Re: Bash is broken
Date: Sun, 13 May 2007 15:55:40 +0100
User-agent: Mutt/1.5.6i

On Sun, May 13, 2007 at 12:31:48AM -0700, Overdorf, Sam wrote:
> The following does not work on my computers:
[...]
> If [ "1" > "2" ]
[...]
> Bash thinks this is I/O redirection and creates a file by the name of
> "2" in my directory.
> 
> This should be a string compare.
[...]

Hi. No, this shouldn't.

"[" is a command just as every other one. And on a command line,
the redirection operators can be put anywhere.

echo 1 > 2 ]

is the same as

> 2 echo 1 ]

or

echo 1 ] > 2

If you want, you can use the [[ ... ]] syntax instead. [[ is not
a command, [[ ... ]] as a whole is a command by itself if you
want and what's inside is treated as a text expression, not as
arguments and redirections to a command.

[[ 1 > 2 ]]

If you want to use "[" (aka test), you need to make sure ">" is
passed as an argument to "[".

[ 1 '>' 2 ]

or [ 1 \> 2 ]

[[ ... ]] is not a standard shell feature, but the > test
operator is not either.

Portably (as in Unix shell scripts), you'd use expr:

if expr "x$1" \> "x$2" > /dev/null

or awk.

if awk 'BEGIN{exit !(ARGV[1]"" > ARGV[2]"")}' "$1" "$2"

Finally, it should be noted that string comparison behavior
depends on the current locale. So if you don't want your script
behavior to depend on who is running it, you may want to fix the
locale:

LC_ALL=C expr ...

-- 
Stéphane




reply via email to

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