Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Google   


Shell Portability and Born shell

News

See also

Recommended  Links

Basic navigation Command Completion 

Shell History

Bash as an Enterprize Shell Shell Programming   Paul Jarc's software lintsh Humor Etc

Note: You might be better off forgetting all this silly noise about compatibility and using bash. Bash is now standard de-facto and is available of almost all enterprise flavors of Unix and commercial linuxes.

It is still not available on HP-UX 11 but that's the problem of HP. Many people including me do not consider HP-UX to a  legitimate Unix ;-). Anyway even they provide bash with binaries available for all HP-UX version you can find.  It is easier to install it on all systems that struggle with compatibility questions.  Solaris, AIX and linuxes have bash installed out of the box and now bash in the natural least command denominator of shells.  But bash 3.xx is pretty close to ksh93 and if you tested you scripts with it, in most cases the script should work OK.  It's a pity that most Linux distributions ignore ksh93 which is another candidate but we can do nothing with mass stupidity in this area :-)

Originally sh was Born shell that has a separate implementation. Those days sh is often implemented as a special compilation of existing ksh version (often ksh93) and in most cases it does not make sense to use Born shell. You will be better off using bash

The second problem in writing portable script is that there are also many subtle differences in utilities provided and their location for various flavors of Unix. There are several ways to deal with this problem. One is to provide symbolic link to "most reasonable location", the second is to provide wrappers and the third that probably the most popular is to encode path and the name of the utility in shell variables.

Both the Bourne shell, and the Korn shell, can use the semicolon and the carriage return interchangeably in their syntax of the if, for, and while built-in commands.  When using the brackets ([ ]) within if commands, you must separate both inside ends of the brackets from the inside characters with a space.

Shell Script Porting Guidelines

Here is a semi-useless table from the FAQ that compares shells.  It is old and from purely academic point of view is incomplete as it does not include ksh93 which is in many respect the pinnacle of traditional Unix shells. But again you need to forget about academic discussion here: it is simpler to switch to bash then struggle with all this stupid complexity. Bash is not perfect but it works well enough to suit your needs. Sometime the best way to conquer obstacle is to go around it :-)

                                    sh   csh  ksh  bash tcsh zsh  rc   es
Job control                          N    Y    Y    Y    Y    Y    N    N
Aliases                              N    Y    Y    Y    Y    Y    N    N
Shell functions                      Y(1) N    Y    Y    N    Y    Y    Y
"Sensible" Input/Output redirection  Y    N    Y    Y    N    Y    Y    Y
Directory stack                      N    Y    Y    Y    Y    Y    F    F
Command history                      N    Y    Y    Y    Y    Y    L    L
Command line editing                 N    N    Y    Y    Y    Y    L    L
Vi Command line editing              N    N    Y    Y    Y(3) Y    L    L
Emacs Command line editing           N    N    Y    Y    Y    Y    L    L
Rebindable Command line editing      N    N    N    Y    Y    Y    L    L
User name look up                    N    Y    Y    Y    Y    Y    L    L
Login/Logout watching                N    N    N    N    Y    Y    F    F
Filename completion                  N    Y(1) Y    Y    Y    Y    L    L
Username completion                  N    Y(2) Y    Y    Y    Y    L    L
Hostname completion                  N    Y(2) Y    Y    Y    Y    L    L
History completion                   N    N    N    Y    Y    Y    L    L
Fully programmable Completion        N    N    N    N    Y    Y    N    N
Mh Mailbox completion                N    N    N    N(4) N(6) N(6) N    N
Co Processes                         N    N    Y    N    N    Y    N    N
Builtin artithmetic evaluation       N    Y    Y    Y    Y    Y    N    N
Can follow symbolic links invisibly  N    N    Y    Y    Y    Y    N    N
Periodic command execution           N    N    N    N    Y    Y    N    N
Custom Prompt (easily)               N    N    Y    Y    Y    Y    Y    Y
Sun Keyboard Hack                    N    N    N    N    N    Y    N    N
Spelling Correction                  N    N    N    N    Y    Y    N    N
Process Substitution                 N    N    N    Y(2) N    Y    Y    Y
Underlying Syntax                    sh   csh  sh   sh   csh  sh   rc   rc
Freely Available                     N    N    N(5) Y    Y    Y    Y    Y
Checks Mailbox                       N    Y    Y    Y    Y    Y    F    F
Tty Sanity Checking                  N    N    N    N    Y    Y    N    N
Can cope with large argument lists   Y    N    Y    Y    Y    Y    Y    Y
Has non-interactive startup file     N    Y    Y(7) Y(7) Y    Y    N    N
Has non-login startup file           N    Y    Y(7) Y    Y    Y    N    N
Can avoid user startup files         N    Y    N    Y    N    Y    Y    Y
Can specify startup file             N    N    Y    Y    N    N    N    N
Low level command redefinition       N    N    N    N    N    N    N    Y
Has anonymous functions              N    N    N    N    N    N    Y    Y
List Variables                       N    Y    Y    N    Y    Y    Y    Y
Full signal trap handling            Y    N    Y    Y    N    Y    Y    Y
File no clobber ability              N    Y    Y    Y    Y    Y    N    F
Local variables                      N    N    Y    Y    N    Y    Y    Y
Lexically scoped variables           N    N    N    N    N    N    N    Y
Exceptions                           N    N    N    N    N    N    N    Y

