It's not so much that it is not portable, but that there's no [
implementation where it is reliable when passed more than 4 arguments.
Even in bash:
$ ALTNAMES='=' bash -c '[ -n "${ALTNAMES}" -a -n "${COMMON_NAME}" ]'
bash: line 0: [: too many arguments
The related section states:
>4 arguments:
The results are unspecified.
[OB XSI] [Option Start] On XSI-conformant systems, combinations of primaries and operators shall be evaluated using the precedence and associativity rules described
previously. In addition, the string comparison binary primaries '=' and "!=" shall have a higher precedence than any unary primary. [Option End]
-a
and -o
should be banned. The right way is to use the &&
and ||
shell operators instead:
if [ -n "$foo" ] && [ -n "$bar" ]; then
I even find it more legible.
Which is the most portable of sed, awk, perl and sh?
sed
, sh
and awk
are portable being specified by POSIX, perl
is not as not being backed by a standard.
Can someone put these tools in order of portability?
If you stick to compliant code, there should be no order of portability for the three POSIX commands.
Which of these is certain to be found on even the most minimal *nix systems?
The three POSIX ones along with many other utilities are mandatory for an OS to be POSIX. OSes lacking some of them due to minimization, or providing incomplete/non-conforming implementations do exist though.
Actually, most (if not all) free and open source Unix like operating systems probably would not pass the conformance process should they try to, and they never try anyway.
Is any of them 100% sure to be present?
I would be surprised to find a *nix like OS lacking a Bourne syntax based shell, but anything is possible, especially with embedded systems.
My guess is that the order is the following:
Some shell will be present as the default, will that always be at /bin/sh?
/bin/sh
is likely to be a Bourne syntax family shell but is not guaranteed to be POSIX compliant, even en POSIX compliant systems. For example, it is /usr/xpg4/bin/sh
on Solaris 10 and older while /bin/sh
is the legacy original Bourne shell which is not POSIX.
Best Answer
[
is synonym of thetest
command and it is simultaneously a bash builtin and separate command. But[[
is a bash keyword and works in some versions only. So for reasons of portability you are better off using single[]
ortest