Presented by David L. Cantrell Jr. on September 23, 2001
Table of Contents
- 1. The Korn Shell
- 1.1. What It Is
- 1.2. Features
- 1.3. So Why Use It?
- 2. Korn Shell Tips
- 2.1. Show the version of ksh you’re currently running.
- 2.2. Compile ksh.
- 2.3. View current option settings.
- 2.4. Fix backspace.
- 2.5. Recall previous commands.
- 2.6. Command completion — Emacs mode
- 2.7. Command completion — vi mode
- 2.8. csh-style directory stack.
- 2.9. Documentation.
- 2.10. Easily set the umask.
- 2.11. Make a bash-style PS1 prompt, like \u@\h:\w\\$, the ksh way.
- 2.12. You can trap keystrokes in your shell scripts.
- 2.13. Make a menu in your shell script.
- 2.14. Autoload your collection of functions each time you start ksh.
- 2.15. Process substitution (only works on systems that use /dev/fd).
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 |
|---|---|
|
|
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.
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.13. Make a menu in your shell script.
PS3='Pick one of the above: '
TMOUT=10
select i in list edit quit
do
done
2.14. Autoload your collection of functions each time you start ksh.
FPATH=$HOME/.ksh.functions
autoload foobar
2.15. Process substitution (only works on systems that use /dev/fd).
paste