bug-bash
[Top][All Lists]
Advanced

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

Re: test -f with no args is true


From: Bob Proulx
Subject: Re: test -f with no args is true
Date: Sat, 15 Apr 2006 16:05:43 -0600
User-agent: Mutt/1.5.9i

Charlie Bennett wrote:
> Hmmmm.  I sat on that blasted committee.

:-)

> I'll file a corrigendum or whatever it's called.  This should at
> least be called out in the rationale.
> 
> No biggie.  I'll just make sure I quote all of my args.

Yes, because test and [ were external commands when the API solidified
they must continue to behave the same even though they are built-in
now or they would break legacy scripts.  So quoting is the way to go.

  if [ -f $filename ]; then  # possible arg problems here

If [ is an external command and $filename is not set then it would not
appear to the external test command.  Of course it is internal now but
must still behave the same.  For robustness that should be:

  if [ -f "$filename" ]; then

As an alternative, you could use the [[ ... ]] syntax.  That is the
newer syntax, covered in POSIX too so should be portable to POSIX
systems, but is always a built-in to the shell.  This avoids need to
quote the variables.  This is okay even if $filename is not set.

  if [[ -f $filename ]]; then

Because it is a built-in the $filename won't be expanded to a null
argument.  However it is not portable to systems with a /bin/sh that
is a Bourne shell since the Bourne shell does not support that syntax.
I still use the [ ... ] form with quoting mostly for that reason.  But
[[ ... ]] does have its attractions and advocates.  I am even starting
to use it casually now.  :-)

Bob




reply via email to

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