On Mon, Jun 25, 2007 at 02:07:56PM -0600, Bob Proulx wrote:
[...]
> following.  These are the same.
> 
>   [ foo -a bar ]
>   [ -n foo -a -n bar ]
> 
> This is defined by standard by the number of arguments.
> 
>   http://www.opengroup.org/onlinepubs/009695399/utilities/test.html
[...]

And it should be noted (and it's noted as well in the page
you're refering to) that while the above is true for strings
such as "foo" and "bar", it is not true in the general case.

[ -n "$foo" -a -n "$bar" ]

is not the expression to test whether both "$foo" and "$bar" are
non-empty, as it would fail for some specific values of $foo or
$bar (try it when $foo contains "=" for instance).

That's why it's useful to remember those rules:

- don't call "[" with more than 3 arguments (beside the "[" and
"]" ones). That is use [ -f "$f" ] && ! [ -h "$f" ] instead of [
-f "$f" -a ! -f "$f" ] for instance.

That at least would keep you out of troubles with POSIX
conformant shells. Then for shells that are still not POSIX
conformant (such as the Bourne shell, or the BSD shs (ash) or
some old kshs).

- prefer [ -n "$string" ] over [ "$string" ]
- [ foo = "$var" ] over [ "$var" = foo ]
- [ "x$a" = "x$b" ] over [ "$a" = "$b" ]
- [ whatever != whatever ] over [ ! whatever = whatever ]

Note that the [[ ... ]] and (( ... )) contructs don't have such
issues, but those are not standard contructs.

-- 
Stéphane


_______________________________________________
Bug-bash mailing list
Bug-bash@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-bash

Reply via email to