[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: manpage note? weird strings that appear to be equal but create hayw
From: |
Pierre Gaston |
Subject: |
Re: manpage note? weird strings that appear to be equal but create haywire comparisons? |
Date: |
Wed, 26 Aug 2009 16:36:42 +0300 |
On Wed, Aug 26, 2009 at 4:19 PM, Greg Wooledge <wooledg@eeg.ccf.org> wrote:
> Assuming the first part was supposed to be var='"*"' ...
>
yup
>
> The bash command [[ \* = $var ]] returns true if $var contains a glob
> pattern against which a literal asterisk * can be matched. (By the way,
> you don't need the \ there. No glob expansion is done inside [[...]] so
> you could use a plain * on the left hand side.)
>
> If $var contains \* then the match is successful. \* as a glob describes
> a literal asterisk, which is what we're trying to match.
> $ var='\*'; [[ * = $var ]]; echo $?
> 0
>
> If $var contains "*" (double quote, asterisk, double quote), then
> the double quotes are considered part of the glob pattern. A bare
> asterisk won't match that glob, because it doesn't have double quotes
> attached to it.
>
> imadev:~$ var='"*"'; [[ * = $var ]]; echo $?
> 1
>
> It only matches double quote, asterisk, double quote:
>
> $ var='"*"'; [[ \"*\" = $var ]]; echo $?
> 0
Thanks, I agree with that, I'm sorry I should have been more explicit,
what was not clear to me was where this special role of the \ is explained,
Because if you use literals [[ something = \* ]] is the same as [[
something = "*" ]]
I found my explanation in the manual under "Pattern Matching":
"A backslash escapes the following character; the escaping backslash is
discarded when matching."
PS, for Linda, http://mywiki.wooledge.org/BashFAQ/031 has more information
about the differences between [[ and [