7. The Korn Shell
Motivation Prerequisites Objectives Presentation Shell Commands Section 7.1. Introduction Section 7.2. Startup Section 7.3. Variables Section 7.4. Aliases Section 7.5. History Section 7.6. Editing Commands Section 7.7. Arithmetic Section 7.8. Tilde Substitution Section 7.9. Menus: select Section 7.10. Functions Section 7.11. Job Control Section 7.12. Enhancements Section 7.13. Sample Project: junk Section 7.14. Command-Line Options Chapter Review |
MotivationThe Korn shell, originally designed by David Korn, is a powerful superset of the original UNIX Bourne shell, and offers improvements in job control, command-line editing, and programming features. It is very popular in UNIX environments and is available on most Linux systems. |
PrerequisitesYou should already have read Chapter 5, "The Linux Shells." |
ObjectivesIn this chapter, I explain and demonstrate the Korn-specific facilities. |
PresentationThe information in this section is presented in the form of several sample Linux command sessions. |
Shell CommandsThis section introduces the following shell commands, listed in alphabetical order:
|
7.1. IntroductionThe Korn shell is available for Linux as the Public Domain Korn Shell (pdksh), a public domain reimplementation that is intended to be a clone of the UNIX ksh. Although sometimes not installed in the default set of Linux packages, pdksh is included in most Linux distributions. Another Linux shell, the Z shell (zsh, written by Paul Falstad), also implements most features of the Korn shell, but does not claim complete compatibility with the original Korn shell. The Korn shell supports all of the shell facilities described in Chapter 5, "The Linux Shells," as well as the control structures and conditional expressions described in Chapter 6, "The Bourne Again Shell." The following features unique to the Korn shell are discussed in this chapter:
|
7.2. StartupThe Korn shell is a regular C program whose executable file is stored as "/bin/ksh." If your chosen shell is "/bin/ksh," an interactive Korn shell is invoked automatically when you log into a Linux system. You may also invoke a Korn shell manually from a script or from a terminal by using the command ksh . ksh has several command-line options that are described at the end of this chapter. When a Korn shell is invoked, the startup sequence is different for interactive shells and noninteractive shells (Figure 7-1). Figure 7-1. Korn shell startup sequence.
The value $ENV is usually set to $HOME/. kshrc in the $HOME/.profile script. After reading the startup files, an interactive Korn shell then displays its prompt and awaits user commands. The standard Korn shell prompt is $, although it may be changed by setting the local variable PS1. Here's an example of a Korn shell ".profile" script, which is executed exactly once at the start of every login session:
TERM=xterm; export TERM # my terminal type. ENV=~/.kshrc; export ENV # environment filename. HISTSIZE=100; export HISTSIZE # remember 100 commands. MAILCHECK=60; export MAILCHECK # seconds between checks. set -o ignoreeof # don't let Control-D log me out. set -o trackall # speed up file searches. stty erase '^H' # set backspace character. tset # set terminal. Some of these commands won't mean much to you right now, but their meaning will become clear as the chapter progresses. Here's an example of a Korn shell ".kshrc" script, which typically contains useful Korn-shell specific information required by all shells, including those that are created purely to execute scripts:
PATH='.:~/bin:/bin:/usr/bin:/usr/local/bin'
PS1='! $ ';export PS1 # put command number in prompt.
alias h="fc -l" # set up useful aliases.
alias ll="ls -l"
alias rm="rm -i"
alias cd="cdx"
alias up="cdx .."
alias dir="/bin/ls"
alias ls="ls -aF"
alias env="printenvsort"
# function to display path and directory when moving
function cdx
{
if 'cd' "$@"
then
echo $PWD
ls -aF
fi
}
Every Korn shell executes this script when it begins, including all subshells. |