KSH(1)

NAME
ksh - Bourne / Korn Shell (Public Domain)

SYNOPSIS
ksh [-st] [-c command] [file [argument ...]]

INTRODUCTION
This document only summarizes the System V, release 2 shell features. All of the System V features except for "restricted mode" and the CDPATH and MAIL* variables are implemented. See also the BUGS section.

Features of the Korn shell are described in more detail. Only a subset of the Korn shell features are currently implemented.

DESCRIPTION

  Command syntax
The ``#'' character begins a one-line comment, unless the ``#'' occurs inside a word. The tokens ``;'', ``|'', ``&'', ``;;'', ``||'', ``&&'', ``('', and ``)'' stand by themselves. A word is a sequence of any other non-whitespace characters, which may also contain quoted strings (quote character are ``''', ``"'', ```'', or a matching ``${ }'' or ``$( )'' pair). A name is an unquoted word made up of letters, digits, or ``_''. Any number of whitespace characters (space and tab) may separate words and tokens.

In the following syntax, { ... }? indicates an optional thing, { ... }* indicates zero or more repetitions, { ... | ... } indicates alternatives.

statement:
    The opening parenthesis of the pattern is optional. Redirection may occur at the beginning or end of a statement.

command:
    Redirection may occur anywhere in a command.

list:

cond:

pipe:

  Alias expansion
Alias expansion occurs when the first word of a statement is a defined alias, except when that alias is already being expanded. It also occurs after the expansion of an alias whose definition ends with a space.

  Shell variables
The following standard special variables exist: !, #, $, -, ?.

CDPATH
    Not yet implemented.

ENV If this variable is set at start-up (after any profile files are executed), the expanded value is used as shell start-up file. It typically contains function and alias definitions.

FCEDIT
    The editor used by the fc command.

IFS Internal field separator, used during substitution and the read command.

HOME
    The default directory for the cd command.

MAIL MAILCHECK MAILPATH
    Not yet implemented.

PATH
    The search path for executable commands and .'d files.

PS1 PS2
    PS1 is the primary prompt for interactive shells. Dollar substitution is performed, and ! is replaced with the command number (see fc).

PWD OLDPWD
    The current and previous working directories.

SECONDS
    The number of seconds since the shell was started.

  Substitution
In addition to the System Vr2 substitutions, the following are available.

$(command)
    Like `command`, but no escapes are recognized.

$(<file)
    Equivalent to $(cat file), but without forking.

