delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2005/08/19/15:28:13

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
From: ericblake AT comcast DOT net (Eric Blake)
To: cygwin AT cygwin DOT com
Subject: Re: bash test -n && test -z return tru
Date: Fri, 19 Aug 2005 19:27:55 +0000
Message-Id: <081920051927.27330.430632BB00050BE200006AC222007507440A050E040D0C079D0A@comcast.net>
X-Authenticated-Sender: ZXJpY2JsYWtlQGNvbWNhc3QubmV0

> On Fri, Aug 19, 2005 at 03:01:19PM -0400, Poor Yorick wrote:
> >from reading the bash man pages, I would have thought the -n and -z were 
> >mutually exclusive.  Therefore I don't understand this result:
> >
> >~>$ [ -n $(which nonexisingfilename 2>/dev/null) ] && [ -z $(which 
> >nonexistingfilename 2&>/dev/null) ]  && echo hello
> >hello
> >~>$
> >
> >can anyone help explain this?
> 
> This defaults to
> 
>   [ -n ] && [ -z ] && echo hello
> 
> I would have expected a syntax error in that case.

Nope.  POSIX requires that when there is one argument, it evaluates
to true if it is not empty.  Neither -n nor -z is empty, so they are both
true.  Remember, if there are no quotes around a word, it is subject to
field splitting, such that an empty string disappears from the argument
list altogether, and you are only giving one argument to [.

> 
> Regardless of that, however, if you want to actually make this work you
> need to do something like:
> 
>   [ -n "$(which nonexisingfilename 2>/dev/null)" ]

Yep - adding the quotes now forces the second word to remain,
even if it is empty, and with two arguments to [, -n and -z now
become mutually exclusive unary operators instead of strings.


--
Eric Blake
volunteer cygwin bash maintainer



--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019