Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn and Microsoft Windows.

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 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:

  1. In what flavor of /bin/sh are those scripts written?
  2. Can you suggest any references regarding writing portable (Bourne) shell scripts?
  3. I would love to find an equivalent reference for what unix utilities *and options* to use for increasing script portability. Any suggestions?

Dave's Answer:

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!



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

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

Posted by: Jim at May 7, 2007 11:39 AM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]