${#var}
    The length of the string value of var, or the number of arguments if var is * or @.

${var#pattern} ${var##pattern}
    If pattern matches the beginning of the value of var, the matched text is deleted from the result of substitution. A single # results in the shortest match, two #'s results in the longest match.

${var%pattern} ${var%%pattern}
    Like # substition, but deleting from the end of the value.

  Expressions
Expressions can be used with the let command, as numeric arguments to the test command, and as the value of an assignment to an integer variable.

Expression may contain alpha-numeric variable identifiers and integer constants and may be combined with the following operators. [list them]

  Command execution
After evaluation of keyword assignments and arguments, the type of command is determined. A command may execute a shell function, a shell built-in, or an executable file.

Any keyword assignments are then performed according to the type of command. In function calls assignments are local to the function. Assignments in built-in commands marked with a † persist, otherwise they are temporary. Assignments in executable commands are exported to the sub-process executing the command.

There are several built-in commands.

:   Only expansion and assignment are performed. This is the default if a command has no arguments.

. file
    Execute the commands in file without forking. The file is searched in the directories of $PATH. Passing arguments is not implemented.

alias [name=value ...]
    Without arguments, alias lists all aliases and their values. For any name without a value, its value is listed. Any name with a value defines an alias, see "Alias Expansion" above. Korn's tracked aliases are not implemented, but System V command hashing is (see "hash").

break [levels]

builtin command arg ...
    Command is executed as a built-in command.

cd [path]
    Set the working directory to path. If path is missing, the home directory ($HOME) is used. If path is -, the previous working directory is used. The PWD and OLDPWD variables are reset. The System V two argument form is not implemented.

continue [levels]

echo ...
    Echo is replaced with the alias echo='print' in the Korn shell.

eval command ...

exec command arg ...
    The executable command is executed without forking. If no arguments are given, any IO redirection is permanent.

exit [status]

fc [-l] [-n] [first [last]]
    A simple subset of Korn's ``fix command''. First and last select commands. Commands can be selected by history number, or a string specifing the most recent command starting with that string. The -l option lists the command on stdout, and -n inhibits the default command numbers. Without -l, the selected commands can be edited by the $FCEDIT editor, then executed by the shell.

fc -s [old=new] [command]
    Re-execute the selected command (the previous command by default) after performing the optional substitution of old with new. This non-standard command is usually accessed with the predefined alias r="fc -s".

getopts
    See the attached manual page.

hash [-r] [name ...]
    Without arguments, any hashed executable command pathnames are listed. The -r flag causes all hashed commands to be removed. Each name is searched as if it where a command name and added to the hash table if it is an executable command.

kill [-signal] process ...
    Send a signal (TERM by default) to the named process. The signal may be specified as a number or a mnemonic from <signal.h> with the SIG prefix removed.

let [expression ...]
    Each expression is evaluated, see "Expressions" above. A zero status is returned if the last expression evaluates to a non-zero value, otherwise a non-zero status is returned.

print [-nreun] [argument ...]
    Print prints its arguments on the standard output, separated by spaces, and terminated with a newline. The -n option eliminates the newline.

    By default, certain C escapes are translated. These include \b, \f, \n, \r, \t, \v, and \### (# is an octal digit). \c is equivalent to the -n option. This expansion may be inhibitted with the -r option, and may be re-enabled with the addition of the -e option.

read [-run] name ...
    The first variable name may be of the form name?prompt.

readonly [name ...]

return [status]

set [±[a-z]] [±o keyword] ...
    Set (-) or clear (+) a shell option:

set [--] arg ...
    Set shell arguments.

shift [number]

test
    See the attached manual page.

times

trap [handler] [signal ...]

typeset [±irtx] [name[=value] ...]
    If no arguments are given, lists all variables and their attributes.

If options but no names are given, lists variables with specified attributes, and their values if unless ``+'' is used.

If names are given, set the attributes of the named variables. Variables may also be assigned a value. If used inside a function, the created variable are local to the function.

The attributes are as follows. -i The variable's value is stored as an integer.
-x The variable is exported to the enviroment.
-r The variable is read-only cannot be reassigned a value.
-t Trace (not implemented).
-f List functions instead of variable.

ulimit [ -<OZ> ] [ n ]

    -f  Impose a size limit of n blocks on files written by the shell and its child processes (files of any size may be read).

    If no option is given, -f is assumed. If n is omitted, the current limit is printed. As far as ulimit is concerned, a ``block'' is 512 bytes.

    You may lower your own resource limit, but only a super-user (see su(1M)) can raise a limit.

umask [value]

unalias name ...
    The aliases for the given names are removed.

unset [-f] name ...

wait [process-id]

whence [-v] name ...
    For each name, the type of command is listed. The -v flag causes function and alias values to be listed.

FILES
~/.profile
/etc/profile

SEE ALSO
Sh(1) on System V or Sun OS.

UNIX Shell Programming, Stephan G. Kochan, Patrick H. Wood, Hayden.

KornShell: Command and Programming Language (not yet published), Morris Bolsky and David Korn.

AUTHORS
Based on the public domain 7th edition Bourne shell.

System V and Korn modifications by Eric Gisin, with contributions by Ron Natalie, Arnold Robbins, Doug Gwyn, Erik Baalbergen, AT&T.

DIFFERENCES FROM AT&T VERSION
Vi editing mode is not implemented. The select statement is not implemented. "fc -e -" is not implemented, use "fc -s". The variables RANDOM and PPID are not implemented. Variable arrays are not implemented. Variable attributes other than integer are not implemented. The ERR and EXIT traps are not implemented for functions. Alias expansion is inhibited at the beginning of an alias definition in the AT&T version.

BUGS
Interactive shells will occasionally hang while waiting for a job.

The 8th bit is stripped in emacs mode.

TEST(1)

Please report any other bugs by mail to egisin@Waterloo.EDU or egisin@UWaterloo.CA.

NAME
test - test condition (Korn and 8th edition)

SYNOPSIS
test expression
[ expression ]

DESCRIPTION
Testevalutates the expression and returns zero status if true, and non-zero status otherwise. It is normally used as the controlling command of the if and while statements.

The following basic expressions are available.

The above basic expressions may be combined with the following operators.

AUTHOR
GETOPTS(1)

Erik Baalbergen. Modified by Arnold Robbins.

NAME
getopts - parse command options

SYNOPSIS
getopts optstring name [arg ...]

DESCRIPTION
getopts is used by shell procedures to parse positional parameters and to check for legal options. It supports all applicable rules of the command syntax standard (see Rules 3-10, intro(1)). It should be used in place of the getopt(1) command. (See the WARNING, below.)

optstring must contain the option letters the command using getopts will recognize; if a letter is followed by a colon, the option is expected to have an argument which should be separated from it by white space.

Each time it is invoked, getopts will place the next option in the shell variable name and the index of the next argument to be processed in the shell variable OPTIND. Whenever the shell or a shell procedure is invoked, OPTIND is initialized to 1.

When an option requires an option-argument, getopts places it in the shell variable OPTARG.

If an illegal option is encountered, ? will be placed in name.

When the end of the options is encountered, getopts exits with a non-zero exit status. The special option ``--'' may be used to delimit the end of the options.

By default, getopts parses the positional parameters. If extra arguments (arg ...) are given on the getopts command line, getopts will parse them instead.

So all new commands will adhere to the command syntax standard described in intro(1), they should use getopts(1) or getopt(3C) to parse positional parameters and check for options that are legal for that command (see WARNINGS, below).

EXAMPLE
The following fragment of a shell program shows how one might process the arguments for a command that can take the options a or b, as well as the option o, which requires an option-argument:

    while getopts abo: c
    do
    	case $c in
    	a|b)	FLAGS=$FLAGS$c;;
    	o)	OARG=$OPTARG;;
    	\?)	echo $USAGE 1>&2
    		exit 2;;
    	esac
    done
    shift OPTIND-1

This code will accept any of the following as equivalent:

    cmd -a -b -o "xxx z yy" file
    cmd -a -b -o "xxx z yy" -- file
    cmd -ab -o "xxx z yy" file
    cmd -ab -o "xxx z yy" -- file

SEE ALSO
intro(1), sh(1).
getopt(3C) in the Programmer's Reference Manual.
UNIX System V Release 3.0 Release Notes.

WARNING
Although the following command syntax rule (see intro(1)) relaxations are permitted under the current implementation, they should not be used because they may not be supported in future releases of the system. As in the EXAMPLE section above, a and b are options, and the option o requires an option-argument:

    cmd -aboxxx file	(Rule 5 violation: options with
    	option-arguments must not be grouped with other options)
    cmd -ab -oxxx file	(Rule 6 violation: there must be
    	white space after an option that takes an option-argument)

Changing the value of the shell variable OPTIND or parsing different sets of arguments may lead to unexpected results.

DIAGNOSTICS
getopts prints an error message on the standard error output when it encounters an option letter not included in optstring.