Using the Korn Shell
Last updated: Sun Sep 23 2001
Presented at the Wed Sep 19 2001 LUG@GT meeting by David Cantrell ([email protected]).
This document is copyright 2001 David Cantrell.
Table of Contents
1.
The Korn Shell
1.1. What It Is
The Korn Shell, or ksh, is the Bourne-compatible shell created by David Korn
of AT&T Research. It offers a very powerful programming/scripting language as
well as a command shell.
- There are two major versions in use today. The 1988 release
(ksh88) and the 1993 release (ksh93).
- Most commercial UNIX vendors ship ksh88 as /bin/ksh.
- SunOS 5.x include ksh93 as /usr/dt/bin/dtksh. This particular
build integrates support with CDE.
- Slackware Linux includes ksh93 as /bin/ksh (starting with the
8.0 release).
- Red Hat Linux includes the Public Domain Korn Shell as /bin/ksh.
This is not a 100% ksh88 or ksh93 compatible shell.
1.2. Features
| Command Shell |
Programming Language |
- vi or Emacs command editing modes
- History file has configurable size and is shared across
all shell instances
- Process substitution
- Self generating documentation for shell builtins (text,
HTML, or troff).
- Command name completion in both editing modes.
- Job control - ksh allows for managing multiple jobs at
the same time.
- cd command - change to similarly named directories or
completely rewrite cd with your own shell function.
|
- 100% Bourne shell compatible.
- Open multiple files at the same time.
- Keystroke and debugging traps builtin.
- Menu primitive (resizes according to the terminal size).
- Associative and indexed array support.
- Discipline functions
- Builtin integer arithmetic as well as access to math.h
functions.
- Substring operators and recursive function support.
- Co-process facility
|
1.3. So Why Use It?
GNU bash, zsh, tcsh, and other open source shells offer great alternatives to
ksh. But there are some reasons you may want to run ksh over another shell.
- Guaranteed compatibility with future releases.
- ksh is common on commercial UNIX systems.
- Same across all platforms it supports.
- Includes some language features not found in other shells.
2.
Korn Shell Tips
The following examples are to be executed from a ksh command prompt.
2.1. Show the version of ksh you're currently running.
print ${.sh.version}
This works in ksh93 only.
what /bin/ksh | grep Version
This works only on systems with the what command.
2.2. Compile ksh.
Download ast-base, ast-base-locale, and INIT.
$ cd /tmp
$ mkdir -p ast/lib/package/tgz
$ cp ast-base* ast-base-locale* INIT* ast/lib/package/tgz
$ cd ast
$ gzip -dc lib/package/tgz/INIT* | tar -xvf -
$ bin/package read
$ bin/package make
2.3. View current option settings.
(This command is also used to modify options, see the ksh man page for
descriptions.)
set -o
2.4. Fix backspace.
stty erase ^h
2.5. Recall previous commands.
history [ -{num} | {num} {num} | {num} ]
That's limit list to num, display from num to num, and show commands starting
from num.
r [ {num} | {string cmd starts with} ]
Recall a previous command by its history number or by typing in the first few
characters of its name.
2.6. Command completion -- Emacs mode
[esc] [=]
Display list of pathnames that result from expanding the word under the
cursor.
[esc] [esc]
Append characters to the word under the cursor to complete the pathname of an
existing file.
[esc] [*]
Replace words under the cursor with the list of pathnames that result from
expanding the word.
2.7. Command completion -- vi mode
[=]
Display list of pathnames that result from expanding the word under the
cursor.
[\]
Append characters to the word under the cursor to complete the pathname of an
existing file.
[*]
Replace words under the cursor with the list of pathnames that result from
expanding the word.
2.8. csh-style directory stack.
Download the
code here.
2.9. Documentation.
All ksh builtins have self-generated text, HTML, and troff documentation.
Just type --help, --man, or --html for the appropriate version.
2.10. Easily set the umask.
umask -S =rx,u+w
2.11. Make a bash-style PS1 prompt, like \u@\h:\w\\$, the ksh way.
export PS1='${USER}@$(hostname):${PWD/#$HOME/~}\$ '
2.12. You can trap keystrokes in your shell scripts.
typeset -A Keytable
trap 'eval "${Keytable[${.sh.edchar}]}"' KEYBD
2.13. Quickly change to /usr/openwin/bin from /usr/local/bin.
$ pwd
/usr/local/bin
$ cd local openwin
/usr/openwin/bin
$ pwd
/usr/openwin/bin
2.14. Create, modify, and get information on variables and functions.
typeset --help | more
2.15. Make a menu in your shell script.
PS3='Pick one of the above: '
TMOUT=10
select i in list edit quit
do
case $i in
list) cat "$foo" ;;
edit) ${EDITOR-vi} "$foo" ;;
quit) break ;;
"") print -u2 you must select one of the above ;;
esac
done
2.16. Autoload your collection of functions each time you start ksh.
FPATH=$HOME/.ksh.functions
autoload foobar
2.17. Process substitution (only works on systems that use /dev/fd).
paste <(cut -f1 file1) <(cut -f3 file2) | tee >(pro1) >(pro2)