4 .SA 1 \" right justified
5 .ND "December 21, 1993"
6 .TL "311466-6713" "61175" \" charging case filing case
7 Introduction to \f5ksh-93\fP
8 .AU "David G. Korn" DGK MH 11267 7975 3C-526B "(research!dgk)"
9 .TM 11267-931221-26 \" technical memo + TM numbers
11 .OK Shell "Command interpreter" Language UNIX \" keyword
12 .AS 2 \" abstract start for TM
14 major rewrite of \f5ksh\fP,
15 a program that serves as a command language
18 UNIX is a registered trademark of Novell.
21 As with \f5ksh\fP, \f5ksh-93\fP
22 is essentially compatible with the System V version of the Bourne shell\*(Rf,
25 .I "An Introduction to the UNIX
27 BSTJ - Vol. 57, No. 6 part 2, pages 1947-1972, 1978.
29 and compatible with previous versions of \f5ksh\fP.
30 \f5ksh-93\fP is intended to comply with the IEEE POSIX 1003.2
33 .I "POSIX \- Part 2: Shell and Utilities,"
34 IEEE Std 1003.2-1992, ISO/IEC 9945-2, IEEE, 1993.
37 In addition to changes in the language required
38 by these standards, the primary focus of \f5ksh-93\fP
39 is related to shell programming.
40 \f5ksh-93\fP provides the programming power of several
41 other interpretive languages such as \f5awk\fP\*(Rf,
47 .I "The AWK Programming Language,"
52 Lloyd H. Nakatani and Laurence W. Ruedisueli,
53 .I "The FIT Programming Language Primer",
54 TM 1126-920301-03, 1992.
58 Larry Wall and Randal Schwartz,
59 .I "Programming perl,"
60 O'Reilly & Assoc, 1990.
66 .I "Tcl: An Embeddable Command Language",
67 Proceedings of the Washington USENIX meeting, pp. 133-146, 1990.
71 assumes that the reader is already familiar with the Bourne shell.
72 It introduces most of the features of \f5ksh-93\fP
73 relative to the Bourne shell; both
74 as a command language and as a programming language.
76 a sample script written in \f5ksh-93\fP.
80 The term "shell" is used to describe a program that provides
85 UNIX is a registered trademark of USL
87 system shell is a user level program, and not part of
88 the operating system itself,
89 anyone can write a new shell or modify an existing one.
90 This has caused an evolutionary progress
91 in the design and implementation of shells,
92 with the better ones surviving.
93 The most widely available UNIX system shells are the Bourne shell\*(Rf,
96 .IR "An Introduction to the UNIX Shell" ,
97 Bell System Technical Journal,
98 Vol. 57, No. 6, Part 2, pp. 1947-1972, July 1978.
100 written by Steve Bourne
101 at AT&T Bell Laboratories,
105 .IR "An Introduction to the C Shell" ,
106 Unix Programmer's Manual, Berkeley Software Distribution,
107 University of California, Berkeley, 1980.
109 written by Bill Joy at the University of California, Berkeley,
110 and the KornShell language \*(Rf,
112 Morris Bolsky and David Korn,
113 .IR "The KornShell Command and Programming Language" ,
116 written by David Korn
117 at AT&T Bell Laboratories.
118 The Bourne shell is available on almost all versions of the UNIX
120 The C Shell is available with all Berkeley Software Distribution (BSD) UNIX systems and on many other systems.
122 is available on System V Release 4 systems.
123 In addition, it is available on many other systems.
124 The source for the KornShell language is available from the AT&T Toolchest,
125 an electronic software distribution system.
126 It runs on all known versions of the UNIX system and
127 on many UNIX system look-alikes.
129 There have been several articles comparing the UNIX system shells.
133 .IR "The Korn Shell: An Emerging Standard" ,
134 UNIX/World, pp. 74-81, September 1986.
136 highlights some of the new features
137 introduced by the KornShell language.
141 .IR "Proficiency and Power are Yours With the Korn Shell" ,
142 UNIX/World, pp. 103-107, September 1987.
144 explains some of the advantages of using the KornShell language.
148 .I "Comparing UNIX Shells,"
150 Edited by the Waite Group, Howard W. Sams & Co., 1987.
152 provides a more detailed comparison of the three shells,
153 both as a command language and as a programming language.
155 The KornShell language is a superset of the
156 Bourne shell. The KornShell language has many of the popular C shell features,
157 plus additional features of its own.
158 Its initial popularity stems primarily from its improvements as
160 The primary interactive benefit of the KornShell command language
161 is a visual command line editor that allows you to
162 make corrections to your current command line
163 or to earlier command lines,
164 without having to retype them.
168 the power of the KornShell language as a high-level programming language,
169 as described by Dolotta and Mashey\*(Rf,
171 T. A. Dolotta and J. R. Mashey,
172 .I "Using the shell as a Primary Programming Tool,"
173 Proc. 2nd. Int. Conf. on Software Engineering, 1976,
176 may prove to be of greater significance.
177 \f5ksh-93\fP provides the programming power of several
178 other interpretive languages such as \f5awk\fP,
183 An application that was originally written in the C programming language
184 was rewritten in the KornShell language.
185 More than 20,000 lines of C code were replaced with KornShell scripts
186 totaling fewer than 700 lines.
187 In most instances there was no perceptible difference in performance
188 between the two versions of the code.
190 The KornShell language has been embedded into windowing systems
191 allowing graphical user interfaces to be developed in shell
192 rather than having to build applications that need to be
194 The \f5wksh\fP program\*(Rf
197 .IR "WKSH - Korn Shell with X-Windows Support",
200 provides a method of developing OpenLook or Motif
201 applications as \f5ksh\fP scripts.
203 This memo is an introduction to \f5ksh-93\fP,
204 the program that implements an enhanced version
205 of the KornShell language.
206 It is referred to as \f5ksh\fP in the rest of this memo.
207 The memo describes the KornShell language based on the
208 features of the 12/28/93 release of \f5ksh\fP.
209 This memo is not a tutorial, only an introduction.
210 The second edition of reference [9] gives
211 a more complete treatment of the KornShell language.
213 A concerted effort has been made to achieve both System V Bourne shell
214 compatibility and IEEE POSIX compatibility
215 so that scripts written for either of these shells
216 can run without modification with \f5ksh\fP.
217 In addition, \f5ksh-93\fP attempts to
218 be compatible with older versions of \f5ksh\fP.
219 When there are conflicts between versions of the shell,
220 \f5ksh-93\fP selects the behavior dictated by the IEEE POSIX
222 The description of features in this memo assumes
223 that the reader is already familiar with the Bourne shell.
224 .H 1 "COMMAND LANGUAGE"
225 There is no separate command language.
226 All features of the language, except job control,
228 used both within a script and interactively from a terminal.
229 However, features that are more likely to be used
230 while running commands interactively from a terminal
232 .H 2 "Setting Options"
233 By convention, UNIX commands
234 consist of a command name followed by options and other arguments.
235 Options are either of the form \f5-\fP\fIletter\fP,
236 or \f5-\fP\fIletter value\fP.
237 In the former case, several options may be grouped after a single \f5-\fP.
238 The argument \f5--\fP signifies an end to the option list and is
239 only required when the first non-option argument begins with
241 Most commands print an error message which
242 shows which options are permitted
243 when given incorrect arguments.
244 In addition, the option sequence \f5-?\fP causes most commands
245 to print a usage message which lists the valid options.
247 Ordinarily, \f5ksh\fP executes a command by
248 using the command name to locate a program to run
249 and by running the program as a separate process.
250 Some commands, referred to as
252 are carried out by \f5ksh\fP itself,
253 without creating a separate process.
254 The reasons that some commands are built-in are presented later.
255 In nearly all cases the distinction
256 between a command that is built-in and one that
257 is not is invisible to the user.
259 all commands that are built-in follow command line conventions.
261 \f5ksh\fP has several options that can be set by the user
262 as command line arguments at invocation and as option arguments to the
264 Most other options can be set with a single letter option or as a name
265 that follows the \f5-o\fP option.
268 to display the current option settings.
269 Some of these options, such as
276 are enabled automatically by \f5ksh\fP
277 when the shell is connected to a terminal device.
278 Other options, such as
282 are normally placed in a startup file.
287 to print an error message when you use
289 to redirect output to a file that already exists.
290 If you want to redirect to an existing file, then
300 is used to prevent the
305 from exiting the shell and possibly logging you out.
306 You must type \f5exit\fP
308 Most of the options are described in this memo as appropriate.
309 .H 2 "Command Aliases"
311 Command aliases provide a mechanism of associating a command name and
312 arguments with a shorter name.
313 Aliases are defined with the \f5alias\fP
315 The form of an \f5alias\fP
316 command definition is:
318 \f5alias\fP \fIname\fP\f5=\fP\fIvalue\fP
319 As with most other shell assignments, no space is allowed before or after
321 The characters of an alias name cannot be characters that are
322 special to the shell.
323 The replacement string,
325 can contain any valid shell script,
326 including meta-characters such as pipe symbols and i/o-redirection
327 provided that they are quoted.
332 cannot take arguments.
333 The equivalent functionality of aliases with arguments can
334 be achieved with shell functions, described later.
336 As a command is being read,
337 the command name is checked against a list of
341 the name is replaced by the alias value associated with the
344 When rescanning the value for an alias, alias substitutions
345 are performed except for an alias that is currently being processed.
346 This prevents infinite loops in alias substitutions.
347 For example with the aliases, \f5alias\ l=ls\ 'ls=ls\ -C'\fP,
348 the command name \f5l\fP becomes \f5ls\fP, which becomes \f5ls\ -C\fP.
349 Ordinarily, only the command name word is processed for alias substitution.
350 However, if the value of an alias ends in a space,
351 then the word following the alias is also checked for alias substitution.
352 This makes it possible
353 to define an alias whose first argument is the name of a command
354 and have alias substitution performed on this argument,
356 \f5nohup='nohup\ '\fP.
358 Aliases can be used to redefine built-in commands so that
361 \f5alias test=./test\fP
362 can be used to look for \f5test\fP
363 in your current working directory rather than
364 using the built-in \f5test\fP command.
365 Reserved words such as
366 \f5for\fP and \f5while\fP
367 cannot be changed by aliasing.
368 The command \f5alias\fP,
369 without arguments, generates
370 a list of aliases and corresponding alias values.
371 The \f5unalias\fP command removes the name and text of an alias.
373 Aliases are used to save typing and to improve readability of scripts.
374 Several aliases are predefined by \f5ksh\fP.
375 For example, the predefined alias
377 \f5alias integer='typeset -i'\fP
378 allows the integer variables \f5i\fP and \f5j\fP
379 to be declared and initialized with the command
381 \f5integer i=0 j=1\fP
383 While aliases can be defined in scripts,
384 it is not recommended.
385 The location of an alias command can be important
386 since aliases are only processed when a command is read.
388 procedure (the shell equivalent of an include file)
389 is read all at once (unlike
391 which are read a command at
392 a time) so that any aliases defined there will not effect any commands
394 Predefined aliases do not have this problem.
395 .H 2 "Command Re-entry"
397 When run interactively,
399 commands you type at a terminal in a file.
401 \fB\s-1HISTFILE\s+1\fP
402 is set to the name of a file to which the user
404 then the commands are stored in this
408 \fB$\s-1HOME\s+1/.sh_history\fP
409 is checked for write access and if this fails
410 an unnamed file is used to hold the history lines.
411 Commands are always appended to this file.
412 Instances of \f5ksh\fP
413 that run concurrently and use the same history file
414 name, share access to the history file so that a command
415 entered in one shell will be available for editing in another
417 The file may be truncated when \f5ksh\fP
418 determines that no other shell is using the history file.
419 The number of commands accessible to the user is determined by the value of the
420 \fB\s-1HISTSIZE\s+1\fP
421 variable at the time the shell is invoked.
422 The default value is 256.
423 Each command may consist of one or more lines since a compound
424 command is considered one command.
431 then it is replaced by the command number each time the prompt is given.
433 A built-in command named \f5hist\fP
434 is used to list and/or edit
435 any of these saved commands.
438 is used to specify listing of previous commands.
439 The command can always be specified with
440 a range of one or more commands.
441 The range can be specified by giving the command
442 number, relative or absolute, or by giving
443 the first character or characters of the command.
444 When given without specifying the range,
446 commands are listed, each
447 preceded by the command number.
449 If the listing option is not selected,
450 then the range of commands specified,
451 or the last command if no range is given,
452 is passed to an editor program before
453 being re-executed by \f5ksh\fP.
454 The editor to be used may be specified
457 and following it with the editor name.
458 If this option is not specified, the
459 value of the shell variable
460 \fB\s-1HISTEDIT\s+1\fP
461 is used as the name of the editor,
462 providing that this variable has a non-null value.
463 If this variable is not set, or is null,
466 option has not been selected,
470 When editing has been complete,
471 the edited text automatically becomes
472 the input for \f5ksh\fP.
473 As this text is read by \f5ksh\fP, it is echoed onto the terminal.
477 option causes the editing to be bypassed
478 and just re-executes the command.
479 In this case only a single command can be specified as the range
480 and an optional argument of the form
481 \fIold\fP\fB=\fP\fInew\fP
482 may be added which requests a simple string substitution
486 \f5alias r='hist -s'\fP
487 has been pre-defined so that
488 the single key-stroke
490 can be used to re-execute the previous command
491 and the key-stroke sequence,
493 can be used to re-execute the last command that starts with
495 with the first occurrence of the string \f5abc\fP
496 replaced with the string \f5def\fP.
499 re-executes the most recent command starting with the letter \f5c\fP,
500 with standard output redirected to
502 .H 2 "In-line editing"
504 Lines typed from a terminal frequently need changes made
505 before entering them.
506 With the Bourne shell the only method to fix up commands
507 is by backspacing or killing the whole line.
508 \f5ksh\fP offers options that allow the user to edit parts of the
509 current command line before submitting the command.
510 The in-line edit options make the command line into a single
511 line screen edit window.
512 When the command is longer than the width of the terminal,
513 only a portion of the command is visible.
514 Moving within the line automatically makes that portion visible.
515 Editing can be performed on this window until the
518 The editing modes have editing directives that access the history file
519 in which previous commands are saved.
520 A user can copy any of the most recent
521 \fB\s-1HISTSIZE\s+1\fP
522 commands from this file into the input edit window.
523 You can locate commands by searching or by position.
525 The in-line editing options do not use the
530 They work on most standard terminals.
531 They only require that the backspace character moves the cursor left
532 and the space character overwrites the current character on the screen
533 and moves the cursor to the right.
534 Very few terminals or terminal emulators do not have
537 There is a choice of editor options.
543 option is selected by turning on the
545 option of the \f5set\fP
551 variables ends with any of these suffixes
552 the corresponding option is turned on.
553 A large subset of each of these editors'
554 features is available within the shell. Additional
555 functions, such as file name completion, have also been added.
561 mode the user positions the cursor to the point
562 needing correction and inserts, deletes, or replaces
563 characters as needed.
564 The only difference between these two modes is the
565 meaning of the directive
567 Control keys and escape sequences are used for cursor
568 positioning and control functions.
569 The available editing functions are listed in the manual page.
574 starts in insert mode and enters control mode when the
575 user types ESC ( 033 ).
578 key, which submits the current command for processing,
579 can be entered from either mode.
580 The cursor can be anywhere on the line.
581 A subset of commonly used
583 editing directives are available.
588 directives that normally move up and down by one
593 copying the command into the input edit window.
594 For reasons of efficiency,
595 the terminal is kept in canonical mode until an
599 and on earlier versions of the UNIX operating system,
600 this doesn't work correctly.
609 must be used in this case.
611 Most of the code for the editing options does not rely on the
612 \f5ksh\fP code and can be used in a stand-alone mode with most any command
613 to add in-line edit capability.
615 all versions of the in-line editors have some features that
616 use some shell specific code. For example,
617 with all edit modes, the
619 directive applied to command words
620 (the first word on the line,
621 or the first word after a
627 lists all aliases, functions, or commands
628 that match the portion of the given current word.
629 When applied to other words, this directive
630 prints the names of files that match the current
632 The ESC\fB-*\fP directive
633 adds the expanded list of matching files to the command line.
636 is added to the word if it doesn't contain any file pattern matching
637 characters before the expansion.
644 indicates command completion when applied to
645 command names, otherwise it indicates pathname completion.
646 With command or pathname completion,
647 the list generated by the
648 ESC-= directive is examined to find
649 the longest common prefix.
650 With command completion, only the last component of
651 the pathname is used to compute the longest command prefix.
652 If the longest common prefix is a complete match,
653 then the word is replaced by the pathname, and a
656 pathname is a directory, otherwise a space is added.
661 from control mode gives the same behavior.
664 It is possible to intercept keys as they are entered and
665 apply new meanings or bindings.
668 is evaluated each time
669 \f5ksh\fP processes characters entered
671 other than those typed
672 while entering a search string or an argument to an
673 edit directive such as
676 The action associated with this trap can change the value of
677 the entered key to cause the key to perform a different
684 variable contains the contents of the current input line
685 and the \fB.sh.edcol\fP
686 variable gives the current cursor position within this line.
688 variable contains the
690 character when the trap is entered from
693 Otherwise, this value is null.
695 variable contains the character or
696 escape sequence that caused the trap.
697 A key sequence is either a single character,
699 followed by a single character,
702 followed by a single character.
703 In the \fBvi\fP edit mode,
704 the characters after the
706 must be entered within half a second after the
708 The value of \fB.sh.edchar\fP
709 at the end of the trap will be used as
712 Using the associative array facility of \f5ksh\fP described later,
713 and the function facility of \f5ksh\fP, it is easy to write
714 a single trap so that keys can be bound dynamically. For example,
719 \f5typeset -A Keytable
720 trap 'eval "${Keytable[${.sh.edchar}]}"' KEYBD
721 function keybind # key action
723 typeset key=$(print -f "%q" "$2")
725 2) Keytable[$1]='.sh.edchar=${.sh.edmode}'"$key"
727 1) unset Keytable[$1]
729 *) print -u2 "Usage: $0 key [action]"
739 The job control mechanism
740 is almost identical to the version introduced in \f5csh\fP
741 of the Berkeley UNIX operating system,
742 version 4.1 and later.
743 The job control feature allows the user to stop and
744 restart programs, and to move programs to and from the
745 foreground and the background.
746 It will only work on systems that provide support for
749 even systems without job control have a
751 option which, when enabled, will report the progress
752 of background jobs and enable the user to \f5kill\fP
753 jobs by job number or job name.
755 An interactive shell associates a
757 with each pipeline typed in from the terminal
758 and assigns it a small integer number
759 called the job number.
760 If the job is run asynchronously,
761 the job number is printed at the terminal.
762 At any given time, only one job owns the terminal,
763 i.e., keyboard signals are only sent to the processes in one job.
764 When \f5ksh\fP creates a foreground job,
765 it gives it ownership of the terminal.
766 If you are running a job and wish to stop
772 signal to all processes in the current job.
773 The shell receives notification that the processes
774 have stopped and takes back control of the terminal.
776 There are commands to continue programs in the foreground
778 There are several ways to refer to jobs.
781 introduces a job name.
782 You can refer to jobs by name or number as described in the manual page.
783 The built-in command \f5bg\fP
784 allows you to continue a job in the background,
785 while the built-in command \f5fg\fP
786 allows you to continue a job in the foreground even
787 though you may have started it in the background.
789 A job being run in the background will stop if it tries
790 to read from the terminal.
791 It is also possible to stop background jobs that try to write on
792 the terminal by setting the terminal options
795 There is a built-in command \f5jobs\fP
796 that lists the status of all running and stopped jobs.
798 you are informed of the change of state (running or stopped)
800 jobs just before each prompt.
801 If you want to be notified about background job completions
802 as soon as they occur without waiting for a prompt, then use the
805 When you try to exit the shell while jobs are stopped or running,
806 you will receive a message from \f5ksh\fP.
807 If you ignore this message and try to exit again,
808 all stopped processes will be terminated.
809 In addition, for login shells, the
811 signal will be sent to
813 unless the job has been disowned with the
817 A built-in version of \f5kill\fP
818 makes it possible to use
820 numbers as targets for signals.
821 Signals can be selected by number or name.
822 The name of the signal is the name found in the
825 .B /usr/include/sys/signal.h
832 provides a means to map individual signal names to and from
834 In addition, if no signal name or number is given,
836 generates a list of valid signal names.
837 .H 2 "Changing Directories"
840 maintains a logical view of the file system hierarchy
841 which makes symbolic links transparent.
842 For systems that have symbolic links,
843 this means that if \f5/bin\fP is a symbolic link to \f5/usr/bin\fP
844 and you change directory to \f5/bin\fP, \f5pwd\fP will indicate
845 that you are in \f5/bin\fP, not \f5/usr/bin\fP.
847 generates the physical pathname of the present working
848 directory by resolving all the symbolic links.
851 command will take you where you expect to go even if you cross
853 A subsequent \f5cd\ ..\fP in the example above
854 will place you in \f5/\fP, not \f5/usr\fP.
855 On systems with symbolic links,
859 to be treated physically.
861 \f5ksh\fP remembers your last directory
863 \fB\s-1OLDPWD\s+1\fP.
865 built-in can be given with argument
867 to return to the previous directory
868 and print the name of the directory.
869 Note that \f5cd\ -\fP
870 done twice returns you to the starting directory,
871 not the second previous directory.
874 manager has been written as shell
880 directories from the stack.
884 reads commands from a terminal,
885 it issues a prompt whenever it is ready
886 to accept more input and then
887 waits for the user to respond.
891 can be set to be the number of seconds that the shell will wait for
892 input before terminating.
893 A 60 second warning message is printed
896 The shell uses two prompts.
898 defined by the value of the
901 is issued at the start of each command.
902 The secondary prompt,
903 defined by the value of the
906 is issued when more input is needed to complete a command.
908 \f5ksh\fP allows the user to specify a list of files or directories
909 to check before issuing the
913 \fB\s-1MAILPATH\s+1\fP
916 ) separated list of file names to be checked for changes
917 periodically. The user is notified
918 before the next prompt.
919 Each of the names in this list can be followed by a
921 and a message to be given when a change has been detected in the file.
922 The prompt will be evaluated for parameter expansion, command
923 substitution and arithmetic expansion which are described later.
926 within a mail message will evaluate to the name of the file that
929 \fB\s-1MAILCHECK\s+1\fP
930 is used to specify the minimal interval in seconds before
931 new mail is checked for.
933 In addition to replacing each
935 in the prompt with the command number,
940 for parameter expansions, arithmetic expansions,
941 and command substitutions as described below
942 to generate the prompt.
943 The expansion characters that are to be applied when
944 the prompt is issued must be quoted to prevent the
945 expansions from occurring when assigning the value to
948 \f3\s-1PS1\s+1="$\s-1PWD\s+1"\fP
951 to be set to the value of
953 at the time of the assignment whereas
954 .B \s-1PS1\s+1='$\s-1PWD\s+1'
957 to be expanded at the time the prompt is issued.
959 Command substitution may require a separate process
960 to execute and cause the prompt display to be somewhat
962 when the return key is pressed several times in a row.
967 Some variables are maintained by \f5ksh\fP
968 so that their values can be used with
972 variable stores the pathname of the current working directory.
976 is the value of the most
977 recent assignment plus the elapsed time.
978 By default, the time is measured in milli-seconds,
981 is a floating point variable, the
982 number of places after the decimal point in the expanded
985 \f5typeset\ -F\fP\fIplaces\fP\f5\ SECONDS\fP.
986 In a roundabout way, this variable
987 can be used to generate a time stamp into the
989 prompt without creating a process at each prompt.
990 The following code explains how you can do this on
991 System V. On BSD, you need a different command to initialize
999 # . this script and use $TIME as part of your PS1 string to
1000 # get the time of day in your prompt
1001 typeset -RZ2 _x1 _x2 _x3
1002 (( SECONDS=$(date '+3600*%H+60*%M+%S') ))
1003 _s='_x1=(SECONDS/3600)%24,_x2=(SECONDS/60)%60,_x3=SECONDS%60,0'
1004 TIME='"${_d[_s]}$_x1:$_x2:$_x3"'
1005 # PS1=${TIME}whatever
1011 .H 2 "Tilde substitution"
1015 at the beginning of a word has special meaning to \f5ksh\fP.
1016 If the characters after the
1020 match a user login name in the password database, then the
1022 and the name are replaced by
1023 that user's login directory.
1024 If no match is found, the original word
1028 by itself, or in front of a
1030 is replaced by the value of the
1039 is replaced by the value of
1044 .H 2 "Output formats"
1045 The output of built-in commands and traces have values quoted so that they
1046 can be re-input to the shell.
1047 This makes it easy to cut and paste shell output on systems
1048 which use a pointing device such as a mouse.
1049 In addition, output can be saved in a file for reuse.
1051 .H 2 "The \fB\s-1ENV\s+1\fP file"
1052 When an interactive \f5ksh\fP starts, it evaluates the
1054 variable to arrive at a file name.
1055 If this value is not null,
1056 \f5ksh\fP attempts to read and process
1057 commands in a file by this name.
1058 Earlier versions of \f5ksh\fP read the \fB\s-1ENV\s+1\fP file
1059 for all invocations of the shell primarily to allow
1060 function definitions to be available for all shell
1062 The function search path, \fB\s-1FPATH\s+1\fP, described later,
1063 eliminated the primary need for this capability and it was
1064 removed because the high performance cost was no longer
1066 .H 1 "PROGRAMMING LANGUAGE"
1067 The KornShell vastly extends the set of applications that
1068 can be implemented efficiently at the shell level.
1069 It does this by providing simple yet powerful mechanisms
1070 to perform arithmetic, pattern matching,
1071 substring generation,
1073 Users can write applications as separate functions that can
1074 be defined in the same file or in a library of functions
1075 stored in a directory and loaded on demand.
1076 .H 2 "String Processing"
1077 The shell is primarily a string processing language.
1078 By default, variables hold variable length strings.
1079 There are no limits to the length of strings. Storage
1080 management is handled by the shell automatically.
1081 Declarations are not required.
1082 With most programming languages, string constants are designated
1083 by enclosing characters in single quotes or double quotes.
1084 Since most of the words in the language are strings, the shell
1085 requires quotes only when a string contains characters that
1086 are normally processed specially by the shell, but their
1087 literal meaning is intended.
1088 However, since the shell is a string processing language,
1089 and some characters can occur as literals and as language metacharacters,
1090 quoting is an important part of the language.
1092 There are four quoting mechanisms in \f5ksh\fP.
1093 The simplest is to enclose a sequence of characters inside single quotes.
1094 All characters between a pair of single quotes have their literal meaning;
1095 the single quote itself cannot appear.
1098 immediately preceding
1099 a single quoted string
1100 causes all the characters until the matching single quote
1101 to be interpreted as an ANSI-C language string.
1102 Thus, \f5'\en'\fP represents characters \f5\e\fP and
1103 \f5n\fP, whereas, \f5$'\en'\fP
1104 represents the new-line character.
1105 Double quoted strings remove the special meaning of all characters
1111 so that parameter expansion and command substitution (defined below)
1113 The final mechanism for quoting a character is by preceding it with the
1116 This mechanism works outside of quoted strings and for the characters
1122 in double quoted strings.
1124 Variables are designated by
1126 strings of alphanumeric
1127 characters beginning with an alphabetic character
1128 separated by a \fB\s+2.\s-2\fP.
1129 Upper and lower case characters are distinct, so that the variable
1133 are names of different variables.
1135 limit to the length of the name of a variable.
1136 You do not have to declare variables.
1137 You can assign a value to a variable by writing the name of the
1138 variable, followed by an equal sign, followed by a character string
1139 that represents its value.
1140 To create a variable whose name
1141 contains a \fB\s+2.\s-2\fP,
1142 the variable whose name consists of
1143 the characters before the last \fB\s+2.\s-2\fP
1145 You reference a variable by
1146 putting the name inside curly braces and
1147 preceding the braces with a dollar sign.
1148 The braces may be omitted when the name
1150 If \f5x\fP and \f5y\fP
1151 are two shell variables, then
1152 to define a new variable,
1155 the concatenation of the values of
1156 \f5x\fP and \f5y\fP,
1163 can be thought of as meaning
1165 You can also capture the output of any command with the notation
1167 This is referred to as command substitution.
1170 assigns the output from the \f5date\fP
1171 command to the variable \f5x\fP.
1172 Command substitution in the
1173 Bourne shell is denoted by enclosing the command between
1178 complicated quoting rules.
1179 Thus, it is hard to write \f5sed\fP
1180 patterns which contains back slashes within command substitution.
1181 Putting the pattern in single quotes
1183 \f5ksh\fP accepts the Bourne shell command substitution syntax
1184 for backward compatibility.
1188 the \fIcommand\fP itself to contain quoted strings even if the substitution
1189 occurs within double quotes. Nesting is legal.
1191 The special command substitution of the form
1195 which is faster because
1197 command doesn't have to run.
1198 .H 2 "Shell Parameters and Variables"
1200 There are three types of parameters used by \f5ksh\fP,
1201 special parameters, positional parameters, and named
1202 parameters which are called variables.
1203 \f5ksh\fP defines the same special parameters,
1213 as in the Bourne shell.
1215 Positional parameters are set when the shell is invoked,
1216 as arguments to the \f5set\fP built-in,
1217 and by calls to functions (see below) and \fB\s+2.\s-2\fP
1219 They are named by numbers starting at 1.
1221 The third type of parameter is a variable.
1222 As mentioned earlier,
1223 \f5ksh\fP uses variables whose names
1224 consist of one or more
1225 alpha-numeric strings separated by a \fB\s+2.\s-2\fP.
1226 There is no need to specify the
1228 of a variable in the shell because, by default,
1229 variables store strings of arbitrary length
1230 and values will automatically be converted to numbers
1231 when used in an arithmetic context.
1232 However, \f5ksh\fP variables
1233 can have one or more
1235 that control the internal representation of the variable,
1236 the way the variable is printed, and its access or
1240 allows variables to represent arrays of values
1241 and references to other variables.
1243 built-in command of \f5ksh\fP
1244 assigns attributes to variables.
1245 Two of the attributes,
1249 are available in the Bourne shell.
1250 Most of the remaining attributes are discussed here.
1251 The complete list of attributes appears in the manual.
1253 built-in of \f5ksh\fP removes
1254 values and attributes of variables.
1255 When a variable is exported, certain of its attributes are also exported.
1257 Whenever a value is assigned to a variable,
1258 the value is transformed according to the attributes of the variable.
1259 Changing the attribute of a variable can change its value.
1264 are for left and right field justification respectively.
1265 They are useful for aligning columns in a report.
1266 For each of these attributes, a width can be defined explicitly or else
1267 it is defined the first time an assignment is made to the variable.
1268 Each assignment causes justification of the field, truncating
1270 Assignment to fixed sized variables
1271 provides one way to generate a substring consisting of
1272 a fixed number of characters from
1273 the beginning or end of a string.
1274 Other methods are discussed later.
1280 are used for upper case and lower case
1281 formatting, respectively.
1282 Since it makes no sense to have both attributes on simultaneously,
1283 turning on either of these attributes turns the other off.
1284 The following script,
1285 using \f5read\fP and \f5print\fP which are described later,
1286 provides an example of the use of shell variables
1288 This script reads a file of lines each consisting of five fields separated by
1290 and prints fields 4 and 2 in upper case in columns 1-15, left justified,
1291 and columns 20-25 right-justified respectively.
1296 \f5typeset -uL15 f4 # 15 character left justified
1297 typeset -uR6 f2 # 6 character right justified
1298 IFS=: # set field separator to :
1299 while read -r f1 f2 f3 f4 f5 # read line, split into fields
1300 do print -r -- "$f4 $f2" # print fields 4 and 2
1312 attributes are used to represent numbers.
1313 Each can be followed by a decimal number.
1316 attribute causes the value to be represented as an integer and it
1317 can be followed by a number representing the numeric base when expanding
1319 Whenever a value is assigned to an integer variable, it is evaluated
1320 as an arithmetic expression
1321 and then truncated to an integer.
1325 attribute causes the value to be represented in scientific
1326 notation whenever its value is expanded. The number following the
1328 determines the number of significant figures, and defaults to 6.
1331 attribute causes the value to be represented with a fixed number
1332 of places after the decimal point.
1333 Assignments to variables with the
1337 attributes cause the evaluation of the right hand side of the assignment.
1339 \f5ksh\fP allows one-dimensional
1341 in addition to simple variables.
1342 There are two types of arrays; associative arrays
1344 The subscript for an associative array is an arbitrary
1345 string, whereas the subscript for an indexed array is
1346 an arithmetic expression that is evaluated to yield an integer
1348 Any variable can become an indexed array
1349 by referring to it with
1352 All elements of an array need not exist.
1353 Subscripts for arrays
1355 integer between 0 and some maximum value, otherwise
1357 The maximum value may vary from one machine to another but
1359 Evaluation of subscripts is described in
1361 Attributes apply to the whole array.
1363 Assignments to array variables can be made to individual elements
1365 assignment commands or the
1368 Additionally, values can be assigned sequentially with
1369 compound assignment as described below, or by the
1372 option of the \f5set\fP command.
1373 Referencing of subscripted variables requires the character
1375 but also requires braces around the array element name.
1376 The braces are needed to avoid conflicts with the
1377 file name generation mechanism.
1378 The form of any array element reference is:
1380 .BI ${ name [ subscript ]}
1385 can be used to generate all elements of an array,
1386 as they are used for expansion of positional parameters.
1387 The list of currently defined subscripts for a given
1388 variable can be generated with
1397 attribute causes the variable to be treated
1398 as a reference to the variable defined by its value.
1399 Once this attribute is set, all references to this variable
1400 become references to the variable named by the value
1402 For example, if \f5foo=bar\fP, then setting the reference
1403 attribute on \f5foo\fP will cause all subsequent references
1404 to \f5foo\fP to behave as the variable whose name is \f5$foo\fP
1405 was referenced, which in this case is the variable \f5bar\fP.
1406 Unsetting this attribute breaks the association.
1407 Reference variables are usually used inside functions whose
1408 arguments are the names of shell variables.
1409 The names for reference variables cannot contain a \fB\s+2.\s-2\fP.
1410 Whenever a shell variable is referenced, the portion of the
1411 variable up to the first \fB\s+2.\s-2\fP
1412 is checked to see whether it matches the name of a reference
1414 If it does, then the name of the variable actually used
1415 consists of the concatenation of the name of the variable
1416 defined by the reference plus the remaining portion of the
1417 original variable name.
1418 For example, using the predefined alias, \f5alias\ nameref='typeset\ -n'\fP,
1423 \f5\^.bar.home.bam="hello world"
1424 nameref foo=.bar.home
1426 \fBhello world\fP\fP
1431 .H 2 "Compound Assignment"
1432 Compound assignments are used to assign values to arrays
1433 and compound data structures.
1434 The syntax for a compound assignment is
1435 .IB name =( assignment-list )
1438 is the name of the variable to which you want to assign values.
1439 No space is permitted between the variable name and the \fB=\fP
1440 but can appear between the \fB=\fP and the open parenthesis.
1441 New-lines can appear between the parentheses.
1443 The \fIassignment-list\fP can be in several different forms
1444 yielding different results.
1445 If \fIassignment-list\fP is simply a list of words, then
1446 the words are processed as they are with the \f5for\fP command
1447 and assigned sequentially as an indexed array.
1451 creates an indexed array \f5foo\fP and assigns the
1452 file names in the current directory to each index starting
1455 The second form for \fIassignment-list\fP is a list of assignments
1456 of the special form \fB[\fP\fIword\fP\fB]=\fP\fIword\fP.
1457 No space is permitted before or after the \fB=\fP.
1458 In this case, the variable given by \fIname\fP becomes
1459 an associative array with the given arguments as subscripts.
1462 \f5bar=( [color]=red [shape]=box )\fP
1463 creates an associate array named \f5bar\fP whose
1464 subscripts are \f5color\fP and \f5shape\fP.
1466 The third form for \fIassignment-list\fP is a list of
1467 normal assignments, including compound assignments.
1468 These assignments cause sub-variables to be assigned
1469 corresponding to the given assignments.
1470 In addition to assignments, the \fIassignment-list\fP
1471 can contain \f5typeset\fP commands.
1472 In addition to creating sub-variables,
1473 the effect of a compound assignment is to make
1474 the value of the original variable be a parenthesized
1475 assignment list of its components.
1476 For example, the assignment
1488 colors=( red green yellow )
1495 is equivalent to the assignments
1504 foo.colors=( red green yellow )
1510 In addition, the value of \f5"$foo"\fP is
1516 colors=( red green yellow )
1529 .H 2 "Substring Generation"
1530 The expansion of a variable or parameter can be modified so that
1531 only a portion of the value results.
1532 It is often necessary to extract a portion of a shell variable or
1533 a portion of an array.
1534 There are several parameter expansion operators that can do this.
1535 One method to generate a substring is with an expansion of
1536 the form \fB${\fP\fIname\fP\fB:\fP\fIoffset\fP\fB:\fP\fIlength\fP\fB}\fP
1537 where \fIoffset\^\fP is an arithmetic expression that defines the
1538 offset of the first character starting from 0, and
1539 \fIlength\^\fP is an arithmetic expression that defines the
1540 length of the substring.
1544 the length of the value of
1550 .BI : offset : length
1551 operators can also be applied to array expansions and to parameters
1555 to generate portions of an array.
1556 For example, the expansion, \fB${\fP\fIname\fP\fB[@]:\fP\fIoffset\fP\fB:\fP\fIlength\fP\fB}\fP, yields up to \fIlength\fP elements of the array \fIname\fP
1557 starting at the element \fIoffset\fP.
1559 The other parameter expansion modifiers use shell patterns
1560 to describe portions of the string to modify and delete.
1561 A description of shell patterns is contained below.
1563 modifiers are applied to special parameters
1567 or to array parameters given as
1568 \fIname\fP\fB[@]\fP or \fIname\fP\fB[*]\fP,
1569 the operation is performed on each element.
1570 There are four parameter expansion modifiers that
1571 strip off leading and trailing substrings
1572 during parameter expansion
1573 by removing the characters matching a given pattern.
1575 the form \fB${\fP\fIname\fP\fB#\fP\fIpattern\fP\fB}\fP
1576 causes the smallest matching prefix of the value of
1579 The largest prefix matching
1587 the form \fB${\fP\fIname\fP\fB%\fP\fIpattern\fP\fB}\fP
1588 causes the smallest matching substring at the end of
1595 causes the largest matching trailing substring to be deleted.
1596 For example, if the shell variable
1605 The value of an expansion can be changed by
1606 specifying a pattern that matches the part that needs to be changed
1608 the parameter expansion modifier
1610 An expansion of the form
1611 \fB${\fP\fIname\fP\fB/\fP\fIpattern\fP\fB/\fP\fIstring\fP\fB}\fP
1612 replaces the first match of \fIpattern\fP with
1613 the value of variable \fIname\fP to \fIstring\fP.
1616 is not necessary when \fIstring\fP is null.
1618 \fB${\fP\fIname\fP\fB//\fP\fIpattern\fP\fB/\fP\fIstring\fP\fB}\fP
1619 changes all occurrences of the \fIpattern\fP into \fIstring\fP.
1620 The parameter expansion modifiers
1624 cause the matching pattern to be anchored to the beginning and
1627 Finally, there are parameter expansion modifiers that yield
1628 the name of the variable, the string length of the value, or the number
1629 of elements of an array.
1630 \fB${!\fP\fIname\fP\fB}\fP
1631 yields the name of the variable which will be \fIname\fP itself
1632 except when \fIname\fP is a reference variable. In this case
1633 it will yield the name of the variable it refers to.
1634 When applied to an array variable,
1635 \fB${!\fP\fIname\fP\fB[@]}\fP and
1636 \fB${!\fP\fIname\fP\fB[*]}\fP
1637 generate the names of all subscripts.
1638 \fB${#\fP\fIname\fP\fB}\fP
1639 will be the length in bytes of
1641 For an array variable
1642 \fB${#\fP\fIname\fP\fB[*]}\fP
1643 gives the number of elements in the array.
1644 .H 2 "Arithmetic Evaluation"
1646 For the most part, the shell is a string processing
1647 language. However, the need for arithmetic has
1649 Many of the characters that are special to the
1650 Bourne shell are needed as arithmetic operators.
1651 To make arithmetic easy to use, and to maintain
1652 compatibility with the Bourne shell, \f5ksh\fP uses matching
1656 to delineate arithmetic expressions.
1657 While single parentheses might have been
1658 more desirable, these already mean
1660 so that another notation was required.
1661 The arithmetic expression
1662 inside the double parentheses
1663 follows the same syntax, associativity and precedence
1666 American National Standard for Information Systems \- Programming
1667 Language \- C, ANSI X3.159-1989.
1669 programming language.
1670 The characters between the matching double parentheses
1671 are processed with the same rules used for double
1672 quotes so that spaces can be used to aid readability
1673 without additional quoting.
1675 All arithmetic evaluations are performed using
1676 double precision floating point arithmetic.
1677 Floating point constants follow the same rules as
1678 the ANSI-C programming language.
1679 Integer arithmetic constants are written as
1684 is a decimal integer between
1685 two and sixty-four and
1687 is any non-negative number.
1689 when no base is specified.
1690 The digits are represented by the characters
1692 For bases less than or equal to 36,
1693 upper and lower case characters can
1694 be used interchangeably to represent the digits
1697 Arithmetic expressions are made from constants,
1698 variables, and operators.
1699 Parentheses may be used for grouping.
1700 The contents inside the double parentheses
1701 are processed with the same expansions as occurs in a double quoted string,
1704 expansions are performed before the expression is evaluated.
1705 However, there is usually no need to use the
1707 to get the value of a variable
1708 because the arithmetic evaluator replaces the name of the variable
1709 by its value within an arithmetic expression.
1712 cannot be used when the variable is the subject of assignment
1713 or an increment operation.
1714 As a rule it is better not to use
1716 in front of variables in an arithmetic expression.
1718 An arithmetic command of the form
1722 is a command that evaluates the enclosed arithmetic expression.
1723 For example, the command
1727 increment the variable \f5x\fP,
1728 assuming that \f5x\fP contains some numerical value.
1729 The arithmetic command is true (return value 0), when the resulting
1730 expression is non-zero, and false (return value 1) when the
1731 expression evaluates to zero.
1732 This makes the command easy to use with the \f5if\fP and \f5while\fP
1735 The \f5for\fP compound command
1736 has been extended for use in arithmetic contexts.
1739 \f5for\fP \fB((\fP \fIexpr1\fP\fB;\fP \fIexpr2\fP \fB;\fP \fIexpr3 \fP\fB))\fP
1740 can be used as the first line of a \f5for\fP loop with the same semantics
1741 as the \f5for\fP statement in the ANSI-C programming language.
1743 Arithmetic evaluations can also be performed as part of the evaluation
1749 expands to the value of the enclosed arithmetic expression.
1750 This expansion can occur wherever parameter expansion is performed.
1751 For example using the \f5ksh\fP command \f5print\fP (described
1754 \f5print $((2+2))\fP
1755 prints the number 4.
1757 The following script prints the first
1759 lines of its standard input onto its standard output,
1762 can be supplied as an optional argument whose default value is 20.
1767 \f5integer n=${1-20} # set n
1768 while (( n-- >=0 )) && read -r line # at most n lines
1769 do print -r -- "$line"
1775 .H 2 "Shell Expansions"
1777 The commands you enter from the terminal or from a script
1778 are divided into words and each word undergoes several
1779 expansions to generate the command name and its arguments.
1780 This is done in two phases.
1781 The first phase recognizes reserved words, spaces and operators
1782 to decide where command boundaries lie.
1783 Alias substitutions take place during this phase.
1784 The second phase performs expansions in the following order:
1788 parameter expansion,
1789 arithmetic expansion,
1790 and command substitution
1791 are performed from left to right.
1796 will cause an error to occur when any variable
1797 that is not set is expanded.
1799 The characters that result from parameter expansion and
1800 command substitution above are checked with the characters
1802 \fB\s-1IFS\s+1\fP variable
1805 (See a description of \f5read\fP below to see how
1806 \fB\s-1IFS\s+1\fP is used.)
1808 \fB\s-1IFS\s+1\fP to a null
1809 value causes field splitting to be skipped.
1811 Pathname generation (as described below)
1812 is performed on each of the fields.
1813 Any field that doesn't match a pathname is left alone.
1818 is used to disable pathname generation.
1820 .H 2 "Pattern Matching"
1821 The shell is primarily a string processing language and uses
1822 patterns for matching file names as well as for matching strings.
1828 are processed specially
1829 by the shell when not quoted.
1830 These characters are used to form patterns that
1832 Patterns are used by the shell to match pathnames,
1833 to specify substrings,
1839 matches any one character.
1842 matches zero or more characters.
1843 The character sequence
1845 defines a character class
1846 that matches any character contained within
1848 A range of characters can be specified by putting a
1850 between the first and last character of the range.
1851 An exclamation mark,
1853 immediately after the
1855 means match all characters except the characters specified.
1856 For example, the pattern
1858 matches any string beginning with an
1860 whose third character is a
1864 (dot) followed by any character except the lower case letters,
1866 The sequence \f5[:alpha:]\fP
1867 inside a character class, matches any set of characters in
1871 Similarly, \f5[:\fP\fIclass\fP\f5:]\fP matches
1872 each of the characters in the given \fIclass\fP
1873 for all the ANSI-C character classes.
1874 For example, \f5[[:alnum:]_]\fP
1875 matches any alpha-numeric character or the character
1885 is a list of one or more patterns separated by a
1887 specially when preceded by
1897 .BI ( pattern-list )
1898 means that the pattern list enclosed in
1902 .BI @( pattern-list )
1903 matches any pattern in the list of patterns enclosed in
1906 .BI *( pattern-list )
1907 matches any string that contains zero or more of each of the enclosed
1910 .BI +( pattern-list )
1911 requires a match of one or more of any of the given patterns.
1912 For instance, the pattern
1914 matches one or more digits optionally followed by a
1917 .BI !( pattern-list )
1918 matches anything except any of the given patterns.
1921 displays all file names in the current directory that do not end in
1924 When patterns are used to generate pathnames when expanding
1925 commands several other rules apply.
1926 A separate match is made
1927 for each file name component of the pathname.
1928 Read permission is required for
1929 any portion of the pathname that contains any special
1931 Search permission is required for every component except
1935 file names in each directory that begin with \fB\s+2.\s-2\fP
1936 are skipped when performing a match.
1937 If the pattern to be matched starts with a leading \fB\s+2.\s-2\fP,
1938 then only files beginning with a \fB\s+2.\s-2\fP,
1939 are examined when reading each directory to find matching files.
1941 \fB\s-1FIGNORE\s+1\fP variable
1943 then only files that do not match this pattern
1945 This overrides the special meaning of \fB\s+2.\s-2\fP
1946 in a pattern and in a file name.
1951 each matching pathname that is the name
1952 of a directory has a trailing
1954 appended to the name.
1956 .H 2 "Conditional Expressions"
1957 The Bourne shell uses the \f5test\fP
1958 command, or the equivalent \f5[\fP
1959 command, to test files for attributes
1960 and to compare strings or numbers.
1961 The problem with \f5test\fP
1962 is that the shell has expanded the words of the \f5test\fP
1964 split them into arguments before \f5test\fP begins execution.
1966 cannot distinguish between operators and operands.
1969 will test whether argument 1 is non-null.
1977 yield a syntax error.
1978 One of the most frequent errors with
1980 occurs when its operands are not within double quotes.
1981 In this case, the argument may expand to more than a single
1982 argument or to no argument at all. In either case this
1983 will likely cause a syntax error.
1984 What makes this most insidious is that these errors are frequently
1985 data dependent. A script that appears to run correctly may abort
1986 if given unexpected data.
1988 To get around these problems,
1990 has a compound command for conditional expression testing
1991 as part of the language.
1996 delimit the range of the command.
1997 Because they are reserved words, not operator characters,
1998 they require spaces to separate them
2004 are not processed for field splitting or for pathname generation.
2005 In addition, since \f5ksh\fP
2006 determines the operators before parameter expansion,
2007 expansions that yield no argument cause no problem.
2008 The operators within
2010 are almost the same as those for the \f5test\fP
2012 All unary operators are of the form
2014 and are followed by a single operand.
2024 to indicate "and" and "or".
2025 Parentheses are used without quoting for grouping.
2027 The right hand side of the string comparison operators
2031 takes a pattern and tests whether the left hand operand
2032 matches this pattern. Quoting the pattern results
2033 is a string comparison rather than the pattern match.
2040 designate lexicographical comparison.
2042 In addition there are several other new comparison primitives.
2043 The binary operators
2047 compare the modification times
2048 of two files to see which file is
2055 tests whether two files
2056 have the same device and i-node number,
2057 i.\ e., a link to the same file.
2061 returns true if its operand is a symbolic link.
2065 returns true if the owner (or group) of the file operand matches
2069 returns true when its operand is the name of an option that is
2072 The following script illustrates some of the uses of
2074 The reference manual contains the complete list of operators.
2080 do # execute foo for numeric directory
2081 if [[ \-d $i && $i == +([0\-9]) ]]
2083 # otherwise if writable or executable file and not mine
2084 elif [[ (\-w $i\(bv\(bv\-x $i) && ! \-O $i ]]
2092 .H 2 "Input and Output"
2094 extended I/O capabilities to enhance the
2095 use of the shell as a programming language.
2096 As with the Bourne shell,
2097 you use the I/O redirection operator,
2099 to control where input comes from,
2100 and the I/O redirection operator,
2102 to control where output goes to.
2103 Each of these operators can be preceded with a single digit that
2104 specifies a file unit number to associate with the file stream.
2105 Ordinarily you specify these I/O redirection operators with a specific
2106 command to which it applies.
2107 However, if you specify I/O redirections with the \f5exec\fP
2109 and don't specify arguments to \f5exec\fP,
2110 then the I/O redirection applies to the current program.
2111 For example, the command
2112 \f5exec\ <\ foobar\fP
2113 opens file \f5foobar\fP
2116 command is also used to close files.
2117 A file descriptor unit can be opened as a copy of an existing
2118 file descriptor unit by using either of the
2122 operators and putting the file descriptor unit of the original file
2125 Thus, \f52>&1\fP means open standard error (file descriptor 2)
2126 as a copy of standard output (file descriptor 1).
2127 A file descriptor value of
2131 indicates that the file should be closed.
2132 To close file unit 5, specify
2134 There are two additional redirection operators with \f5ksh\fP
2135 and the POSIX shell that are not part of the Bourne shell.
2138 operator overrides the effect of the
2140 option described earlier.
2143 operator causes a file to be opened for both reading and writing.
2145 \f5ksh\fP recognizes certain pathnames and treats them
2147 Pathnames of the form
2149 are treated as equivalent to the file defined by file descriptor
2151 These name can be used as the script argument to \f5ksh\fP
2152 and in conditional testing as described above.
2153 On underlying systems that support
2155 in the file system, these names can be passed to other commands.
2156 Pathnames of the form
2157 .BI /dev/tcp/ hostid / port
2159 .BI /dev/udp/ hostid / port
2160 can be used to create
2164 connections to services given by the
2171 cannot use symbolic values. In practice these
2172 numbers are typically generated by command substitution.
2174 \f5exec\ 5>\ /dev/tcp/$(service\ name)\fP
2175 would open file descriptor 5 for sending messages
2176 to hostid and port number defined by the output of \f5service\ name\fP.
2178 The Bourne shell has a built-in command \f5read\fP
2179 for reading lines from standard input (file descriptor 0)
2180 and splitting it into fields based on the value of the
2182 variable, and a command \f5echo\fP
2183 to write strings to standard output.
2184 (On some systems, \f5echo\fP
2185 is not a built-in command and incurs considerable overhead to use.)
2186 Unfortunately, neither of these commands
2187 is able to perform some very basic tasks.
2189 with the Bourne shell,
2191 built-in cannot read a single line that ends in
2197 option to remove the special meaning for
2199 which allows it to be
2200 treated as a regular
2201 character rather than the line continuation character.
2202 With the Bourne shell,
2203 there is no simple way to have more than one file open
2204 at any time for reading.
2205 \f5ksh\fP has options on the \f5read\fP
2206 command to specify the file
2207 descriptor for the input.
2208 The fields that are read from a line can be stored into an indexed
2212 This allows a line to be split into an arbitrary number of fields.
2214 The way the Bourne shell uses the
2215 \fB\s-1IFS\s+1\fP variable to
2216 split lines into fields greatly limits its utility.
2217 Often data files consist of lines that use a character such
2220 to delimit fields with two adjacent delimiters that denote
2222 The Bourne shell treats adjacent delimiters as a single
2225 delimiters that are considered white space characters
2226 have the behavior of the Bourne shell, but other
2227 adjacent delimiters separate
2230 The \f5read\fP command is often used in scripts that interact
2231 with the user by prompting the user and then requesting some
2233 With the Bourne shell two commands are needed; one to
2234 prompt the user, the other to read the reply.
2235 \f5ksh\fP allows these two commands to be combined.
2236 The first argument of the \f5read\fP
2237 command can be followed by a
2239 and a prompt string which is used whenever the input
2240 device is a terminal.
2241 Because the prompt is associated with the \f5read\fP built-in,
2242 the built-in command line editors will be able to re-output
2243 the prompt whenever the line needs to be refreshed when
2244 reading from a terminal device.
2246 With the Bourne shell,
2247 there is no way to set a time limit for waiting for the user
2251 option to \f5read\fP takes a floating
2252 point argument that gives the time in seconds,
2253 or fractions of seconds that the shell should wait for a reply.
2255 The version of the \f5echo\fP command in System V
2256 treats certain sequences beginning with
2258 as control sequences.
2259 This makes it hard to output strings without interpretation.
2260 Most BSD derived systems do not interpret
2263 Unfortunately, the BSD versions of \f5echo\fP accepts a
2265 option to prevent a trailing new-line, but has no way to
2269 Neither of these versions is adequate. Also, because they
2270 are incompatible, it is very hard to write portable shell scripts
2272 The \f5ksh\fP built-in, \f5print\fP,
2273 outputs characters to the terminal or to a file and
2274 subsumes the functions of all versions of \f5echo\fP.
2275 Ordinarily, escape sequences in arguments beginning with
2277 are processed the same as for the System V \f5echo\fP command.
2278 However \f5print\fP follows the standard conventions for
2279 options and has options that make \f5print\fP very versatile.
2282 option can be used to output the arguments without any special meaning.
2285 option can be used here to suppress the trailing new-line
2286 that is ordinarily appended.
2287 As with \f5read\fP, it is possible to specify the file descriptor number
2288 as an option to the command to avoid having to use
2289 redirection operators with each occurrence of the command.
2291 The IEEE POSIX shell and utilities standard committee was unable
2292 to reconcile the differences between the System V and BSD
2293 versions of \f5echo\fP.
2294 They introduced a new command named \f5printf\fP
2295 which takes an ANSI-C format string and a list of options
2296 and outputs the strings using the ANSI-C formatting rules.
2297 Since \f5ksh\fP is POSIX conforming, it accepts \f5printf\fP.
2300 options to \f5print\fP that can be used to specify
2301 a format string which processes the arguments the same way that
2304 The format processing for \f5print\fP and \f5printf\fP has
2305 been extended slightly.
2306 There are three additional formatting directives.
2311 escape sequences to be expanded as they are with the System V \f5echo\fP
2315 format causes quotes to
2316 be placed on the output as required
2317 so that it can be used as shell input.
2318 Special characters in the output of most \f5ksh\fP built-in commands
2319 and in the output from an execution trace
2320 are quoted in an equivalent fashion.
2323 format causes an extended regular expression string to
2324 be converted into a shell pattern.
2325 This is useful for writing shell applications that have
2326 to accept regular expressions as input.
2327 Finally, the escape sequence
2329 which expands to the terminal escape character (octal 033)
2332 The shell is frequently used as a programming language for
2333 interactive dialogues.
2336 statement has been added to the language
2337 to make it easier to
2338 present menu selection alternatives to the
2339 user and evaluate the reply.
2340 The list of alternatives is numbered and put in columns.
2341 A user settable prompt,
2343 is issued and if the answer is
2344 a number corresponding to one of the alternatives,
2345 the select loop variable is set to this value.
2348 variable is used to store the user entered reply.
2353 are used to control the layout of select lists.
2354 .H 2 "Option Parsing"
2355 The \f5getopts\fP built-in command can be used
2356 to process command arguments in a manner consistent
2357 with the way \f5ksh\fP does for its own built-in commands.
2359 The \f5getopts\fP built-in allows users to specify options
2360 as separate arguments or to group options that do not
2361 take arguments together. Options that require arguments
2362 do not require space to separate them from the option argument.
2365 variable stores the value of the option argument
2366 after finding a variable that takes an argument.
2369 variable holds the index of the current options argument.
2370 After processing options, the arguments should be
2372 .B \s-1OPTIND\s+1\-1
2374 remaining arguments be \f5"$@"\fP.
2376 The \f5getopts\fP argument description allows additional
2377 information to be specified along with the options
2378 that is used to generate \fIusage\fP messages for
2379 incorrect arguments and for the option argument \fB\-?\fP.
2380 The example in the APPENDIX uses \f5getopts\fP to process
2383 \f5ksh\fP can spawn a
2388 This process will be run with its standard input and its
2389 standard output connected to the shell. The built-in command \f5print\fP
2392 option will write into the standard input of this
2394 the built-in command \f5read\fP
2397 option will read from the output of this process.
2399 In addition, the I/O redirection operators \fB<&\fP and \fB>&\fP can
2400 be used to move the input or output pipe of the co-process
2401 to a numbered file descriptor.
2402 Use \f5exec\ 3>&\ p\fP to move the input of the co-process
2403 to file descriptor \fB3\fP.
2404 After you have connected to file descriptor \fB3\fP, you
2405 can direct the output of any command to the co-process
2406 by running \fIcommand\fP\f5\ >&3\fP.
2407 Also, by moving the input of the co-process to a numbered descriptor,
2408 it is possible to run a second co-process.
2409 The output of both co-processes will be the file descriptor
2410 associated with \f5read\ -p\fP.
2411 You can use \f5exec\ 4<&\ p\fP to cause the output of these
2412 co-processes to go to file descriptor \fB4\fP of the shell.
2413 Once you have moved the pipe to descriptor \fB4\fP, it is possible
2414 to connect a server to the co-process by running \fIcommand\fP\f5\ 4<&\ p\fP
2415 or to close the co-process pipe with \f5exec\ 4<&\ -\fP.
2418 Function definitions are of the form
2422 \f5function\fP \fIname\fP
2432 A function whose name contains a \fB\s+2.\s-2\fP
2433 is called a \fIdiscipline\fP function.
2434 The portion of the name after the last \fB\s+2.\s-2\fP
2435 is the name of the discipline.
2436 Discipline functions named \f5get\fP, \f5set\fP, and \f5unset\fP
2437 can be assigned to any variable to intercept lookups,
2438 assignments and unsetting of the variable
2439 defined by the portion of the name before the last \fB\s+2.\s-2\fP.
2440 Applications can create additional disciplines for variables
2441 that are created as part of user defined built-ins.
2442 The portion of the name before the last \fB\s+2.\s-2\fP
2443 must refer to the name of an existing variable.
2444 Thus, if \f5p\fP is a reference to \f5PATH\fP, then
2445 the function name \f5p.get\fP and \f5PATH.get\fP
2446 refer to the same function.
2448 The function is invoked either
2452 and optionally following it with arguments
2453 or by using it as an option to the \fB\s+2.\s-2\fP
2455 Positional parameters are saved before each
2456 function call and restored when completed.
2457 The arguments that follow the function name on the calling
2458 line become positional parameters inside the function.
2460 built-in can be used to cause the function to return to
2461 the statement following
2462 the point of invocation.
2464 Functions can also be defined with the System V notation,
2478 Functions defined with this syntax cannot be used as the first
2479 argument to a \fB\s+2.\s-2\fP procedure.
2480 \f5ksh\fP accepts this notation for compatibility only.
2481 There is no need to use this notation when writing
2484 Functions defined with the \f5function\fP\ \fIname\fP syntax
2486 are executed in the current shell environment
2487 and can share named variables with the calling program.
2488 Options, other than execution trace
2490 set by the calling program are
2491 passed down to a function.
2494 the function so that any options set within a function are
2495 restored when the function exits.
2496 Traps ignored by the caller are ignored within the function
2497 and cannot be enabled.
2498 Traps caught by the calling program are reset to their
2499 default action within the function.
2500 In most instances, the default action is
2501 to cause the function to terminate.
2504 defined within a function executes after the function
2506 before the caller resumes.
2508 any variable assignments and
2509 any options set as part of a trap action will be effective
2510 after the caller resumes.
2512 By default, variables are inherited by the function and shared
2513 by the calling program.
2515 for functions defined with the \f5function\fP\ \fIname\fP syntax
2516 that are invoked by name,
2517 environment substitutions preceding the function call
2518 apply only to the scope of the function call.
2519 Also, variables whose names do not contain a \fB\s+2.\s-2\fP
2520 that are defined with the \f5typeset\fP
2521 built-in command are local to the function that they are declared in.
2522 Thus, for the function defined
2539 are local variables with respect to the function
2545 Functions defined with the \fIname\fP\f5()\fP syntax,
2546 and functions invoked as an argument to the \fB\s+2.\s-2\fP
2548 share everything other than positional parameters with the caller.
2549 Assignments that precede the call remain in effect after the
2552 Alias and function names are not passed down to shell scripts
2553 or carried across separate
2554 invocations of \f5ksh\fP.
2557 variable gives a colon separated list of directories that
2558 is searched for function definitions when trying to resolve
2560 Whenever a file name contained in
2562 is found, the complete file is read and all functions
2563 contained within become defined.
2565 Calls that reference functions can be recursive.
2566 Except for special built-ins,
2567 function names take precedence over built-in names and names
2568 of programs when used as command names.
2569 To write a replacement function that invokes the command that
2570 you wish to replace,
2571 you can use the \f5command\fP built-in command.
2572 The arguments to \f5command\fP are the name and arguments
2573 of the program you want to execute.
2574 For example to write a
2576 function which changes the directory and prints out the directory name,
2584 then print -r -- $PWD
2594 variable is a colon separated list that \f5ksh\fP
2595 uses to search for function definitions.
2598 encounters an autoload function,
2601 command on the script containing the function,
2602 and then executes the function.
2604 For interactive shells,
2605 function definitions may also be placed in the
2609 causes the shell to take longer to begin executing.
2610 .H 2 "Process Substitution"
2612 This feature is only available
2613 on versions of the UNIX operating system which support the
2615 directory for naming open files.
2616 Each command argument of the form
2617 \fB<(\fP\fIlist\^\fP\fB)\fP
2619 \fB>(\fP\fIlist\^\fP\fB)\fP
2622 asynchronously connected to some file in the
2625 The name of this file will become the argument to the command.
2628 is selected then writing on this file will provide input for
2633 then the file passed as an argument will contain the output of the
2640 \f5paste <(cut \-f1 \fP\fIfile1\fP\f5) <(cut \-f2 \fP\fIfile2\fP\f5) | tee >(\fP\fIprocess1\fP\f5) >(\fP\fIprocess2\fP\f5)\fP
2653 results side by side, and
2659 as well as putting it onto the standard output.
2660 Note that the file which is passed as an argument to the command is
2663 so that the programs that expect to
2665 on the file will not work.
2666 .H 2 "Finding Commands"
2668 The addition of aliases, functions,
2670 has made it substantially more difficult to know what
2671 a given command name really means.
2673 Commands that begin with reserved words
2674 are an integral part of the shell language itself
2675 and typically define the control flow of the language.
2676 Some control flow commands are not reserved words in
2677 the language but are \fIspecial\fP built-ins.
2678 Special built-ins are built-ins that are considered a
2679 part of the language rather than user definable commands.
2680 The best examples of commands that fit this description
2681 are \f5break\fP and \f5continue\fP.
2682 Because they are not reserved words, they can be the
2683 result of shell expansions and are not effected by quoting.
2684 These commands have the following special properties:
2687 Assignments that precede them apply to the current shell process,
2688 not just to the given command.
2690 An error in the format of these commands cause a shell script
2691 or function that contains them to abort.
2693 They cannot be overridden by shell functions.
2696 Other commands are built-in because they perform side effects
2697 on the current environment that would be nearly impossible
2698 to implement otherwise.
2699 Built-ins such as \f5cd\fP and \f5read\fP
2700 are examples of such built-ins.
2701 These built-ins are semantically equivalent to commands that
2702 are not built-in except that they don't take a path search
2705 A third reason to have a command built-in is so that
2706 it will be unaffected by the setting of the
2709 The \f5print\fP command fits this category.
2710 Scripts that use \f5print\fP will be portable
2711 to all sites that run \f5ksh\fP.
2713 The final reason for having a command be a built-in is
2715 On most systems it is more than an order of magnitude
2716 faster to initiate a command that is built-in than
2717 to create a separate process to run the command.
2718 Examples that fit this category are \f5test\fP
2721 Given a command name \f5ksh\fP decides what it means using
2722 the following order:
2725 Reserved words define commands that form part of the shell
2727 They cannot be quoted.
2729 Alias substitutions occur first as part of the reading of commands.
2730 Using quotes in the command name will prevent alias substitutions.
2736 Commands that are built-in that are not associated with a pathname
2737 such as \f5cd\fP and \f5print\fP.
2739 If the command name contains a
2741 the program or script corresponding to the given name is executed.
2743 A path search locates the pathname corresponding to the command.
2744 If the pathname where it is found matches the pathname associated
2745 with a built-in command, the built-in command is executed.
2746 If the directory where the command is found is listed in the
2748 variable, the file is read into the shell
2749 like a dot script, and a function by that name is invoked.
2750 Once a pathname is found, \f5ksh\fP remembers its location
2751 and only checks relative directories in \fB\s-1PATH\s+1\fP
2752 the next time the command name is used.
2753 Assigning a value to \fB\s-1PATH\s+1\fP
2754 causes \f5ksh\fP to forget the location of all command names.
2758 variable is searched and files found are treated as described above.
2761 The first argument of the \f5command\fP built-in, described earlier,
2762 skips the checks for reserved words and for function definitions.
2763 In all other ways, \f5command\fP behaves like a built-in
2764 that is not associated with a pathname.
2765 As a result, if the first argument of \f5command\fP is
2766 a special built-in, the special properties of this built-in
2768 For example, whereas, \f5exec\ 3<\ foo\fP will cause a script containing
2769 it to abort if the open fails, \f5command\ exec\ 3<\ foo\fP
2770 results in a non-zero exit status but does not abort the script.
2772 You can get a complete list of the special built-in commands
2773 with \f5builtin\ -s\fP.
2774 In addition \f5builtin\fP without arguments gives a list of
2775 the current built-ins and the pathname that they are associated with.
2776 A built-in can be bound to another pathname by giving
2777 the pathname for the built-in. The basename of this path must
2778 be the name of an existing built-in for this to succeed.
2779 Specifying the name of the built-in without a pathname causes
2780 this built-in to be found before a path search.
2781 A built-in can be deleted with the \fB\-d\fP option.
2783 On systems with run time loading of libraries, built-in commands
2784 can be added with the \f5builtin\fP command.
2785 Each command that is to be built-in must be written as a
2786 C function whose name is of the form \f5b_\fP\fIname\fP, where
2787 \fIname\fP is the name of the built-in that is to be added.
2788 The function has the same argument calling convention as
2789 \f5main\fP. The lower eight bits of the return value become
2790 the exit status for this built-in.
2791 Builtins are added by specifying the pathname of the library
2792 as an argument to the \fB\-f\fP option of \f5builtin\fP.
2794 The built-in command,
2798 option, tells how a given command is bound.
2799 A line is printed for each argument to \f5whence\fP
2800 telling what would happen if this argument were used as a command name.
2801 It reports on reserved words, aliases, built-ins, and
2803 If the command is none of the above,
2804 it follows the path search rules and prints the full path-name,
2805 if any, otherwise it prints an error message.
2806 .H 2 "Symbolic Names"
2807 To avoid implementation dependencies, \f5ksh\fP
2808 accepts and generates symbolic names
2809 for built-ins that use numerical values in the Bourne shell.
2813 \f5umask\fP built-in command
2814 accepts and displays
2815 default file creation permissions
2817 It uses the same symbolic notation as the \f5chmod\fP command.
2819 The \f5trap\fP and \f5kill\fP built-in commands
2820 allows the signal names to be given symbolically.
2821 The names of signals and traps
2822 corresponding to signals are the same as the signal name with
2830 .H 2 "Additional Variables"
2831 In addition to the variables discussed earlier, \f5ksh\fP
2832 has other variables that it handles specially.
2833 The variable \fB\s-1RANDOM\s+1\fP
2834 produces a random number in the range 0 to 32767 each time it is referenced.
2835 Assignment to this variable sets the seed for the
2836 random number generator.
2838 The parameter \fB\s-1PPID\s+1\fP
2839 is used to generate the process id of the process which invoked this shell.
2844 This trap is invoked whenever the shell would exit if the
2847 This trap is used by
2848 Fourth Generation Make\*(Rf
2851 .I "The Fourth Generation Make,"
2852 Proceedings of the Portland USENIX meeting, pp. 159-174, 1985.
2854 which runs \f5ksh\fP
2859 gets executed after each command.
2860 This trap can be used for debugging and other purposes.
2864 trap was described earlier.
2866 The primary method for debugging Bourne shell scripts is to
2869 option to enable the execution trace.
2871 the expansions have been performed,
2872 but before each command is executed,
2873 the trace writes to standard error the name and arguments
2874 of each command preceded by a
2876 While the trace is very useful, there is no way
2877 to find out what line of source a given trace line
2884 is evaluated for parameter expansion and
2885 is displayed before each command,
2890 \fB\s-1LINENO\s+1\fP
2891 variable is set to the current line number relative to the
2892 beginning of the current script or function.
2893 It is most useful as part of the
2899 trap can be used to write a break point shell
2900 debugger in \f5ksh\fP.
2901 An example of such a debugger is \f5kshdb\fP.\*(Rf
2904 .IR "Debugging Shell Scripts with \f5kshdb\fP" ,
2905 Unix World, Volume X, No. 5, 1993.
2907 .H 2 "Timing Commands"
2909 Finding the time it takes to execute commands
2910 has been a serious problem with the Bourne shell.
2911 Since the \f5time\fP command is not part of the
2912 language, it is necessary to write a script
2913 in order to time a \f5for\fP or \f5while\fP loop.
2914 The extra time in invoking the shell and processing
2915 the script is accumulated along with the time
2916 to execute the script.
2918 More seriously, the Bourne shell does not give correct
2919 times for pipelines.
2920 The reason for this is that the times for some members
2921 of a pipeline are not counted when computing the time.
2922 As an extreme example,
2923 running \f5time\fP on the script
2925 \f5cat < /dev/null | sort -u bigfile | wc\fP
2926 with the Bourne shell will show very little
2927 user and system time no matter how
2928 large \f5bigfile\fP is.
2930 To correct these problems,
2931 a reserved word \f5time\fP
2932 has been added to replace
2935 Any function, command or pipeline can be preceded by this reserved word
2936 to obtain information about the elapsed, user, and system times.
2937 Since I/O redirections bind to the command, not to
2939 parentheses should be used to redirect the timing information which
2940 is normally printed on file descriptor 2.
2942 There are several documented problems associated with the security of
2943 shell procedures\*(Rf.
2945 F. T. Grampp and R. H. Morris,
2946 .I "UNIX Operating System Security,"
2947 AT&T Bell Labs Tech. Journal, Vol. 63, No. 8, Part 2, pp. 1649-1671, 1984.
2949 These security holes occur primarily because a user can manipulate the
2951 to subvert the intent of a
2954 Sometimes, shell procedures are initiated from
2955 binary programs, without the author's
2956 awareness, by library routines which invoke shells to carry out
2958 When the binary program is run
2960 then the shell procedure runs with the permissions afforded to the
2961 owner of the binary file.
2963 In the Bourne shell,
2966 parameter is used to split each word into separate command arguments.
2967 If a user knows that some
2970 \f5sh\ -c\ /bin/pwd\fP
2971 (or any other command in
2973 then the user sets and exports
2974 .BR \s-1IFS\s+1=\^/ .
2982 The user puts his or her own \f5bin\fP
2983 program into the current directory.
2985 create a copy of the shell,
2988 and then run the \f5/bin/pwd\fP
2989 program so that the original program continues to run successfully.
2990 This kind of penetration is not possible with
2994 parameter only splits arguments that result from command or parameter
2999 programs run programs using
3001 without giving the full pathname.
3005 variable so that the desired command will be found
3006 in his or her local bin, then the same technique described above can
3007 be employed to compromise the security of the system.
3008 To close up this and other security holes,
3010 resets the effective user id to the real user id and the effective
3011 group id to the real group id unless the
3015 is specified at invocation.
3023 files are not processed.
3025 .B /etc/suid_profile
3026 is read and executed.
3027 This gives an administrator control over the
3028 environment to set the
3030 variable or to log setuid shell invocations.
3031 Clearly security of the system is compromised if
3033 or this file is publicly writable.
3035 Some versions of the UNIX operating system look for the characters
3037 as the first two characters of an executable file.
3038 If these characters are found, then the next word on this line is taken
3039 as the interpreter to
3041 for this command and the interpreter is
3043 with the name of the script as argument zero and argument one.
3048 bits are on for this file, then the interpreter
3049 is run with the effective uid and/or gid set accordingly.
3050 This scheme has three major drawbacks.
3052 putting the pathname of the interpreter into the script
3053 makes the script less portable since the interpreter
3054 may be installed in a different directory on another system.
3059 of the interpreter even when the call is invoked from the interpreter
3060 which it must exec. This is inefficient since
3061 \f5ksh\fP can handle a failed exec much faster than starting up
3067 procedures provide an easy target for intrusion.
3072 procedure to a name beginning with a
3074 the interpreter is fooled into thinking that it is being invoked with
3075 a command line option rather than the name of a file.
3076 When the interpreter is the shell, the user gets a privileged
3080 to guard against this simple form of intrusion.
3082 A more reliable way to handle
3086 procedures is provided with
3088 The technique does not require any changes to the operating system
3089 and provides better security.
3090 Another advantage to this method is that it also allows scripts which
3091 have execute permission but no read permission to run. Taking away read
3092 permission makes scripts more secure.
3094 The method relies on a setuid
3096 program to authenticate the
3097 request and exec the shell with the correct mode bits to carry out
3098 the task. This shell is invoked with the requested file already open
3099 for reading. A script which cannot be opened for reading or which
3100 has its setuid and/or setgid bits turned on causes this setuid
3102 program to get \fBexec\fPed.
3103 For security reasons, this program is given the full
3105 \f5/etc/suid_exec\fP.
3106 A description of the implementation of the
3107 \f5/etc/suid_exec\fP
3108 program can be found in
3109 a separate paper\*(Rf.
3112 .I "Parlez-vous Kanji?"
3113 TM-59554-860602-03, 1986.
3116 \f5ksh\fP is written in ANSI-C as a reusable library.
3117 The code can be compiled with C++ and older K&R C as well.
3118 The code uses the IEEE POSIX 1003.1 and ISO 9945-1 standard\*(Rf
3120 .I "POSIX \- Part 1: System Application Program Interface,"
3121 IEEE Std 1003.1-1990, ISO/IEC 9945-1:1990.
3123 wherever possible so that \f5ksh\fP should be able to run
3124 on any POSIX compliant system. In addition, it is possible
3125 to compile \f5ksh\fP for older systems.
3127 Unlike earlier version of the Bourne shell,
3128 \f5ksh\fP treats eight bit characters transparently
3129 without stripping off the
3131 There is also a compile time switch to enable handling multi-byte
3132 and multi-width characters sets.
3134 On systems with dynamic libraries, it is possible to add built-in
3135 commands at run time with the built-in command \f5builtin\fP
3137 It is also possible to embed \f5ksh\fP in applications in
3138 a manner analogous to \f5tcl\fP.
3141 An example of a \f5ksh\fP script is included
3143 This one page program is a variant of the UNIX system
3144 \f5grep\fP(1) program.
3145 Pattern matching for this version of \f5grep\fP
3146 means shell patterns.
3148 The first half uses the \f5getopts\fP command to
3149 find the option flags.
3150 Nearly all options have been implemented.
3151 The second half goes through each line of each file
3152 to look for a pattern match.
3154 This program is not intended to serve as a
3155 replacement for \f5grep\fP
3156 which has been highly tuned for performance.
3158 illustrate the programming power of \f5ksh\fP.
3159 Note that no auxiliary processes are spawned by this script.
3160 It was written and debugged in under two hours.
3161 While performance is acceptable for small files,
3162 this program runs at only one tenth
3163 the speed of \f5grep\fP
3167 \f5ksh\fP executes many scripts faster than the System V Bourne shell;
3168 in some cases more than 10 times as fast.
3169 The primary reason for this is that \f5ksh\fP creates fewer
3171 The time to execute a built-in command or a function is one or two
3172 orders of magnitude faster than performing a \f5fork\fP() and
3173 \f5exec\fP() to create a separate process.
3174 Command substitution and commands inside parentheses
3175 are performed without creating another process, unless necessary
3176 to preserve correct behavior.
3178 Another reason for improved performance is the use of the \fBsfio\fP\*(Rf,
3180 David Korn and Kiem-Phong Vo,
3181 .IR "SFIO - A Safe/Fast String/File I/O,"
3182 Proceedings of the Summer Usenix,
3185 library for I/O. The \fBsfio\fP library buffers all I/O
3186 and buffers are flushed only when required.
3187 The algorithms used in \fBsfio\fP perform better than
3188 traditional versions of standard I/O so that programs that
3189 spend most of their time
3190 formatting output may actually perform better
3191 than versions written in C.
3193 Several of the internal algorithms have been changed
3194 so that the number of subroutine calls has been
3195 substantially reduced.
3196 \f5ksh\fP uses variable sized hash tables for variables.
3197 Scripts that rely heavily on referencing variables execute faster.
3198 More processing is performed while reading the script
3199 so that execution time is saved while running loops.
3200 These changes are not noticeable for scripts that \f5fork()\fP
3202 but they reduce the time that it takes to interpret commands by
3203 more than a factor of two.
3205 Most importantly, \f5ksh\fP provide mechanisms to write applications
3206 that do not require as many processes.
3207 The arithmetic provided by the shell eliminates the need for the
3209 The pattern matching and substring capabilities eliminate the
3210 need to use \f5sed\fP or \f5awk\fP to process strings.
3212 The architecture of \f5ksh\fP makes it easy to make commands
3213 built-ins without changing the semantics at all.
3214 Systems that have run-time binding of libraries allow
3215 applications to be sped up by supplying the critical
3216 programs as shell built-in commands.
3217 Implementations on other systems can add built-in commands
3219 The procedure for writing built-in commands that can be loaded
3220 at run time is in a separate document.\*(Rf,
3223 .IR "Guidelines for writing \f5ksh-93\fP built-in commands,"
3224 to be published, 1994.
3228 The 1988 version of \f5ksh\fP has tens of thousands of regular users
3229 and is a suitable replacement for the Bourne shell.
3230 The 1993 version of \f5ksh\fP is essentially upward compatible with
3231 both the 1988 version of \f5ksh\fP and with the recent IEEE POSIX
3232 and ISO shell standard.
3233 The 1993 version offers many advantages for programming applications,
3234 and it has been rewritten so that it can be used in embedded applications.
3235 It also offers improved performance.
3236 .SG dgk \" signature typist initials
3237 \" .CS 14 24 38 0 0 16 \" cover sheet for TM
3243 .ta .66i 1.33i 2i 2.66i 3.33i 4i 4.66i 5.33i 6i 6.66i 7.33i 8i