Skip to content
Vidar Holen edited this page Oct 4, 2015 · 2 revisions

Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.

And likewise, prefer [ p ] || [ q ] over [ p -o q ].

Problematic code:

[ "$1" = "test" -a -z "$2" ]

Correct code:

[ "$1" = "test" ] && [ -z "$2" ]

Rationale:

-a and -o to mean AND and OR in a [ .. ] test expression is not well defined. They are obsolescent extensions in POSIX and their behavior is almost always undefined.

Using multiple [ .. ] expressions with shell AND/OR operators && and || is well defined and therefore preferred (but note that they have equal precedence, while -a/-o is unspecified but often implemented as -a having higher precedence).

Exceptions:

None.

Clone this wiki locally