Differences between revisions 1 and 23 (spanning 22 versions)
Revision 1 as of 2005-08-07 19:12:53
Size: 4030
Editor: GreyCat
Comment: copied and converted from heiner's wiki
Revision 23 as of 2022-06-20 20:47:27
Size: 3663
Editor: emanuele6
Comment: actually, remove [] around %fmt since the table is describing that in bash, as opposed to ksh, a format specification cannot be used.
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
The KornShell is a reimplementation of the BourneShell. It is downward compatible, but adds many features intented to simplify programming. Most of the features are part of PosixShell, too, and therefore are available on any PosixSystem. The KornShell is a reimplementation of the [[BourneShell|Bourne shell]]. It is downward compatible, but adds many features intended to simplify programming. Most of the features are part of [[PosixShell|POSIX shell]] too, and therefore are available on any POSIX compliant system. In the universe of shells it might be said that the Ksh family is Bash's closest relative. [[Bash]] implements much of the Korn shell's functionality and both share many common conventions. If care is taken it is possible to write polyglot code portable between these shells while still using a superset of POSIX sh features. For example, the (( arithmetic keyword. Incompatibilities do exist in many shared features. For instance, the Bash builtin '''read -a''' is equivalent to Ksh's '''read -A'''.
Line 4: Line 4:
The BashShell also implements most of the functionality. '''ksh88''', and its successor '''ksh93''', is David Korn's "original" Korn shell. Now part of the ast-open package since being open-sourced by AT&T in 2000 under the CPL license, '''ksh93''' is still actively developed and is probably what someone is referring to if using the term "ksh" without further qualification. It is also currently the most featureful. Additionally, there exist several variants under the name "Korn shell". Some of the most common and actively developed include ''mksh'' - the MirBSD Korn shell, and '''pdksh''' ("Public Domain Korn Shell") - intended to be ksh93's open-source replacement while it was still proprietary software.
Line 6: Line 6:
The KornShell is part of any modern (commercial) Unix system, e.g. ["Solaris"], ["AIX"], ["HP-UX"]. An OpenSource version called PdKsh ("Public Domain Korn Shell") is available e.g. for ["Linux"]. The Korn shell (either ksh88 or ksh93, depending on the age of the system) is a standard part of any modern (commercial) Unix system, e.g. [[Solaris]], [[AIX]], [[HP-UX]]. pdksh is often available on BSD or GNU/Linux systems, and a growing number of GNU/Linux systems now offer ksh93, either instead of or in addition to pdksh.
Line 8: Line 8:
The successor to the KornShell is KornShell93, which adds even more features. The source for KornShell93 was recently liberated, and some GNU/Linux distributions now offer it in addition to (or instead of) PdKsh. Incomplete list of ksh93 features with no ''direct'' BashShell equivalent (as of version 4.2), (i.e., no same feature with same syntax by a different name). Some are trivial to accomplish in Bash by other means:
Line 10: Line 10:
 * '''KornShellArrays''' provide arrays with numerical indexes
 * '''TypedVariables''' using the [wiki:Self:TypedefCommand typedef] command. It can be used to print strings left- or right-aligned, print integer numbers with leading zeros, or provide LocalVariables for a function
 * '''ArithmeticExpressions''' in ((...)),
{{{
        ((i=i+1))
        while ((i<10))
        do
            echo "$i"
            ((i=i+1))
        done
}}}

 * '''CommandSubstitution''' with $(_command_), which simplifies nesting. Compare e.g. the BourneShell command
{{{
        lines=`echo \`wc -l < /etc/passwd\``
}}}

  with its KornShell counterpart:

{{{
        lines=$(echo $(wc -l < /etc/passwd))
}}}

 * Powerful '''PatternMatching''' (for filename generation and for usage in {{{case}}} and {{{[[ ... ]]}}}
 * '''ParameterSubstitution''' like {{{${var%pattern}}}}, {{{${var%%pattern}}}} to remove (last/largest) {{{pattern}}} from the end of variable {{{var}}}, and {{{${var#pattern}}}}, {{{${var##pattern}}}} to remove (first/largest) {{{pattern}}} from the front of the variable.
 * [wiki:Self:CoProcess Co-processes], e.g. {{{bc |&}}} to start the "bc" command as a co-process. "{{{read -p}}}" and "{{{print -p}}}" are used to read from or write to the co-process. Other shells (e.g. ["BASH"]) require the user to use e.g. NamedPipes (FiFos) for that purpose
 * built-in '''{{{print}}}''' command. This doesn't sound like a big innovation, but it allows for reliably printing arbitrary characters. Most important uses are "{{{print -n --}}}" to print without trailing NewLine character, and "{{{print -r --}}}" to print arbitrary strings, possibly including EscapeSequences or a BackSlash character.
 * '''[wiki:Self:SelectComand select]''' command for easy menus. {{{select}}} is used like {{{for}}}, but prints an simple interactive menu where a user can select one out of several choices.
 * '''KornShellFunctions''' are similar to traditional BourneShell functions, but can have local variables, local argument handling using [wiki:Self:GetoptsCommand getopts], and local [wiki:Self:TrapCommand traps]
 * Built-in [wiki:Self:TestOperator [[] ("test") with improved quoting behaviour and more conditional operators than the OldTestOperator
 * CommandLineEditing with [wiki:Self:ViEditor vi] or [wiki:Self:EmacsEditor emacs] command sequences

Other, smaller improvements over the BourneShell include
* SignalNames for the [wiki:Self:TrapCommand trap] command, e.g. for {{{trap 'rm temp' EXIT}}} instead of {{{trap 'rm temp' 0}}}
* TildeSubstitution, '''~''' is replaced with the user's HomeDirectory, ~_user_ is replaced with the HomeDirectory of the user _user_, e.g. {{{echo ~root}}}
(Below is a work in progress)
||'''Feature''' ||'''Description''' ||
||{n1..n2[..n3]%fmt} ||Sequence expression with a printf format specification applied to each generated argument. ||
||{n[,m]}(pattern-list) ||ERE-style arbitrary quantification of extended globs. ||
||varname=([typeset [options]] assignment ...) ||Compound and nested variable assignment. Similar to C structs. Bash arrays can only be homogenous datatypes. ||
||%n$ and *n$ ||SUS printf extensions for addressing individual arguments ||
||Floating point numeric types ||- ||
||print (built-in) ||allows for reliably printing arbitrary characters. ||
Line 48: Line 21:
Most of the features of the KornShell are available in the BashShell, too. The following features are '''not''' found in the BashShell:
Line 50: Line 22:
 * co-processes (process |&)
 * built-in "print" command
 * some of the [wiki:Self:TypedefCommand typedef] options
Incomplete list of BashShell features with no direct ksh93 equivalent:
||'''Feature''' ||'''Description''' ||
||;;& ||case...esac delimiter, like ;&, but additionally only continue if the next pattern list matches ||
||pushd/popd/dirs/~[+|-]N ||Ksh93 has external functions in /usr/share/ksh/fun to provide the pushd, popd and dirs commmands. Tilde expansion supports only one level. ||
Line 54: Line 27:
And everyone's favorite KornShell feature that's not in Bash:
Line 56: Line 28:
 * The last command in a pipe is executed in the current shell instead of a subshell. So the following works the way a naive user would expect:
Line 58: Line 29:
  {{{
  i=0
  ls -l | while read line; do
    ((i=$i+1))
  done
  echo "$i lines"
  }}}

Other important notes:

 * Bash co-processes are considerably different and weren't available in Bash at all until version 4. (process |& versus the coproc keyword)
 * Ksh has always executed the last command of a pipeline in the parent shell's environment. Bash has always used a subshell for all pipeline commands. Bash 4.2 adds the '''lastpipe''' shopt option, but it isn't enabled by default.
 * $SECONDS in ksh93 has millisecond precision. Bash only counts seconds and had no high-resolution timestamp mechanism until version 5.0 where $EPOCHSECONDS and $EPOCHREALTIME were introduced.

----
CategoryShell

Korn Shell

The KornShell is a reimplementation of the Bourne shell. It is downward compatible, but adds many features intended to simplify programming. Most of the features are part of POSIX shell too, and therefore are available on any POSIX compliant system. In the universe of shells it might be said that the Ksh family is Bash's closest relative. Bash implements much of the Korn shell's functionality and both share many common conventions. If care is taken it is possible to write polyglot code portable between these shells while still using a superset of POSIX sh features. For example, the (( arithmetic keyword. Incompatibilities do exist in many shared features. For instance, the Bash builtin read -a is equivalent to Ksh's read -A.

ksh88, and its successor ksh93, is David Korn's "original" Korn shell. Now part of the ast-open package since being open-sourced by AT&T in 2000 under the CPL license, ksh93 is still actively developed and is probably what someone is referring to if using the term "ksh" without further qualification. It is also currently the most featureful. Additionally, there exist several variants under the name "Korn shell". Some of the most common and actively developed include mksh - the MirBSD Korn shell, and pdksh ("Public Domain Korn Shell") - intended to be ksh93's open-source replacement while it was still proprietary software.

The Korn shell (either ksh88 or ksh93, depending on the age of the system) is a standard part of any modern (commercial) Unix system, e.g. Solaris, AIX, HP-UX. pdksh is often available on BSD or GNU/Linux systems, and a growing number of GNU/Linux systems now offer ksh93, either instead of or in addition to pdksh.

Incomplete list of ksh93 features with no direct BashShell equivalent (as of version 4.2), (i.e., no same feature with same syntax by a different name). Some are trivial to accomplish in Bash by other means:

(Below is a work in progress)

Feature

Description

{n1..n2[..n3]%fmt}

Sequence expression with a printf format specification applied to each generated argument.

{n[,m]}(pattern-list)

ERE-style arbitrary quantification of extended globs.

varname=([typeset [options]] assignment ...)

Compound and nested variable assignment. Similar to C structs. Bash arrays can only be homogenous datatypes.

%n$ and *n$

SUS printf extensions for addressing individual arguments

Floating point numeric types

-

print (built-in)

allows for reliably printing arbitrary characters.

Incomplete list of BashShell features with no direct ksh93 equivalent:

Feature

Description

;;&

case...esac delimiter, like ;&, but additionally only continue if the next pattern list matches

pushd/popd/dirs/~[+|-]N

Ksh93 has external functions in /usr/share/ksh/fun to provide the pushd, popd and dirs commmands. Tilde expansion supports only one level.

Other important notes:

  • Bash co-processes are considerably different and weren't available in Bash at all until version 4. (process |& versus the coproc keyword)

  • Ksh has always executed the last command of a pipeline in the parent shell's environment. Bash has always used a subshell for all pipeline commands. Bash 4.2 adds the lastpipe shopt option, but it isn't enabled by default.

  • $SECONDS in ksh93 has millisecond precision. Bash only counts seconds and had no high-resolution timestamp mechanism until version 5.0 where $EPOCHSECONDS and $EPOCHREALTIME were introduced.


CategoryShell

KornShell (last edited 2022-06-20 20:47:27 by emanuele6)