[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Problem with if [ -d in bash 4.3.30
From: |
Eric Blake |
Subject: |
Re: Problem with if [ -d in bash 4.3.30 |
Date: |
Tue, 09 Dec 2014 07:14:16 -0700 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 |
On 12/09/2014 06:24 AM, Olivier TROCHERIE wrote:
> With bash 3.2, when I run the following commands:
>
> if [ -d ${VAR:=""} ]
Insufficient quoting. You are executing:
[ -d ]
which is a test whether the single argument "-d" is not empty (which is
true), ergo the exit 0 status.
You MEANT to use:
[ -d "${VAR:=""}" ]
which would evaluate to:
[ -d "" ]
and correctly return status 1, because the empty string "" is not a
directory.
> I get the no answer ("" is not a directory: correct behaviour).
No, with bash 3.2, you are tripping up on a parser bug. Bash 3.2
incorrectly handled double quotes inside unquoted parameter expansion.
>
> If I run the same commands with bash 4.3.30, I get the yes answer, which
> seems to be a bad behavior.
No, you get the correct behavior. Newer bash fixed the parser bug to
comply with POSIX.
For comparison, try:
$ echo . ${VAR:=""} .
. .
$ echo . "${VAR:=""}" .
. .
in modern bash, you can see the difference when the parser is behaving
correctly.
> Would it be possible to fix this issue?
Yes, since the issue is in your script, you can fix your script.
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature