Ask Dave Taylor
  • Facebook
  • Instagram
  • Linkedin
  • Pinterest
  • Twitter
  • YouTube
  • Home
  • YouTube Videos
  • Top Categories
  • Subscribe via Email
  • Ask A Question
  • Meet Dave
  • Home
  • Linux Help
  • What’s acceptable syntax in Unix and Linux sh scripts?

What’s acceptable syntax in Unix and Linux sh scripts?

December 6, 2004 / Dave Taylor / Linux Help, Linux Shell Script Programming / 2 Comments

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?

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!

Let’s Stay In Touch!

Never miss a single article, review or tutorial here on AskDaveTaylor, sign up for my fun weekly newsletter!
Name: 
Your email address:*
Please enter all required fields
Correct invalid entries
No spam, ever. Promise. Powered by FeedBlitz
Please choose a color:
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

2 comments on “What’s acceptable syntax in Unix and Linux sh scripts?”

  1. Jim says:
    May 7, 2007 at 11:39 am

    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

    Reply
  2. Lockergnome's Linux Fanatics says:
    December 6, 2004 at 7:15 pm

    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…

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

Recent Posts

  • How Do I Add and Then Rename a Printer in Ubuntu Linux?
  • Android Complaining Starbucks has “No Internet Access”. Now What?
  • How Do I Update My Autofill Information in Brave?
  • Is It Possible to Save Voicemail Messages on an iPhone?
  • Automatically Sync Time and Show World Clock on my PC?

On Our YouTube Channel

ZAGG Glass Elite Visionguard iPhone 13 Pro Screen Protector -- INSTALLATION & REVIEW

Wuben G2 Keychain Lamp & G3 Unboxing Knife -- DEMO & REVIEW

Categories

  • AdSense, AdWords, and PPC Help (106)
  • Amazon, eBay, and Online Shopping Help, (161)
  • Android Help (196)
  • Apple iPad Help (144)
  • Apple Watch Help (52)
  • Articles, Tutorials, and Reviews (344)
  • Business Advice (199)
  • Chrome OS Help (23)
  • Computer & Internet Basics (758)
  • d) None of the Above (164)
  • Facebook Help (382)
  • Google, Chrome & Gmail Help (177)
  • HTML & Web Page Design (244)
  • Instagram Help (47)
  • iPhone & iOS Help (603)
  • iPod & MP3 Player Help (173)
  • Kindle & Nook Help (92)
  • LinkedIn Help (85)
  • Linux Help (163)
  • Linux Shell Script Programming (86)
  • Mac & MacOS Help (888)
  • Most Popular (16)
  • Outlook & Office 365 Help (24)
  • PayPal Help (69)
  • Pinterest Help (53)
  • Reddit Help (17)
  • SEO & Marketing (81)
  • Spam, Scams & Security (92)
  • Trade Show News & Updates (23)
  • Twitter Help (217)
  • Video Game Tips (66)
  • Web Site Traffic Tips (62)
  • Windows PC Help (912)
  • Wordpress Help (204)
  • Writing and Publishing (72)
  • YouTube Help (45)
  • YouTube Video Reviews (159)
  • Zoom, Skype & Video Chat Help (56)

Archives

Social Connections:

Ask Dave Taylor


Follow Me on Pinterest
Follow me on Twitter
Follow me on LinkedIn
Follow me on Instagram


AskDaveTaylor on Facebook



microsoft insider mvp


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 site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to our terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. Our lawyer says "Thanks for your cooperation."
© 2022 by Dave Taylor. "Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.
Privacy Policy - Terms and Conditions - Accessibility Policy