|[ Team LiB ]|
The Korn Shell
The Korn shell, developed by David Korn of AT&T Bell Laboratories, is a superset of the Bourne shell. That is, the Korn shell uses the same syntax as the Bourne shell, but the Korn shell has more built-in functions that can be defined directly from the shell. The Korn shell provides a more sophisticated form of command editing than does the C shell. The Korn shell also provides a command history and aliases.
The Korn shell provides a complete command and scripting language. The following sections introduce some of the most basic features of the Korn shell.
Reviewing Korn Shell Initialization Files
$ ENV=$HOME/.kshrc;export ENV $
You must set this environment variable in the .profile file; otherwise, the .kshrc file is not found when you log in. The ENV variable has no default setting. Unless you set it, the feature is not used. The . ksh-env file is read each time you start the Korn shell from a command line.
Using Korn Shell Options
The Korn shell has a number of options that specify the user's environment and control execution of commands. To display the current option settings, type set -o and press Return. In the following example, the default options for the Korn shell for the Solaris Operating Environment are displayed.
$ set -o Current option settings allexport off bgnice on emacs off errexit off gmacs off ignoreeof off interactive on keyword off markdirs off monitor on noexec off noclobber off noglob off nolog off nounset off privileged off restricted off trackall off verbose off vi off viraw off xtrace off $
The default options are described in Table 29. Customarily, you set these options in the .ksh-env file.
To enable an option, type set -o option-name and press Return. To disable an option, type set +o option-name and press Return.
For example, entering this line in the user's .ksh-env file sets the in-line editor to vi.
set -o vi
The following example turns off vi as the in-line editor.
set +o vi
You can also set these options from a command line, using the same syntax.
Creating Korn Shell Aliases
The syntax for creating aliases for the Korn shell is alias name=value. The following example creates an alias for the alias command.
$ alias a=alias $
The following example uses the a alias created in the last example to alias the history command to the letter h.
$ a h=history $
The Korn shell comes with a default set of predefined aliases. To display the list, type alias and press Return.
$ alias autoload=typeset -fu false=let 0 functions=typeset -f hash=alias -t - history=fc -l integer=typeset -i nohup=nohup r=fc -e - stop=kill -STOP suspend=kill -STOP $$ true=: type=whence -v $
The default aliases are described in Table 30.
Editing Commands with the Korn Shell In-line Editor
You can use the Korn shell in-line editor to edit the current command before you execute it. You can choose one of three in-line editors: emacs, gmacs, or vi. You specify the in-line editor by using the set -o editor option or by setting either the EDITOR or VISUAL environment variable. This section describes how to use the vi in-line editor to edit commands.
The vi in-line editor is a modified subset of the vi program; it lacks some of the features of vi. The vi in-line editor is automatically in insert mode. You can type commands and execute them by pressing Return without using the vi in-line editor. If you want to edit a command, press Escape to enter command mode. You can move along the command line with the standard cursor movement commands and use standard vi editing commands to edit the contents of the line. When the command is edited, press Return to execute it or press Escape to return to input mode.
If you want to edit the command line in a vi file, type v to open a vi file containing the contents of the command line. When you leave vi, the command is executed. Refer to Table 24 on page 93 for a quick-reference to common vi commands.
Setting the Size of the Korn Shell's History
The Korn shell stores history commands in a file specified by the HISTFILE variable. If the variable is not set, the files are stored in $HOME/.sh_history. You can specify the number of commands stored by using the HISTSIZE variable. If the variable is not set, the most recent 128 commands are saved. When the history list contains the maximum number of commands, then as new commands are entered, the oldest commands become unavailable.
To set a different history size, type HISTSIZE= n;export HISTSIZE and press Return. History is set to the number of lines you specify.
The following example sets the history size to 200.
$ HISTSIZE=200;export HISTSIZE $
You can set the history temporarily for a shell window or set it "permanently" by entering the command as a line in the .profile file.
Displaying Korn Shell History Commands
You can use two commands to show the commands from the history list: fc and history. Because history is aliased to fc -l as one of the default aliases, you can use the commands interchangeably. If you do not specify a range with either the history or fc -l command, the last 16 commands are displayed.
To display the last 16 commands in the history list, type history and press Return. The last 16 commands in the history list are displayed.
$ history 16 pwd 17 ps -el 18 ps -el | grep openwin 19 cd 20 more questionnaire 21 su 22 lp /etc/passwd 23 lpstat -t 24 man ksh 25 du 26 maker & 27 tip -2400 5551212 28 alias h=history 29 find / -name ksh -print 30 df -k 31 history $
An alternative way to display the same information is to type fc -l and press Return.
The history and fc commands take additional arguments that let you specify a range, display the last n number of commands, and display the commands in reverse order. See the ksh(1) manual page for more information.
Using Korn Shell History Commands
To use a command from the history list, type r n to reuse a command by number. The following example reuses command 27.
$ r 27 tip -2400 5551212 (Connection messages are displayed)
To repeat the last command in the history list, type r and press Return.
Editing Korn Shell History Commands
You can display individual history commands and edit them by using the fc command with the following syntax.
fc [-e editor] [-r] [range]
The following syntax also works.
fc -e - [old=new] [command]
You use the -e option to specify an editor. If no editor is specified, the FCEDIT environment variable value is used. If no value is set, the default editor is /bin/ed. The -r option reverses the order of the commands, displaying the most recent commands at the top of the list. If you specify no range, the last command is edited.
For example, to use vi to edit the last command in a history list, type fc -e vi and press Return. A vi file is created containing the last entry from the history list. When you edit the command and save the changes, the command is executed.
|[ Team LiB ]|