bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is this a bug in [[ -f ]]?


From: Eric Blake
Subject: Re: Is this a bug in [[ -f ]]?
Date: Fri, 19 Aug 2011 09:04:43 -0600
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110621 Fedora/3.1.11-1.fc14 Lightning/1.0b3pre Mnenhy/0.8.3 Thunderbird/3.1.11

On 08/19/2011 08:45 AM, Suvayu Ali wrote:
I am trying to test if a file exists and then source it. My problem is
the test succeeds even if the variable is empty! If I pass no argument
at all, it still succeeds. To give you an example:

$ unset bla
$ [ -f $bla ]&&  echo yes
yes
$ [ -f  ]&&  echo yes
yes

Both expected behaviors, and evidence of your lack of quoting.

Remember, the behavior of [] depends on how many arguments are present.

[ -f "$bla" ] (note the "") - guarantees that there are exactly two arguments, so it proceeds with the two-argument test where -f is the operator and "$bla" is the file name.

[ -f ] (which is the same as [ -f $bla ] if $bla is empty, note the lack of "") - there is exactly one argument, so it proceeds with the one-argument test of whether the argument (the literal string -f) is empty (it is not).

Furthermore, [ -f $bla ] is different than [[ -f $bla ]]. [ is a POSIX utility, and mandated to do all argument word expansion before [ ever gets a chance to see what arguments it was given - if $bla is empty or has spaces, you changed the number of arguments that are given to [. [[ is a bash (and ksh) extension that is part of the shell syntax (similar to how () for subshells is part of the syntax), thus it knows how many words, _pre-expansion_, were present, and the fact that $bla was unquoted is not a problem, [[ -f $bla ]] is a safe way to check if $bla is a file even if $bla is empty or contains spaces.

--
Eric Blake   eblake@redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org



reply via email to

[Prev in Thread] Current Thread [Next in Thread]