Key to the table above.

   Y      Feature can be done using this shell.
          
   N      Feature is not present in the shell.
          
   F      Feature can only be done by using the shells function
          mechanism.
          
   L      The readline library must be linked into the shell to enable
          this Feature.
Notes:
  • This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Some amount of grammar and spelling errors should be expected.
  • The site contain some broken links as it develops like a living tree... Please try to use Google, Open directory, etc. to find a replacement link (see HOWTO search the WEB for details). We would appreciate if you can mail us a correct link.
Google Search
Open directory

Research Index

Old News

[Oct 11, 2006] IBM Redbooks Solaris to Linux Migration A Guide for System Administrators

IBM wants you to migrate from Solaris to Linux but  sometimes it is better to migrate in opposite direction and the guide is useful for this purpose too :-). Still moving from ksh to bash makes sense.
16.3 Moving from ksh to bash

Migrating your sh and ksh scripts to bash is typically a straightforward process. However, there is a minor difference to be aware of. If you are using the read statement in your script, as shown in Example 16-1, the second line echo results in a blank line. This is a known issue in bash and applies to both single and multiple variable reads.

Example 16-1 Sample read statement

> echo "A B C" | read x y z
> echo $x $y $z
> _

Here are some suggested workarounds for the read command:

> read x y z < <(echo "A B C")
> echo $x $y $z
A B C
> _

Using awk

> x=`echo "A B C" | awk `{ print $2 }'`
> echo $x
B
> _

For more information about using bash, refer to the following Web site:

http://www.gnu.org/software/bash/

Recommended Links


In case of broken links please try to use Google search. If you find the page please notify us about new location
Google     

Paul Jarc's software lintsh

Paul Jarc's software lintsh is a Bourne shell that optionally warns about suspicious or nonportable constructs. It is intended to help script authors write correct, portable scripts; it will also be suitable for use as /bin/sh. It does not exist yet, and it might never, or perhaps the warning functionality could be added to an existing shell. Regardless, this list of gotchas will be maintained, and you can get into the habit of coding to avoid them.

The following programs are known to be in use as /bin/sh, in addition to the /bin/sh programs maintained by OS vendors. Please let me know about any not listed here.

You can also contribute by emailing me suggestions for constructs to warn about. My experience is mostly with bash and pdksh, so information about issues with other shells is especially appreciated. I am not interested in how shells behave in interactive mode; scripts are only affected by differences in behavior in noninteractive mode. Here's the list of issues so far. (Some are bugs in particular versions; these will still be warned about as long as buggy installations are thought to be common, or as long as I forget to update this page.)

Related Links

Constructs likely to indicate a programming error

Constructs meaningful to all shells, but with different meanings to different shells

Constructs not meaningful to some shells

These will also be erroneous for lintsh; no separate warning will be necessary. These include:

 

Etc

 

Solaris Test command

DESCRIPTION

OPERANDS

USAGE

EXAMPLES

ENVIRONMENT VARIABLES

EXIT STATUS


Copyright © 1996-2008 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Standard disclaimer: The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.

Last modified: June 02, 2008