[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
System User's Guide: Operating System and Devices

Korn Shell or POSIX Shell Commands

A Korn shell command is one of the following:

When you issue a command in the Korn shell or POSIX shell , the shell evaluates the command and acts as follows:

If the command does not contain a / (slash), the Korn shell or POSIX shell continues with the following actions:

The Korn shell, or POSIX shell, searches each directory in a specified path for an executable file. The PATH shell variable defines the search path for the directory containing the command. Alternative directory names are separated with a : (colon). The default path is /usr/bin: (specifying the /usr/bin directory, and the current directory, in that order). The current directory is specified by two or more adjacent colons, or by a colon at the beginning or end of the path list.

If the file has execute permission but is not a directory or an a.out file, the shell assumes that it contains shell commands. The current shell process spawns a subshell to read the file. All nonexported aliases, functions, and named parameters are removed from the file. If the shell command file has read permission, or if the setuid or setgid bits are set on the file, then the shell runs an agent that sets up the permissions and carries out the shell with the shell command file passed down as an open file. A parenthesized command is run in a subshell without removing nonexported quantities.

This section discusses:

Korn Shell Compound Commands

A compound command is a list of simple commands, a pipeline, or it can begin with a reserved word. Most of the time you will use compound commands such as if, while, and for when you are writing shell scripts.

List of Korn Shell or POSIX Shell Compound Commands

for Identifier [in Word ...] ;do List ;done
                          Each time a for command is executed, the Identifier parameter is set to the next word taken from the in Word ... list. If the in Word ... command is omitted, then the for command executes the do List command once for each positional parameter that is set. Execution ends when there are no more words in the list. Refer to "Parameter Substitution in the Korn Shell" for more information on positional parameters.
select Identifier [in Word ...] ;do List ;done
                          A select command prints on standard error (file descriptor 2) the set of words specified, each preceded by a number. If the in Word ... command is omitted, then the positional parameters are used instead. The PS3 prompt is printed and a line is read from the standard input. If this line consists of the number of one of the listed words, then the value of the Identifier parameter is set to the word corresponding to this number.
If the line read from standard input is empty, the selection list is printed again. Otherwise, the value of the Identifier parameter is set to null. The contents of the line read from standard input is saved in the REPLY parameter. The List parameter is executed for each selection until a break or an end-of-file character is encountered. Refer to "Parameter Substitution in the Korn Shell or POSIX Shell" for more information on positional parameters.
case Word in [[ ( ] Pattern [ | Pattern] ... ) List ;;] ... esac
                          A case command executes the List parameter associated with the first Pattern parameter that matches the Word parameter. The form of the patterns is the same as that used for file name substitution.
if List ;then List [elif List ;then List] ... [;else List] ;fi
                          The List parameter specifies a list of commands to be run. The shell executes the if List command first. If a zero exit status is returned, it executes the then List command. Otherwise, the commands specified by the List parameter following the elif command are executed.
If the value returned by the last command in the elif List command is zero, the then List command is executed. If the value returned by the last command in the then List command is zero, the else List command is executed. If no commands specified by the List parameters are executed for the else or then command, the if command returns a zero exit status.
while List ;do List ;done
                         
until List ;do List ;done
                          The List parameter specifies a list of commands to be run. The while command repeatedly executes the commands specified by the List parameter. If the exit status of the last command in the while List command is zero, the do List command is executed. If the exit status of the last command in the while List command is not zero, the loop terminates. If no commands in the do List command are executed, then the while command returns a zero exit status. The until command may be used in place of the while command to negate the loop termination test.
(List)
                          The List parameter specifies a list of commands to run. The shell executes the List parameter in a separate environment.
Note: If two adjacent open parentheses are needed for nesting, you must insert a space between them in order to differentiate between the command and arithmetic evaluation.
{List;}
                          The List parameter specifies a list of commands to run. The List parameter is simply executed.
Note: Unlike the metacharacters ( ) (parentheses), { } (braces) denote reserved words (used for special purposes, not as user-declared identifiers). To be recognized, these reserved words must appear at the beginning of a line or after a ; (semicolon).
[[Expression]] Evaluates the Expression parameter. If the expression is true, the command returns a zero exit status.
function Identifier {List ;} or function Identifier () {List ;}
                          Defines a function that is referenced by the Identifier parameter. The body of the function is the specified list of commands enclosed by { } (braces). The () consists of two operators, so mixing blank characters with the identifier, ( and ) is permitted, but is not necessary.
time Pipeline
                          Executes the Pipeline parameter. The elapsed time, user time, and system time are printed to standard error.

Functions

The function reserved word defines shell functions. The shell reads and stores functions internally. Alias names are resolved when the function is read. The shell executes functions in the same manner as commands, with the arguments passed as positional parameters. Refer to "Parameter Substitution in the Korn Shell or POSIX Shell" for more information on positional parameters.

The Korn shell or POSIX shell executes functions in the environment from which functions are invoked. All of the following are shared by the function and the invoking script, and side effects can be produced:

The following are not shared between the function and the invoking script, and there are no side effects:

Note: In earlier versions of the Korn shell, traps other than EXIT and ERR were shared by the function as well as the invoking script.

If trap on 0 or EXIT is executed inside the body of a function, the action is executed after the function completes, in the environment that called the function. If the trap is executed outside the body of a function, the action is executed upon exit from the Korn shell. In earlier versions of the Korn shell, no trap on 0 or EXIT outside the body of a function was executed upon exit from the function.

When a function is executed, it has the same syntax-error and variable-assignment properties described in "Korn Shell or POSIX Shell Built-in Commands."

The compound command is executed whenever the function name is specified as the name of a simple command. The operands to the command temporarily will become the positional parameters during the execution of the compound command. The special parameter # will also change to reflect the number of operands. The special parameter 0 will not change.

The return special command is used to return from function calls. Errors within functions return control to the caller.

Function identifiers are listed with the -f or +f option of the typeset special command. The -f option also lists the text of functions. Functions are undefined with the -f option of the unset special command.

Ordinarily, functions are unset when the shell executes a shell script. The -xf option of the typeset special command allows a function to be exported to scripts that are executed without a separate invocation of the shell. Functions that must be defined across separate invocations of the shell should be specified in the ENV file with the -xf option of the typeset special command.

The exit status of a function definition is zero if the function was not successfully declared. Otherwise, it will be greater than zero. The exit status of a function invocation is the exit status of the last command executed by the function.


[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]