Dave, I ran across your sites and Wicked Cool Shell Scripts book while trying to convince myself that `sleep 0` is a valid and relatively portable command to use freely in my unix shell scripts.
I looked at a few of the Bourne shell scripts and they seem to use constructs like $( … ) and $(( … )) which are not standard Bourne shell (to my understanding). So I actually have three questions, rather than just one:
- In what flavor of /bin/sh are those scripts written?
- Can you suggest any references regarding writing portable (Bourne) shell scripts?
- I would love to find an equivalent reference for what unix utilities *and options* to use for increasing script portability. Any suggestions?
Well, this is a bonus shell scripting question, I’d say! I think that the first thing to realize is that the Posix standard for the “sh” shell includes a number of structures and constructs that are still not fully supported in some commercial Unixes (in particular Solaris 9’s default /bin/sh isn’t Posix compliant).
According to Posix’s sh man page, the $( ) notation is valid and legit. Of course, the man page doesn’t explicitly talk about the notation, but an example about half-way down the page shows it in use, in the line: set $(getconf PATH).
That’s not entirely true. The man page might not be complete, but this section on command substitution documents that the sequence $(x) is functionally identical to `x`.
On the same page, the section on arithmetic expansion explains how the $(( )) notation allows for easy access to rudimentary integer arithmetic functionality within a shell script. This is, of course, a good thing because it finally means that the long-used expr command can be retired and many scripts sped up quite a bit.
So in terms of your question about what flavor of /bin/sh the scripts are written in, I’d say “the Posix flavor”. By this point at the end of 2004 there’s not much excuse why the default shell on your Linux or Unix box wouldn’t be completely Posix-compliant.
In terms of references on writing portable scripts, I’d say that your best bet is to familiarize yourself with the Posix standard and make sure that you stick with the commands, parameters and shell functionality detailed therein. There are also some Web sites worth a possible visit in this regard too: Writing Portable Bourne Shell Scripts, Shell Script Portability Guidelines, and What to Watch Out for When Writing Portable Shell Scripts. There’s also a book on the subject, though I haven’t read it so can’t say whether it’s good or not: Portable Shell Programming. It is out of print, so half.com or another used book venue might be a lot cheaper than Amazon’s marketplace.
I hope that helps clarify these issues with the evolution of the Bourne Shell into the world of Posix!
I checked the bash FAQ and it appears that the $() construct has been available in gnu bash since version 2.2 and the $(()) arithmetic function has been around since 2.0
I am running Ubuntu 7.04 right now and it uses 3.2.13.
A quick check of some boxes that I have access too shows:
Solaris 8 — bash 2.03
Solaris 9 — bash 2.05
Solaris 10 — bash 3.0.16
What’s acceptable syntax in Unix and Linux sh scripts?
Dave, I ran across your sites and Wicked Cool Shell Scripts book while trying to convince myself that `sleep 0` is a valid and relatively portable command to use freely in my unix shell scripts. I looked at a few of the Bourne shell scripts and they se…