just another one of my rumblings

Lombard, David N dnlombar at ichips.intel.com
Mon Apr 28 13:38:20 UTC 2008


On Fri, Apr 25, 2008 at 11:26:11PM -0400, Mike Frysinger wrote:
> On Friday 25 April 2008, Paul Fox wrote:
> >  > [ -n "$var" ] is clear.  [ "$var" ] is not.
> >
> > it's exactly analogous to "if (foo != 0)" vs. "if (foo)".
> >
> > it's all subjective.  i'd probably have to look up "-n".  :-)
> 
> i doubt it's the same.  most people who understand shell coding know what 
> the -n test means.  i would doubt that most people know what the default 
> behavior is if no operator is specified at all.  unlike C coders who know 
> that "true" means "non-0".

Completely agree! "-n" clearly means a non-empty string. Without the "-n" or
"-z", are we interested in the variable's string or numerical vaue?

BusyBox v1.8.2 (2008-04-24 13:04:12 PDT) built-in shell (ash)
Enter 'help' for a list of built-in commands.

Ash:
    # i=;[ "$i" ] && echo OK
    # i=i;[ "$i" ] && echo OK
    OK
    # i=0;[ "$i" ] && echo OK
    OK
    # i=1;[ "$i" ] && echo OK
    OK
    #

OTOH, Perl is consistent in this manner:

    $ perl -e '$i="";if( $i ) {print "OK\n"}'
    $ perl -e '$i="i";if( $i ) {print "OK\n"}'
    OK
    $ perl -e '$i=0;if( $i ) {print "OK\n"}'
    $ perl -e '$i=1;if( $i ) {print "OK\n"}'
    OK
    $ perl -e '$i="0";if( $i ) {print "OK\n"}'
    $ perl -e '$i="1";if( $i ) {print "OK\n"}'
    OK
    $

-- 
David N. Lombard, Intel, Irvine, CA
I do not speak for Intel Corporation; all comments are strictly my own.



More information about the busybox mailing list