Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Under some answers, I see comments that recommend avoiding shell specific commands in answers.

How do I know which commands, operators, etc exist in all shells? Is there a list of standards?

  • man builtins gives a list of commands. Are those the only commands that I can use in a portable shell script that works in all shells?
  • Can a built-in be shell specific?
  • Do the standards for Linux differ from that of other Unixes?
  • What about syntax? Can punctuation, operators, etc be different in some shells?
share|improve this question

2 Answers 2

up vote 12 down vote accepted

Greg's Wiki has a post on adapting bash scripts for Dash that points out a lot of 'bashisms' - extra features that are non-standard but are a part of bash. Avoiding those bashisms can help to make your script friendlier to different environments. This particularly answers some of your questions. For instance, yes, there are operators that differ (like ==), but there are a standard Posix set that should work in all environments.

For more thorough reading, you can check out the Posix standard, which all shells should comply to. Particularly, the volume on "Shell & Utilities".

What I find more challenging than the shell differences are the command differences. Many Linux systems have GNU find, but if you're writing a portable script, don't rely on your own man find, because there are lots of systems out there with BSD find, which has a different feature set. If you're writing scripts for busybox, you'll find there are different versions with entirely different ncs. Those are the kinds of things that always get me when I deploy a script to different environments.

For extra reading on good shell scripting practices, there is also a good resource on David Pashley's blog: Writing Robust Bash Shell Scripts

Also, make sure you read Gilles answers and comments on this site. He has lots of tips about making sure you use portable code.

share|improve this answer

Writing a shell script in a specific shell means having that shell installed. The only standard is to have csh and sh installed over all Unix variants. So, if you wanted your script to run on Solaris, *BSD, and GNU then you would have to write it in, say, the Bourne shell.

However, most Unix commands have different syntaxes under different implementations -- look at ps under Solaris, FreeBSD, and GNU -- so depending on what tools you use, your script may not be portable anyway. Where the shell is installed will matter as well. Is it /bin/bash /usr/bin/bash, /usr/local/bin/bash, or somewhere else?

I am unaware of any standards defining a shell. Have a look at rc or http://192.220.96.201/es/es-usenix-winter93.html`">es for weird non-standard shells. However, those seem to still conform to some common ideas.

share|improve this answer
7  
POSIX defines sh. –  Random832 Sep 12 '11 at 12:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.