[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Bug in [ -d ... ] ?
From: |
Eric Blake |
Subject: |
Re: Bug in [ -d ... ] ? |
Date: |
Thu, 2 Nov 2017 09:57:00 -0500 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 |
On 11/02/2017 06:03 AM, Michael F Gordon wrote:
> Can anyone explain the following?
Yes. You used incorrect quoting.
>
> tla19> set|grep FOOFOOFOO
> tla19> if [ -d ${FOOFOOFOO:=""} ]; then echo YES;fi
> YES
Because you forgot to quote the variable expansion, you ended up executing:
if [ -d ]; then echo YES; fi
and since the one-argument form of [] succeeds if the single argument is
not the empty string, and -d is not the empty string, you got an answer
of YES.
If you want the two-argument form of [], which treats -d as a unary
operator testing whether the second argument names a directory, then you
should have written:
if [ -d "${FOOFOOFOO:=""}" ]; ...
> tla19> echo $BASH_VERSION
> 4.4.12(1)-release
>
> I don't get YES printed with 4.1.2(1)-release on Scientific Linux 6. I do
> with 4.2.46(1)-release on Scientific Linux 7 and it happens with the latest
> release on both.
Are you sure you tested correctly? test has always behaved differently
for one argument than for two.
At any rate, this is not a bug in bash, but more likely in your script.
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org
signature.asc
Description: OpenPGP digital signature