Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 13 Jan 1999 20:36:20 +1000
From:      Greg Black <gjb@acm.org>
To:        cjclark@home.com
Cc:        freebsd-questions@FreeBSD.ORG (FreeBSD Questions)
Subject:   Re: Documentation of '[]' 
Message-ID:  <19990113103621.29307.qmail@alice.gba.oz.au>
In-Reply-To: <199901130625.BAA04054@cc942873-a.ewndsr1.nj.home.com>  of Wed, 13 Jan 1999 01:25:05 EST
References:  <199901130625.BAA04054@cc942873-a.ewndsr1.nj.home.com> 

next in thread | previous in thread | raw e-mail | index | archive | help
> I am by no means a sh expert and just am interested in playing it safe
> in when I can use '[].' Is this documented in the manpages somewhere?

If you are writing scripts that might one day be used on very
old and long obsolete Unix variants (meaning more than ten years
old), you might prefer to use "test" rather than "[" -- but if
you only use relatively modern systems, then you can always use
"[".  In the old days, the man page for sh(1) and other Bourne
style shells included a warning about this.  I don't know (or
care) if they still do.  I have only used "test" in teaching
situations since the early 1980s -- "[" has been fine in the
real world.

It's to some degree a matter of taste.  Take the following
snippet of code:

    if test -e some_file
    then
        some_program
    else
        echo "file missing!"
        exit 1
    fi

Now compare with this:

    [ -e some_file ] && some_program || { echo oops ; exit 1 }

Some people think the first version is a model of clarity.
Others, including me, consider the second to be better because
you can take in the whole concept with a quick scan of a single
line.  YMMV.

-- 
Greg Black <gjb@acm.org>


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?19990113103621.29307.qmail>