On Wed, 28 Feb 2001, Alexander Skwar wrote:

> So sprach David Walluck am Wed, Feb 28, 2001 at 09:22:46AM -0500:
> > > [ x"$var" = "x" ]; then echo var is empty; fi
> > > 
> > 
> > [ x"$var" = x ] is better because of the problem that was just described
> 
> Uh?  Why is [ x"$var" = x ] better than [ x"$var" = "x" ] ?  I fail to see
> the difference.. Hmm, or does x"$var" expand to x"" if $var is empty?
> 
> Alexander Skwar

This is just the way I wrote it I meant the x method vs. -n method, not
your x method vs. the way I wrote it. But x"$var" expands to x"" which is
just x because the quotes aren't anything, they are not escaped.

But I think if on the left hand side you don't quote x, then don't quote
it on the right hand side either, so:

x"$var" = x"" is what I use.

which should be the same as

x"$var" = x

so seems "x$var" = "x"

but with x"$var" = "x" the quotes don't match up from the left hand side
to the right hand side.

>From http://sources.redhat.com/autobook/autobook/autobook_216.html#SEC216

"Although technically equivalent, test is preferable to [ in shell code
written in conjunction with Autoconf, since `[' is also used for M4
quoting in Autoconf. Your code will be much easier to read (and write) if
you abstain from the use of `['. 

Except in the most degenerate shells, test is a shell builtin to save the
overhead of starting another process, and is no slower than `['. It does
mean, however, that there is a huge range of features which are not
implemented often enough that you can use them freely within a truly
portable script. The less obvious ones to avoid are `-a' and `-o' -- the
logical `and' and `or' operations."
 
Later on he has this example:

"$ for foo in "" "!" "bar" "baz quux"; do
>   test x"$foo" = x"bar" && echo 1 || echo 0
> done
0
0
1
0

Here, you can see that using the `x' prefix for the first operand saves
test from interpreting the `!' argument as a real option, or from choking
on an empty string -- something you must always be aware of, or else the
following behaviour will ensue: 

$ foo=!
$ test "$foo" = "bar" && echo 1 || echo 0
test: argument expected
0
$ foo=""
$ test "$foo" = "bar" && echo 1 || echo 0
test: argument expected
0

Also, the double quote marks help test cope with strings that contain
whitespace. Without the double quotes, you will see this errors..."

So you can see that he says using x and double quotes are good, but there
is nothing that discusses what we were, that is, does the placement of the
x inside or outside the double quotes matter? I can imagine a degenerate
shell which interprets double quotes literally, though this shell may not
actually exist, I still try to make my double quotes match up just in
case. Therefore, I suggest x"$var" = x"" or "x$var" = "x". Though as you
noted, if you look around, you see the x is almost never quoted on the
right hand side, but almost always is on the left, so this must not be a
real problem. But you see, it is problematic enough to confuse us :)

-- 
Sincerely,

David Walluck
<[EMAIL PROTECTED]>


Reply via email to