[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: test "$foo" = "$bar" vs test x"$foo" = x"$bar"
From: |
Mike Miller |
Subject: |
Re: test "$foo" = "$bar" vs test x"$foo" = x"$bar" |
Date: |
Wed, 25 Feb 2015 12:07:38 -0500 |
On Wed, Feb 25, 2015 at 11:27:56 -0500, Jordi Gutiérrez Hermoso wrote:
> That is, I thought you needed to either do
>
> test x$foo = x$bar
>
> or
>
> test "$foo" = "$bar"
>
> but having both the leading x and the quotes is redundant.
I'm not entirely sure, but I had thought the reason for the x"$foo"
pattern was in case $foo was something like "-d". Adding a known
character in front ensures that the test command doesn't try to
interpret it as an option and possibly throw an error. And the quotes
are to ensure that it's treated as a single argument whether it is
empty or has spaces or whatever.
We have several variables that we only ever set to "yes" or "no", so
quotes or any other special handling are not needed for those.
Variables that come from the environment or from command-line options
definitely need quotes, and may also need an x-prefix. I don't have an
example that demonstrates whether the x-prefix is needed or not.
--
mike