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

Re: /etc/shells management

Guido Guenther wrote:
> Branden Robinson wrote:
> > if [ -n "$var" ]; then

I always prefer this myself too.  It is portable.

> Just of out curiosity, is this in any way different from the shorter:
> if [ "$var" ]; then 

For Debian, no.  But for those of us trying to program portably across
systems, yes.  This has the possibility of generating a syntax error
if $var expanded to be something that started with a '-' and therefore
looked like an option to the test operator.  It is not portable to use
that construct.  This is heritage from the Bourne shell which also
exists in the Korn shell which became the basis for the POSIX shell
much later.

For example on HP-UX:

  /bin/sh -c 'var=-f ; [ "$var" ] && echo yes'
  /bin/sh: test: Specify a parameter with this command.


  /bin/sh -c 'var=-f ; [ -n "$var" ] && echo yes'


Attachment: pgpKFfnS8qp3M.pgp
Description: PGP signature

Reply to: