Tulosta
Shell - mitä tarkoitan / käytän - Which shell ?
Shell frontpage | Howto

Shell scripting

Shell Posix-standard based on Bourne Shell (bsh) and extension for bsh based from : Korn Shell (ksh88). There are lot of other shells which try to be also Posix/BourneShell compatible: bash, ash, dash, zsh, ...

Some of the shells has good interactive using properties, better than bsh/ksh (if not like vi or emacs), but when I like to make compatible scripts, then my answer is ksh93. Most pure Posix shell is maybe ash/dash, but others include lot of good extension for scripting. I'll say that if you use #!/bin/sh then shell must be ash, dash or ksh. Those shell include full support for Posix. But if your script include line #!/bin/sh, I'll say: it include only Bourne Shell compatible lines. Why I say ksh93, not ksh ? Because some of *nix systems still use old ksh88 version.

On the older *nix systems sh has been Bourne-Shell. Today situation is grazy: sh is ksh or posix-sh or bash or ash or dash or ...

I have used shell scripting since 1984, most of my code in last 20 years has done using shell script. My answer for script shell is ksh ( ksh93).

It support old Bourne Shell syntax and include Posix-standard.

It also include some usable extension for scripting, example:

  • read and pipe works together
  • socket tcp+udp, today also in bash (v4)
  • keyboard interrupt/trap
  • assosiative arrays - hash table, today also in bash (v4)
  • floating point arithmetic
  • read timeout, today also in bash (v4)
  • math library functions
  • printf %T date services

Why /bin/sh is not so good?

Basic problem has defined in previous section.
Solution ? Make selection which shell you use in your script. Make selection between ksh, ash, bash or zsh. My answer is ksh (read previous section), but you make your own. Ofcourse most of scripts can be compatible, works all of those shells, if use pure old Bourne Shell syntax.

Copy shell binary version to the some fixed dir and name: ex. /usr/local/bin/mysh and use that path in your script. After this you know which shell you are using. After this your first line in script: #!/usr/local/bin/mysh

My sh - which version it is ?

Bash ? Look value of variable BASH_VERSINFO. If you have not bash then you get nothing. Newest (2009/8 status) is release 4. echo "$BASH_VERSINFO" Bourne Shell ? Original version for command substitution was use commandline between `  `. After ksh88 has published, it has been also in Posix-standard and I think every other than Bourne Shell can use more readable $(  ) command substitution marking. I like this because (  ) is subshell, so adding $ before it is something "take value of command".

echo `date` # works in every shell, also in bsh echo $(date) # not works in bourne shell, but works in every other shells

Zsh ? Look value of variable ZSH_VERSION. If you have not zsh then you get nothing. Newest (2009/8 status) is version 4.3. echo "$ZSH_VERSION"

Ksh ? There is old ksh88 version in many commercial Unix, but since 1993 has been ksh93. Ksh93 is easy to see, look variable .sh.version: echo "${.sh.version}" # works in ksh93 but not in ksh88 Ksh88, not bash ? Next command works with every ksh, but not in any bash. typeset -L x

Ash/dash ? If all previous variable testing give always empty string or error, then maybe it is ash (or dash). If typeset, echo substring, ... give also error then maybe ash ... typeset #error a=1234 echo ${a:2:1} # error print # give error printf some # works

C-Shell

Syntax is different, I stopped csh scripting about 20 years ago. I don't know any reason why to use it. If I need something more, then I use awk, perl, php, python, C, java, ...

Some answers for csh.