Differences between revisions 3 and 4
Revision 3 as of 2008-03-25 13:36:54
Size: 4078
Editor: p54BFD458
Comment: use in-place inc. instead of re-assigning
Revision 4 as of 2008-03-25 13:38:27
Size: 4073
Editor: p54BFD458
Comment: we don't even need the line variable here
Deletions are marked like this. Additions are marked like this.
Line 60: Line 60:
  ls -l | while read line; do   ls -l | while read; do

Korn Shell

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 BashShell also implements most of the functionality.

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 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.

        ((i=i+1))
        while ((i<10))
        do
            echo "$i"
            ((i=i+1))
        done

        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: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: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:GetoptsCommand getopts], and local [wiki:TrapCommand traps]

  • Built-in [wiki:TestOperator [[] ("test") with improved quoting behaviour and more conditional operators than the OldTestOperator

  • CommandLineEditing with [wiki:ViEditor vi] or [wiki:EmacsEditor emacs] command sequences

Other, smaller improvements over the BourneShell include

  • Earlier versions of the Bourne shell don't know yet signal names for the [wiki:TrapCommand trap] command, e.g. 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

Most of the features of the KornShell are available in the BashShell, too. The following features are not found in the BashShell:

  • co-processes (process |&)

  • built-in "print" command
  • some of the [wiki:TypedefCommand typedef] options

And everyone's favorite KornShell feature that's not in Bash:

  • 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:
    •   i=0
        ls -l | while read; do
          ((++i))
        done
        echo "$i lines"

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