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

Re: simple shell question



On 2015-02-18 21:40, Paul Gevers wrote:
> I am wondering about the following, what is the practical difference in
> a shell script between
> [ "$foo" ] and [ -n "$foo" ]

POSIX [1] mandates it:
  | -n  string
  |     True if the length of string is non-zero; otherwise, false.

  | string
  |     True if the string string is not the null string; otherwise,
  |     false.

The bash builtin test / [ ] complies with that:
  | string
  | -n string
  |        True if the length of string is non-zero.

As does [ ] from test(1):
  | -n STRING
  |        the length of STRING is nonzero
  |
  | STRING equivalent to -n STRING

> or
> [ ! "$foo" ] and [ -z "$foo" ]

These follow from above.

Regards,
Christian

[1] http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html


Reply to: