BABYL OPTIONS:
Version: 5
Labels:
Note:   This is the header of an rmail file.
Note:   If you are seeing it in rmail,
Note:    it means the file has no messages in it.

1,filed,,
Received: by ATHENA-PO-2.MIT.EDU (5.45/4.7) id AA13253; Fri, 2 Dec 88 11:50:47 EST
Received: by ATHENA.MIT.EDU (5.45/4.7) id AA29832; Fri, 2 Dec 88 11:49:43 EST
Received: from relay2.cs.net by RELAY.CS.NET id af27085; 2 Dec 88 3:29 EST
Received: from tektronix.tek.com by RELAY.CS.NET id aa11011; 2 Dec 88 3:16 EST
Received: by tektronix.TEK.COM (5.51/6.24)
	id AA13023; Thu, 1 Dec 88 23:30:29 PST
Received: by copper.MDP.TEK.COM (4.12/6.24)
	id AA16123; Thu, 1 Dec 88 23:29:41 pst
Date: Thu, 1 Dec 88 23:29:41 pst
From: Steve Summit <stevesu%copper.mdp.tek.com@RELAY.CS.NET>
Message-Id: <8812020729.AA16123@copper.MDP.TEK.COM>
To: jh@ATHENA.MIT.EDU

*** EOOH ***
Date: Thu, 1 Dec 88 23:29:41 pst
From: Steve Summit <stevesu%copper.mdp.tek.com@RELAY.CS.NET>
To: jh@ATHENA.MIT.EDU

Subject: Release notes for ksh

This is a list of changes that have been made since the June 29th '84,
version of ksh.  Several enhancements have been added. Certain
seldomly used features and some redundant features have been removed.
The rest are bug fixes.  Some of the code has been reorganized and
the in-line editing and history code has been put into a separate
library so that other programs can use it.  The code for BSD style
job control for S5R2 systems with the SXT driver has been included but
type-ahead does not work with it so it is not recommend that it be
used unless the driver is modified.  The default compilation
excludes this code.

1.	The PS1 parameter is now expanded for parameter substitution before
	printing it.

2.	The substring built-in has been removed.  The # and % operators
	of parameter substitution should be used instead.  Two new
	operators, ## and %% are added to parameter substituion to delete
	the largest matching substring on the left and right.  For
	compatability, a substring function has been included. 

3.	There is a new option, markdirs, which when enabled with set -o
	causes any generated file names which are names of directories,
	to have a / added to the end.

4.	The R, X, S, t and T options have been added to the vi edit mode.
	The <ESC>_ command in the vi has been modified to allow a number
	which is the word number of the previous command.  Deleted text
	is now remembered from command to command.  The * command of emacs
	and vi will not add a trailing * if there are file name generation
	characters already in the word.

5.	A test -L pathname has been added which returns true if pathname
	is a symbolic link.  On systems without symbolic lines, the
	result is always false.

6.	All readonly variables cannot be unset.  PWD is now readonly.

7.	The history file now puts a magic number in the beginning.  Old style
	ksh history files are recognized and read correctly, and esh
	history files are recognized and removed.  The history file is no longer
	limited to 32,000 bytes.

8.	All functions are now saved in the history file and can therefore be
	listed with the typeset -f built-in.  A built-in alias functions has
	been added whose value is 'typeset -f'.

9.	The code size has been reduced slightly make it easier to port to 16-bit
	machines.  The makefiles now automatically configure for PC/IX.
	There is a version of the stdio package that can be used with ksh
	to further reduce the code size.

10.	The shell no longer forks when invoked as 'sh -c simple-command'.  This
	should improve performance.

11.	Arrays can now contain up to 512 elements.  This is increased from
	128 in the previous version.

12.	A new option, -s, has been added to the set builtin to sort the
	positional parameters $*.

13.	The ignoreeof option can now be set for non-login shells.

14.	Traps can now be set by name as well as by number.  An action of -
	clears traps.  A new trap, ERR, can be set which gets invoked when
	a command has a non-zero exit value.

15.	If the value of an alias ends in white-space, then the word after
	the alias will also be checked for alias susbstitution.  A builtin
	alias nohup='nohup ' has been added.

16.	The IFS parameter is now used whenever a command argument requires
	command or parameter substituion or when processing a read.  This
	should improve compatibility with the Bourne shell.

17.	If the last argument of set is -- then $* is cleared and $# is set
	to zero.  Previously there was no way to clear an argument list.

18.	Errors with shell built-ins no longer cause the script to terminate
	they just cause the command to terminate with a non-zero exit code.
	
19.	Bugs fixed in all versions.
	a.	Here documents inside command substitution removed the
		/tmp file too soon and didn't work.  This  worked correctly
		in previous editions and has been fixed again.

	b.	The trap on 0 was not executed after functions aborted
		due to an interrupt.  This has been fixed so that the
		trap is always executed after a function completes.

	c.	A line of the form echo anything;kill $$ from within a
		script, caused the shell script to die before the output
		is flushed.  The buffer will always be flushed correctly.

	d.	A command substituion of the form `cd xxx` no longer has
		the side effect of changing the users working directory.
		Command substitution of most built-ins is performed in
		the current shell environment.

	e.	echo ${x[*]};echo ${x[9]};echo ${x[*]} produced a bad
		subscript error.  This has been fixed.

	f.	The rsh option now starts correctly and the ENV file now
		executes without restriction for a restricted shell.

	g.	Several corrections have been made to the manual page.

	h.	The ignoreeof option was fixed to work correctly with login
		shells that were invoked as -sh -c command.

	i.	Directories can no longer be executed as . scripts.

	j.	Whence -v with no arguments no produces an error message
		rather than referencing address zero.

	k.	The cd -  built-in no longer references address zero when
		the OLDPWD variable is not defined.

	l.	The shell variable $0 now correctly reports the name of the
		function within the scope of a function.

	m.	A bug in the shell which caused it to abort when very long
		lines in the history file were scanned while in an edit
		mode has been fixed.

	n.	If a while loop had standard input redirected and a built-in
		command re-directed standard input from within the command, then
		the standard input of the while loop got incorrectly positioned.
		This problem has been fixed.

	o.	A simple command entered as, < file command, now checks
		command to see if it is an alias.

	p.	The set -e option now exits if built-ins return non-zero
		exit codes.

	q.	Subscripted variables did not work correctly when used as
		subscripts.  Now, any valid arithmetic expression should work
		as a subscript.

	r.	Commands that fail in the vi edit mode now reset the repeat
		count.

	s.	A trap on exit no longer effects the exit value of a shell
		procedure.

	t.	The -t option of the shell will now cause commands of the form
		command1;command2 to terminate after command1.

	u.	The values of $0 and $* are now set correctly when executing
		sh -c 'command' args ...

	v.	Information about quoting was being discarded when evaluation
		command substitution so that the command echo `echo \$x` would
		print out the value of x rather than $x.  This has been fixed.
		This bug is also in the Bourne shell.

	w.	Comment lines ending in \ no longer cause line continuation

	x.	Processes started as process |& are now listed with the jobs
		command.

	y.	A built-in command or a null command with the same file
		descriptor redirected too many times ( >x >y >x ... ) caused
		the shell to run out of file descriptors and abort.  This has
		been fixed.

	z.	Here documents of the form cat <<!
		${x-"abc"}
		!
		left the 8th bin turned on for abc if x wasn't set.  This was
		also a bug in the Bourne shell.

20.	Bug fixes BSD only.

	a.	On BSD 4.2, a command substitution of the form x=`eval /bin/date`
		did not work correctly.

	b.	The ulimit command now correctly handles the -t option.  The
		previous version treated time as measured in milliseconds as
		was incorrectly described in the BSD 4.2 manual.

	c.	The cd built-in now keeps present working directory correctly
		when crossing symbolic link boundaries.

	d.	Buffering when standard input is a pipe did not work correctly
		because of an incorrect return value in lseek.  The problem
		has been worked around.

	e.	Added 4.2 BSD signals are now handled by the shell.

1,,
Received: by ATHENA-PO-2.MIT.EDU (5.45/4.7) id AA13262; Fri, 2 Dec 88 11:51:16 EST
Received: by ATHENA.MIT.EDU (5.45/4.7) id AA29848; Fri, 2 Dec 88 11:51:01 EST
Received: from relay2.cs.net by RELAY.CS.NET id ah27085; 2 Dec 88 3:29 EST
Received: from tektronix.tek.com by RELAY.CS.NET id aa11022; 2 Dec 88 3:17 EST
Received: by tektronix.TEK.COM (5.51/6.24)
	id AA12949; Thu, 1 Dec 88 23:29:09 PST
Received: by copper.MDP.TEK.COM (4.12/6.24)
	id AA16112; Thu, 1 Dec 88 23:28:23 pst
Date: Thu, 1 Dec 88 23:28:23 pst
From: Steve Summit <stevesu%copper.mdp.tek.com@RELAY.CS.NET>
Message-Id: <8812020728.AA16112@copper.MDP.TEK.COM>
To: jh@ATHENA.MIT.EDU

*** EOOH ***
Date: Thu, 1 Dec 88 23:28:23 pst
From: Steve Summit <stevesu%copper.mdp.tek.com@RELAY.CS.NET>
To: jh@ATHENA.MIT.EDU

Subject: README file for ksh

To generate the binary, just type make in the top level directory.
The binary is the file named sh/ksh which can be copied.  Ksh has
been sucessfully installed as /bin/sh on several machines.
The file sh.doc contains the troff (man) description of this Shell.
The file sh.memo contains an old troff (mm) memo describing ksh.  The
file RELEASE contains the changes made in this release since the previous one.
The module sh/msg.c has a list of about twenty-five default aliases compiled in.
The number of builtin aliases is in sh/builtins.h.
Alias names starting with / become tracked aliases.

The file sh/timeout.h contains a parameter TIMEOUT which can be set to
the number of seconds that the shell will wait for input before it
warns you and exits.  The default is 0 which never times out.  You can
set TIMEOUT to shorten the timeout interval but not to increase it.

This version contains one line edit modes for vi and/or emacs.
The emacs version was given to me by Mike Veach at IH.
The vi version was given to me by Pat Sullivan at CB.
If the EDITOR or VISUAL variable ends in macs or vi, the corresponding
Edit mode is selected.  The set -o(+o) can be used to turn editing
modes on(off).  A library of the code for the editing and history features
can be generated with the file sh/makelib.  This library can be added to
programs to enable emacs or vi mode input.  The library generated with
makelib is called libedit.a.

This version removes the archaic definition of ^ as a synonym for pipe.
If it is essential to preserve the old meaning then see the file
sh/ctype.c for instructions. The scripts /usr/src/:mkcmd and /etc/shutdown
use the ^ as a synonym for | and must be changed if you install this
shell as /bin/sh.

It is advisable that you put the line PWD=$HOME;export PWD into the
/etc/profile file to reduce initialization time for ksh.  If you
install this shell as /bin/sh you may have to make /bin/pwd a setuid
program to handle cases where setuid programs started by /etc/cron
change directories and then invoke shell procedures.

The makefiles should automatically configure themselves for System V,
System III, BSD 4.1, BSD 4.1c,  and BSD 4.2.  The makefiles currently know
about the 3B's, IBM maxi-UNIX, uts, and pdp11 class machines including PC/IX.

Good luck!!

1,,
Received: by ATHENA-PO-2.MIT.EDU (5.45/4.7) id AA13275; Fri, 2 Dec 88 11:51:48 EST
Received: by ATHENA.MIT.EDU (5.45/4.7) id AA29858; Fri, 2 Dec 88 11:51:34 EST
Received: from relay2.cs.net by RELAY.CS.NET id ai27085; 2 Dec 88 3:29 EST
Received: from tektronix.tek.com by RELAY.CS.NET id aa11024; 2 Dec 88 3:17 EST
Received: by tektronix.TEK.COM (5.51/6.24)
	id AA12960; Thu, 1 Dec 88 23:29:41 PST
Received: by copper.MDP.TEK.COM (4.12/6.24)
	id AA16117; Thu, 1 Dec 88 23:28:56 pst
Date: Thu, 1 Dec 88 23:28:56 pst
From: Steve Summit <stevesu%copper.mdp.tek.com@RELAY.CS.NET>
Message-Id: <8812020728.AA16117@copper.MDP.TEK.COM>
To: jh@ATHENA.MIT.EDU

*** EOOH ***
Date: Thu, 1 Dec 88 23:28:56 pst
From: Steve Summit <stevesu%copper.mdp.tek.com@RELAY.CS.NET>
To: jh@ATHENA.MIT.EDU

Subject: Compatibility notes for ksh


			KSH VS. SH




I have not made a complete comparison between 5.2 /bin/sh and ksh.  A
direct comparison of the manuals may uncover more incompatibilities than
I have listed here.  In addition, I have omitted some incompatibilities
that are bugs in 5.0 sh that may have been fixed for 5.2.  I have
also omitted incompatibilities in cases that sh clearly is incorrect,
such as in cases where it core dumps. I have also omitted cases which
are bugs in ksh.  I have omitted built-ins in ksh which are not in /bin/sh
since this can be circumvented by using the alias facility if necessary.

The following is a list of known incompatibilities between ksh and sh:

1.	The IFS parameter is only effective for the read built-in and
	after parameter and command substitution in ksh.  Thus, IFS=x;
	exit will execute e on the file it with sh but will exit with ksh.

2.	If an environment parameter is modified by ksh, the new value
	will be passed to the children.  In sh you must export the
	parameter for this to happen.

3.	Time is a reserved word in ksh.  Thus time a | b will time the
	pipeline in ksh while only a will be timed with sh.  You can
	also time built-in commands and functions with ksh, you can't
	with sh.

4.	Select and function are reserved words in ksh.

5.	Parameter assignments only have scope for the command or function
	they precede in ksh.  In sh, built-in commands and functions
	treat parameter assignments as globals.  (Notice that 5.0 and
	5.2 treat parameter assignments to pwd and echo in an
	incompatible way).

6.	The output of some built-in commands and error messages is different
	in a few cases, for example times produces two lines of output in ksh.

7.	While loops with redirection are not executed in a separate process
	in ksh so assignments made within loops remain in effect after the
	loop completes.

8.	The semantics of functions are somewhat different.  Ksh can have
	local variables and allow recursive functions.  Errors in functions
	abort the function but not the script that they are in.

9.	The name space for functions and variables is separate in ksh.  In
	/bin/sh they share the same space. The unset builtin requires
	a -f flag to unset a function in ksh.

10.	Words that begin with ~ may be expanded in ksh. Sh does not have
	this feature.

11.	The character ^ is not special in ksh.  In sh it is an archaic
	synonym for |.

12.	Whenever (( occurs where a command name is valid, ksh assumes
	that an arithmetic expression follows.  In sh this means a
	sub-shell inside a sub-shell.

13.	Non-blank contiguous IFS delimiters generate a null input argument.
	Therefore, you can use IFS=: and correctly read the /etc/profile
	file even when fields are omitted.  In sh, multiple delimiters
	count as a single delimiter.

14.	Arithmetic test comparison operators (-eq, -lt, ...) allow any
	arithmetic expressions.  Sh allows only constants.  If you say
	test x -eq 0 in sh, which is meaningless, it returns true, but
	in ksh it depends on the value of the variable x.  If there
	is no variable x, then ksh produces an error message.

15.	The environment handed down to a program is not sorted in ksh.
	A user should not reply in this quirk of sh since any user
	program can provide an environment list which does not have
	to be sorted. (Getenv(3) does not assume a sorted list).

16.	There is an alias hash in ksh which does what the 5.2 has
	built-in does except for the -r flag.  In ksh, you must say
	PATH=$PATH to achieve the same result.

17.	The expansion of "$@" with no arguments produces the null string
	in the Bourne shell and produces nothing with ksh when there are
	no arguments.  I am not sure whether this is a bug in the Bourne
	shell or intentional.  The manual page leads me to think that it
	is a bug.  Set -- with no arguments unsets the positional parameter
	list in ksh.  Thus, scripts that use set -- "$@" when there are
	so positional parameters will not break.


1,,
Received: by ATHENA-PO-2.MIT.EDU (5.45/4.7) id AA13482; Fri, 2 Dec 88 12:05:48 EST
Received: by ATHENA.MIT.EDU (5.45/4.7) id AA00174; Fri, 2 Dec 88 12:05:23 EST
Received: from relay2.cs.net by RELAY.CS.NET id aa28828; 2 Dec 88 7:25 EST
Received: from tektronix.tek.com by RELAY.CS.NET id aa12374; 2 Dec 88 7:11 EST
Received: by tektronix.TEK.COM (5.51/6.24)
	id AA12940; Thu, 1 Dec 88 23:28:40 PST
Received: by copper.MDP.TEK.COM (4.12/6.24)
	id AA16106; Thu, 1 Dec 88 23:27:36 pst
Date: Thu, 1 Dec 88 23:27:36 pst
From: Steve Summit <stevesu%copper.mdp.tek.com@RELAY.CS.NET>
Message-Id: <8812020727.AA16106@copper.MDP.TEK.COM>
To: jh@ATHENA.MIT.EDU

*** EOOH ***
Date: Thu, 1 Dec 88 23:27:36 pst
From: Steve Summit <stevesu%copper.mdp.tek.com@RELAY.CS.NET>
To: jh@ATHENA.MIT.EDU


Here is a higher-level paper on ksh; format it with *roff -mm.
(Both of these documents are ca. 1986 and somewhat obsolete;
the Korn shell has evolved slightly since then.  Any differences
should be minor, though.  There may be significant new features,
unknown to me, not documented herein.  The more recent ksh offering
from AT&T is dubbed "ksh-i" because it is said to be able to support
international, i.e. 8-bit, character sets, the shell's internal use
of the parity bit for quoting purposes having been reworked.)

.nr N 2
.SA 1  \"  right justified
.TL \" ca "312510-0101" "49059-6"  \" charging case filing case 
Introduction to KSH ( Issue 2)
.AU "D. G. Korn" DGK \" ca MH 59545 7975 5D-112
.TM  \" ca 59545-840222-06  \"  technical memo + TM numbers
.AS 1   \" abstract start for TM 
Ksh is a command language (shell) for the UNIX*\ 
.FS  *
UNIX is a trademark of Bell Laboratories.
.FE
operating system.
It is essentially compatible with the System V version of the Bourne shell\*(Rf ,
.RS
S. R. Bourne,
.I "An Introduction to the UNIX
Shell,"
BSTJ - Vol. 57, No. 6 part 2, pages 1947-1972.
.RF
has many additional features,
such as those found in
.IR Csh\*(Rf ,
.RS
W. Joy,
.I "An Introduction to the C Shell,"
University of California, Berkeley, 1980.
.RF
and executes faster than either of these shells.
This memo introduces many of the additional features
and explains some of the reasons for the better
performance.
This memo
assumes that the reader is already familiar with the Bourne shell.
The Appendix contains
a sample script written in Ksh.
The manual page for the current version is also included.
.AE   \" abstract end
.OK Shell "Command interpreter" Language UNIX  \" keyword
.MT 4 \" ca 1  \"  memo type
.H 1 "Introduction"
.P
Over the past two or three years several papers
have been written describing new command interpreters
for the UNIX system.
These papers can be divided into two categories:
Those that improve the shell as a programming language,
and those that improve the shell as a command interpreter.
Most of the papers fall into the latter category.
In particular,
.IR vicmd \*(Rf
.RS
S. L. Arnold,
.I "Vicmd a Visual Shell for Video Terminals,"
TM-81-54533-12, 1981.
.RF
preserves the friendly environment of
.I vi
(from which this memo was entered),
and adds a facility for convenient command entry.
An
.I emacs
oriented shell
has also been written by Veach\*(Rf.
.RS
J. L. Steffen and
M. T. Veach,
.I "The Edit Shell - Connecting Screen Editing with the History List,"
USENIX Association Toronto Proceedings, 1983.
.RF
The
.IR 2dsh \*(Rf
.RS
M. J. Rochkind,
.I "2dsh - An Experimental Shell for Connecting Processes With Multiple Data Streams,"
TM-80-9323-3.
.RF
shell allows the setup of more complicated networks of processes than
just pipelines.
The
.IR See-shell \*(Rf
.RS
Wayne T. Wilner,
.I "See-Shell: a Graphical User-Interface for UNIX Systems,"
Bell Laboratories internal memorandum, 1982.
.RF
proposes a Small-Talk like interface\*(Rf
.RS
D. C. Smith, C. Irby, R. Kimball, and B. Verplank,
.I "Designing the Star User Interface,"
BYTE, April, 1982, pp. 242-282.
.RF
suitable for bit-mapped terminals such as the BLIT\*(Rf
.RS
R. Pike,
.I "The Blit Programmer's manual,"
Bell Labs, 1982.
.RF
and the Apollo\*(Rf.
.RS
P. J. Leach,
P. H. Levine,
B. P. Douros,
J. A. Hamilton,
D. L. Nelson,
and B. L. Stumfp,
.I "The Architecture of an Integrated Local Network,"
IEEE Journal of Selected Areas in Communications,
Local Area Networks Special Issue, November 1983.
.RF
Perhaps the most widely used shell,
other than the Bourne shell, is
.IR Csh ,
which runs under the Berkeley UNIX operating system.
Csh has many attractive command interpreter features
not currently in the Bourne shell;
most notably, job control,
history,
arithmetic,
and command name aliasing.
On the other hand,
many people (including this author),
think that the Bourne shell is superior as a programming language.
The history mechanism of Csh has recently been added as a local modification
to the Bourne shell by
J. L. Steffen\*(Rf.
.RS
J. L. Steffen,
.I "An Input History for the Bourne Shell,"
TM-82-55426-3, 1982.
.RF
.P
The use of the shell as a programming language has been
described by Dolotta and Mashey\*(Rf
.RS
T. A. Dolotta and J. R. Mashey,
.I "Using the shell as a Primary Programming Tool,"
Proc. 2nd. Int. Conf. on Software Engineering, 1976,
pages 169-176.
.RF
and has been used by many people here
at Bell Laboratories.
Kolettis\*(Rf
.RS
N. J. Kolettis.
.I "Extended Shell - A Potential Real Time Interpreter,"
TM-77-4145-01, 1977.
.RF
presented extensions to the Bourne shell to provide
message passing facilities and other inter-process communication and
synchronization features.
The Form shell\*(Rf
.RS
D. G. Korn and D. A. Lambeth,
.I "Form Shell,"
TM-80-9224-3, 1980.
.RF
added form entry/edit capabilities to the Bourne shell.
A proposal for a more programming language oriented shell has
been proposed by Sturzenbecker\*(Rf.
.RS
M. C. Sturzenbecker,
.I "A New Command Language for UNIX and related systems,"
TM-82-45192-3.
.RF
.P
This memo describes
.IR Ksh ,
aka the Korn-shell.
This memo is not a tutorial, only an introduction.
Ksh is a direct descendant of the
Form shell
with most of the
form entry/edit features removed and with many new
features added.
The primary focus of this work has been to provide an
enhanced programming environment in addition
to the major command entry features of
.IR Csh .
Improved performance has been a major objective.
Many of the additions have been provided so that
medium sized programming tasks can be written
at the shell level without a serious performance
penalty.
A concerted effort has been made to achieve System V Bourne shell
compatibility so that scripts written for the Bourne shell
can run without modification with Ksh.
The description of features in this memo assumes
that the reader is already familiar with the Bourne shell.
.P
A version of Ksh has been run on several machines
including a VAX 11/780, VAX 11/750, PDP-11/70, PDP-11/23, IBM-370,
AT&T 3B20, and Apollo Domain.
It has been run on top of several versions of the UNIX operating system including
System III, System V,
BSD 4.1, BSD 4.1c, 4.2, and Apollo UNIX.
The shell is in use in several centers at AT&T Bell Laboratories,
and has been installed as
.I /bin/sh
on  VAXEN running System V, BSD 4.1., BSD 4.2
and on a 3B-20 running System V.
.H 1 "Shell Variables"
.P
The ability to define and use variables to store and retrieve values
is an important feature in most programming languages.
Ksh has variables with
.I identifiers
that follow the
same rules as the Bourne shell.
Since all variables have string representations,
there is no need to specify the
.I type
of each variable in the shell.
In Ksh,
each variable can have one or more
.I attributes
that control the internal representation of the variable,
the way the variable is printed, and its access or
scope.
Two of the attributes,
.I readonly
and
.IR export ,
are available in the Bourne shell.
The
.B typeset
built-in command of Ksh
assigns attributes to variables.
The list of attributes appears in the manual page.
The
.B unset
built-in of the Ksh removes
values and attributes of parameters.
.P
Whenever a value is assigned to a variable,
the value is transformed according to the attributes of the variable.
Changing the attribute of a variable can change its value.
There are three attributes for field justification,
as might be needed for formatting a report.
For each of these attributes,
the first time an assignment is made to the variable its
size is remembered.
Each assignment causes justification of the field, truncating
if necessary.
Assignment to fixed sized variables
provides a simple way to generate a substring consisting of
a fixed number of characters from
the beginning or end of a string.
.P
The attributes
.B \-u
and
.BR \-l ,
are used for upper case and lower case
formatting respectively.
Since it makes no sense to have both attributes on simultaneously,
turning on either of these attributes turns the other off.
The following script provides an example of the use of shell variables
with attributes.
This script reads a file of lines each consisting of five fields separated by
.B :
and prints fields 4 and 2 in upper case in columns 1-15, left justified,
and columns 20-25 right-justified respectively.
.sp
.nf
.in .5i
.ta 3.4i
.B
typeset \-Lu f4=123456789012345	# 15 character left justified
typeset \-Ru f2=123456       	# 6 character right justified
IFS=:
set \-f                     	# skip file name generation
while   read \-r f1 f2 f3 f4 f5	# read line, split into fields
do      print \-r "$f4    $f2" 	# print fields 4 and 2
done
.fi
.ta
.in
.sp
.R
.P
The integer attribute,
.BR \-i ,
causes the variable to be internally represented as an integer.
The first assignment to an
.I integer
variable determines the output
.I base
(see below).
This base will be used
whenever the variable is printed.
Assignment to
.I integer
typed variables result in arithmetic evaluation,
as described below,
of the right hand side.
.P
Ksh allows one-dimensional
.I arrays
in addition to simple variables.
Any variable can become an array
by referring to it with
a
.IR subscript .
All elements of an array need not exist.
Subscripts for arrays
must evaluate to an
integer between 0 and 127, otherwise
an error results.
Evaluation of subscripts is described in
the next section.
Attributes apply to the whole array.
.P
Assignments to array variables are made with the
.B typeset
built-in.
Referencing of subscripted variables requires the character
.BR $ ,
but also requires braces around the array element name.
The braces are needed to avoid conflicts with the
file name generation mechanism.
The form of any array element reference is:
.ce
.BI ${ name [ subscript ]} .
A subscript value of
.B *
or
.B @
can be used to generate all elements of an array,
as they are used for expansion of positional parameters.
.P
A few additional operations are available on shell variables.
\f3${#\fP\f2name\fP\f3}\fP
will be then length in bytes of
\f3$\fP\f2name\fP.
For an array variable
\f3${#\fP\f2name\fP\f3[*]}\fP
gives the number of elements in the array.
.P
There are two parameter substitution modifiers that
have been added to strip off leading and trailing substrings
during parameter substitution.
The modifier
.B #
strips off from the left
and the modifier
.B %
strips off from the right.
For example, if the shell variable
.B i
has value
.BR file.c ,
then the expression
.B ${i%.c}
has value
.BR file .
The
.B substring
built-in has been added to Ksh
to enable the user to generate a substring of a given string.
The built-in allows the user to specify an
expression to be deleted from the left and right ends of the string.
The resulting substring is printed out.
Command substitution can be used to assign the output of
.B substring
to a shell variable.
.H 1 "Arithmetic Evaluation"
.P
The built-in command,
.B let ,
provides the ability to do integer arithmetic.
All arithmetic evaluations are performed using
.I long
arithmetic.
Arithmetic constants are written as
.br
.ce
.IB base # number
where
.I base
is a decimal integer between
two and thirty-six and
.I number
is any positive integer.
Base ten is used
if no base is specified.
.P
Arithmetic expressions are made from constants,
variables, and one or more of the fourteen operators
listed in the manual page.
Operators are evaluated in order of precedence.
Parentheses may be used for grouping.
A variable does not have to have an integer attribute
to be used within an arithmetic expression.
The name of the variable is replaced
by its value within an arithmetic
expression.
The statement
.ce
.B
let x=x+1
.R
can be used to
increment a variable
.BR x .
Note that there is no space before or after the operators
.B +
and
.BR = .
This is because each argument to
.B let
is an expression to evaluate.
The last expression determines
the value returned by
.BR let .
If the last expression evaluates to a non-zero value,
the let returns true.
Otherwise,
.B let
returns false.
.P
Note that many of the arithmetic operators have special meaning
to the shell and must be quoted.
Since this can be burdensome, an alternate form of arithmetic
evaluation syntax has been provided.
For any command that begins with
.BR (( ,
all the characters until the matching
.B ))
are treated as a quoted arithmetic expression.
The double parentheses 
usually avoids incompatibility with the Bourne shell's use of
parentheses for grouping a set of commands
to be run in a sub-shell.
Expressions inside double parentheses can contain blanks
and special characters without quoting.
More precisely,
.ce
.B
(( ... ))
.R
is equivalent to
.ce
.B
let " ... "
.R
.P
The following script prints the first
.I n
lines of its standard input onto its standard output,
where
.I n
is supplied as an argument or is 20 if omitted.
.B
.sp
.nf
.in .5i
.ta 4i
typeset \-i n=${1-20}                    	# set n
while   read \-r line && (( (n=n\-1)>=0 ))	# at most n lines
do      print \-r \- "$line"			
done
.fi
.ta
.in
.sp
.R
.H 1 "Functions and Command Aliasing"
.P
Two new mechanisms have been provided for creating
pseudo-commands, i. e.,
things that look like commands,
but do not always create a process.
The first technique is called command name
.IR aliasing .
.P
As a command is being read,
the command name is checked against a list of
.I alias
names.
If it is found,
the name is replaced by the text associated with the
.I alias
and then rescanned.
The text of an alias is not checked for aliases
so recursive definitions are not allowed.
.P
Aliases are defined with the
.B alias
built-in.
The form of an
.B alias
command is:
.ce
.BI "alias " name = value
Except for the first character,
which must be printable, the
.I alias
name must be a valid
.IR identifier .
The replacement text,
.I value,
can contain any valid shell script,
including metacharacters such as pipe symbols and i/o-redirection.
Aliases can be used to redefine built-in commands so that
the alias
.ce
.B "alias test=./test"
can be used to look for
.I test
in your current working directory rather than
using the built-in
.B test
command.
Keywords such as
.I for
and
.I while
cannot be changed by aliasing.
The command
.BR alias ,
without arguments, generates
a list of aliases and corresponding texts.
The
.B unalias
command removes the name and text of an alias.
.P
Aliases are used to save typing and to improve readability of scripts.
For example, the alias
.B
alias integer=\(fmtypeset \-i\(fm
.R
allows integer the variables
.B i
and
.B j
to be declared and initialized with the command
.BR "integer i=0 j=1" .
.P
One frequent use of aliases is to alias a command name to the
full path-name of the program.  This eliminates the path search
but requires knowledge of where that program will be stored.
To reduce the amount of path searching,
.I tracked
aliases have been introduced.
A tracked alias is not given a value.  Its value is
defined at the first
reference by a path-search as the full path-name equivalent of the name, and
remains defined
until the
.B PATH
variable is changed.
Programs found in directories that do not begin with
.B /
that occur earlier in the path-search than the value of the
tracked alias, take precedence over tracked aliases.
.P
Tracked aliases provide an alternative to the
.I Csh
command hashing facility. Tracked aliases do not require time for
initialization and allow for new commands to be introduced
without the need for re-hashing.
An option to the shell allows all command names that are
valid alias names to become tracked aliases.
.P
.I Functions
are more general than aliases but also more costly.
Functions definitions are of the form
.sp
.in +.5i
.BI "function " name
.br
.B {
.br
	any shell script
.br
.B }
.sp
.in
The function is invoked by writing
.I name
and optionally following it with arguments.
Positional parameters are saved before each
function call and restored when completed.
Functions are executed in the current shell environment
and can share named variables with the calling program.
The
.B return
built-in can be used to cause the function to return to
the statement following
the point of invocation.
.P
By default, variables are inherited by the function and shared
by the calling program.
However,
environment substitutions preceding the function call
apply only to the scope of the function call.
Also, variables defined with the
.BR typeset ,
built-in command are local to the function that they are declared in.
Thus, for the function defined
.B
.sp
.nf
.in .5i
function  name
{
     typeset \-i x=10
     let z=x+y
     print $z
}
.fi
.ta
.in
.sp
.R
invoked as
.BR "y=13 name" ,
.B x
and
.B y
are local variables with respect to the function
.B name
while
.B z
is global.
.P
Alias and function names are never directly carried across separate
invocations of Ksh, but can be passed down to sub-shells.
The
.B \-x
flag is used with
.B alias
to carry aliases to sub-shells while the
.B \-fx
flags of
.B typeset
are used to do the same for functions.
.P
Several of the UNIX commands can be aliased to Ksh built-ins.
Some of these are automatically set each time the shell is invoked.
In addition,
about twenty frequently used UNIX commands are set as tracked aliases.
Each user can create a file for aliases and functions.
Aliases and functions that are to be available for all shell invocations
should be put into the Ksh startup file.
By setting and exporting the environment variable,
.BR ENV ,
to the name of this file,
the aliases and functions will be defined each time Ksh
is invoked.
The value of the
.B ENV
variable undergoes macro and command substitution prior to its use.
Since the
.B ENV
file is not invoked automatically for a log in shell,
it is usually necessary to include the line
.B "eval . $ENV"
in your
.I .profile
file.
.H 1 "Input and Output"
.P
An extended I/O capability has been added
to enhance the
use of the shell as a programming language.
The Bourne shell has a built-in
.B read
for reading lines from file descriptor 0,
but does not have any internal output mechanism.
As a result, the
.B echo(1)
command has been used to produce output for a shell procedure.
This is inefficient and also restrictive.
For example, there is no way to read in a line
from a terminal and to
.I echo
the line exactly as is.
In the Bourne shell,
the
.B read
built-in cannot be used to read lines that end in
.BR `\e'  ,
and the
.B echo
command will treat certain sequences as control sequences.
In addition,
there is no way to have more than one file open
at any time for reading.
.P
Ksh has options on the
.B read
command to specify the file
descriptor for the input.
The
.B exec
built-in can be used to open and close file streams.
The
.B \-r
option allows a
.B `\e'
at the end of an input line to be treated as a regular
character rather than the line continuation character.
The first argument of the
.B read
command can be followed by a
.B ?
and a prompt to produce a prompt at the
terminal before the read.
If the input is not from a terminal device then
the prompt is not issued.
.P
The Ksh built-in,
.BR print ,
is used to output characters to the terminal or to a file.
Again, it is possible to specify the file descriptor number
as an option to the command.
Ordinarily, the arguments to this command are processed
the same as for
.BR echo(1) .
However, the
.B \-r
flag can be used to output the arguments without any special meaning.
The
.B \-n
flag can be used here to suppress the trailing new-line
that is ordinarily appended.
.P
To improve performance of existing shell programs,
an alias for
.B echo
is defined by the shell when it is invoked.
For the AT&T-UNIX version, the alias is
.ce
\fBalias echo=\(fmprint \-\(fm\fR,
where the
.B \-
signifies that there are no more options,
while for the Berkeley UNIX version
.ce
\fBalias echo=\(fmprint \-R\(fm\fR,
where the
.B \-R
option allows only the
.B \-n
flag to be recognized as the next argument,
gives the desired result.
.P
The shell is frequently used as a programming language for
interactive dialogues.
The
.B select
statement has been added to the language
to make it easier to
present menu selection alternatives to the
user and evaluate the reply.
The list of alternatives is numbered and put in columns.
A user settable prompt,
.BR PS3 ,
is issued and if the answer is
a number corresponding to one of the alternatives,
the select loop variable is set to this value.
In any case, the
.B REPLY
variable is used to store the user entered reply.
.H 1 "Command Re-entry"
.P
An interactive shell saves the
commands you type at a terminal in a file.
If the variable
.B HISTFILE
is set to the name of a file to which the user
has write access,
then the commands are stored in this
.I history
file.
Otherwise the file
.I $HOME/.history
is checked for write access and if this fails
an unnamed file is used to hold the history lines.
This file is truncated if this is a top level shell.
The number of commands accessible to the user, is determined by the value of the
.B HISTSIZE
variable at the time the shell is invoked.
The default value is 64.
A command may consist of one or more lines since a compound
command is considered one command.
If the character
.B !
is placed within the
.I "primary prompt"
string,
then it is replaced by the command number each time the prompt is given.
Whenever the file is named,
all shells which use this file share access to the same history.
.P
A built-in command
.B fc
(fix command) is used to list and/or edit
any of these saved commands.
The command can always be specified with
a range of one or more commands.
The range can be specified by giving the command
number, relative or absolute, or by giving
the first character or characters of the command.
The option
.B \-l
is used to specify listing of previous commands.
When given without specifying the range,
the last 16
commands are listed, each
preceded by the command number.
.P
If the listing option is not selected,
then the range of commands specified,
or the last command if no range is given,
is passed to an editor program before
being re-executed by Ksh.
The editor to be used may be specified
with the option
.B \-e
and following it with the editor name.
If this option is not specified, the
value of the shell variable
.B FCEDIT
is used as the name of the editor,
providing that this variable has non-null value.
If this variable is not set, or is null,
and the
.B \-e
option has not been selected,
then
.I /bin/ed
is used.
When editing has been complete,
the edited text automatically becomes
the input for Ksh.
As this text is read by Ksh, it is echoed onto the terminal.
.P
An editor name of
.B \-
is used to bypass the editing and just re-execute the command.
In this case only a single command can be specified as the range
and an optional argument of the form
\f2old\fP\f3=\fP\f2new\fP
may be added which requests a simple string substitution
prior to evaluation.
A convenient alias,
.ce
.B
alias r=\(fmfc \-e \-\(fm
.R
has been pre-defined so that
the single key-stroke
.B r
can be used to re-execute the previous command
and the key-stroke sequence,
.B
r abc=def c
.R
can be used to re-execute the last command that starts with
the letter
.B c
with the first occurrence of the string
.B abc
replaced with the string
.BR def .
Typing
.B
r c > file
.R
re-executes the most recent command starting with the letter
.BR c ,
with standard output redirected to
.IR file .
.H 1 "In-line editing"
.P
Lines typed from a terminal frequently need changes made
before entering them.
With the Bourne shell the only method to fix up commands
is by backspacing or killing the whole line.
Ksh offers options that allow the user to edit parts of the
current command line before submitting the command.
The in-line edit options make the command line into a single
line screen edit window.
When the command is longer than the width of the terminal,
only a portion of the command is visible.
Moving within the line automatically makes that portion visible.
Editing can be performed on this window until the
.I return
key is pressed.
The editing modes have commands that access the history file
in which previous commands are saved.
A user can copy any of the most recent
.B HISTSIZE
commands from this file into the input edit window.
You can locate commands by searching or by position.
.P
The in-line editing options do not use the
.I termcap
database.
They work on most standard terminals.
They only require that the backspace character moves the cursor left
and the space character overwrites the current character on the screen.
.P
There is a choice of editor options.
The
.IR emacs ,
.IR gmacs ,
or
.I vi
option is selected by turning on the
corresponding
option of the
.B set
command.
If the value of the
.B EDITOR
or
.B VISUAL
ends any of these suffixes
the corresponding options is turned on.
A large subset of each of each of these editors
features are available within the shell.  Additional
functions, such as file name completion, have also been added.
.P
The code for the
.I emacs
and
.I gmacs
editing option was supplied by Mike Veach.
In the
.I emacs
or
.I gmacs
mode the user positions the cursor to the point
needing correction and inserts, deletes, or replaces
characters as needed.
The only difference between these two modes is the
meaning of the command
.BR ^T .
Control keys and escape sequences are used for cursor
positioning and control functions.
The available editing functions are listed in the manual page.
.P
The code for the
.I vi
editing option was supplied by Pat Sullivan.
The
.I vi
editing mode
starts in insert mode and enters control mode when the
user types
.BR ESC (
.BR 033 ).
The
.I return
key, which submits the current command for processing,
can be entered from either mode.
The cursor can be anywhere on the line.
A subset of commonly used
.I vi
commands are available.
The
.B k
and
.B j
command that normally move up and down by one
.IR line ,
move up and down one
.I command
in the history file,
copying the command at into the input edit window.
For reasons of efficiency,
the terminal is kept in canonical mode until an
.B ESC
is typed.
On some terminals,
and on earlier versions of the UNIX operating system,
this doesn't work correctly.
The
.B viraw
option
of the
.B set
command, which always uses
.B raw
or
.B cbreak
mode,
must be used in this case.
.H 1 "Job Control"
.P
The job control mechanism 
is almost identical to the version found in
.I Csh
of the Berkeley UNIX operating system,
version 4.1.
The job control feature allows the user to stop and
restart programs, and to move programs to and from the
foreground and the background.
It will only work on systems that provide support for
these features.
However,
even systems without job control have a
.B monitor
option which when enabled will report the progress
of background jobs and enable the user to
.B kill
jobs by job number or job name.
.P
An interactive shell associates a
.I job
with each pipeline typed in from the terminal
and assigns them a small integer number
called the job number.
If the job is run asynchronously,
the job number is printed at the terminal.
At any given time, only one job owns the terminal,
i. e., keyboard signals are only sent to the processes in one job.
When Ksh creates a foreground job,
it gives it ownership of the terminal.
If you are running a job and wish to stop
it you hit the key
.B ^Z
(control-Z)
which sends a
.B STOP
signal to all processes in the current job.
The shell receives notification that the processes
have stopped and takes back control of the terminal.
.P
There are commands to continue programs in the foreground
and background.
There are several ways to refer to jobs.
The character
.B %
introduces a job name.
You can refer to jobs by name or number as described in the manual page.
The built-in command
.B bg
allows you to continue a job in the background,
while the built-in command
.B fg
allows you to continue a job in the foreground even
though you may have started it in the background.
.P
A job being run in the background will stop if it tries
to read from the terminal.
It is also possible to stop background jobs that try to write on
the terminal by setting the terminal options
appropriately.
.P
There is a built-in command
.B jobs
that lists the status of all running and stopped jobs.
In addition,
you are notified of the change of state of any background
jobs just before each prompt.
When you try to leave the shell while jobs are stopped or running,
you will receive a message from Ksh.
If you ignore this message and try to leave again,
all stopped processes will be terminated.
.P
A built-in version of
.B kill
makes it possible to use
.I job
numbers as targets for signals.
Signals can be selected by number or name.
The name of the signal is the name found in the
.I include
file
.I /usr/include/signal.h
with the prefix
.B SIG
removed.
The list of valid signal names can be generated with the
.B \-l
flag of
.BR kill .
.H 1 "Miscellaneous"
.P
Ksh has several additional features to enhance functionality and performance.
This section lists most of these features.
.H 2 "Tilde substitution"
The character
.B \(ap
at the beginning of a word has special meaning to Ksh.
If the characters after the
.B \(ap
up to a
.B /
match a user login name in the
.B /etc/passwd
file, then the
.B \(ap
and the name are replaced by
that user's login directory.
If no match is found, the original word
is unchanged.
A
.B \(ap
by itself, or in front of a
.BR / ,
is replaced by the value of the
.B HOME
parameter.
A
.B \(ap
followed by a
.B +
or
.B \-
is replaced by the value of
the parameter
.B PWD
and
.B OLDPWD
respectively.
Tilde substitution takes place when the script is read,
not while it is executed.
.H 2 "Built-in I/O Redirection"
.P
All built-in commands can be redirected.
.H 2 "Added options"
.P
Several options have been added to the shell and
all options have names
that can be used in place of flags for setting and resetting options.
The command
.B "set \-o"
will list the current option settings.
.P
The option,
.B \-f
or
.B noglob
is used to disable file name generation.
It can be applied at invocation or as an option to the
.B set
command.
.P
The option
.B ignoreeof
can be used in a top-level shell to prevent
.B ^D
from logging you out.
You must type
.B exit
to log out.
.P
The
.B \-h
or
.B trackall
option will cause all commands whose name is a valid alias
name to become a
.I tracked
alias.
.P
The job
.B monitor
option will cause a report to be printed when each
background job completes.
It is automatically enabled for systems that have
job control.
.P
If the
.B bgnice
option is set,
background jobs are run at a lower priority.
.H 2 "Previous Directory"
.P
Ksh remembers your last directory
in the variable
.BR OLDPWD .
The
.B cd
built-in can be given with argument
.B \-
to return to the previous directory.
Note that
.B "cd \-"
done twice returns you to the starting directory,
not the second previous directory.
A directory
.I stack
can be implemented with shell internals
by using an array and writing shell
.I functions
to
.I push
and
.I pop
directories from the stack.
.H 2 "Additional Variables and Parameters"
.P
Several new parameters have special meaning to Ksh.
The variable
.B PWD
is used to hold the current working directory of the shell.
The command,
.BR pwd ,
is aliased to
.B "print \- $PWD"
for better response.
The variable
.B OLDPWD
is used to hold the previous working directory of the shell.
.P
The variable
.B FCEDIT
is used by the
.B fc
built-in described above.
The variables
.B VISUAL
and
.B EDITOR
are used for determining the edit modes as described above.
.P
The variable
.B ENV
is used to define the startup file for non-login
Ksh invocations.
.P
The variables
.B HISTSIZE
and
.B HISTFILE
control the size and location of the file containing
commands entered at a terminal.
.P
The parameter
.B MAILPATH
is a colon (
.B :
) separated list of file names to be checked for changes
periodically. The user is notified
before the next prompt.
Each of the names in this list can be followed by a
.B ?
and a prompt to be given when a change has been detected in the file.
The prompt will be evaluated for macro and command substitution.
The parameter
.B MAILCHECK
is used to specify the minimal interval in seconds before
new mail is checked for.
.P
The variable
.B RANDOM
produces a random number each time it is referenced.
Assignment to this variable sets the seed for the
random number generator.
.P
The parameter
.B PPID
is used to generate the parent process id of the shell.
.P
The value of the parameter
.B _
is the last argument of the previous command.
.P
The parameter
.B TMOUT
can be set to be the number of seconds that the shell will wait for
input before terminating.
.P
The
.B COLS
variable can be used to adjust the width of the edit window for
the in-line edit modes.
.H 2 "Modified variables"
.P
The input field separator parameter,
.BR IFS ,
has meaning for the
.B read
built-in,
for the
.B set
built-in,
and while expanding
.B for
and
.B select
lists.
In all other instances it is ignored.
.H 2 "Timing Commands"
.P
A keyword
.B time
has been added to replace
the
.B time
command.
Any function, command or pipeline can be preceded by this keyword
to obtain information about the elapsed, user and system times.
Since I/O redirection bind to the command, not to
.BR time ,
parenthesis should be used to redirect the timing information which
is normally printed on file descriptor 2.
.H 2 "Command Substitution"
.P
The special command substitution of the form
\fB\(gacat file\(ga\fR
can be replaced by
\fB\(ga< file\(ga\fR,
which is faster because no separate process is created.
.H 2 "Whence"
.P
The addition of
.IR aliases ,
.IR functions ,
and more built-ins
has made it substantially more difficult to know what
a given command word really means.
A built-in command,
.B whence
when used with the
.B \-v
option has been provided to answer this question.
A line is printed for each argument to
.B whence
telling what would happen if this argument were used as a command name.
It reports on keywords, aliases, built-ins, and
functions.
If the command is none of the above,
it follows the path search rules and prints the full path-name,
if any, otherwise it prints an error message.
.H 2 "Additional test operators"
.P
The operators
.B \-ot
and
.B \-nt
can be used to compare the modification times
of two files to see which is file is
.I "older than"
or
.I "newer than"
the other.
The operator
.B \-ef
is used to see if two files
have the same device and i-node number,
i.\ e., a link to the same file.
.H 2 "Shell Accounting"
.P
There is a compile time option to the shell to generate
an accounting message for each shell script.
The changes needed to provide this feature were supplied
by Foregger\*(Rf
.RS
T. H. Foregger,
.I "Shell Accounting,"
Case 40094-21, July 1982.
.RF
and have been adopted as described in his memo.
.H 2 "Coded in Standard C"
.P
The Bourne shell is coded in an ALGOL-68 like dialect of C.
Ksh is coded in standard C.
.H 2 "No special meaning for ^"
The Bourne shell uses
.B ^
as an archaic synonym for
.B | .
The
.B ^
is not a special character to Ksh.
.H 1 "Example"
.P
An example of a Ksh script is included
in the Appendix.
This one page program is a variant of the UNIX
.I grep(1)
program.
Pattern matching for this version of
.I grep
means shell patterns consisting of
.BR ? ,
.BR * ,
and
.BR [] .
.P
The first half examines option flags.
Note that all options except
.B \-b
have been implemented.
The second half goes through each line of each file
to look for a pattern match.
.P
This program is not intended to serve as a
replacement for
.BR grep ;
just as an illustration of the programming power of Ksh.
Note that no auxiliary processes are spawned by this script.
It was written and debugged in under two hours.
While performance is acceptable for small programs,
this program runs at only one tenth
the speed of
.B grep
for large files.
.H 1 "Performance"
.P
Ksh executes many scripts faster than the System V Bourne shell.
One major reason is that many of the functions provided by
.I echo(1)
and
.I expr(1)
are built-in.
The time to execute a built-in function is one or two
orders of magnitude faster than performing a fork and
execute of the shell.
Command substitution of built-ins is performed without
creating another process, and often without even
creating a temporary file.
.P
Another reason for improved performance is that all I/O is buffered.
Output buffers are flushed only when required.
Several of the internal algorithms have been changed
so that the number of subroutine calls has been
substantially reduced.
Ksh uses hash tables for variables.
Scripts that rely heavily on referencing variables execute faster.
More processing is performed while reading the script
so that execution time is saved while running loops.
.P
Scripts that do little internal processing and create many processes
run a little slower because the time to
.I fork
Ksh is slower than for the Bourne shell.
.H 1  "Conclusion"
.P
More than one thousand people have tried Ksh and use it regularly.
Ksh is a suitable replacement for the Bourne shell.
It offers new features,
better performance,
and is essentially upward compatible with the Bourne shell.
Many of the known bugs of the Bourne shell
have been eliminated.
.SG dgk \"  signature typist initials 
.NS 0 \" start notation 
Members of Center 5954
Laboratory 4542 Supervision
D. A. Lambeth
N. J. Kolettis
W. P. Weber
E. G. Bradford
J. L. Steffen
S. L Arnold
M. C. Sturzenbecker
M. T. Veach
J. Krist
.NE  \" end notation
.bp
.ce
APPENDIX
.nf
	#
	#	SHELL VERSION OF GREP
	#
	vflag= xflag= cflag= lflag= nflag=
	set -f
	while	((1))			# look for grep options
	do	case	"$1" in
		\-v*)	vflag=1;;
		\-x*)	xflag=1;;
		\-c*)	cflag=1;;
		\-l*)	lflag=1;;
		\-n*)	nflag=1;;
		\-b*)	print \(fmb option not supported\(fm;;
		\-e*)	shift;expr="$1";;
		\-f*)	shift;expr=\*`< $1\*`;;
		\-*)	print $0: \(fmunknown flag\(fm;exit 2;;
		*)	if	test "$expr" = \(fm\|\(fm
			then	expr="$1";shift
			fi
			test "$xflag" |\|| expr="*${expr}*"
			break;;
		esac
		shift			# next argument
	done
	noprint=$vflag$cflag$lflag	# don't print if these flags set
	integer n=0 c=0 tc=0 nargs=$#	# initialize counters
	for i in "$@"			# go thru the files
	do	if	((nargs<=1))
		then	fname=\(fm\|\(fm
		else	fname="$i":
		fi
		test "$i"  &&  exec 0< $i	# open file if necessary
		while	read \-r line		# read in a line
		do	let n=n+1
			case	"$line" in
			$expr)			# line matches pattern
				if	test "$noprint" = ""
				then	print \-r "$fname${nflag:+$n:}$line"
				fi
				let c=c+1 ;;
			*)			# not a match
				if	test "$vflag"
				then	print \-r "$fname${nflag:+$n:}$line"
				fi;;
			esac
		done
		if	test "$lflag" && ((c))
		then	print \- $i
		fi
		let tc=tc+c n=0 c=0
	done
	test "$cflag" && print $tc	#  print count if cflag is set
	let tc				#  set the exit value
.fi
.SK
.CS 11 21 34 0 0 21  \" cover sheet for TM

1,,
Received: by ATHENA-PO-2.MIT.EDU (5.45/4.7) id AA13514; Fri, 2 Dec 88 12:07:30 EST
Received: by ATHENA.MIT.EDU (5.45/4.7) id AA00179; Fri, 2 Dec 88 12:05:57 EST
Received: from relay2.cs.net by RELAY.CS.NET id aa28839; 2 Dec 88 7:27 EST
Received: from tektronix.tek.com by RELAY.CS.NET id aa12380; 2 Dec 88 7:13 EST
Received: by tektronix.TEK.COM (5.51/6.24)
	id AA12889; Thu, 1 Dec 88 23:25:47 PST
Received: by copper.MDP.TEK.COM (4.12/6.24)
	id AA16100; Thu, 1 Dec 88 23:24:15 pst
Date: Thu, 1 Dec 88 23:24:15 pst
From: Steve Summit <stevesu%copper.mdp.tek.com@RELAY.CS.NET>
Message-Id: <8812020724.AA16100@copper.MDP.TEK.COM>
To: jh@ATHENA.MIT.EDU

*** EOOH ***
Date: Thu, 1 Dec 88 23:24:15 pst
From: Steve Summit <stevesu%copper.mdp.tek.com@RELAY.CS.NET>
To: jh@ATHENA.MIT.EDU


Are you still looking for ksh docs?  I realized it would be easier
to grab a fresh copy 3,000 miles away than look for it in the
basement.  THe following is a man page; format it with *roff -man.

.ds sH KSH
.ds Sh Ksh
.ds sh ksh
.ds OK [\|
.ds CK \|]
.ds ' \s+4\v@.3m@\'\v@-.3m@\s-4
.ds ` \s+4\v@.3m@\`\v@-.3m@\s-4
.de P
.br
..
.TH \*(sH 1 "2 February 1985"
.SH NAME
ksh \- the Korn shell, command programming language
.SH SYNOPSIS
.B ksh
[
.B \-acefhikmnorstuvx
] [
.B \-o
option ] .\|.\|.
[ arg  .\|.\|. ]
.SH DESCRIPTION
.I \*(Sh\^
is a command programming language
that executes commands read from a terminal
or a file.
It is largely (but not totally) upwardly compatible with the standard
.IR sh (1).
A restricted version of
.I \*(sh
is available;
it is used to set up login names and execution environments whose
capabilities are more controlled than those of the standard shell.
See
.I Invocation\^
below
for the meaning of arguments to the shell.
.SS Definitions.
A
.I metacharacter\^
is one of the following characters:
.RS
.PP
\f3;   &   (   )   \(bv   <   >   new-line   space   tab\fP
.RE
.PP
A
.I blank\^
is a
.B tab
or a
.BR space .
An
.I identifier\^
is a sequence of letters, digits, or underscores
starting with a letter or underscore.
Identifiers are used as names for
.IR aliases ,
.IR functions ,
and
.IR "named parameters" .
A
.I word\^
is a sequence of
.I characters\^
separated by one or more non-quoted
.IR metacharacters .
.SS Commands.
A
.I simple-command\^
is a sequence of
.I blank\^
separated words
which may be preceded by a parameter assignment list.
(See
.I Environment\^
below).
The first word specifies the name of the command to
be executed.
Except as specified below,
the remaining words are passed as arguments
to the invoked command.
The command name is passed as argument 0
(see
.IR exec (2)).
The
.I value\^
of a simple-command is its exit status
if it terminates normally, or (octal) 200+\f2status\^\fP if
it terminates abnormally (see
.IR signal (2)
for a list of
status values).
.PP
A
.I pipeline\^
is a sequence of one or more
.I commands\^
separated by
.BR \(bv .
The standard output of each command but the last
is connected by a
.IR pipe (2)
to the standard input of the next command.
Each command is run as a separate process;
the shell waits for the last command to terminate.
The exit status of a pipeline is the exit
status of the last command.
.PP
A
.I list\^
is a sequence of one or more
pipelines
separated by
.BR ; ,
.BR & ,
.BR && ,
or
.BR \(bv\|\(bv ,
and optionally terminated by
.BR ; ,
.BR & ,
or
.BR \(bv& .
Of these five symbols,
.BR ; ,
.BR & ,
and
.BR \(bv&
have equal precedence,
which is lower than that of
.B &&
and
.BR \(bv\|\(bv .
The symbols
.B &&
and
.B \(bv\|\(bv
also have equal precedence.
A semicolon
.RB ( ; )
causes sequential execution of the preceding pipeline; an ampersand
.RB ( & )
causes asynchronous execution of the preceding pipeline (i.e., the shell does
.I not\^
wait for that pipeline to finish).
The symbol
.B \(bv&
causes asynchronous execution of the preceding command or pipeline
with a two-way pipe established to the parent shell.
The standard input and output of the spawned command
can be written to and read from by the parent shell
using the
.B \-p
option of
the special commands
.B read
and
.B print\^
described later.
Only one such command can be active
at any given time.
The symbol
.B &&
.RB (\| \(bv\|\(bv \^)
causes the
.I list\^
following it to be executed only if the preceding
pipeline
returns a zero (non-zero) value.
An arbitrary number of new-lines may appear in a
.I list,\^
instead of semicolons,
to delimit commands.
.PP
A
.I command\^
is either a simple-command
or one of the following.
Unless otherwise stated,
the value returned by a command is that of the
last simple-command executed in the command.
.TP
\f3for\fP \f2identifier\^\fP \*(OK \f3in\fP \f2word\^\fP .\|.\|. \*(CK \f3do\fP \f2list\^\fP \f3done\fP
Each time a
.B for
command is executed,
.I identifier\^
is set to the next
.I word\^
taken from the
.B in
.I word\^
list.
If
.BI in " word\^"
\&.\|.\|.
is omitted, then
the
.B for
command executes the \f3do\fP \f2list\^\fP once for each positional parameter
that is set
(see
.I "Parameter Substitution\^"
below).
Execution ends when there are no more words in the list.
.TP
\f3select\fP \f2identifier\^\fP \*(OK \f3in\fP \f2word\^\fP .\|.\|. \*(CK \f3do\fP \f2list\^\fP \f3done\fP
A
.B select
command prints on standard error (file descriptor 2), the set of
.IR word s,
each preceded by a number.
If
.BI in " word\^"
\&.\|.\|.
is omitted, then
the
positional parameters
are used instead
(see
.I "Parameter Substitution\^"
below).
The
.SM
.B 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
.BR word s,
then the value of the parameter
.I identifier\^
is set to the
.I word\^
corresponding to this number.
If this line is empty the selection list is
printed again.
Otherwise the value of the parameter
.I identifier\^
is set to
.BR null .
The contents of the line read from standard input is
saved in
the parameter
.SM
.BR REPLY.
The
.I list\^
is executed for each selection until a
.B break\^
or
.I end-of-file\^
is encountered.
.TP
\f3case\fP \f2word\^\fP \f3in\fP \*(OK \f2pattern\^\fP \*(OK \(bv \
\f2pattern\^\fP \*(CK .\|.\|. \f3)\fP \f2list\^\fP \f3;;\fP \*(CK .\|.\|. \f3esac\fP
A
.B case
command executes the
.I list\^
associated with the first
.I pattern\^
that matches
.IR word .
The form of the patterns is
the same as that used for
file-name generation (see 
.I "File Name Generation\^"
below).
.TP
\f3if\fP \f2list\^\fP \f3then\fP \f2list\^\fP \*(OK \
\f3elif\fP \f2list\^\fP \f3then\fP \f2list\^\fP \*(CK .\|.\|. \
\*(OK \f3else\fP \f2list\^\fP \*(CK \f3f\&i\fP
The
.I list\^
following \f3if\fP is executed and,
if it
returns a zero exit status, the
.I list\^
following
the first
.B then
is executed.
Otherwise, the
.I list\^
following \f3elif\fP
is executed and, if its value is zero,
the
.I list\^
following
the next
.B then
is executed.
Failing that, the
.B else
.I list\^
is executed.
If no
.B else
.I list\^
or
.B then
.I list\^
is executed, then the
.B if
command returns a zero exit status.
.TP
.PD 0
\f3while\fP \f2list\^\fP \f3do\fP \f2list\^\fP \f3done\fP
.TP
\f3until\fP \f2list\^\fP \f3do\fP \f2list\^\fP \f3done\fP
.PD
A
.B while
command repeatedly executes the
.B while
.I list\^
and, if the exit status of the last command in the list is zero, executes
the
.B do
.IR list ;
otherwise the loop terminates.
If no commands in the
.B do
.I list\^
are executed, then the
.B while
command returns a zero exit status;
.B until
may be used in place of
.B while
to negate
the loop termination test.
.TP
\f3(\fP\f2list\^\fP\f3)\fP
.br
Execute
.I list\^
in a separate environment.
Note, that if two adjacent open parentheses are
needed for nesting, a space must be inserted to avoid
arithmetic evaluation as described below.
.TP
\f3{ \fP\f2list\^\fP\f3;}\fP
.br
.I list\^
is simply executed.
Note that
.B {
is a
.I keyword\^
and requires a blank
in order to be recognized.
.TP
.PD 0
\f3function\fP \f2identifier\^\fP  \f3{\fP \f2list\^\fP \f3;}\fP
.TP
\f2identifier\^\fP  \f3() {\fP \f2list\^\fP \f3;}\fP
.PD
Define a function which is referenced by
.IR identifier .
The body of the function is the
.I list\^
of commands between
.B {
and
.BR } .
(See
.I Functions\^
below).
.TP
\f3time \fP\f2pipeline\^\fP
.br
The
.I pipeline\^
is executed and the elapsed time as well as
the user and system time are printed on standard error.
.PP
The following keywords
are only recognized as the first word of a command
and when not quoted:
.if t .RS
.PP
.B
.if n if then else elif fi case esac for while until do done { } function select time
.if t if   then   else   elif   fi   case   esac   for   while   until   do   done   {   }   function   select   time
.if t .RE
.SS Comments.
A word beginning with
.B #
causes that word and all the following characters up to a new-line
to be ignored.
.SS Aliasing.
The first word of each command is replaced by the text of an
.B alias
if an
.B alias
for this word has been defined.
The
first character of an
.B alias
name can be any printable character,
but the rest of the characters
must be the same as for a valid
.IR identifier .
The replacement string can contain any
valid shell script
including the metacharacters listed above.
The first word of each command of the
replaced text will not be tested for additional aliases.
If the last character of the alias value is a
.I blank\^
then the word following the alias will also be checked for alias
substitution.
Aliases can be used to redefine special
builtin commands but cannot be used to redefine
the keywords listed above.
Aliases can be created, listed, and exported with the
.B alias
command and can be removed with the
.B unalias
command.
Exported aliases remain in effect for sub-shells
but must be reinitialized for separate invocations
of the shell (See
.I Invocation\^
below).
.PP
.I Aliasing\^
is performed when
scripts are read,
not while they are executed.
Therefore,
for an alias to take effect
the
.B
alias
command has to be executed before
the command which references the alias is read.
.PP
Aliases are frequently used as a short hand for full path
names.
An option to the aliasing facility allows the value of the alias
to be automatically set to the full pathname of
the corresponding command.
These aliases are called
.I tracked
aliases.
The value of a
.I tracked
alias is defined the first time the identifier
is read and becomes undefined each time
the
.SM
.B PATH
variable is reset.
These aliases remain
.I tracked
so that the next
subsequent reference will redefine the value.
Several tracked aliases are compiled into the shell.
The
.B \-h
option of the
.B set
command makes each command name which is an
.I identifier\^
into a tracked alias.
.PP
The following
.I exported aliases
are compiled into the shell
but can be unset or redefined:
.RS 20
.PD 0
.TP
.B "echo=\(fmprint \-\(fm"
.TP
.B "false=\(fmlet 0\(fm"
.TP
.B "functions=\(fmtypeset \-f\(fm"
.TP
.B "history=\(fmfc \-l\(fm"
.TP
.B "integer=\(fmtypeset \-i\(fm"
.TP
.B "nohup=\(fmnohup \(fm"
.TP
.B "pwd=\(fmprint \- $\s-1PWD\s+1\(fm"
.TP
.B "r=\(fmfc \-e \-\(fm"
.TP
.B "true=\(fm:\(fm"
.TP
.B "type=\(fmwhence \-v\(fm"
.TP
.B "hash=\(fmalias \-t\(fm"
.PD
.RE 
.SS Tilde Substitution.
After alias substitution is performed, each word
is checked to see if it begins with an unquoted
.BR \(ap .
If it does, then the word up to a
.B /
is checked to see if it matches a user name in the
.B /etc/passwd
file.
If a match is found, the
.B \(ap
and the matched login name is replaced by the
login directory of the matched user.
This is called a
.I tilde
substitution.
If no match is found, the original text is left unchanged.
A
.B \(ap
by itself, or in front of a
.BR / ,
is replaced by the value of the
.B
.SM HOME
parameter.
A
.B \(ap
followed by a
.B +
or
.B \-
is replaced by the value of
the parameter
.B
.SM PWD
and
.B
.SM OLDPWD
respectively.
.PP
In addition, the value of each
.I "keyword parameter"
is checked to see if it begins with a
.B \(ap
or if a
.B \(ap
appears after a
.BR : .
In either of these cases a
.I tilde
substitution is attempted.
.SS Command Substitution.
The standard output from a command enclosed in
a pair of grave accents (\^\f3\*`\^\*`\fP\^) may be used as part or all
of a word;
trailing new-lines are removed.
The command substitution
\^\f3\*`\^cat file\^\*`\fP\^
can be replaced by the equivalent but faster
\^\f3\*`\^<file\^\*`\fP\^.
Command substitution of most special commands
that do not perform input/output redirection are
carried out without creating a separate process.
.SS Parameter Substitution.
A
.I parameter\^
is an
.IR identifier ,
a digit,
or any of the characters
.BR \(** ,
.BR @ ,
.BR # ,
.BR ? ,
.BR \- ,
.BR $ ,
and
.BR !\\^ .
A
.I named parameter\^
(a parameter denoted by an identifier)
has a
.I value\^
and zero or more
.IR attributes .
.I Named parameters \^
can be assigned
.I values\^
and
.I attributes
by using the
.B typeset\^
special command.
The attributes supported by the shell are described
later with the
.B typeset\^
special command.
Exported parameters pass values and attributes to
sub-shells but only values to the environment.
.PP
The shell supports a limited one-dimensional array facility.
An element of an array parameter is referenced by a
.IR subscript .
A
.I subscript\^
is denoted by a
.BR [ ,
followed by an
.I arithmetic expression\^
(see Arithmetic evaluation below)
followed by a
.BR ] .
The value of all
subscripts must be in the
range of
0 through 511.
Arrays need not be declared.
Any reference to a named parameter
with a valid subscript is
legal and an array will be created if necessary.
Referencing an array without a subscript
is equivalent to referencing the first element.
.PP
The
.I value\^
of a 
.I named parameter\^
may also be assigned by writing:
.RS
.PP
.IB name = value\^\|
\*(OK
.IB name = value\^
\*(CK .\|.\|.
.RE
.PP
.PD 0
If the integer attribute,
.BR \-i ,
is set for
.I name\^
the
.I value\^
is subject to arithmetic evaluation as described below.
.PP
Positional parameters,
parameters denoted by a number,
may be assigned values with the
.B set\^
special command.
Parameter
.B $0
is set from argument zero when the shell
is invoked.
.PP
The character
.B $
is used to introduce substitutable
.IR parameters .
.TP
\f3${\fP\f2parameter\^\fP\f3}\fP
The value, if any, of the parameter is substituted.
The braces are required when
.I parameter\^
is followed by a letter, digit, or underscore
that is not to be interpreted as part of its name
or when a named parameter is subscripted.
If
.I parameter\^
is a digit then it is a positional parameter.
If
.I parameter\^
is
.BR \(**
or
.BR @ ,
then all the positional
parameters, starting with
.BR $1 ,
are substituted
(separated by spaces).
If an array
.I identifier\^
with subscript
.B \(**
or
.B @
is used,
then the value
for each of the
elements
is substituted
(separated by spaces).
.TP
\f3${#\fP\f2parameter\^\fP\f3}\fP
If
.I parameter\^
is not
.BR \(** ,
the length of the value of the
.I parameter\^
is substituted.
Otherwise, the number of positional parameters is substituted.
.TP
\f3${#\fP\f2identifier\fP\f3[*]}\fP
The number of elements in the array
.I identifier\^
is substituted.
.TP
\f3${\fP\f2parameter\^\fP\f3:\-\fP\f2word\^\fP\f3}\fP
If
.I parameter\^
is set and is non-null then substitute its value;
otherwise substitute
.IR word .
.TP
\f3${\fP\f2parameter\^\fP\f3:=\fP\f2word\^\fP\f3}\fP
If
.I parameter\^
is not set or is null then set it to
.IR word ;
the value of the parameter is then substituted.
Positional parameters may not be assigned to
in this way.
.TP
\f3${\fP\f2parameter\^\fP\f3:?\fP\f2word\^\fP\f3}\fP
If
.I parameter\^
is set and is non-null then substitute its value;
otherwise, print
.I word\^
and exit from the shell.
If
.I word\^
is omitted then a standard message is printed.
.TP
\f3${\fP\f2parameter\^\fP\f3:+\fP\f2word\^\fP\f3}\fP
If
.I parameter\^
is set and is non-null then substitute
.IR word ;
otherwise substitute nothing.
.TP
.PD 0
\f3${\fP\f2parameter\^\fP\f3#\fP\f2pattern\^\fP\f3}\fP
.TP
\f3${\fP\f2parameter\^\fP\f3##\fP\f2pattern\^\fP\f3}\fP
.PD
If
the shell
.I pattern\^
matches the beginning of the value of
.IR parameter ,
then the value of
this substitution is the value of the
.I parameter\^
with the matched portion deleted;
otherwise the value of this
.I parameter\^
is substituted.
In the first form the smallest matching pattern is deleted and in the
latter form the largest matching pattern is deleted.
.TP
.PD 0
\f3${\fP\f2parameter\^\fP\f3%\fP\f2pattern\^\fP\f3}\fP
.TP
\f3${\fP\f2parameter\^\fP\f3%%\fP\f2pattern\^\fP\f3}\fP
.PD
If
the shell
.I pattern\^
matches the end of the value of
.IR parameter ,
then the value of
.I parameter\^
with the matched part deleted;
otherwise substitute the value of
.IR parameter .
In the first form the smallest matching pattern is deleted and in the
latter form the largest matching pattern is deleted.
.PD
.PP
In the above,
.I word\^
is not evaluated unless it is
to be used as the substituted string,
so that, in the following example,
.B pwd\^
is executed only if
.B d\^
is not set or is null:
.RS
.PP
echo \|${d:\-\^\*`\^pwd\^\*`\^}
.RE
.PP
If the colon (
.B : )
is omitted from the above expressions,
then the shell only checks whether
.I parameter\^
is set or not.
.PP
The following
parameters
are automatically set by the shell:
.RS
.PD 0
.TP
.B #
The number of positional parameters in decimal.
.TP
.B \-
Flags supplied to the shell on invocation or by
the
.B set
command.
.TP
.B ?
The decimal value returned by the last executed command.
.TP
.B $
The process number of this shell.
.TP
.B _
The last argument of the previous command.
This parameter is not set for commands which are asynchronous.
.TP
.B !
The process number of the last background command invoked.
.TP
.B
.SM PPID
The process number of the parent of the shell.
.TP
.B
.SM PWD
The present working directory set by the
.B cd
command.
.TP
.B
.SM OLDPWD
The previous working directory set by the
.B cd
command.
.TP
.B
.SM RANDOM
Each time this parameter is referenced, a random integer is generated.
The sequence of random numbers can be initialized by assigning
a numeric value to
.SM
.BR RANDOM .
.TP
.B
.SM REPLY
This parameter is set by the
.B select
statement and by
the
.B read
special command when no arguments are supplied.
.PD
.RE
.PP
The following
parameters
are used by the shell:
.RS
.PD 0
.TP
.B
.SM CDPATH
The search path for the
.I cd
command.
.TP
.B
.SM COLUMNS
If this variable is set,
the value is used to define the width of the edit window
for the shell edit modes and for printing
.B select
lists.
.TP
.B
.SM EDITOR
If the value of this variable ends in
.IR emacs ,
.IR gmacs ,
or
.I vi
and the
.B
.SM VISUAL
variable is not set,
then the corresponding option
(see Special Command
.B set
below)
will be turned on.
.TP
.SM
.B ENV
If this parameter is set, then
parameter substitution is performed on
the value to generate
the  pathname of the script that will be
executed when the
.I shell\^
is invoked.
(See
.I Invocation\^
below.)
This file is typically used for
.I alias
and
.I function
definitions.
.TP
.B
.SM FCEDIT
The default editor name for the
.B fc
command.
.TP
.SM
.B IFS
Internal field separators,
normally
.BR space ,
.BR tab ,
and
.B new-line
that is used to separate command words which result from
command or parameter substitution
and for separating words with  the special command
.BR read .
.TP
.SM
.B HISTFILE
If this parameter is set when the shell is invoked, then
the value is the  pathname of the file that will be
used to store the command history.
(See
.I "Command re-entry\^"
below.)
.TP
.SM
.B HISTSIZE
If this parameter is set when the shell is invoked, then
the number of previously entered commands that
are accessible by this shell
will be greater than or equal to this number.
The default is 128.
.TP
.B
.SM HOME
The default argument (home directory) for the
.B cd
command.
.TP
.B
.SM MAIL
If this parameter is set to the name of a mail file
.I and\^
the
.B
.SM MAILPATH
parameter is not set,
then the shell informs the user of arrival of mail
in the specified file.
.TP
.B
.SM MAILCHECK
This variable specifies how often (in seconds) the
shell will check for changes in the modification time
of any of the files specified by the
.B
.SM MAILPATH
or
.B
.SM MAIL
parameters.
The default value is 600 seconds.
If set to 0,
the shell will check before each prompt.
.TP
.B
.SM MAILPATH
A colon (
.B :
)
separated list of file names.
If this parameter is set
then the shell informs the user of
any modifications to the specified files
that have occurred within the last
.B
.SM MAILCHECK
seconds.
Each file name can be followed by a
.B ?
and a message that will be printed.
The message will undergo parameter and command substitution
with the parameter,
.B $_
defined as the name of the file that has changed.
The default message is
.I you have mail in $_\^.
.TP
.B
.SM PATH
The search path for commands (see
.I Execution\^
below).
The user may not change
.B \s-1PATH\s+1
if executing under
the restricted shell
(except in
.I .profile\^
).
.TP
.SM
.B PS1
The value of this parameter is expanded for paramter
substitution to define the
primary prompt string which by default is
.RB `` "$ \|" ''.
The character
.B !
in the primary prompt string is replaced by the
.I command\^ 
number (see
.I Command Re-entry
below).
.TP
.SM
.B PS2
Secondary prompt string, by default
.RB `` "> \|" ''.
.TP
.SM
.B PS3
Selection prompt string
used within a
.B select
loop, by default
.RB `` "#? \|" ''.
.TP
.SM
.B SHELL
The pathname of the
.I shell\^
is kept in the environment.
At invocation, if the value of this variable contains an
.B r
in the basename,
then the shell becomes restricted.
.TP
.B
.SM TMOUT
If set to a value greater than zero,
the shell will terminate if a command is not entered within
the prescribed number of seconds.
(Note that the shell can be compiled with a maximum bound
for this value which cannot be exceeded.)
.TP
.B
.SM VISUAL
If the value of this variable ends in
.IR emacs ,
.IR gmacs ,
or
.I vi
then the corresponding option
(see Special Command
.B set
below)
will be turned on.
.PD
.RE
.PP
The shell gives default values to
\f3\s-1PATH\s+1\fP, \f3\s-1PS1\s+1\fP, \f3\s-1PS2\s+1\fP, \f3\s-1MAILCHECK\s+1\fP,
\f3\s-1TMOUT\s+1\fP  and \f3\s-1IFS\s+1\fP,
while
.SM
.BR HOME ,
.SM
.B SHELL
.SM
.B ENV
and
.SM
.B MAIL
are
not set at all by the shell (although
.SM
.B HOME
.I is\^
set by
.IR login (1)).
On some systems
.SM
.B MAIL
and
.SM
.B SHELL
are also
set by
.IR login (1)).
.SS Blank Interpretation.
After parameter and command substitution,
the results of substitutions are scanned for the field separator
characters (
those found in
.SM
.B IFS
)
and split into distinct arguments where such characters are found.
Explicit null arguments (\^\f3"\^"\fP or \f3\*\(fm\^\*\(fm\fP\^) are retained.
Implicit null arguments
(those resulting from
.I parameters\^
that have no values) are removed.
.SS File Name Generation.
Following substitution, each command
.I word\^
is scanned for
the characters
.BR \(** ,
.BR ? ,
and
.B \*(OK\^ 
unless the
.B \-f
option has been
.BR set .
If one of these characters appears
then the word is regarded as a
.IR pattern .
The word is replaced with alphabetically sorted file names that match the pattern.
If no file name is found that matches the pattern, then
the word is left unchanged.
When a
.I pattern\^
is used for file name generation,
the character
.B .
at the start of a file name
or immediately following a
.BR / ,
as well as the character
.B /
itself,
must be matched explicitly.
In other instances of pattern matching the
.B /
and
.B .
are not treated specially.
.PP
.PD 0
.RS
.TP
.B \(**
Matches any string, including the null string.
.TP
.B ?
Matches any single character.
.TP
.BR \*(OK \^.\|.\|.\^ \*(CK
Matches any one of the enclosed characters.
A pair of characters separated by
.B \-
matches any
character lexically between the pair, inclusive.
If the first character following the opening "[ \|"
is a "! \|" then any character not enclosed is matched.
A
.B \-
can be included in the character set by putting it as the
first or last character.
.PD
.RE
.SS Quoting.
Each of the
.I metacharacters\^
listed above (See
.I Definitions
above).
has a special meaning to the shell
and cause termination of a word unless quoted.
A character may be
.I quoted\^
(i.e., made to stand for itself)
by preceding
it with a
.BR \e .
The pair
.B \enew-line
is ignored.
All characters enclosed between a pair of single quote marks (\^\f3\(fm\^\(fm\fP\^),
except a single quote,
are quoted.
Inside double quote marks
(\f3"\^"\fP),
parameter and command substitution occurs and
.B \e
quotes the characters
.BR \e ,
.BR \(fm ,
\f3"\fP,
and
.BR $ .
.B
"$\(**"
is equivalent to
\f3"$1 \|$2\fP \|.\|.\|.\f3"\fP,
whereas
.B
"$@"
is equivalent to
.B
"$1"\|
.B
"$2"\|
\&.\|.\|.\^.
.PP
The special meaning of keywords can be removed by quoting any
character of the keyword.
The recognition of special command names listed below cannot be altered
by quoting them.
.SS Arithmetic Evaluation.
An ability to perform integer arithmetic
is provided with the special command
.BR let .
Evaluations are performed using
.I long\^
arithmetic.
Constants are of the form
\*(OK\f2base\f3#\^\f1\*(CK\f2n\^\fP
where
.I base\^
is a decimal number between two and thirty-six
representing the arithmetic base
and
.I n\^
is a number in that base.
If
.I base\^
is omitted 
then base 10 is used.
.PP
An internal integer representation of a
.I named parameter\^
can be specified with the
.B \-i
option of the
.B typeset
special command.
When this attribute is selected
the first assignment to the
parameter determines the arithmetic base
to be used when 
parameter substitution occurs.
.PP
Since many of the arithmetic operators require
quoting, an alternative form of the
.B let
command is provided.
For any command which begins with a
.BR (( ,
all the characters until a matching
.B ))
are treated as a quoted expression.
More precisely,
.B ((
\&.\|.\|.
.B ))
is equivalent to
.B let\^
\f3"\fP \|.\|.\|.\f3"\fP.
.SS Prompting.
When used interactively,
the shell prompts with the value of
.SM
.B PS1
before reading a command.
If at any time a new-line is typed and further input is needed
to complete a command, then the secondary prompt
(i.e., the value of
.BR \s-1PS2\s+1 )
is issued.
.SS Input/Output.
Before a command is executed, its input and output
may be redirected using a special notation interpreted by the shell.
The following may appear anywhere in a simple-command
or may precede or follow a
.I command\^
and are
.I not\^
passed on to the invoked command.
Command and parameter substitution occurs before
.I word\^
or
.I digit\^
is used except as noted below.
File name generation
occurs only if the pattern matches a single file
and blank interpretation is not performed.
.TP 14
.BI < word
Use file
.I word\^
as standard input (file descriptor 0).
.TP
.BI > word
Use file
.I word\^
as standard output (file descriptor 1).
If the file does not exist then it is created;
otherwise, it is truncated to zero length.
.TP
.BI >\h@-.3m@> word
Use file
.I word\^
as standard output.
If the file exists then output is appended to it (by first seeking to the end-of-file);
otherwise, the file is created.
.TP
\f3<\h@-.3m@<\fP\*(OK\f3\-\fP\*(CK\f2word\fP
The shell input is read up to a line that is the same as
.IR word ,
or to an end-of-file.
No parameter substitution, command substitution or
file name generation is performed on
.IR word .
The resulting document,
called a
.IR here-document ,
becomes
the standard input.
If any character of
.I word\^
is quoted, then no interpretation
is placed upon the characters of the document;
otherwise, parameter and command substitution occurs,
.B \enew-line
is ignored,
and
.B \e
must be used to quote the characters
.BR \e ,
.BR $ ,
.BR \*` ,
and the first character of
.IR word .
If
.B \-
is appended to
.BR <\h@-.3m@< ,
then all leading tabs are stripped from
.I word\^
and from the document.
.TP
.BI <& digit
The standard input is duplicated from file descriptor
.I digit
(see
.IR dup (2)).
Similarly for the standard output using
.BR >& 
.IR digit .
.TP
.B <&\-
The standard input is closed.
Similarly for the standard output using
.BR >&\- .
.PP
If one of the above is preceded by a digit,
then the
file descriptor number referred to is that specified
by the digit
(instead of the default 0 or 1).
For example:
.RS
.PP
\&.\|.\|. \|2>&1
.RE
.PP
means file descriptor 2 is to be opened
for writing as a duplicate
of file descriptor 1.
.PP
The order in which redirections are specified is significant.
The shell evaluates each redirection in terms of the 
.RI ( "file descriptor" ", " file ) 
association at the time of evaluation.
For example:
.RS
.PP
\&.\|.\|. \|1>\f2fname\^\fP 2>&1
.RE
.PP
first associates file descriptor 1 with file 
.IR fname\^ .
It then associates file descriptor 2 with the file associated with file
descriptor 1 (i.e. 
.IR fname\^ ).
If the order of redirections were reversed, file descriptor 2 would be associated 
with the terminal (assuming file descriptor 1 had been) and then file descriptor 
1 would be associated with file 
.IR fname\^ .
.PP
If a command is followed by
.B &
and job control is not active,
then the default standard input
for the command
is the empty file
.BR /dev/null .
Otherwise, the environment for the execution of a command contains the
file descriptors of the invoking shell as modified by
input/output specifications.
.SS Environment.
The
.I environment\^
(see
.IR environ (7))
is a list of name-value pairs that is passed to
an executed program in the same way as a normal argument list.
The names must be
.I identifiers\^
and the values are character strings.
The shell interacts with the environment in several ways.
On invocation, the shell scans the environment
and creates a
parameter
for each name found,
giving it the corresponding value and marking it
.I export .
Executed commands inherit the environment.
If the user modifies the values of these
parameters
or creates new ones,
using the
.B export
or
.B typeset \-x
commands they become part of the
environment.
The environment seen by any executed command is thus composed
of any name-value pairs originally inherited by the shell,
whose values may be modified by the current shell,
plus any additions
which must be noted in
.B export
or
.B typeset \-x
commands.
.PP
The environment for any
.I simple-command\^
or function
may be augmented by prefixing it with one or more parameter assignments.
A parameter assignment argument is a word of the form
.IR identifier=value .
Thus:
.RS
.PP
\s-1TERM\s+1=450 \|cmd \|args				and
.br
(export \|\s-1TERM\s+1; \|\s-1TERM\s+1=450; \|cmd \|args)
.RE
.PP
are equivalent (as far as the above execution of
.I cmd\^
is concerned).
.PP
If the
.B \-k
flag is set,
.I all\^
parameter assignment arguments are placed in the environment,
even if they occur after the command name.
The following
first prints
.B "a=b c"
and then
.BR c:
.PP
.RS
.nf
echo \|a=b \|c
set \|\-k
echo \|a=b \|c
.fi
.RE
.SS Functions.
.PP
The
.B function\^
keyword, described in the
.I Commands
section above,
is used to define shell functions.
Shell functions are read in and stored internally.
Alias names are resolved when the function is read.
Functions are executed like commands with the arguments
passed as positional parameters.
(See
.I Execution
below).
.PP
Functions execute in the same process as the caller and
share all files, traps ( other than
.SM
.B EXIT
and
.SM
.BR ERR )
and present working directory with the
caller.
A trap set on
.SM
.B EXIT
inside a function
is executed after the function completes.
Ordinarily,
variables are shared between the calling program
and the function.
However,
the
.B typeset
special command used within a function
defines local variables whose scope includes
the current function and
all functions it calls.
.PP
The special command
.B return
is used to return
from function calls.
Errors within functions return control to the caller.
.PP
Function identifiers
can be listed with the
.B \-f
option of the
.B typeset
special command.
The text of functions will also
be listed.
Function can be undefined with the
.B \-f
option of the
.B unset
special command.
.PP
Ordinarily,
functions are unset when the shell executes a shell script.
The
.B \-xf
option of the
.B typeset
command allows a function to be exported
to scripts that are executed without a separate
invocation of the shell.
Functions that need to be defined across separate
invocations of the shell should be placed in the
.B
.SM
ENV
file.
.SS Jobs.
.PP
If the
.B monitor
option of the
.B set
command is turned on,
an interactive shell associates a \fIjob\fR with each pipeline.  It keeps
a table of current jobs, printed by the
.B jobs
command, and assigns them small integer numbers.  When
a job is started asynchronously with
.BR & ,
the shell prints a line which looks
like:
.PP
.DT
	[1] 1234
.PP
indicating that the job which was started asynchronously was job number
1 and had one (top-level) process, whose process id was 1234.
.PP
This paragraph and the next require features that are
not in all versions of UNIX and may not apply.
If you are running a job and wish to do something else you may hit the key
\fB^Z\fR (control-Z) which sends a STOP signal to the current job.
The shell will then normally indicate that the job has been `Stopped',
and print another prompt.  You can then manipulate the state of this job,
putting it in the background with the
.B bg
command, or run some other
commands and then eventually bring the job back into the foreground with
the foreground command
.BR fg .
A \fB^Z\fR takes effect immediately and
is like an interrupt in that pending output and unread input are discarded
when it is typed.
.PP
A job being run in the background will stop if it tries to read
from the terminal.  Background jobs are normally allowed to produce output,
but this can be disabled by giving the command ``stty tostop''.
If you set this
tty option, then background jobs will stop when they try to produce
output like they do when they try to read input.
.PP
There are several ways to refer to jobs in the shell.  The character
.B %
introduces a job name.  If you wish to refer to job number 1, you can
name it as
.B %1 . 
Jobs can also be named by prefixes of the string typed in to
.B kill
or restart them.
Thus, on systems that support job control,
.RB ` fg
.BR %ed '
would normally restart
a suspended
.IR ed (1)
job, if there were a suspended job whose name began with
the string `ed'. 
.PP
The shell maintains a notion of the current and previous jobs.
In output pertaining to jobs, the current job is marked with a
.B +
and the previous job with a
.BR \- .
The abbreviation
.B %+
refers
to the current job and
.B %\-
refers to the previous job.
.B %%
is also a synonym for the current job.
.PP
This shell learns immediately whenever a process changes state.
It normally informs you whenever a job becomes blocked so that
no further progress is possible, but only just before it prints
a prompt.  This is done so that it does not otherwise disturb your work.
.PP
When you try to leave the shell while jobs are running or stopped, you will
be warned that `You have stopped(running) jobs.'  You may use the
.B jobs
command to see what they are.  If you do this or immediately try to
exit again, the shell will not warn you a second time, and the stopped
jobs will be terminated.
.SS Signals.
The \s-1INT\s+1 and \s-1QUIT\s+1 signals for an invoked
command are ignored if the command is followed by
.B & 
and job
.B monitor
option is not active.
Otherwise, signals have the values
inherited by the shell from its parent,
with the exception of signal 11
(but see also
the
.B trap
command below).
.SS Execution.
Each time a command is executed, the above substitutions
are carried out.
If the command name matches one
of the
.I "Special Commands\^"
listed below,
it is executed within the
current shell process.
Next, the command name is checked to see if
it matches one of the user defined functions.
If it does,
the positional parameters are saved
and then reset to the arguments of the
.I function\^
call.
When the
.I function\^
completes or issues a
.BR return ,
the positional parameter list is restored
and any trap set on
.SM
.B EXIT
within the function is executed.
The value of a
.I function\^
is the value of the last command executed.
A function is also executed in the
current shell process.
If a command name is not a
.I "special command\^"
or a user defined
.IR function ,
a process is created and
an attempt is made to execute the command via
.IR exec (2).
.PP
The shell parameter
.B
.SM PATH
defines the search path for
the directory containing the command.
Alternative directory names are separated by
a colon
.RB ( : ).
The default path is
.B :/bin:/usr/bin
(specifying the current directory,
.BR /bin ,
and
.BR /usr/bin ,
in that order).
Note that the current directory
is specified by a null path name,
which can appear immediately after the
equal sign, between colon delimiters,
or at the end of the path list.
If the command name contains a \f3/\fP then the search path
is not used.
Otherwise, each directory in the path is
searched for an executable file.
If the file has execute permission but is not a
directory or an
.B a.out
file,
it is assumed to be a file containing shell commands.
A sub-shell is spawned to read it.
All non-exported aliases,
functions,
and named parameters are removed in this case.
A parenthesized command is also executed in
a sub-shell.
.SS Command Re-entry.
The text of the last
.B
.SM
HISTSIZE
(default 128)
commands entered from a terminal device
is saved in a
.I history
file.
The file
.B \s-1$HOME\s+1/.history
is used if the
.B
.SM
HISTFILE
variable is not set
or is not writable.
A shell can access the commands of
all
.I interactive
shells which use the same named
.SM
.BR HISTFILE .
The special command
.B fc\^
is used to list or
edit a portion this file.
The portion of the file to be edited or listed can be selected by
number or by giving the first character or
characters of the command.
A single command or range of commands can be specified.
If you do not specify an editor program as
an argument to
.B fc\^
then the value of the parameter
.SM
.B FCEDIT
is used.
If
.SM
.B FCEDIT
is not defined then
.I /bin/ed
is used.
The edited command(s) is printed and re-executed upon
leaving the editor.
The editor name
.B \-
is used to skip the editing phase and
to re-execute the command.
In this case a substitution parameter of the form
\f2old\fP\f3=\fP\f2new\fP
can be used to modify the command before execution.
For example, if
.B r
is aliased to
.B \(fmfc \-e \-\(fm
then typing
`\f3r bad=good c\fP'
will re-execute the most recent command which starts with the letter
.BR c ,
replacing the string
.B bad
with the string
.BR good .
.SS In-line Editing Options
Normally, each command line entered from a terminal device is simply
typed followed by a new-line (`RETURN' or `LINE\ FEED').
If either the
.IR emacs ,
.IR gmacs ,
or
.I vi
option is active, the user can edit the command line.
To be in either of these edit modes
.B set
the corresponding
option.
An editing option is automatically selected each time the
.SM
.B VISUAL
or
.SM
.B EDITOR
variable is assigned a value ending in either of these
option names.
.PP
The editing features require that the user's terminal
accept `RETURN' as carriage return without line feed
and that a space (`\ ' must overwrite the current character on
the screen.
ADM terminal users should set the "space\ -\ advance"
switch to `space'.
Hewlett-Packard series 2621 terminal users should set the straps to
`bcGHxZ\ etX'.
.PP
The editing modes implement a concept where the user is looking through a
window at the current line.
The window width is the value of
.SM
.B COLUMNS
if it is defined, otherwise 80.
If the line is longer than the window width minus two, a mark is
displayed at the end of the window to notify the user.
As the cursor moves and reaches the window boundaries the window will be
centered about the cursor.
The mark is a
.BR > " ("
.BR < ", "
.BR * )
if the line extends on the 
right (left, both) side(s) of the window.
.SS Emacs Editing Mode
This mode is entered by enabling either the
.I emacs
or
.I gmacs
option.
The only difference between these two modes is the way
they handle
.BR ^T .
To edit, the user
moves the cursor to the point needing correction and
then inserts or deletes characters or words as needed.
All the editing commands are control characters or escape
sequences.
The notation for control characters is caret (
.B ^
) followed
by the character.
For example,
.B ^F
is the notation for control
.BR F .
This is entered by depressing `f' while holding down the
`CTRL' (control) key.
The `SHIFT' key is
.I not 
depressed.
(The notation
.B ^?
indicates the DEL (delete) key.)
.PP
The notation for escape sequences is
.B M-
followed by a
character.
For example,
.B M-f
(pronounced Meta f)
is entered by depressing ESC
(ascii
.B 033
)
followed by `f'.
(
.B M-F
would be the notation for ESC followed by `SHIFT' (capital) `F'.)
.PP
All edit commands
operate from any place on the line
(not just at the beginning).
Neither the "RETURN" nor the "LINE FEED" key is
entered after edit commands except when noted.
.PP
.PD 0
.TP 10
.BI ^F
Move cursor forward (right) one character. 
.PP
.TP 10
.BI M-f
Move cursor forward one word.
(The editor's idea of a word is a string of characters
consisting of only letters, digits and underscores.)
.PP
.TP 10
.BI ^B 
Move cursor backward (left) one character.
.PP
.TP 10
.BI M-b
Move cursor backward one word.
.PP
.TP 10
.BI ^A 
Move cursor to start of line.
.PP
.TP 10
.BI ^E 
Move cursor to end of line.
.PP
.TP 10
.BI ^] char 
Move cursor to character
.I char
on current line.
.PP
.TP 10
.BI ^X^X
Interchange the cursor and mark.
.PP
.TP 10
.I erase
(User defined erase character as defined
by the stty command, usually
.B ^H
or
.BR # .)
Delete previous character.
.PP
.TP 10
.BI ^D 
Delete current character.
.PP
.TP 10
.BI M-d
Delete current word.
.PP
.TP 10
.BI M-^H
(Meta-backspace) Delete previous word.
.PP
.TP 10
.BI M-h
Delete previous word.
.PP
.TP 10
.BI M-^?
(Meta-DEL) Delete previous word (if your interrupt character is
.B ^?
(DEL, the default) then this command will not work).
.PP
.TP 10
.BI ^T
Transpose current character with next character in
.I emacs
mode.
Transpose two previous characters in
.I gmacs
mode.
.PP
.TP 10
.BI ^C
Capitalize current character.
.PP
.TP 10
.BI M-C
Capitalize current word.
.PP
.TP 10
.BI ^K 
Kill from the cursor to the end of the line.
If given a parameter of zero then kill from
the start of line to the cursor.
.PP
.TP 10
.BI ^W
Kill from the cursor to the mark.
.PP
.TP 10
.BI M-p
Push the region from the cursor to the mark on the stack.
.PP
.TP 10
.I kill
(User defined kill character as defined
by the stty command, usually
.B ^G
or
.BR @ .)
Kill the entire current line.
If two
.I kill
characters are entered in succession, all
kill characters from then on cause a line feed
(useful when using paper terminals).
.PP
.TP 10
.BI ^Y
Restore last item removed from line. (Yank item back to the line.)
.PP
.TP 10
.BI ^L 
Line feed and print current line.
.PP
.TP 10
.BI ^@
(Null character) Set mark.
.PP
.TP 10
.BI M-
(Meta space) Set mark.
.PP
.TP 10
.BI ^J	
(New\ line)  Execute the current line.
.PP
.TP 10
.BI ^M	
(Return)  Execute the current line.
.PP
.TP 10
.I eof
End-of-file character,
normally
.BR ^D ,
will terminate the shell
if the current line is null.
.PP
.TP 10
.BI ^P
Fetch previous command. 
Each time 
.B ^P 
is entered
the previous command back in time is accessed.
.PP
.TP 10
.BI M-<
Fetch the least recent (oldest) history line.
.PP
.TP 10
.BI M->
Fetch the most recent (youngest) history line.
.PP
.TP 10
.BI ^N
Fetch next command. 
Each time 
.B ^N 
is entered
the next command forward in time is accessed.
.PP
.TP 10
.BI ^R string
Reverse search history for a previous command line containing
.IR string .
If a parameter of zero is given the search is forward.
.I String
is terminated by a "RETURN" or "NEW\ LINE".
.PP
.TP 10
.B  ^O
Operate \- Execute the current line and fetch
the next line relative to current line from the
history file.
.PP
.TP 10
.BI M- digits
(Escape) Define numeric parameter, the digits
are taken as a parameter to the next command.
The commands that accept a parameter are 
.BR ^F ,
.BR ^B ,
.IR erase ,
.BR ^D ,
.BR ^K ,
.BR ^R ,
.B ^P
and
.BR ^N .
.PP
.TP 10
.BI M- letter
Soft-key \- Your alias list is searched for an
alias by the name
.BI _ letter
and if an alias of this name is defined, its
value will be inserted on the line.
The
.I letter
must not be one of the above meta-functions.
.PP
.TP 10
.B  M-_
The last parameter of the previous command is inserted
on the line.
.PP
.TP 10
.B  M-.
The last parameter of the previous command is inserted
on the line.
.PP
.TP 10
.B  M-*
Attempt file name generation on the current word.
.PP
.TP 10
.BI ^U
Multiply parameter of next command by 4.
.PP
.TP 10
.BI \e
Escape next character.  
Editing characters, the user's erase, kill and 
interrupt (normally
.B ^?
)
characters 
may be entered
in a command line or in a search string if preceded by a
.BR \e .
The
.B \e
removes the next character's
editing features (if any).
.PP
.TP 10
.BI ^V
Display version of the shell.
.PD
.SS Vi Editing Mode
There are two typing modes.
Initially, when you enter a command you are in the
.I input\^
mode.
To edit, the user enters
.I control\^
mode by typing ESC (
.B 033
) and
moves the cursor to the point needing correction and
then inserts or deletes characters or words as needed.
Most control commands accept an optional repeat
.I count
prior to the command.
.P
When in vi mode on most systems,
canonical processing is initially enabled and the
command will be echoed again if the speed is 1200 baud or greater and it
contains any control characters or less than one second has elapsed
since the prompt was printed.
The ESC character terminates canonical processing for the remainder of the command
and the user can than modify the command line.
This scheme has the advantages of canonical processing with the type-ahead
echoing of raw mode.
.P
If the option
.B viraw\^
is also set, the terminal will always have canonical processing
disabled.  This mode is implicit for systems that do not support two
alternate end of line delimiters,
and may be helpful for certain terminals.
.SS "\ \ \ \ \ Input Edit Commands"
.PP
.RS
By default the editor is in input mode.
.PD 0
.TP 10
.I erase
(User defined erase character as defined
by the stty command, usually
.B ^H
or
.BR # .)
Delete previous character.
.TP 10
.BI ^W
Delete the previous blank separated word.
.TP 10
.BI ^D
Terminate the shell.
.TP 10
.BI ^V
Escape next character.  
Editing characters, the user's erase or kill
characters may be entered
in a command line or in a search string if preceded by a
.BR ^V .
The
.B ^V 
removes the next character's
editing features (if any).
.TP 10
.BI \e
Escape the next
.I erase
or
.I kill
character.
.P
.RE
.SS "\ \ \ \ \ Motion Edit Commands"
.RS
These commands will move the cursor.
.TP 10
[\f2count\fP]\f3l\fP
Cursor forward (right) one character. 
.TP 10
[\f2count\fP]\f3w\fP
Cursor forward one alpha-numeric word.
.TP 10
[\f2count\fP]\f3W\fP
Cursor to the beginning of the next word that follows a blank.
.TP 10
[\f2count\fP]\f3e\fP
Cursor to end of word.
.TP 10
[\f2count\fP]\f3E\fP
Cursor to end of the current blank delimited word.
.TP 10
[\f2count\fP]\f3h\fP
Cursor backward (left) one character.
.TP 10
[\f2count\fP]\f3b\fP
Cursor backward one word.
.TP 10
[\f2count\fP]\f3B\fP
Cursor to preceding blank separated word.
.TP 10
[\f2count\fP]\f3f\fP\f2c\fP
Find the next character \fIc\fP in the current line.
.TP 10
[\f2count\fP]\f3F\fP\f2c\fP
Find the previous character \fIc\fP in the current line.
.TP 10
[\f2count\fP]\f3t\fP\f2c\fP
Equivalent to
.B f
followed by
.BR h .
.TP 10
[\f2count\fP]\f3T\fP\f2c\fP
Equivalent to
.B F
followed by
.BR l .
.TP 10
.B ;
Repeats the last single character find command,
.BR f ,
.BR F ,
.BR t ,
or
.BR T .
.TP 10
.B ,
Reverses the last single character find command.
.TP 10
.B 0 
Cursor to start of line.
.TP 10
.B ^ 
Cursor to first non-blank character in line.
.TP 10
.B $ 
Cursor to end of line.
.RE
.SS "\ \ \ \ \ Search Edit Commands"
.RS
These commands access your command history.
.TP 10
[\f2count\fP]\f3k\fP
Fetch previous command. 
Each time 
.B k 
is entered
the previous command back in time is accessed.
.TP 10
[\f2count\fP]\f3\-\fP
Equivalent to
.BR k .
.TP 10
[\f2count\fP]\f3j\fP
Fetch next command. 
Each time 
.B j 
is entered
the next command forward in time is accessed.
.TP 10
[\f2count\fP]\f3+\fP
Equivalent to
.BR j .
.TP 10
[\f2count\fP]\f3G\fP
The command number
.I count
is fetched.
The default is the least recent history command.
.TP 10
.BI / string
Search backward through history for a previous command containing
.IR string .
.I String
is terminated by a "RETURN" or "NEW\ LINE".
If \fIstring\fP is null the previous string will be used.
.TP 10
.BI ? string
Same as
.B /
except that search will be in the forward direction.
.TP 10
.B n
Search for next match of the last pattern to
.B /
or
.B ?
commands.
.TP 10
.B N
Search for next match of the last pattern to
.B /
or
.BR ? ,
but in reverse direction.
Search history for the \fIstring\fP entered by the previous \fB/\fP command.
.RE
.SS "\ \ \ \ \ Text Modification Edit Commands"
.RS
These commands will modify the line.
.TP 10
.B a
Enter input mode and enter text after the current character.
.TP 10
.B A
Append text to the end of the line.  Equivalent to
.BR $a .
.TP 10
[\f2count\fP]\f3c\fP\f2motion\fP
.TP 10
\f3c\fP[\f2count\fP]\f2motion\fP
Delete current character through the character
.I motion
moves the cursor to and enter input mode.
If \fImotion\fP is
.BR c ,
the entire line will be deleted and
input mode entered.
.TP 10
.B C
Delete the current character through the end of line and enter input mode.
Equivalent to
.BR c$ .
.TP 10
.B S
Equivalent to
.BR cc .
.TP 10
.B D
Delete the current character through the end of line.
.TP 10
[\f2count\fP]\f3d\fP\f2motion\fP
.TP 10
\f3d\fP[\f2count\fP]\f2motion\fP
Delete current character through the character
.I motion
moves the cursor to.  Equivalent to
.BR d$ .
If \fImotion\fP is
.B d ,
the entire line will be deleted.
.TP 10
.B i
Enter input mode and insert text before the current character.
.TP 10
.B I
Insert text before the beginning of the line.  Equivalent to
the two character sequence
.BR ^i .
.TP 10
[\f2count\fP]\f3P\fP
Place the previous text modification before the cursor.
.TP 10
[\f2count\fP]\f3p\fP
Place the previous text modification after the cursor.
.TP 10
.B R
Enter input mode and
replace characters on the screen with characters you type overlay fashion.
.TP 10
.BI r c
Replace the current character with
.IR c .
.TP 10
[\f2count\fP]\f3x\fP
Delete current character.
.TP 10
[\f2count\fP]\f3X\fP
Delete preceding character.
.TP 10
[\f2count\fP]\f3.\fP
Repeat the previous text modification command.
.TP 10
.B \(ap
Invert the case of the current character and advance the cursor.
.TP 10
[\f2count\fP]\f3_\fP
Causes the
.I count\^
word of the previous command to be appended and
input mode entered.
The last word is used
if
.I count\^
is omitted.
.TP 10
.B *
Causes an
.B *
to be appended to the current word and file name generation attempted.
If no match is found,
it rings the bell.  Otherwise, the word is replaced
by the matching pattern and input mode is entered.
.RE
.SS "\ \ \ \ \ Other Edit Commands"
.RS
Miscellaneous commands.
.TP 10
.B u
Undo the last text modifying command.
.TP 10
.B U
Undo all the text modifying commands performed on the line.
.TP 10
[\f2count\fP]\f3v\fP
Returns the command
.BI "fc \-e ${\s-1VISUAL\s+1:\-${\s-1EDITOR\s+1:\-vi}}" " count"
in the input buffer.
If
.I count\^
is omitted, then the current line is used.
.TP 10
.BI ^L 
Line feed and print current line.
Has effect only in control mode.
.TP 10
.BI ^J	
(New\ line)  Execute the current line, regardless of mode.
.TP 10
.BI ^M	
(Return)  Execute the current line, regardless of mode.
.TP 10
.B \#
Equivalent to
\f3I#\fP\f2<cr>\fP.
Useful for causing the current line to be
inserted in the history without being executed.
.RE
.PD
.SS Special Commands.
The following simple-commands are executed in the shell process.
Input/Output redirection is permitted.
File descriptor 1 is the default output location.
Parameter assignment lists preceding the command do not
remain in effect when the command completes unless noted.
.TP
\f3:\fP \*(OK \f2arg\^\fP .\|.\|. \*(CK
Parameter assignments remain in effect after the command completes.
The command only expands parameters.
A zero exit code is returned.
.br
.ne 2
.TP
\f3\|. \f2file\^\fP \*(OK \f2arg\^\fP .\|.\|. \*(CK
Parameter assignments remain in effect after the command completes.
Read and execute commands from
.I file\^
and return.
The commands are executed in the current shell environment.
The search path
specified by
.B
.SM PATH
is used to find the directory containing
.IR file .
If any arguments
.I arg\^
are given,
they become the positional parameters.
Otherwise the positional parameters are unchanged.
.TP
\f3alias\fP \*(OK \f3\-tx\fP \*(CK \*(OK \f2name\fP\*(OK \f2=value\^\fP \*(CK  .\|.\|. \*(CK
.I Alias\^
with no arguments prints the list of aliases
in the form
.I name=value\^
on standard output.
An
.I alias\^
is defined
for each name whose
.I value\^
is given.
A trailing space in
.I value\^
causes the next word to be checked for
alias substitution.
The
.B \-t
flag is used to set and list tracked aliases.
The value of a tracked alias is the full pathname
corresponding to the given
.IR name .
The value becomes undefined when the value of
.SM
.B PATH
is reset but the aliases remained tracked.
Without the
.B \-t
flag,
for each
.I name\^
in the argument list
for which no
.I value\^
is given, the name
and value of the alias is printed.
The
.B \-x
flag is used to set or print exported aliases.
An exported alias is defined across sub-shell environments.
Alias returns true unless a
.I name\^
is given for which no alias has been defined.
.TP
\f3bg\fP \*(OK \f3%\f2job\^\fP \*(CK
This command is only built-in on systems that support job control.
Puts the specified
.I job\^
into the background.
The current job is put in the background
if
.I job\^
is not specified.
.TP
\f3break\fP \*(OK \f2n\^\fP \*(CK
Exit from the enclosing
.BR for
.BR while
.BR until
or
.B select\^
loop, if any.
If
.I n\^
is specified then break
.I n\^
levels.
.TP
\f3continue\fP \*(OK \f2n\^\fP \*(CK
Resume the next iteration of the enclosing
.BR for
.BR while
.BR until
or
.B select\^
loop.
If
.I n\^
is specified then resume at the
.IR n -th
enclosing loop.
.TP
.PD 0
\f3cd\fP \*(OK \f2arg\^\fP \*(CK
.TP
\f3cd\fP  \f2old\^\fP \f2new\^\fP
.PD
This command can be in either of two forms.
In the first form it
changes the current directory to
.IR arg .
If
.I arg\^
is
.B \-
the directory is changed to the previous
directory.
The shell
parameter
.B
.SM HOME
is the default
.IR arg .
The parameter
.SM
.B PWD
is set to the current directory.
The shell parameter
.B
.SM CDPATH
defines the search path for
the directory containing 
.IR arg .
Alternative directory names are separated by
a colon
.RB ( : ).
The default path is
.B <null>
(specifying the current directory).
Note that the current directory is specified by a null path name,
which can appear immediately after the equal sign
or between the colon delimiters anywhere else in the path list.
If 
.I arg
begins with a \f3/\fP then the search path
is not used.
Otherwise, each directory in the path is
searched for
.IR arg .
.P
The second form of
.B cd
substitutes the string
.I new
for the string
.I old
in the current directory name,
.SM
.B PWD
and tries to change to this new directory.
.P
The
.B cd\^
command may not be executed by
the restricted shell.
.TP
\f3eval\fP \*(OK \f2arg\^\fP .\|.\|. \*(CK
The arguments are read as input
to the shell
and the resulting command(s) executed.
.TP
\f3exec\fP \*(OK \f2arg\^\fP .\|.\|. \*(CK
Parameter assignments remain in effect after the command completes.
If
.I arg\^
is given,
the command specified by
the arguments is executed in place of this shell
without creating a new process.
Input/output arguments may appear and
affect the current process.
If no 
arguments are given
the effect of this command is to
modify file descriptors
as prescribed by the input/output redirection list.
In this case,
any file descriptor numbers greater than 2 that are
opened with this mechanism are closed when invoking
another program.
.TP
\f3exit\fP \*(OK \f2n\^\fP \*(CK
Causes the shell to exit
with the exit status specified by
.IR n .
If
.I n\^
is omitted then the exit status is that of the last command executed.
An end-of-file will also cause the shell to exit
except for a
shell which has the
.I ignoreeof
option (See
.B set
below) turned on.
.TP
\f3export\fP \*(OK \f2name\^\fP .\|.\|. \*(CK
The given
.IR name s
are marked for automatic
export to the
.I environment\^
of subsequently-executed commands.
.TP
.PD 0
\f3fc\fP \*(OK \f3\-e \f2ename\^\fP \ \*(CK \*(OK \f3\-\f3nlr\^\f1 \*(CK \*(OK \f2first\^\fP \*(CK \*(OK \f2last\^\fP \*(CK
.TP
\f3fc \-e \-\fP  \*(OK \f2old\fP\f3\=\fP\f2new\^\fP \*(CK \*(OK \f2command\^\fP \*(CK
.PD
In the first form,
a range of commands from
.I first\^
to
.I last\^
is selected from the last
.SM
.B HISTSIZE
commands that were typed at the terminal.
The arguments
.I first\^
and
.I last\^
may be specified as a number or as a string.
A string is used to locate the most recent command starting with
the given string.
A negative number is used as an offset to the current command number.
If the flag
.BR \-l ,
is selected,
the commands are listed on standard output.
Otherwise, the editor program
.I ename\^
is invoked on a file containing these
keyboard commands.
If
.I ename\^
is not supplied, then the value of the parameter
.SM
.B FCEDIT
(default /bin/ed)
is used as the editor.
When editing is complete, the edited command(s)
is executed.
.I last\^
is not specified
then it will be set to
.IR first .
If
.I first\^
is not specified
the default is the previous command
for editing and \-16 for listing.
The flag
.B \-r
reverses the order of the commands and
the flag
.B \-n
suppresses command numbers when listing.
In the second form the
.I command\^
is re-executed after the substitution
\f2old\^\fP\f3=\fP\f2new\^\fP
is performed.
.TP
\f3fg\fP \*(OK \f3%\f2job\^\fP \*(CK
This command is only built-in on systems that support job control.
If
.I job\^
is specified it brings it to the foreground.
Otherwise, the current job is
brought into the foreground.
.TP
\f3jobs\fP \*(OK \f3\-l\^\fP \*(CK
Lists the active jobs; given the
.B \-l
options lists process id's in addition to the normal information.
.TP
\f3kill\fP \*(OK \f3\-\f2sig\^\fP \*(CK \f2process\^\fP .\|.\|.
Sends either the TERM (terminate) signal or the
specified signal to the specified jobs or processes.
Signals are either given by number or by names (as given in
.I /usr/include/signal.h,
stripped of the prefix ``SIG'').
The signal names are listed by
.BR "kill \-l'" .
There is no default, saying just `kill' does not
send a signal to the current job.
If the signal being sent is TERM (terminate) or HUP (hangup),
then the job or process will be sent a CONT (continue) signal
if it is stopped.
The argument
.I process\^
can be either a process id or a job.
.TP
\f3let\fP  \f2arg\^\fP .\|.\|.
Each
.I arg
is an
.IR "arithmetic expression"
to be evaluated.
All calculations are done as long
integers and no check for overflow
is performed.
Expressions consist of constants,
named parameters, and operators.
The following set of operators,
listed in order of decreasing precedence,
have been implemented:
.RS
.PD 0
.TP
.B \-
unary minus
.TP
.B !
logical negation
.TP
.B "*  /  %"
.br
multiplication, division, remainder
.TP
.B "+  \-"
addition, subtraction
.TP
.B "<=  >=  <  >"
.br
comparison
.TP
.B "==  !="
.br
equality  inequality
.TP
.B =
arithmetic replacement
.PD
.PP
Sub-expressions in parentheses
.B (\|)
are evaluated first and can be used
to override the above precedence rules.
The evaluation within a precedence group
is from right to left for the
.B =
operator
and from left to right for the others.
.PP
A parameter name must be a valid
.IR identifier .
When a parameter is encountered,
the value associated with the
parameter name is substituted and expression evaluation resumes.
Up to nine levels of recursion are
permitted.
.PP
The return code is
0 if the value of the last expression
is non-zero, and 1 otherwise.
.RE
.TP
\f3newgrp\fP \*(OK \f2arg\^\fP .\|.\|. \*(CK
Equivalent to
.BI "exec newgrp" " arg\^"
\&.\|.\|.\^.
.TP
\f3print\fP \*(OK \f3\-Rnprsu\^\fP\*(OK\f2n\fP \*(CK  \*(CK \*(OK \f2arg\^\fP .\|.\|. \*(CK
The shell output mechanism.
With no flags or with flag
.BR \- ,
the arguments are printed
on standard output as described by
.IR echo (1).
In raw mode,
.B \-R
or
.BR \-r ,
the escape conventions of
.I echo
are ignored.
The
.B \-R
option will print all subsequent arguments and options
other than
.BR \-n .
The
.B \-p
option causes the 
arguments to be written onto the pipe
of the process spawned with
.B \(bv& 
instead of standard output.
The
.B \-s
option causes the 
arguments to be written onto the history file
instead of standard output.
The
.B \-u
flag can be used to specify a one digit
file descriptor unit number
.B n\^
on which the
output will be placed.
The default is 1.
If the flag
.B \-n
is used, no 
.B new-line\^
is added to the output.
.TP
\f3read\fP \*(OK \f3\-prsu\^\fP\*(OK \f2n\^\fP \*(CK \*(CK \*(OK \f2name\f3?\f2prompt\^\f1 \*(CK \*(OK \f2name\^\fP .\|.\|. \*(CK
The shell input mechanism.
One line is read and
is broken up into words using the characters in
.B
.SM IFS
as separators.
In raw mode,
.B \-r,
a 
.B \e
at the end of a line does not signify
line continuation.
The first
word is assigned to the first
.IR name ,
the second word
to the second
.IR name ,
etc., with leftover words assigned to the last
.IR name .
The
.B \-p
option causes the input line
to be taken from the input pipe
of a process spawned by the shell
using
.BR \(bv& .
If the
.B \-s
fag is present,
the input will be saved as a command in the history file.
The flag
.B \-u
can be used to specify a one digit file
descriptor unit to read from.
The file descriptor can be opened with the
.B exec\^
special command.
The default value of
.I n\^
is 0.
If
.IR name
is omitted then
.SM
.B REPLY
is used as the default
.IR name.
The return code is 0 unless an end-of-file is encountered.
An end-of-file with the
.B \-p
option causes cleanup for this process
so that another can be spawned.
If the first argument contains a
.BR ? ,
the remainder of this word is used as a
.I prompt\^
when the shell is interactive.
If the given file descriptor is open for writing
and is a terminal device then the prompt is placed
on this unit.
Otherwise the prompt is issued on file descriptor 2.
The return code is 0 unless an end-of-file is encountered.
.TP
\f3readonly\fP \*(OK \f2name\fP .\|.\|. \*(CK
The given
.IR names
are marked
readonly and these
names cannot be changed
by subsequent assignment.
.TP
\f3return\fP \*(OK \f2n\^\fP \*(CK
Causes a shell
.I function
to return
to the invoking script
with the return status specified by
.IR n .
If
.I n\^
is omitted then the return status is that of the last command executed.
If
.B return
is invoked while not in a
.I function
then it is the same as an
.BR exit .
.TP
\f3set\fP \*(OK \f3\-aefhkmnostuvx\fP \*(CK \*(OK \f3\-o\fP \f2option\^\fP .\|.\|. \*(CK \*(OK \f2arg\^\fP .\|.\|. \*(CK
The flags for this command have meaning as follows:
.RS
.PD 0
.TP 8
.B \-a
All subsequent parameters that are defined are automatically exported.
.TP 8
.B \-e
If the shell is non-interactive and if a command fails,
execute the
.SM
.B ERR
trap, if set,
and exit immediately.
This mode is disabled while reading profiles.
.TP 8
.B \-f
Disables file name generation.
.TP 8
.B \-h
Each command whose name is an
.I identifier\^
becomes a tracked alias when first encountered.
.TP 8
.B \-k
All parameter assignment arguments are placed in the environment for a command,
not just those that precede the command name.
.TP 8
.B \-m
Background jobs will run in a separate process group
and a line will print upon completion.
The exit status of background jobs is reported in a completion message.
On systems with job control,
this flag is turned on automatically for
interactive shells.
.TP 8
.B \-n
Read commands but do not execute them.
.TP 8
.B \-o
The following argument can be one of the following option names:
.RS
.TP 8
.B allexport
Same as
.BR \-a .
.TP 8
.B errexit
Same as
.BR \-e .
.TP 8
.B emacs
Puts you in an
.I emacs
style in-line editor for command entry.
.TP 8
.B gmacs
Puts you in a
.I gmacs
style in-line editor for command entry.
.TP 8
.B ignoreeof
The shell will not exit on end-of-file.
The command
.B exit
must be used.
.TP 8
.B keyword
Same as
.BR \-k .
.TP 8
.B markdirs
All directory names resulting from file name generation have a trailing
.B /
appended.
.TP 8
.B monitor
Same as
.BR \-m .
.TP 8
.B noexec
Same as
.BR \-n .
.TP 8
.B noglob
Same as
.BR \-f .
.TP 8
.B nounset
Same as
.BR \-u .
.TP 8
.B verbose
Same as
.BR \-v .
.TP 8
.B trackall
Same as
.BR \-h .
.TP 8
.B vi
Puts you in insert mode of a
.I vi\^
style in-line editor
until you hit escape character
.BR 033 .
This puts you in move mode.
A return sends the line.
.TP 8
.B viraw
Each character is processed as it is typed
in
.I vi\^
mode.
.TP 8
.B xtrace
Same as
.BR \-x .
.TP 8
   
If no option name is supplied then the current option settings are printed.
.RE
.TP 8
.B \-s
Sort the positional parameters.
.TP 8
.B \-t
Exit after reading and executing one command.
.TP 8
.B \-u
Treat unset parameters as an error when substituting.
.TP 8
.B \-v
Print shell input lines as they are read.
.TP 8
.B \-x
Print commands and their arguments as they are executed.
.TP 8
.B \-
Turns off
.B \-x
and
.B \-v
flags and stops examining arguments for flags.
.TP 8
.B \-\-
Do not change any of the flags; useful in setting
.B $1
to a value beginning with
.BR \- .
If no arguments follow this flag then the positional parameters are unset.
.PD
.PP
Using
.B \+
rather than
.B \-
causes these flags to be turned off.
These flags can also be used upon invocation of the shell.
The current set of flags may be found in
.BR $\- .
The remaining arguments are positional
parameters and are assigned, in order,
.if t to\p
.if n to
.BR $1 ,
.BR $2 ,
\&.\|.\|.\^.
If no arguments are given then the values
of all names are printed on the standard output.
.RE
.TP
\f3shift\fP \*(OK \f2n\^\fP \*(CK
.br
The positional parameters from
\f3$\fP\f2n\fP\f3+1\fP
\&.\|.\|.
are renamed
.B $1
\&.\|.\|.\^
, default
.I n\^
is 1.
The parameter
.I n\^
can be any arithmetic expression that evaluates to a non-negative
number less than or equal to
.BR $# .
.TP
\f3test\fP \*(OK \f2expr\^\fP \*(CK
.br
Evaluate conditional expression
.IR expr .
See
.IR test (1)
for usage and description.
The arithmetic comparison operators
are not restricted to integers.
They allow any arithmetic expression.
Four additional primitive expressions are allowed:
.RS
.PD 0
.TP
\f3\-L\fP \f2file\^\fP
True if
.I file\^
is a symbolic link.
.TP
\f2file1\^\fP \f3\-nt\fP \f2file2\^\fP
True if
.I file1\^
is newer than
.IR file2 .
.TP
\f2file1\^\fP \f3\-ot\fP \f2file2\^\fP
True if
.I file1\^
is older than
.IR file2 .
.TP
\f2file1\^\fP \f3\-ef\fP \f2file2\^\fP
True if
.I file1\^
has the same device and i-node number as
.IR file2 .
.PD
.RE
.TP
\f3times\fP
.br
Print the accumulated user and system times for
the shell and for processes
run from the shell.
.TP
\f3trap\fP \*(OK \f2arg\^\fP \*(CK \*(OK \f2sig\^\fP \*(CK .\|.\|.
.I arg\^
is a command to be read and executed when the shell
receives signal(s)
.IR sig .
(Note that
.I arg\^
is scanned once when
the trap is set and once when the trap
is taken.)
Each
.I sig\^
can be given as a number or as the name of the signal.
Trap commands are executed in order of signal number.
Any attempt to set a trap on a signal that
was ignored on entry to the current shell
is ineffective.
An attempt to trap on signal 11 (memory fault) produces an error.
If
.I arg\^
is omitted or is
.BR \- ,
then all trap(s)
.I sig\^
are reset
to their original values.
If
.I arg\^
is the null
string then this signal is ignored by the shell and by the commands
it invokes.
If
.I sig\^
is
.SM
.B ERR
then
.I arg\^
will be executed whenever a command has a non-zero exit code.
This trap is not inherited by functions.
If
.I sig\^
is
.B 0
or
.SM
.B EXIT
and the
.B trap
statement is executed inside the body of a function,
then the command
.I arg\^
is executed
after the function completes.
If
.I sig\^
is
.B 0
or
.SM
.B EXIT
for a
.B trap
set outside any function
then the command
.I arg\^
is executed
on exit from the shell.
The
.B trap
command
with no arguments prints a list
of commands associated with each signal number.
.TP
\f3typeset\fP \*(OK \f3\-FLRZefilprtux\^\fP\*(OK\f2n\fP \*(CK \*(OK \f2name\fP\*(OK \f2=value\^\fP \*(CK \^ \*(CK  .\|.\|. \*(CK
Parameter assignments remain in effect after the command completes.
When invoked inside a function,
a new instance of the parameter
.I name\^
is created.
The parameter value and type are restored
when the function completes.
The following list of attributes may be specified:
.RS
.PD 0
.TP
.B \-F
This flag provides UNIX to host-name file mapping on non-UNIX
machines.
.TP
.B \-L
Left justify and remove leading blanks from
.IR value .
If
.I n
is non-zero it defines the width 
of the field,
otherwise it is determined by the width of the value of
first assignment.
When the parameter is assigned to, it is
filled on the right with blanks or truncated, if necessary,  to
fit into the field.
Leading zeros are removed if the
.B \-Z
flag is also set.
The
.B \-R
flag is turned off.
.TP
.B \-R
Right justify and fill with leading blanks.
If
.I n
is non-zero it defines the width 
of the field,
otherwise it is determined by the width of the value of
first assignment.
The field is left filled with blanks or
truncated from the end if the
parameter is reassigned.
The
.B L
flag is turned off.
.TP
.B \-Z
Right justify and fill with leading zeros if
the first non-blank character is a digit and the
.B \-L
flag has not been set.
If
.I n
is non-zero it defines the width 
of the field,
otherwise it is determined by the width of the value of
first assignment.
.TP
.B \-e
Tag the parameter as having an error.
This tag is currently unused by the shell and can be
set or cleared by the user.
.TP
.B \-f
The names refer to function names rather than
parameter names.
No assignments can be made and the only other
valid flag is
.BR \-x .
.TP
.B \-i
Parameter is an integer.
This makes arithmetic faster.
If
.I n
is non-zero it defines the output arithmetic base, 
otherwise the first assignment determines the output base.
.TP
.B \-l
All upper-case characters
converted to lower-case.
The upper-case flag,
.B \-u
is turned off.
.TP
.B \-p
The output of this command, if any,  is written onto the two-way pipe
.TP
.B \-r
The given
.IR names
are marked
readonly and these
names cannot be changed
by subsequent assignment.
.TP
.B \-t
Tags the named parameters.
Tags are user definable and have no special
meaning to the shell.
.TP
.B \-u
All lower-case characters are converted
to upper-case characters.
The lower-case flag,
.B \-l
is turned off.
.TP
.B \-x
The given
.IR name s
are marked for automatic
export to the
.I environment\^
of subsequently-executed commands.
.PD
.PP
Using
.B \+
rather than
.B \-
causes these flags to be turned off.
If no
.I name\^
arguments are given but flags are specified,
a list of
.I names\^
(and optionally the
.I values\^
)
of the
.I parameters\^
which have these
flags set
is printed.
(Using
.B \+
rather than
.B \-
keeps the
values to be printed.)
If no
.IR name s
and flags
are given,
the
.I names\^
and
.I attributes\^
of all
.I parameters\^
are printed.
.RE
.TP
\f3ulimit\fP \*(OK \f3\-cdfmpt\fP \*(CK \*(OK \f2n\^\fP \*(CK
.RS
.PD 0
.TP
.B \-c
imposes a size limit of 
.I n\^
blocks on the size of core dumps
(\s-1BSD\s+1 only).
.TP
.B \-d
imposes a size limit of 
.I n\^
blocks on the size of the data area
(\s-1BSD\s+1 only).
.TP
.B \-f
imposes a size limit of 
.I n\^
blocks on files written by child processes (files of any size may be read).
.TP
.B \-m
imposes a soft limit of 
.I n\^
blocks on the size of physical memory
(\s-1BSD\s+1 only).
.TP
.B \-p
changes the pipe size to
.I n\^
(\s-1UNIX\s+1/\s-1RT\s+1 only).
.TP
.B \-t
imposes a time limit of 
.I n\^
seconds to be used by each process
(\s-1BSD\s+1 only).
.PD
.PP
If no option is given,
.B \-f
is assumed.
If
.I n\^
is not given the current limit is printed.
.RE
.TP
\f3umask\fP \*(OK \f2nnn\^\fP \*(CK
The user file-creation mask is set to
.I nnn\^
(see
.IR umask (2)).
If
.I nnn\^
is omitted, the current value of the mask is printed.
.TP
\f3unalias\fP \f2name\^\fP .\|.\|.
The
.IR
parameters
given by the list of
.IR name s
are removed from the
.I alias\^
list.
.TP
\f3unset\fP \*(OK \f3\-f\fP \*(CK \f2name\^\fP .\|.\|.
The parameters given by the list of
.IR name s
are unassigned,
i. e.,
their values and attributes are erased.
Readonly variables cannot be unset.
If the flag,
.BR \-f ,
is set, then the names refer to
.I function\^
names.
.TP
\f3wait\fP \*(OK \f2n\^\fP \*(CK
Wait for the specified process and
report its termination status.
If
.I n\^
is not given then all currently active child processes are waited for.
The return code from this command is that of
the process waited for.
.TP
\f3whence\fP \*(OK \f3\-v\fP \*(CK \f2name\^\fP .\|.\|.
For each
.IR name ,
indicate how it
would be interpreted if used as a command name.
.P
The flag,
.BR \-v ,
produces a more verbose report.
.SS Invocation.
If the shell is invoked by
.IR exec (2),
and the first character of argument zero
.RB ( $0 )
is
.BR \- ,
then the shell is assumed to be a
.I login
shell and
commands are read from
.B /etc/profile
and then from either
.B .profile
in the current directory or
.BR \s-1$HOME\s+1/.profile ,
if either file exists.
Next, commands are read from
the file named by
performing parameter substitution on
the value of the environment parameter
.SM
.B ENV
if the file exists.
Commands are then read as described below;
the following flags are interpreted by the shell
when it is invoked:
.PP
.PD 0
.TP 10
.BI \-c "\| string\^"
If the
.B \-c
flag is present then
commands are read from
.IR string .
.TP
.B \-s
If the
.B \-s
flag is present or if no
arguments remain
then commands are read from the standard input.
Shell output,
except for the output of some of the 
.I Special commands\^
listed above,
is written to
file descriptor 2.
.TP
.B \-i
If the
.B \-i
flag is present or
if the shell input and output are attached to a terminal (as told by
.IR gtty (2))
then this shell is
.IR interactive .
In this case \s-1TERMINATE\s+1 is ignored (so that \f3kill 0\fP
does not kill an interactive shell) and \s-1INTERRUPT\s+1 is caught and ignored
(so that
.B wait
is interruptible).
In all cases, \s-1QUIT\s+1 is ignored by the shell.
.TP
.B \-r
If the
.B \-r
flag is present the shell is a restricted shell.
.PD
.PP
The remaining flags and arguments are described under the
.B set
command above.
.SS Restricted Shell Only.
The "restricted shell"
is used to set up login names and execution environments whose
capabilities are more controlled than those of the standard shell.
The actions of
the restricted shell
are identical to those of
.IR \*(sh ,
except that the following are disallowed:
.RS
.PD 0
.PP
changing directory (see
.IR cd (1)),
.br
setting the value of
.SM
.B SHELL
or
.SM
.BR PATH\*S,
.br
specifying path or
command names containing
.BR / ,
.br
redirecting output
.RB ( >
and
.BR >> ).
.PD
.RE
.PP
The restrictions above are enforced
after \f3.profile\fP and the
.SM
.B ENV
files are interpreted.
.PP
When a command to be executed is found to be a shell procedure,
the restricted shell
invokes
.I sh\^
to execute it.
Thus, it is possible to provide to the end-user shell procedures 
that have access to the full power of
the standard shell,
while imposing a limited menu of commands;
this scheme assumes that the end-user does not have write and
execute permissions in the same directory.
.PP
The net effect of these rules is that the writer of the
.B .profile
has complete control over user actions,
by performing guaranteed setup actions
and leaving the user in an appropriate directory
(probably
.I not\^
the login directory).
.PP
The system administrator often sets up a directory
of commands
(i.e.,
.BR /usr/rbin )
that can be safely invoked by
the restricted shell.
Some systems also provide a restricted editor
.IR red .
.SH EXIT STATUS
Errors detected by the shell, such as syntax errors,
cause the shell
to return a non-zero exit status.
If the shell is being used non-interactively
then execution of the shell file is abandoned.
Otherwise, the shell returns the exit status of
the last command executed (see also the
.B exit
command above).
.SH FILES
/etc/passwd
.br
/etc/profile
.br
\s-1$HOME\s+1/\f3.\fPprofile
.br
/tmp/sh\(**
.br
/dev/null
.SH SEE ALSO
cat(1),
cd(1),
echo(1),
emacs(1),
env(1),
gmacs(1),
newgrp(1),
test(1),
umask(1),
vi(1),
dup(2),
exec(2),
fork(2),
gtty(2),
pipe(2),
signal(2),
umask(2),
ulimit(2),
wait(2),
rand(3),
a.out(5),
profile(5),
environ(7).
.SH AUTHOR
David Korn
.SH CAVEATS
.PP
If a command which is a
.I "tracked alias"
is executed, and then a command with the same name is 
installed in a directory in the search path before the directory where the
original command was found, the shell will continue to 
.I exec\^
the original command.
Use the 
.B \-t
option of the
.B alias\^
command to correct this situation
.PP
If you move the current directory or one above it, 
.B pwd\^
may not give the correct response.
Use the 
.B cd\^
command with a full path name
to correct this situation.
.PP
Some very old shell scripts contain a
.B ^
as a synonym for the pipe character
.BR \(bv .
