I have found some very good answers here on the differences between [
and [[
in 'if' statements. For specific named shells, it seems to be a good idea to use [[
over [
(and it is faster, too).
I'm still unclear on portability, however. If my goal is to write a script for POSIX compatible shells, scripts that I would begin with #!/bin/sh
, how permissible is this syntax. From what I've read, double brackets are not in the POSIX standard, but in the "real world" how much of an issue is this really?
I suppose what I mean is that I want a portable script, but it is good enough for it to work on all viable POSIX shells post, say, 2000-2005. Meaning, it's not part of the standard, true, but realistically any shell from 2005 onward is almost certain to accept the syntax.
Also, a more general question concerning script portability and POSIX: how do other authors handle this? I understand use cases and target audience govern much of the decision, but do you try to stick as close to POSIX as possible to cut down on possible incompatibilities from the Internet at large, or do you avail yourself of all the nice things that most shells support that have yet to make it anywhere near a POSIX standard?
I began by trying to make most of my scripts as Bourne and POSIX compliant as possible. Over time, though, from reading more of the Bash documentation and from seeing other people's scripts on the Net, I have to say that making use of these other features can make for scripts that are much easier to read and in many cases much smaller.