Subject: Re: simple shell test question
To: Christopher Vance <vance@aurema.com>
From: Rob Windsor <windsor@warthog.com>
List: netbsd-users
Date: 06/06/2002 10:56:27
On Thu, 06 Jun 2002 16:08:36 +1000, verily did Christopher Vance write:

>: On Thu, 06 Jun 2002 05:48:59 -0000, verily did Jim Breton write:

>:> In my travels I have often seen shell syntax like the following:

>:> [ x"$VAR" != x"value" ]

>:> What is the point of doing that instead of the following?

>:> [ "$VAR" != "value" ]

>:> Does the 'x' have some special meaning?  (If so, are there any other
>:> characters with special meanings?)

> Nothing special about 'x', and that's the point - it turns the first
> word into an ordinary word, even when VAR starts with '-'.  Let's say
> VAR is '-r'; in that case it thinks you're doing somethin like
> 	[ -r != "value" ]
> which is syntactically incorrect.  It'll check for a file called '!='
> and then barf on the extra argument.

That's not quite the case, or should we say, that only occurs with grossly 
ancient flavors of /bin/sh that also puke as I mentioned with a "argument 
expected".

You can run the following script on NetBSD or Solaris (which is known to 
have a highly unmodern /bin/sh) and it properly handles a variable with 
"-r" as the content.

    #!/bin/sh

    PATH=/bin:/usr/bin ; export PATH
    LD_LIBRARY_PATH=/usr/lib ; export LD_LIBRARY_PATH

    FOO=-r

    if [ "$FOO" != "-r" ]; then
        echo "no match"
    else
        echo "match"
    fi

Rob++
----------------------------------------
Internet: windsor@warthog.com                             __o
Life: Rob@Carrollton.Texas.USA.Earth                    _`\<,_
                                                       (_)/ (_)
The weather is here, wish you were beautiful.