[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]

System User's Guide: Operating System and Devices

Korn Shell or POSIX Shell Commands

The Korn shell is an interactive command interpreter and command programming language. It conforms to the Portable Operating System Interface for Computer Environments (POSIX), an international standard for operating systems. POSIX is not an operating system, but is a standard aimed at portability of applications, at the source level, across many systems. POSIX features are built on top of the Korn shell. The Korn shell (also known as the POSIX shell) offers many of the same features as the Bourne and C shells, such as I/O redirection capabilities, variable substitution, and file name substitution. It also includes several additional command and programming language features:

Arithmetic evaluation The Korn shell, or POSIX shell, can perform integer arithmetic using the built-in let command, using any base from 2 to 36. Arithmetic Evaluation in the Korn Shell or POSIX Shell further describes this feature.
Command history The Korn shell, or POSIX shell, stores a file that records all of the commands you enter. You can use a text editor to alter a command in this history file and then reissue the command. For more information about the command history feature, see Korn Shell or POSIX Shell Command History .
Coprocess facility The Coprocess Facility enables you to run programs in the background and send and receive information to these background processes.
Editing The Korn shell, or POSIX shell, offers inline editing options that enable you to edit the command line. Editors similar to emacs, gmacs, and vi are available. Inline Editing in the Korn Shell or POSIX Shell further describes this feature.

This section discusses the following:

Korn Shell Environment

All variables (with their associated values) known to a command at the beginning of its execution constitute its environment. This environment includes variables that a command inherits from its parent process and variables specified as keyword parameters on the command line that calls the command. The shell interacts with the environment in several ways. When it is started, the shell scans the environment and creates a parameter for each name found, giving the parameter the corresponding value and marking it for export. Executed commands inherit the environment.

If you modify the values of the shell parameters or create new ones using the export or typeset -x commands, the parameters become part of the environment. The environment seen by any executed command is therefore composed of any name-value pairs originally inherited by the shell, whose values might be modified by the current shell, plus any additions that resulted from using the export or typeset -x commands. The executed command (subshell) will see any modifications it makes to the environment variables it has inherited, but it needs to export these variables for its child shells or processes to see the modified values.

The environment for any simple command or function is changed by prefixing with one or more parameter assignments. A parameter assignment argument is a word of the form Identifier=Value. Thus, the two following expressions are equivalent (as far as the execution of the command is concerned).

TERM=450 Command arguments

(export TERM; TERM=450; Command arguments)

If the -k flag is set, all parameter assignment arguments are placed in the environment, even if they occur after the command name. The following first prints a=b c and then c:

echo a=b c
set -k
echo a=b c

Note: This feature is intended for use with scripts written for early versions of the shell. Its use in new scripts is strongly discouraged.

Shell Startup

You can start the Korn shell with either the ksh command or the psh command (POSIX shell).

If the shell is started by the exec command, and the first character of zero argument ($0) is the hyphen (-), then the shell is assumed to be a login shell. The shell first reads commands from the /etc/profile file, and then from either the .profile file in the current directory or from the $HOME/.profile file, if either file exists. Next, the shell reads commands from the file named by performing parameter substitution on the value of the ENV environment variable, if the file exists.

If you specify the File [Parameter] parameter when invoking the Korn shell or POSIX shell , the shell runs the script file identified by the File parameter, including any parameters specified. The script file specified must have read permission; any setuid and setgid settings are ignored. The shell then reads the commands.

Note: Do not specify a script file with the -c or -s flags.

Refer to Parameter Substitution in the Korn Shell or POSIX Shell for more information on positional parameters.

Shell Prompt

When used interactively, the shell prompts with the value of the PS1 parameter before reading a command. If at any time a new line is entered and the shell requires further input to complete a command, the shell issues the secondary prompt (the value of the PS2 parameter).

Korn Shell or POSIX Shell Command History

The Korn shell or POSIX shell saves commands entered from your terminal device to a history file. If set, the HISTFILE variable value is the name of the history file. If the HISTFILE variable is not set or cannot be written, the history file used is $HOME/.sh_history. If the history file does not exist and the Korn shell cannot create it, or if it does exist and the Korn shell does not have permission to append to it, then the Korn shell uses a temporary file as the history file. The shell accesses the commands of all interactive shells using the same named history file with appropriate permissions.

By default, the Korn shell or POSIX shell saves the text of the last 128 commands entered from a terminal device. The history file size (specified by the HISTSIZE variable) is not limited, although a very large history file can cause the Korn shell to start up slowly.

Command History Substitution

Use the fc built-in command to list or edit portions of the history file. To select a portion of the file to edit or list, specify the number or the first character or characters of the command. You can specify a single command or range of commands.

If you do not specify an editor program as an argument to the fc regular built-in command, the editor specified by the FCEDIT variable is used. If the FCEDIT variable is not defined, then the /usr/bin/ed file is used. The edited command or commands are printed and run when you exit the editor.

The editor name hyphen (-)is used to skip the editing phase and run the command again. In this case, a substitution parameter of the form Old=New can be used to modify the command before it is run. For example, if r is aliased to fc -e -, then typing r bad=good c runs the most recent command that starts with the letter c, and replaces the first occurrence of the bad string with the good string.

For more information about using the history shell command, see Listing Previously Entered Commands (history Shell Command) and the fc command in the AIX 5L Version 5.1 Commands Reference.

[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]