da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin. \" use troff -mm
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.nr C 3
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.nr N 2
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.SA 1 \" right justified
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ND "December 21, 1993"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.TL "311466-6713" "61175" \" charging case filing case
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIntroduction to \f5ksh-93\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.AU "David G. Korn" DGK MH 11267 7975 3C-526B "(research!dgk)"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.TM 11267-931221-26 \" technical memo + TM numbers
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.MT 1 \" memo type
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.OK Shell "Command interpreter" Language UNIX \" keyword
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.AS 2 \" abstract start for TM
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh-93\fP is a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmajor rewrite of \f5ksh\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china program that serves as a command language
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin(shell) for the UNIX*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.FS *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinUNIX is a registered trademark of Novell.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.FE
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoperating system.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAs with \f5ksh\fP, \f5ksh-93\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis essentially compatible with the System V version of the Bourne shell\*(Rf,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinS. R. Bourne,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I "An Introduction to the UNIX
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinShell,"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBSTJ - Vol. 57, No. 6 part 2, pages 1947-1972, 1978.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand compatible with previous versions of \f5ksh\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh-93\fP is intended to comply with the IEEE POSIX 1003.2
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand ISO 9945-2\*(Rf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I "POSIX \- Part 2: Shell and Utilities,"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIEEE Std 1003.2-1992, ISO/IEC 9945-2, IEEE, 1993.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinshell standard.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn addition to changes in the language required
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinby these standards, the primary focus of \f5ksh-93\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis related to shell programming.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh-93\fP provides the programming power of several
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinother interpretive languages such as \f5awk\fP\*(Rf,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAl Aho,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBrian Kernighan,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinPeter Weinberger,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I "The AWK Programming Language,"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAddison Wesley, 1988.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5FIT\fP\*(Rf,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinLloyd H. Nakatani and Laurence W. Ruedisueli,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I "The FIT Programming Language Primer",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinTM 1126-920301-03, 1992.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5PERL\fP\*(Rf,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinLarry Wall and Randal Schwartz,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I "Programming perl,"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinO'Reilly & Assoc, 1990.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5tcl\fP\*(Rf.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinJohn K. Ousterhout,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I "Tcl: An Embeddable Command Language",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinProceedings of the Washington USENIX meeting, pp. 133-146, 1990.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis memo
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinassumes that the reader is already familiar with the Bourne shell.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIt introduces most of the features of \f5ksh-93\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinrelative to the Bourne shell; both
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas a command language and as a programming language.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe Appendix contains
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china sample script written in \f5ksh-93\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.AE \" abstract end
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 1 "INTRODUCTION"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe term "shell" is used to describe a program that provides
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china command language
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chininterface.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBecause the UNIX*\
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.FS *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinUNIX is a registered trademark of USL
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.FE
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsystem shell is a user level program, and not part of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe operating system itself,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinanyone can write a new shell or modify an existing one.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis has caused an evolutionary progress
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinin the design and implementation of shells,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwith the better ones surviving.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe most widely available UNIX system shells are the Bourne shell\*(Rf,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinS. R. Bourne,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IR "An Introduction to the UNIX Shell" ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBell System Technical Journal,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinVol. 57, No. 6, Part 2, pp. 1947-1972, July 1978.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwritten by Steve Bourne
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinat AT&T Bell Laboratories,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe C shell\*(Rf,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinW. Joy,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IR "An Introduction to the C Shell" ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinUnix Programmer's Manual, Berkeley Software Distribution,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinUniversity of California, Berkeley, 1980.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwritten by Bill Joy at the University of California, Berkeley,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand the KornShell language \*(Rf,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinMorris Bolsky and David Korn,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IR "The KornShell Command and Programming Language" ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinPrentice Hall, 1989.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwritten by David Korn
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinat AT&T Bell Laboratories.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe Bourne shell is available on almost all versions of the UNIX
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsystem.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe C Shell is available with all Berkeley Software Distribution (BSD) UNIX systems and on many other systems.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe KornShell
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis available on System V Release 4 systems.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn addition, it is available on many other systems.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe source for the KornShell language is available from the AT&T Toolchest,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinan electronic software distribution system.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIt runs on all known versions of the UNIX system and
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinon many UNIX system look-alikes.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThere have been several articles comparing the UNIX system shells.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinJason Levitt\*(Rf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinJason Levitt,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IR "The Korn Shell: An Emerging Standard" ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinUNIX/World, pp. 74-81, September 1986.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinhighlights some of the new features
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinintroduced by the KornShell language.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinRich Bilancia\*(Rf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinRich Bilancia,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IR "Proficiency and Power are Yours With the Korn Shell" ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinUNIX/World, pp. 103-107, September 1987.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinexplains some of the advantages of using the KornShell language.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinJohn Sebes\*(Rf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinJohn Sebes,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I "Comparing UNIX Shells,"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinUNIX Papers,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinEdited by the Waite Group, Howard W. Sams & Co., 1987.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprovides a more detailed comparison of the three shells,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinboth as a command language and as a programming language.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe KornShell language is a superset of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBourne shell. The KornShell language has many of the popular C shell features,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinplus additional features of its own.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIts initial popularity stems primarily from its improvements as
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china command language.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe primary interactive benefit of the KornShell command language
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis a visual command line editor that allows you to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmake corrections to your current command line
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor to earlier command lines,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwithout having to retype them.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinHowever,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinin the long run,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe power of the KornShell language as a high-level programming language,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas described by Dolotta and Mashey\*(Rf,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinT. A. Dolotta and J. R. Mashey,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I "Using the shell as a Primary Programming Tool,"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinProc. 2nd. Int. Conf. on Software Engineering, 1976,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinpages 169-176.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmay prove to be of greater significance.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh-93\fP provides the programming power of several
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinother interpretive languages such as \f5awk\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5FIT\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5PERL\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5tcl\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAn application that was originally written in the C programming language
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwas rewritten in the KornShell language.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinMore than 20,000 lines of C code were replaced with KornShell scripts
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintotaling fewer than 700 lines.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn most instances there was no perceptible difference in performance
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbetween the two versions of the code.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe KornShell language has been embedded into windowing systems
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinallowing graphical user interfaces to be developed in shell
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinrather than having to build applications that need to be
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincompiled.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe \f5wksh\fP program\*(Rf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinJ. S. Pendergrast,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IR "WKSH - Korn Shell with X-Windows Support",
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinUSL. 1991.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprovides a method of developing OpenLook or Motif
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinapplications as \f5ksh\fP scripts.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis memo is an introduction to \f5ksh-93\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe program that implements an enhanced version
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinof the KornShell language.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIt is referred to as \f5ksh\fP in the rest of this memo.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe memo describes the KornShell language based on the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfeatures of the 12/28/93 release of \f5ksh\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis memo is not a tutorial, only an introduction.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe second edition of reference [9] gives
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china more complete treatment of the KornShell language.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA concerted effort has been made to achieve both System V Bourne shell
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincompatibility and IEEE POSIX compatibility
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinso that scripts written for either of these shells
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan run without modification with \f5ksh\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn addition, \f5ksh-93\fP attempts to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbe compatible with older versions of \f5ksh\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhen there are conflicts between versions of the shell,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh-93\fP selects the behavior dictated by the IEEE POSIX
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstandard.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe description of features in this memo assumes
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthat the reader is already familiar with the Bourne shell.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 1 "COMMAND LANGUAGE"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThere is no separate command language.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAll features of the language, except job control,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan be
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinused both within a script and interactively from a terminal.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinHowever, features that are more likely to be used
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhile running commands interactively from a terminal
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare presented here.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Setting Options"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBy convention, UNIX commands
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinconsist of a command name followed by options and other arguments.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOptions are either of the form \f5-\fP\fIletter\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor \f5-\fP\fIletter value\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn the former case, several options may be grouped after a single \f5-\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe argument \f5--\fP signifies an end to the option list and is
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinonly required when the first non-option argument begins with
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china \f5-\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinMost commands print an error message which
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinshows which options are permitted
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhen given incorrect arguments.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn addition, the option sequence \f5-?\fP causes most commands
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto print a usage message which lists the valid options.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOrdinarily, \f5ksh\fP executes a command by
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinusing the command name to locate a program to run
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand by running the program as a separate process.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSome commands, referred to as
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IR built-ins ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare carried out by \f5ksh\fP itself,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwithout creating a separate process.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe reasons that some commands are built-in are presented later.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn nearly all cases the distinction
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbetween a command that is built-in and one that
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis not is invisible to the user.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinHowever, nearly
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinall commands that are built-in follow command line conventions.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP has several options that can be set by the user
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas command line arguments at invocation and as option arguments to the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5set\fP command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinMost other options can be set with a single letter option or as a name
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthat follows the \f5-o\fP option.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinUse
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5set\ -o\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto display the current option settings.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSome of these options, such as
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B interactive
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B monitor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin(see
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I "Job Control"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbelow),
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare enabled automatically by \f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhen the shell is connected to a terminal device.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOther options, such as
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B noclobber
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR ignoreeof ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare normally placed in a startup file.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B noclobber
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption causes
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto print an error message when you use
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B >
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto redirect output to a file that already exists.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf you want to redirect to an existing file, then
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinyou have to use
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B >|
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto override
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B noclobber
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B ignoreeof
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis used to prevent the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I end-of-file
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincharacter, normally
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B ^D
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin(Control- d),
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfrom exiting the shell and possibly logging you out.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinYou must type \f5exit\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto log out.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinMost of the options are described in this memo as appropriate.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Command Aliases"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinCommand aliases provide a mechanism of associating a command name and
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinarguments with a shorter name.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAliases are defined with the \f5alias\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbuilt-in.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe form of an \f5alias\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand definition is:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ce
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5alias\fP \fIname\fP\f5=\fP\fIvalue\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAs with most other shell assignments, no space is allowed before or after
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe \f5=\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe characters of an alias name cannot be characters that are
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinspecial to the shell.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe replacement string,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I value,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan contain any valid shell script,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinincluding meta-characters such as pipe symbols and i/o-redirection
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprovided that they are quoted.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinUnlike
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5csh\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinaliases in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincannot take arguments.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe equivalent functionality of aliases with arguments can
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbe achieved with shell functions, described later.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAs a command is being read,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe command name is checked against a list of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I alias
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinnames.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf it is found,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe name is replaced by the alias value associated with the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I alias
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand then rescanned.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhen rescanning the value for an alias, alias substitutions
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare performed except for an alias that is currently being processed.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis prevents infinite loops in alias substitutions.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor example with the aliases, \f5alias\ l=ls\ 'ls=ls\ -C'\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe command name \f5l\fP becomes \f5ls\fP, which becomes \f5ls\ -C\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOrdinarily, only the command name word is processed for alias substitution.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinHowever, if the value of an alias ends in a space,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen the word following the alias is also checked for alias substitution.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis makes it possible
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto define an alias whose first argument is the name of a command
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand have alias substitution performed on this argument,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfor example
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5nohup='nohup\ '\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAliases can be used to redefine built-in commands so that
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe alias,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ce
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5alias test=./test\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan be used to look for \f5test\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinin your current working directory rather than
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinusing the built-in \f5test\fP command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinReserved words such as
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5for\fP and \f5while\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincannot be changed by aliasing.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe command \f5alias\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwithout arguments, generates
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china list of aliases and corresponding alias values.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe \f5unalias\fP command removes the name and text of an alias.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAliases are used to save typing and to improve readability of scripts.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSeveral aliases are predefined by \f5ksh\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor example, the predefined alias
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ce
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5alias integer='typeset -i'\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinallows the integer variables \f5i\fP and \f5j\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto be declared and initialized with the command
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ce
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5integer i=0 j=1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhile aliases can be defined in scripts,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinit is not recommended.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe location of an alias command can be important
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsince aliases are only processed when a command is read.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA \fB\s+2.\s-2\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprocedure (the shell equivalent of an include file)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis read all at once (unlike
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstart up files
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhich are read a command at
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china time) so that any aliases defined there will not effect any commands
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwithin this script.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinPredefined aliases do not have this problem.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Command Re-entry"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhen run interactively,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP saves the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommands you type at a terminal in a file.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf the variable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1HISTFILE\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis set to the name of a file to which the user
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinhas write access,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen the commands are stored in this
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I history
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfile.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOtherwise the file
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB$\s-1HOME\s+1/.sh_history\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis checked for write access and if this fails
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinan unnamed file is used to hold the history lines.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinCommands are always appended to this file.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinInstances of \f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthat run concurrently and use the same history file
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinname, share access to the history file so that a command
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinentered in one shell will be available for editing in another
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinshell.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe file may be truncated when \f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindetermines that no other shell is using the history file.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe number of commands accessible to the user is determined by the value of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1HISTSIZE\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable at the time the shell is invoked.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe default value is 256.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinEach command may consist of one or more lines since a compound
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand is considered one command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf the character
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B !
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis placed within the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I "primary prompt"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstring,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1PS1\s+1\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen it is replaced by the command number each time the prompt is given.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA built-in command named \f5hist\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis used to list and/or edit
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinany of these saved commands.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe option
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-l
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis used to specify listing of previous commands.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe command can always be specified with
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china range of one or more commands.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe range can be specified by giving the command
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinnumber, relative or absolute, or by giving
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe first character or characters of the command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhen given without specifying the range,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe last 16
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommands are listed, each
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinpreceded by the command number.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf the listing option is not selected,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen the range of commands specified,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor the last command if no range is given,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis passed to an editor program before
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbeing re-executed by \f5ksh\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe editor to be used may be specified
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwith the option
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-e
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand following it with the editor name.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf this option is not specified, the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvalue of the shell variable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1HISTEDIT\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis used as the name of the editor,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinproviding that this variable has a non-null value.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf this variable is not set, or is null,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-e
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption has not been selected,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5/bin/ed\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis used.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhen editing has been complete,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe edited text automatically becomes
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe input for \f5ksh\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAs this text is read by \f5ksh\fP, it is echoed onto the terminal.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-s
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption causes the editing to be bypassed
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand just re-executes the command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn this case only a single command can be specified as the range
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand an optional argument of the form
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fIold\fP\fB=\fP\fInew\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmay be added which requests a simple string substitution
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprior to evaluation.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA convenient alias,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ce
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5alias r='hist -s'\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinhas been pre-defined so that
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe single key-stroke
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5r\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan be used to re-execute the previous command
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand the key-stroke sequence,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5r\ abc=def\ c\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan be used to re-execute the last command that starts with
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe letter \f5c\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwith the first occurrence of the string \f5abc\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinreplaced with the string \f5def\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinTyping
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5r\ c\ >\ file\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinre-executes the most recent command starting with the letter \f5c\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwith standard output redirected to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IR file .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "In-line editing"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinLines typed from a terminal frequently need changes made
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbefore entering them.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWith the Bourne shell the only method to fix up commands
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis by backspacing or killing the whole line.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP offers options that allow the user to edit parts of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincurrent command line before submitting the command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe in-line edit options make the command line into a single
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinline screen edit window.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhen the command is longer than the width of the terminal,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinonly a portion of the command is visible.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinMoving within the line automatically makes that portion visible.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinEditing can be performed on this window until the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I return
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinkey is pressed.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe editing modes have editing directives that access the history file
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinin which previous commands are saved.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA user can copy any of the most recent
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1HISTSIZE\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommands from this file into the input edit window.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinYou can locate commands by searching or by position.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe in-line editing options do not use the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I termcap
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I terminfo
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindatabases.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThey work on most standard terminals.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThey only require that the backspace character moves the cursor left
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand the space character overwrites the current character on the screen
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand moves the cursor to the right.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinVery few terminals or terminal emulators do not have
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthis behavior.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThere is a choice of editor options.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR emacs ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR gmacs ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B vi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption is selected by turning on the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincorresponding
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption of the \f5set\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf the value of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1EDITOR\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1VISUAL\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariables ends with any of these suffixes
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe corresponding option is turned on.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA large subset of each of these editors'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfeatures is available within the shell. Additional
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfunctions, such as file name completion, have also been added.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B emacs
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B gmacs
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmode the user positions the cursor to the point
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinneeding correction and inserts, deletes, or replaces
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincharacters as needed.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe only difference between these two modes is the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmeaning of the directive
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR ^T .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinControl keys and escape sequences are used for cursor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinpositioning and control functions.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe available editing functions are listed in the manual page.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B vi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinediting mode
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstarts in insert mode and enters control mode when the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinuser types ESC ( 033 ).
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I return
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinkey, which submits the current command for processing,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan be entered from either mode.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe cursor can be anywhere on the line.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA subset of commonly used
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I vi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinediting directives are available.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B k
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B j
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindirectives that normally move up and down by one
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IR line ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmove up and down one
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I command
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinin the history file,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincopying the command into the input edit window.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor reasons of efficiency,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe terminal is kept in canonical mode until an
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinESC
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis typed.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOn some terminals,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand on earlier versions of the UNIX operating system,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthis doesn't work correctly.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B viraw
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhich always uses
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I raw
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I cbreak
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmode,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmust be used in this case.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinMost of the code for the editing options does not rely on the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP code and can be used in a stand-alone mode with most any command
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto add in-line edit capability.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinHowever,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinall versions of the in-line editors have some features that
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinuse some shell specific code. For example,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwith all edit modes, the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinESC-=
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindirective applied to command words
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin(the first word on the line,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor the first word after a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR ; ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR | ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR ( ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR & )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinlists all aliases, functions, or commands
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthat match the portion of the given current word.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhen applied to other words, this directive
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprints the names of files that match the current
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinword.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe ESC\fB-*\fP directive
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinadds the expanded list of matching files to the command line.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA trailing
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis added to the word if it doesn't contain any file pattern matching
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincharacters before the expansion.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B emacs
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B gmacs
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmode,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinESC-ESC
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinindicates command completion when applied to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand names, otherwise it indicates pathname completion.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWith command or pathname completion,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe list generated by the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinESC-= directive is examined to find
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe longest common prefix.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWith command completion, only the last component of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe pathname is used to compute the longest command prefix.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf the longest common prefix is a complete match,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen the word is replaced by the pathname, and a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B /
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis appended if
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinpathname is a directory, otherwise a space is added.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B vi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmode,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \e
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfrom control mode gives the same behavior.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Key Binding"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIt is possible to intercept keys as they are entered and
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinapply new meanings or bindings.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA trap named
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1KEYBD\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis evaluated each time
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP processes characters entered
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfrom the keyboard,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinother than those typed
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhile entering a search string or an argument to an
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinedit directive such as
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B r
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinin vi-mode.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe action associated with this trap can change the value of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe entered key to cause the key to perform a different
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoperation.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhen the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1KEYBD\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintrap is entered,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe \fB.sh.edtext\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable contains the contents of the current input line
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand the \fB.sh.edcol\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable gives the current cursor position within this line.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe \fB.sh.edmode\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable contains the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B ESC
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincharacter when the trap is entered from
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B vi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chininsert mode.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOtherwise, this value is null.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe \fB.sh.edchar\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable contains the character or
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinescape sequence that caused the trap.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA key sequence is either a single character,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B ESC
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfollowed by a single character,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B ESC[
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfollowed by a single character.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn the \fBvi\fP edit mode,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe characters after the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B ESC
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmust be entered within half a second after the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR ESC .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe value of \fB.sh.edchar\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinat the end of the trap will be used as
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe input sequence.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinUsing the associative array facility of \f5ksh\fP described later,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand the function facility of \f5ksh\fP, it is easy to write
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china single trap so that keys can be bound dynamically. For example,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.nf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in .5i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ta 4i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5typeset -A Keytable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintrap 'eval "${Keytable[${.sh.edchar}]}"' KEYBD
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfunction keybind # key action
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin typeset key=$(print -f "%q" "$2")
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin case $# in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin 2) Keytable[$1]='.sh.edchar=${.sh.edmode}'"$key"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin ;;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin 1) unset Keytable[$1]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin ;;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *) print -u2 "Usage: $0 key [action]"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin ;;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin esac
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ta
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.fi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Job Control"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe job control mechanism
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis almost identical to the version introduced in \f5csh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinof the Berkeley UNIX operating system,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinversion 4.1 and later.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe job control feature allows the user to stop and
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinrestart programs, and to move programs to and from the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinforeground and the background.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIt will only work on systems that provide support for
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthese features.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinHowever,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chineven systems without job control have a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B monitor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption which, when enabled, will report the progress
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinof background jobs and enable the user to \f5kill\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinjobs by job number or job name.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAn interactive shell associates a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I job
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwith each pipeline typed in from the terminal
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand assigns it a small integer number
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincalled the job number.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf the job is run asynchronously,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe job number is printed at the terminal.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAt any given time, only one job owns the terminal,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chini.e., keyboard signals are only sent to the processes in one job.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhen \f5ksh\fP creates a foreground job,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinit gives it ownership of the terminal.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf you are running a job and wish to stop
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinit you hit the key
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B ^Z
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin(control-\fBZ\fP)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhich sends a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1STOP\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsignal to all processes in the current job.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe shell receives notification that the processes
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinhave stopped and takes back control of the terminal.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThere are commands to continue programs in the foreground
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand background.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThere are several ways to refer to jobs.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe character
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B %
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinintroduces a job name.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinYou can refer to jobs by name or number as described in the manual page.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe built-in command \f5bg\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinallows you to continue a job in the background,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhile the built-in command \f5fg\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinallows you to continue a job in the foreground even
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthough you may have started it in the background.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA job being run in the background will stop if it tries
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto read from the terminal.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIt is also possible to stop background jobs that try to write on
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe terminal by setting the terminal options
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinappropriately.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThere is a built-in command \f5jobs\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthat lists the status of all running and stopped jobs.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn addition,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinyou are informed of the change of state (running or stopped)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinof any background
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinjobs just before each prompt.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf you want to be notified about background job completions
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas soon as they occur without waiting for a prompt, then use the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B notify
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhen you try to exit the shell while jobs are stopped or running,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinyou will receive a message from \f5ksh\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf you ignore this message and try to exit again,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinall stopped processes will be terminated.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn addition, for login shells, the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1HUP\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsignal will be sent to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinall background jobs
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinunless the job has been disowned with the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B disown
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA built-in version of \f5kill\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmakes it possible to use
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I job
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinnumbers as targets for signals.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSignals can be selected by number or name.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe name of the signal is the name found in the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I include
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfile
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B /usr/include/sys/signal.h
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwith the prefix
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1SIG\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinremoved.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-l
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption of \f5kill\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprovides a means to map individual signal names to and from
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsignal number.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn addition, if no signal name or number is given,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5kill\ -l\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chingenerates a list of valid signal names.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Changing Directories"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBy default,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmaintains a logical view of the file system hierarchy
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhich makes symbolic links transparent.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor systems that have symbolic links,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthis means that if \f5/bin\fP is a symbolic link to \f5/usr/bin\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand you change directory to \f5/bin\fP, \f5pwd\fP will indicate
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthat you are in \f5/bin\fP, not \f5/usr/bin\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5pwd\ -P\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chingenerates the physical pathname of the present working
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindirectory by resolving all the symbolic links.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBy default,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe \f5cd\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand will take you where you expect to go even if you cross
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsymbolic links.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA subsequent \f5cd\ ..\fP in the example above
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwill place you in \f5/\fP, not \f5/usr\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOn systems with symbolic links,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5cd\ -P\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincauses
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B ..
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto be treated physically.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP remembers your last directory
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinin the variable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1OLDPWD\s+1\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe \f5cd\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbuilt-in can be given with argument
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto return to the previous directory
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand print the name of the directory.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinNote that \f5cd\ -\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindone twice returns you to the starting directory,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinnot the second previous directory.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA directory
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I stack
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmanager has been written as shell
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I functions
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I push
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I pop
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindirectories from the stack.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Prompts"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhen \f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinreads commands from a terminal,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinit issues a prompt whenever it is ready
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto accept more input and then
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwaits for the user to respond.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1TMOUT\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan be set to be the number of seconds that the shell will wait for
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chininput before terminating.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA 60 second warning message is printed
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbefore terminating.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe shell uses two prompts.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe primary prompt,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindefined by the value of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1PS1\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis issued at the start of each command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe secondary prompt,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindefined by the value of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1PS2\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis issued when more input is needed to complete a command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP allows the user to specify a list of files or directories
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto check before issuing the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1PS1\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprompt.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe variable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1MAILPATH\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis a colon (
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B :
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin) separated list of file names to be checked for changes
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinperiodically. The user is notified
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbefore the next prompt.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinEach of the names in this list can be followed by a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B ?
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand a message to be given when a change has been detected in the file.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe prompt will be evaluated for parameter expansion, command
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsubstitution and arithmetic expansion which are described later.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe parameter
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B $_
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwithin a mail message will evaluate to the name of the file that
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinhas changed.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe parameter
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1MAILCHECK\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis used to specify the minimal interval in seconds before
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinnew mail is checked for.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn addition to replacing each
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B !
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinin the prompt with the command number,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP expands
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe value of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1PS1\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfor parameter expansions, arithmetic expansions,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand command substitutions as described below
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto generate the prompt.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe expansion characters that are to be applied when
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe prompt is issued must be quoted to prevent the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinexpansions from occurring when assigning the value to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1PS1\s+1.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor example,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f3\s-1PS1\s+1="$\s-1PWD\s+1"\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincauses
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1PS1\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto be set to the value of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1PWD\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinat the time of the assignment whereas
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1PS1\s+1='$\s-1PWD\s+1'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincauses
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1PWD\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto be expanded at the time the prompt is issued.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinCommand substitution may require a separate process
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto execute and cause the prompt display to be somewhat
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinslow, especially
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhen the return key is pressed several times in a row.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinTherefore, its use
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwithin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1PS1\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis discouraged.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSome variables are maintained by \f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinso that their values can be used with
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1PS1\s+1.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1PWD\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable stores the pathname of the current working directory.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe value of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1SECONDS\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis the value of the most
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinrecent assignment plus the elapsed time.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBy default, the time is measured in milli-seconds,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbut since
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1SECONDS\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis a floating point variable, the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinnumber of places after the decimal point in the expanded
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvalue can be
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinspecified with
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5typeset\ -F\fP\fIplaces\fP\f5\ SECONDS\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn a roundabout way, this variable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan be used to generate a time stamp into the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1PS1\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprompt without creating a process at each prompt.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe following code explains how you can do this on
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSystem V. On BSD, you need a different command to initialize
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1SECONDS\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.nf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in .5i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin# . this script and use $TIME as part of your PS1 string to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin# get the time of day in your prompt
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintypeset -RZ2 _x1 _x2 _x3
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin(( SECONDS=$(date '+3600*%H+60*%M+%S') ))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin_s='_x1=(SECONDS/3600)%24,_x2=(SECONDS/60)%60,_x3=SECONDS%60,0'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinTIME='"${_d[_s]}$_x1:$_x2:$_x3"'
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin# PS1=${TIME}whatever
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.fi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ta
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Tilde substitution"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe character
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \(ap
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinat the beginning of a word has special meaning to \f5ksh\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf the characters after the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \(ap
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinup to a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B /
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmatch a user login name in the password database, then the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \(ap
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand the name are replaced by
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthat user's login directory.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf no match is found, the original word
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis unchanged.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \(ap
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinby itself, or in front of a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR / ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis replaced by the value of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1HOME\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinparameter.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \(ap
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfollowed by a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B +
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis replaced by the value of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B $\s-1PWD\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B $\s-1OLDPWD\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinrespectively.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Output formats"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe output of built-in commands and traces have values quoted so that they
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan be re-input to the shell.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis makes it easy to cut and paste shell output on systems
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhich use a pointing device such as a mouse.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn addition, output can be saved in a file for reuse.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "The \fB\s-1ENV\s+1\fP file"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhen an interactive \f5ksh\fP starts, it evaluates the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B $\s-1ENV\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable to arrive at a file name.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf this value is not null,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP attempts to read and process
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommands in a file by this name.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinEarlier versions of \f5ksh\fP read the \fB\s-1ENV\s+1\fP file
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfor all invocations of the shell primarily to allow
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfunction definitions to be available for all shell
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chininvocations.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe function search path, \fB\s-1FPATH\s+1\fP, described later,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chineliminated the primary need for this capability and it was
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinremoved because the high performance cost was no longer
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindeemed acceptable.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 1 "PROGRAMMING LANGUAGE"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe KornShell vastly extends the set of applications that
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan be implemented efficiently at the shell level.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIt does this by providing simple yet powerful mechanisms
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto perform arithmetic, pattern matching,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsubstring generation,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand arrays.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinUsers can write applications as separate functions that can
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbe defined in the same file or in a library of functions
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstored in a directory and loaded on demand.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "String Processing"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe shell is primarily a string processing language.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBy default, variables hold variable length strings.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThere are no limits to the length of strings. Storage
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmanagement is handled by the shell automatically.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinDeclarations are not required.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWith most programming languages, string constants are designated
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinby enclosing characters in single quotes or double quotes.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSince most of the words in the language are strings, the shell
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinrequires quotes only when a string contains characters that
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare normally processed specially by the shell, but their
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinliteral meaning is intended.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinHowever, since the shell is a string processing language,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand some characters can occur as literals and as language metacharacters,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinquoting is an important part of the language.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThere are four quoting mechanisms in \f5ksh\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe simplest is to enclose a sequence of characters inside single quotes.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAll characters between a pair of single quotes have their literal meaning;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe single quote itself cannot appear.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B $
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinimmediately preceding
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china single quoted string
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincauses all the characters until the matching single quote
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto be interpreted as an ANSI-C language string.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThus, \f5'\en'\fP represents characters \f5\e\fP and
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5n\fP, whereas, \f5$'\en'\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinrepresents the new-line character.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinDouble quoted strings remove the special meaning of all characters
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinexcept
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR $ ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR \(ga ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR \e ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinso that parameter expansion and command substitution (defined below)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare performed.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe final mechanism for quoting a character is by preceding it with the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinescape character
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR \e\^ .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis mechanism works outside of quoted strings and for the characters
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR $ ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR \(ga ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB"\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \e
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinin double quoted strings.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinVariables are designated by
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinone or more
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstrings of alphanumeric
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincharacters beginning with an alphabetic character
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinseparated by a \fB\s+2.\s-2\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinUpper and lower case characters are distinct, so that the variable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B A
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare names of different variables.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThere is no
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinlimit to the length of the name of a variable.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinYou do not have to declare variables.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinYou can assign a value to a variable by writing the name of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable, followed by an equal sign, followed by a character string
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthat represents its value.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinTo create a variable whose name
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincontains a \fB\s+2.\s-2\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe variable whose name consists of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe characters before the last \fB\s+2.\s-2\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmust already exist.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinYou reference a variable by
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinputting the name inside curly braces and
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinpreceding the braces with a dollar sign.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe braces may be omitted when the name
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis alphanumeric.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf \f5x\fP and \f5y\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare two shell variables, then
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto define a new variable,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5z\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhose value is
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe concatenation of the values of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5x\fP and \f5y\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinyou just say
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5z=$x$y\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIt is that easy.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B $
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan be thought of as meaning
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin"value of."
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinYou can also capture the output of any command with the notation
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BI $( command ) .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis is referred to as command substitution.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor example,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5x=$(date)\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinassigns the output from the \f5date\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand to the variable \f5x\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinCommand substitution in the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBourne shell is denoted by enclosing the command between
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbackquotes,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin(\fB\(ga\^\(ga\fP).
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis notation
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsuffers from some
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincomplicated quoting rules.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThus, it is hard to write \f5sed\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinpatterns which contains back slashes within command substitution.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinPutting the pattern in single quotes
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis of little help.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP accepts the Bourne shell command substitution syntax
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfor backward compatibility.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BI $( command )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinnotation allows
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe \fIcommand\fP itself to contain quoted strings even if the substitution
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoccurs within double quotes. Nesting is legal.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe special command substitution of the form
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5$(cat\ file)\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan be replaced by
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5$(<\ file)\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhich is faster because
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe \f5cat\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand doesn't have to run.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Shell Parameters and Variables"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThere are three types of parameters used by \f5ksh\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinspecial parameters, positional parameters, and named
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinparameters which are called variables.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP defines the same special parameters,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR 0 ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR * ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR @ ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR # ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR ? ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR $ ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR ! ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR \- ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas in the Bourne shell.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinPositional parameters are set when the shell is invoked,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas arguments to the \f5set\fP built-in,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand by calls to functions (see below) and \fB\s+2.\s-2\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprocedures.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThey are named by numbers starting at 1.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe third type of parameter is a variable.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAs mentioned earlier,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP uses variables whose names
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinconsist of one or more
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinalpha-numeric strings separated by a \fB\s+2.\s-2\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThere is no need to specify the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I type
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinof a variable in the shell because, by default,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariables store strings of arbitrary length
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand values will automatically be converted to numbers
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhen used in an arithmetic context.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinHowever, \f5ksh\fP variables
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan have one or more
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I attributes
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthat control the internal representation of the variable,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe way the variable is printed, and its access or
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinscope.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn addition,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinallows variables to represent arrays of values
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand references to other variables.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe \f5typeset\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbuilt-in command of \f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinassigns attributes to variables.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinTwo of the attributes,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I readonly
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IR export ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare available in the Bourne shell.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinMost of the remaining attributes are discussed here.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe complete list of attributes appears in the manual.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe \f5unset\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbuilt-in of \f5ksh\fP removes
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvalues and attributes of variables.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhen a variable is exported, certain of its attributes are also exported.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhenever a value is assigned to a variable,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe value is transformed according to the attributes of the variable.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinChanging the attribute of a variable can change its value.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe attributes
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-L
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-R
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare for left and right field justification respectively.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThey are useful for aligning columns in a report.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor each of these attributes, a width can be defined explicitly or else
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinit is defined the first time an assignment is made to the variable.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinEach assignment causes justification of the field, truncating
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif necessary.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAssignment to fixed sized variables
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprovides one way to generate a substring consisting of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china fixed number of characters from
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe beginning or end of a string.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOther methods are discussed later.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe attributes
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-u
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-l
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare used for upper case and lower case
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinformatting, respectively.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSince it makes no sense to have both attributes on simultaneously,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinturning on either of these attributes turns the other off.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe following script,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinusing \f5read\fP and \f5print\fP which are described later,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprovides an example of the use of shell variables
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwith attributes.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis script reads a file of lines each consisting of five fields separated by
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B :
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand prints fields 4 and 2 in upper case in columns 1-15, left justified,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand columns 20-25 right-justified respectively.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.nf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in .5i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ta 3.4i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5typeset -uL15 f4 # 15 character left justified
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintypeset -uR6 f2 # 6 character right justified
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIFS=: # set field separator to :
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhile read -r f1 f2 f3 f4 f5 # read line, split into fields
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindo print -r -- "$f4 $f2" # print fields 4 and 2
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindone\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.fi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ta
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR \-i ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR \-E ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR \-F ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinattributes are used to represent numbers.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinEach can be followed by a decimal number.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinattribute causes the value to be represented as an integer and it
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan be followed by a number representing the numeric base when expanding
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinits value.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhenever a value is assigned to an integer variable, it is evaluated
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas an arithmetic expression
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand then truncated to an integer.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-E
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinattribute causes the value to be represented in scientific
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinnotation whenever its value is expanded. The number following the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-E
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindetermines the number of significant figures, and defaults to 6.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-F
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinattribute causes the value to be represented with a fixed number
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinof places after the decimal point.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAssignments to variables with the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-E
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-F
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinattributes cause the evaluation of the right hand side of the assignment.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP allows one-dimensional
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I arrays
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinin addition to simple variables.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThere are two types of arrays; associative arrays
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand indexed arrays.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe subscript for an associative array is an arbitrary
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstring, whereas the subscript for an indexed array is
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinan arithmetic expression that is evaluated to yield an integer
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinindex.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAny variable can become an indexed array
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinby referring to it with
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinan integer
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IR subscript .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAll elements of an array need not exist.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSubscripts for arrays
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmust evaluate to an
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chininteger between 0 and some maximum value, otherwise
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinan error results.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe maximum value may vary from one machine to another but
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis at least 4095.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinEvaluation of subscripts is described in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe next section.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAttributes apply to the whole array.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAssignments to array variables can be made to individual elements
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvia parameter
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinassignment commands or the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B typeset
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbuilt-in.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAdditionally, values can be assigned sequentially with
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincompound assignment as described below, or by the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-A
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I name
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption of the \f5set\fP command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinReferencing of subscripted variables requires the character
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR $ ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbut also requires braces around the array element name.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe braces are needed to avoid conflicts with the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfile name generation mechanism.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe form of any array element reference is:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ce
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BI ${ name [ subscript ]}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSubscript values of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B @
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan be used to generate all elements of an array,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas they are used for expansion of positional parameters.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe list of currently defined subscripts for a given
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable can be generated with
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BI ${! name [@]} ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BI ${! name [*]} .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-n
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I nameref
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinattribute causes the variable to be treated
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas a reference to the variable defined by its value.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOnce this attribute is set, all references to this variable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbecome references to the variable named by the value
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinof this variable.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor example, if \f5foo=bar\fP, then setting the reference
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinattribute on \f5foo\fP will cause all subsequent references
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto \f5foo\fP to behave as the variable whose name is \f5$foo\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwas referenced, which in this case is the variable \f5bar\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinUnsetting this attribute breaks the association.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinReference variables are usually used inside functions whose
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinarguments are the names of shell variables.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe names for reference variables cannot contain a \fB\s+2.\s-2\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhenever a shell variable is referenced, the portion of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable up to the first \fB\s+2.\s-2\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis checked to see whether it matches the name of a reference
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf it does, then the name of the variable actually used
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinconsists of the concatenation of the name of the variable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindefined by the reference plus the remaining portion of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoriginal variable name.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor example, using the predefined alias, \f5alias\ nameref='typeset\ -n'\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.nf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in .5i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ta 3.4i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5\^.bar.home.bam="hello world"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinnameref foo=.bar.home
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprint ${foo.bam}
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fBhello world\fP\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.fi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ta
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Compound Assignment"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinCompound assignments are used to assign values to arrays
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand compound data structures.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe syntax for a compound assignment is
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IB name =( assignment-list )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhere
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fIname\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis the name of the variable to which you want to assign values.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinNo space is permitted between the variable name and the \fB=\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbut can appear between the \fB=\fP and the open parenthesis.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinNew-lines can appear between the parentheses.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe \fIassignment-list\fP can be in several different forms
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinyielding different results.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf \fIassignment-list\fP is simply a list of words, then
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe words are processed as they are with the \f5for\fP command
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand assigned sequentially as an indexed array.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor example,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ce
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5foo=( * )\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincreates an indexed array \f5foo\fP and assigns the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfile names in the current directory to each index starting
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinat zero.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe second form for \fIassignment-list\fP is a list of assignments
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinof the special form \fB[\fP\fIword\fP\fB]=\fP\fIword\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinNo space is permitted before or after the \fB=\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn this case, the variable given by \fIname\fP becomes
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinan associative array with the given arguments as subscripts.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor example,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ce
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5bar=( [color]=red [shape]=box )\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincreates an associate array named \f5bar\fP whose
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsubscripts are \f5color\fP and \f5shape\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe third form for \fIassignment-list\fP is a list of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinnormal assignments, including compound assignments.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThese assignments cause sub-variables to be assigned
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincorresponding to the given assignments.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn addition to assignments, the \fIassignment-list\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan contain \f5typeset\fP commands.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn addition to creating sub-variables,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe effect of a compound assignment is to make
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe value of the original variable be a parenthesized
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinassignment list of its components.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor example, the assignment
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.nf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in .5i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ta 3.4i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5foo=(
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin left=bar
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin typeset -i count=3
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin point=(
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin x=50
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin y=60
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin colors=( red green yellow )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin right=bam
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin) \fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ta
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.fi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis equivalent to the assignments
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.nf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in .5i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ta 3.4i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5foo.left=bar
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfoo.count=3
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfoo.point.x=50
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfoo.point.y=60
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfoo.colors=( red green yellow )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfoo.right=bam\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ta
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.fi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn addition, the value of \f5"$foo"\fP is
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.nf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in .5i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ta 3.4i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5(
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin colors=( red green yellow )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin left=bar
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin typeset -i count=3
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin point=(
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin y=60
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin x=50
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin right=bam
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin)\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ta
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.fi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Substring Generation"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe expansion of a variable or parameter can be modified so that
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinonly a portion of the value results.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIt is often necessary to extract a portion of a shell variable or
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china portion of an array.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThere are several parameter expansion operators that can do this.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOne method to generate a substring is with an expansion of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe form \fB${\fP\fIname\fP\fB:\fP\fIoffset\fP\fB:\fP\fIlength\fP\fB}\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhere \fIoffset\^\fP is an arithmetic expression that defines the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoffset of the first character starting from 0, and
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fIlength\^\fP is an arithmetic expression that defines the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinlength of the substring.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BI : length\^
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis omitted,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe length of the value of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I name\^
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstarting at
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I offset\^
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis used.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BI : offset : length
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoperators can also be applied to array expansions and to parameters
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B @
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto generate portions of an array.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor 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
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstarting at the element \fIoffset\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe other parameter expansion modifiers use shell patterns
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto describe portions of the string to modify and delete.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA description of shell patterns is contained below.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhen these
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmodifiers are applied to special parameters
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B @
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor to array parameters given as
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fIname\fP\fB[@]\fP or \fIname\fP\fB[*]\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe operation is performed on each element.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThere are four parameter expansion modifiers that
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstrip off leading and trailing substrings
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinduring parameter expansion
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinby removing the characters matching a given pattern.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAn expansion of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe form \fB${\fP\fIname\fP\fB#\fP\fIpattern\fP\fB}\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincauses the smallest matching prefix of the value of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I name\^
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto be removed.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe largest prefix matching
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I pattern\^
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis removed by using
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B ##
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chininstead of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR # .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSimilarly,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinan expansion of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe form \fB${\fP\fIname\fP\fB%\fP\fIpattern\fP\fB}\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincauses the smallest matching substring at the end of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I name\^
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto be removed.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAgain, using
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B %%
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chininstead of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR % ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincauses the largest matching trailing substring to be deleted.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor example, if the shell variable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B file
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinhas value
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR foo.c ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen the expression
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B ${file%.c}.o
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinhas value
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR foo.o .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe value of an expansion can be changed by
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinspecifying a pattern that matches the part that needs to be changed
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinafter the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe parameter expansion modifier
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR / .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAn expansion of the form
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB${\fP\fIname\fP\fB/\fP\fIpattern\fP\fB/\fP\fIstring\fP\fB}\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinreplaces the first match of \fIpattern\fP with
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe value of variable \fIname\fP to \fIstring\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe second
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B /
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis not necessary when \fIstring\fP is null.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe expansion
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB${\fP\fIname\fP\fB//\fP\fIpattern\fP\fB/\fP\fIstring\fP\fB}\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinchanges all occurrences of the \fIpattern\fP into \fIstring\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe parameter expansion modifiers
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B /#
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B /%
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincause the matching pattern to be anchored to the beginning and
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinend respectively.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFinally, there are parameter expansion modifiers that yield
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe name of the variable, the string length of the value, or the number
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinof elements of an array.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB${!\fP\fIname\fP\fB}\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinyields the name of the variable which will be \fIname\fP itself
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinexcept when \fIname\fP is a reference variable. In this case
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinit will yield the name of the variable it refers to.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhen applied to an array variable,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB${!\fP\fIname\fP\fB[@]}\fP and
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB${!\fP\fIname\fP\fB[*]}\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chingenerate the names of all subscripts.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB${#\fP\fIname\fP\fB}\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwill be the length in bytes of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB$\fP\fIname\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor an array variable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB${#\fP\fIname\fP\fB[*]}\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chingives the number of elements in the array.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Arithmetic Evaluation"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor the most part, the shell is a string processing
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinlanguage. However, the need for arithmetic has
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinlong been obvious.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinMany of the characters that are special to the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBourne shell are needed as arithmetic operators.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinTo make arithmetic easy to use, and to maintain
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincompatibility with the Bourne shell, \f5ksh\fP uses matching
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B ((
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B ))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto delineate arithmetic expressions.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhile single parentheses might have been
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmore desirable, these already mean
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I subshell\^
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinso that another notation was required.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe arithmetic expression
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chininside the double parentheses
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfollows the same syntax, associativity and precedence
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas the ANSI-C\*(Rf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAmerican National Standard for Information Systems \- Programming
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinLanguage \- C, ANSI X3.159-1989.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprogramming language.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe characters between the matching double parentheses
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare processed with the same rules used for double
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinquotes so that spaces can be used to aid readability
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwithout additional quoting.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAll arithmetic evaluations are performed using
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindouble precision floating point arithmetic.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFloating point constants follow the same rules as
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe ANSI-C programming language.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinInteger arithmetic constants are written as
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ce
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IB base # number,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhere
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I base\^
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis a decimal integer between
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintwo and sixty-four and
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I number\^
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis any non-negative number.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBase ten is used
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhen no base is specified.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe digits are represented by the characters
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR 0-9a-zA-Z_@ .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor bases less than or equal to 36,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinupper and lower case characters can
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbe used interchangeably to represent the digits
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfrom 10 thru 35.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinArithmetic expressions are made from constants,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariables, and operators.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinParentheses may be used for grouping.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe contents inside the double parentheses
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare processed with the same expansions as occurs in a double quoted string,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinso that all
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B $
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinexpansions are performed before the expression is evaluated.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinHowever, there is usually no need to use the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B $
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto get the value of a variable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbecause the arithmetic evaluator replaces the name of the variable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinby its value within an arithmetic expression.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B $
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincannot be used when the variable is the subject of assignment
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor an increment operation.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAs a rule it is better not to use
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B $
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinin front of variables in an arithmetic expression.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAn arithmetic command of the form
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin(( ... ))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.R
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis a command that evaluates the enclosed arithmetic expression.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor example, the command
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ce
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5(( x++ ))\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan be used to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinincrement the variable \f5x\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinassuming that \f5x\fP contains some numerical value.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe arithmetic command is true (return value 0), when the resulting
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinexpression is non-zero, and false (return value 1) when the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinexpression evaluates to zero.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis makes the command easy to use with the \f5if\fP and \f5while\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincompound commands.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe \f5for\fP compound command
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinhas been extended for use in arithmetic contexts.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe syntax,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ce
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5for\fP \fB((\fP \fIexpr1\fP\fB;\fP \fIexpr2\fP \fB;\fP \fIexpr3 \fP\fB))\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan be used as the first line of a \f5for\fP loop with the same semantics
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas the \f5for\fP statement in the ANSI-C programming language.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinArithmetic evaluations can also be performed as part of the evaluation
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinof a command line.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe syntax
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin$((\ ...\ ))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.R
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinexpands to the value of the enclosed arithmetic expression.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis expansion can occur wherever parameter expansion is performed.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor example using the \f5ksh\fP command \f5print\fP (described
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinlater)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ce
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5print $((2+2))\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprints the number 4.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe following script prints the first
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I n
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinlines of its standard input onto its standard output,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhere
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I n
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan be supplied as an optional argument whose default value is 20.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.nf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in .5i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ta 4i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5integer n=${1-20} # set n
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhile (( n-- >=0 )) && read -r line # at most n lines
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindo print -r -- "$line"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindone\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.fi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ta
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Shell Expansions"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe commands you enter from the terminal or from a script
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare divided into words and each word undergoes several
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinexpansions to generate the command name and its arguments.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis is done in two phases.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe first phase recognizes reserved words, spaces and operators
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto decide where command boundaries lie.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAlias substitutions take place during this phase.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe second phase performs expansions in the following order:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BL
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.LI
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinTilde substitution,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinparameter expansion,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinarithmetic expansion,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand command substitution
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare performed from left to right.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe option
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-u
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR nounset ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwill cause an error to occur when any variable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthat is not set is expanded.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.LI
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe characters that result from parameter expansion and
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand substitution above are checked with the characters
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinin the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1IFS\s+1\fP variable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfor possible
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfield splitting.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin(See a description of \f5read\fP below to see how
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1IFS\s+1\fP is used.)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSetting
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1IFS\s+1\fP to a null
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvalue causes field splitting to be skipped.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.LI
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinPathname generation (as described below)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis performed on each of the fields.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAny field that doesn't match a pathname is left alone.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe option,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-f
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR noglob ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis used to disable pathname generation.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.LE
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Pattern Matching"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe shell is primarily a string processing language and uses
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinpatterns for matching file names as well as for matching strings.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe characters
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR ? ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR * ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B [
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare processed specially
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinby the shell when not quoted.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThese characters are used to form patterns that
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmatch strings.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinPatterns are used by the shell to match pathnames,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto specify substrings,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand for
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B case
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommands.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe character
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B ?
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmatches any one character.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe character
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmatches zero or more characters.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe character sequence
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR [ ... ]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindefines a character class
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthat matches any character contained within
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR [\^] .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA range of characters can be specified by putting a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbetween the first and last character of the range.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAn exclamation mark,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR ! ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinimmediately after the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR [ ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmeans match all characters except the characters specified.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor example, the pattern
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5a?c*.[!a-z]\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmatches any string beginning with an
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR a ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhose third character is a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR c ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand that ends in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin(dot) followed by any character except the lower case letters,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR a\-z .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe sequence \f5[:alpha:]\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chininside a character class, matches any set of characters in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe ANSI-C
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B alpha
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinclass.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSimilarly, \f5[:\fP\fIclass\fP\f5:]\fP matches
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chineach of the characters in the given \fIclass\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfor all the ANSI-C character classes.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor example, \f5[[:alnum:]_]\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmatches any alpha-numeric character or the character
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR _ .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintreats
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstrings of the form
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BI ( pattern-list
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR ) ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhere
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I pattern-list
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis a list of one or more patterns separated by a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR \(bv ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinspecially when preceded by
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR * ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR ? ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR + ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR @ ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR ! .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B ?
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinpreceding
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BI ( pattern-list )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmeans that the pattern list enclosed in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B (\^)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis optional.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAn
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BI @( pattern-list )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmatches any pattern in the list of patterns enclosed in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR () .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BI *( pattern-list )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmatches any string that contains zero or more of each of the enclosed
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinpatterns,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhereas
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BI +( pattern-list )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinrequires a match of one or more of any of the given patterns.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor instance, the pattern
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B +([0\-9])?(.)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmatches one or more digits optionally followed by a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR . (dot).
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BI !( pattern-list )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmatches anything except any of the given patterns.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor example,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5print\ !(*.o)\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindisplays all file names in the current directory that do not end in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR .o .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhen patterns are used to generate pathnames when expanding
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommands several other rules apply.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA separate match is made
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfor each file name component of the pathname.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinRead permission is required for
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinany portion of the pathname that contains any special
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinpattern character.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSearch permission is required for every component except
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinpossibly the last.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBy default,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfile names in each directory that begin with \fB\s+2.\s-2\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare skipped when performing a match.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf the pattern to be matched starts with a leading \fB\s+2.\s-2\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen only files beginning with a \fB\s+2.\s-2\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare examined when reading each directory to find matching files.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1FIGNORE\s+1\fP variable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis set,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen only files that do not match this pattern
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare considered.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis overrides the special meaning of \fB\s+2.\s-2\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinin a pattern and in a file name.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B markdirs
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption is set,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chineach matching pathname that is the name
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinof a directory has a trailing
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B /
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinappended to the name.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Conditional Expressions"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe Bourne shell uses the \f5test\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand, or the equivalent \f5[\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand, to test files for attributes
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand to compare strings or numbers.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe problem with \f5test\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis that the shell has expanded the words of the \f5test\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand and
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsplit them into arguments before \f5test\fP begins execution.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5test\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincannot distinguish between operators and operands.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn most cases
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5test\ "$1"\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwill test whether argument 1 is non-null.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinHowever,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif argument 1 is
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR \-f ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen \f5test\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwill treat
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-f
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas an operator and
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinyield a syntax error.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOne of the most frequent errors with
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5test\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoccurs when its operands are not within double quotes.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn this case, the argument may expand to more than a single
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinargument or to no argument at all. In either case this
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwill likely cause a syntax error.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhat makes this most insidious is that these errors are frequently
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindata dependent. A script that appears to run correctly may abort
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif given unexpected data.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinTo get around these problems,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinhas a compound command for conditional expression testing
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas part of the language.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe reserved words
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B [[
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindelimit the range of the command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBecause they are reserved words, not operator characters,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthey require spaces to separate them
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfrom arguments.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe words between
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B [[
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare not processed for field splitting or for pathname generation.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn addition, since \f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindetermines the operators before parameter expansion,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinexpansions that yield no argument cause no problem.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe operators within
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR [[ ... ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare almost the same as those for the \f5test\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAll unary operators are of the form
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BI \- letter
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand are followed by a single operand.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinInstead of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR \-o ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR [[ ... ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinuses
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B &&
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \(bv\(bv
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto indicate "and" and "or".
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinParentheses are used without quoting for grouping.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe right hand side of the string comparison operators
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B ==
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B !=
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintakes a pattern and tests whether the left hand operand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmatches this pattern. Quoting the pattern results
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis a string comparison rather than the pattern match.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe operators
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B <
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B >
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwithin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR [[ ... ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindesignate lexicographical comparison.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn addition there are several other new comparison primitives.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe binary operators
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-ot
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-nt
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincompare the modification times
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinof two files to see which file is
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I "older than"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I "newer than"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe other.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe binary operator
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-ef
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintests whether two files
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinhave the same device and i-node number,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chini.\ e., a link to the same file.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe unary operator
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-L
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinreturns true if its operand is a symbolic link.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe unary operator
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-O
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin(\fB\-G\fP)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinreturns true if the owner (or group) of the file operand matches
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthat of the caller.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe unary operator
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-o
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinreturns true when its operand is the name of an option that is
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincurrently on.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe following script illustrates some of the uses of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR [[ ... ]] .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe reference manual contains the complete list of operators.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.nf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in .5i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ta 4i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5for i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindo # execute foo for numeric directory
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if [[ \-d $i && $i == +([0\-9]) ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin then foo
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin # otherwise if writable or executable file and not mine
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin elif [[ (\-w $i\(bv\(bv\-x $i) && ! \-O $i ]]
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin then bar
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindone\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.fi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ta
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Input and Output"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP has
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextended I/O capabilities to enhance the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinuse of the shell as a programming language.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAs with the Bourne shell,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinyou use the I/O redirection operator,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR < ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto control where input comes from,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand the I/O redirection operator,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR > ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto control where output goes to.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinEach of these operators can be preceded with a single digit that
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinspecifies a file unit number to associate with the file stream.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOrdinarily you specify these I/O redirection operators with a specific
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand to which it applies.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinHowever, if you specify I/O redirections with the \f5exec\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand don't specify arguments to \f5exec\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen the I/O redirection applies to the current program.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor example, the command
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5exec\ <\ foobar\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinopens file \f5foobar\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfor reading.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe \f5exec\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand is also used to close files.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA file descriptor unit can be opened as a copy of an existing
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfile descriptor unit by using either of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B <&
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B >&
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoperators and putting the file descriptor unit of the original file
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinafter the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR & .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThus, \f52>&1\fP means open standard error (file descriptor 2)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas a copy of standard output (file descriptor 1).
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA file descriptor value of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinafter the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B &
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinindicates that the file should be closed.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinTo close file unit 5, specify
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5exec\ 5<&-\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThere are two additional redirection operators with \f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand the POSIX shell that are not part of the Bourne shell.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B >|
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoperator overrides the effect of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B noclobber
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption described earlier.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B <\^>
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoperator causes a file to be opened for both reading and writing.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP recognizes certain pathnames and treats them
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinspecially.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinPathnames of the form
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BI /dev/fd/ n\^
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare treated as equivalent to the file defined by file descriptor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IR n .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThese name can be used as the script argument to \f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand in conditional testing as described above.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOn underlying systems that support
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B /dev/fd
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinin the file system, these names can be passed to other commands.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinPathnames of the form
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BI /dev/tcp/ hostid / port
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BI /dev/udp/ hostid / port
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan be used to create
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B tcp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B udp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinconnections to services given by the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I hostid\^
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinnumber and
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I port\^
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinnumber.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I hostid\^
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincannot use symbolic values. In practice these
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinnumbers are typically generated by command substitution.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor example,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5exec\ 5>\ /dev/tcp/$(service\ name)\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwould open file descriptor 5 for sending messages
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto hostid and port number defined by the output of \f5service\ name\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe Bourne shell has a built-in command \f5read\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfor reading lines from standard input (file descriptor 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand splitting it into fields based on the value of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1IFS\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable, and a command \f5echo\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto write strings to standard output.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin(On some systems, \f5echo\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis not a built-in command and incurs considerable overhead to use.)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinUnfortunately, neither of these commands
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis able to perform some very basic tasks.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor example.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwith the Bourne shell,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe \f5read\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbuilt-in cannot read a single line that ends in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR \e .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWith \f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe \f5read\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbuilt-in has a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-r
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption to remove the special meaning for
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \e
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhich allows it to be
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintreated as a regular
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincharacter rather than the line continuation character.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWith the Bourne shell,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthere is no simple way to have more than one file open
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinat any time for reading.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP has options on the \f5read\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand to specify the file
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindescriptor for the input.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe fields that are read from a line can be stored into an indexed
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinarray with the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-A
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption to read.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis allows a line to be split into an arbitrary number of fields.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe way the Bourne shell uses the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1IFS\s+1\fP variable to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsplit lines into fields greatly limits its utility.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOften data files consist of lines that use a character such
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B :
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto delimit fields with two adjacent delimiters that denote
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china null field.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe Bourne shell treats adjacent delimiters as a single
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfield delimiter.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWith \f5ksh\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindelimiters that are considered white space characters
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinhave the behavior of the Bourne shell, but other
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinadjacent delimiters separate
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinnull fields.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe \f5read\fP command is often used in scripts that interact
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwith the user by prompting the user and then requesting some
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chininput.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWith the Bourne shell two commands are needed; one to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprompt the user, the other to read the reply.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP allows these two commands to be combined.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe first argument of the \f5read\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand can be followed by a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B ?
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand a prompt string which is used whenever the input
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindevice is a terminal.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBecause the prompt is associated with the \f5read\fP built-in,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe built-in command line editors will be able to re-output
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe prompt whenever the line needs to be refreshed when
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinreading from a terminal device.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWith the Bourne shell,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthere is no way to set a time limit for waiting for the user
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinresponse to read.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-t
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption to \f5read\fP takes a floating
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinpoint argument that gives the time in seconds,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor fractions of seconds that the shell should wait for a reply.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe version of the \f5echo\fP command in System V
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintreats certain sequences beginning with
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \e
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas control sequences.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis makes it hard to output strings without interpretation.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinMost BSD derived systems do not interpret
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \e
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincontrol sequences.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinUnfortunately, the BSD versions of \f5echo\fP accepts a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-n
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption to prevent a trailing new-line, but has no way to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincause the string
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-n
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto be printed.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinNeither of these versions is adequate. Also, because they
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare incompatible, it is very hard to write portable shell scripts
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinusing \f5echo\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe \f5ksh\fP built-in, \f5print\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoutputs characters to the terminal or to a file and
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsubsumes the functions of all versions of \f5echo\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOrdinarily, escape sequences in arguments beginning with
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \e
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare processed the same as for the System V \f5echo\fP command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinHowever \f5print\fP follows the standard conventions for
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoptions and has options that make \f5print\fP very versatile.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-r
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption can be used to output the arguments without any special meaning.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-n
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption can be used here to suppress the trailing new-line
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthat is ordinarily appended.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAs with \f5read\fP, it is possible to specify the file descriptor number
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas an option to the command to avoid having to use
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinredirection operators with each occurrence of the command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe IEEE POSIX shell and utilities standard committee was unable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto reconcile the differences between the System V and BSD
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinversions of \f5echo\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThey introduced a new command named \f5printf\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhich takes an ANSI-C format string and a list of options
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand outputs the strings using the ANSI-C formatting rules.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSince \f5ksh\fP is POSIX conforming, it accepts \f5printf\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinHowever, there is a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-f
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoptions to \f5print\fP that can be used to specify
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china format string which processes the arguments the same way that
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5printf\fP does.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe format processing for \f5print\fP and \f5printf\fP has
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbeen extended slightly.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThere are three additional formatting directives.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B %b
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinformat causes the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \e
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinescape sequences to be expanded as they are with the System V \f5echo\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B %q
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinformat causes quotes to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbe placed on the output as required
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinso that it can be used as shell input.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSpecial characters in the output of most \f5ksh\fP built-in commands
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand in the output from an execution trace
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare quoted in an equivalent fashion.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B %P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinformat causes an extended regular expression string to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbe converted into a shell pattern.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis is useful for writing shell applications that have
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto accept regular expressions as input.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFinally, the escape sequence
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \e\^E
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhich expands to the terminal escape character (octal 033)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinhas been added.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe shell is frequently used as a programming language for
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chininteractive dialogues.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5select\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatement has been added to the language
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto make it easier to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinpresent menu selection alternatives to the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinuser and evaluate the reply.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe list of alternatives is numbered and put in columns.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA user settable prompt,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1PS3\s+1\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis issued and if the answer is
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china number corresponding to one of the alternatives,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe select loop variable is set to this value.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn any case, the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1REPLY\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable is used to store the user entered reply.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe shell variables
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1LINES\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1COLUMNS\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare used to control the layout of select lists.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Option Parsing"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe \f5getopts\fP built-in command can be used
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto process command arguments in a manner consistent
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwith the way \f5ksh\fP does for its own built-in commands.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe \f5getopts\fP built-in allows users to specify options
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas separate arguments or to group options that do not
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintake arguments together. Options that require arguments
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindo not require space to separate them from the option argument.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1OPTARG\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable stores the value of the option argument
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinafter finding a variable that takes an argument.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1OPTIND\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable holds the index of the current options argument.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAfter processing options, the arguments should be
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinshifted by
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1OPTIND\s+1\-1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto make the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinremaining arguments be \f5"$@"\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe \f5getopts\fP argument description allows additional
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chininformation to be specified along with the options
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthat is used to generate \fIusage\fP messages for
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinincorrect arguments and for the option argument \fB\-?\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe example in the APPENDIX uses \f5getopts\fP to process
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinits arguments.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Co-process"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP can spawn a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I co-process
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinby adding a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B "|&"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinafter a command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis process will be run with its standard input and its
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstandard output connected to the shell. The built-in command \f5print\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwith the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-p
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption will write into the standard input of this
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprocess and
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe built-in command \f5read\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwith the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-p
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption will read from the output of this process.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn addition, the I/O redirection operators \fB<&\fP and \fB>&\fP can
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbe used to move the input or output pipe of the co-process
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto a numbered file descriptor.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinUse \f5exec\ 3>&\ p\fP to move the input of the co-process
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto file descriptor \fB3\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAfter you have connected to file descriptor \fB3\fP, you
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan direct the output of any command to the co-process
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinby running \fIcommand\fP\f5\ >&3\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAlso, by moving the input of the co-process to a numbered descriptor,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinit is possible to run a second co-process.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe output of both co-processes will be the file descriptor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinassociated with \f5read\ -p\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinYou can use \f5exec\ 4<&\ p\fP to cause the output of these
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinco-processes to go to file descriptor \fB4\fP of the shell.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOnce you have moved the pipe to descriptor \fB4\fP, it is possible
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto connect a server to the co-process by running \fIcommand\fP\f5\ 4<&\ p\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor to close the co-process pipe with \f5exec\ 4<&\ -\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Functions"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFunction definitions are of the form
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in +.5i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.nf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5function\fP \fIname\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.br
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.br
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin any shell script
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.br
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.fi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA function whose name contains a \fB\s+2.\s-2\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis called a \fIdiscipline\fP function.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe portion of the name after the last \fB\s+2.\s-2\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis the name of the discipline.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinDiscipline functions named \f5get\fP, \f5set\fP, and \f5unset\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan be assigned to any variable to intercept lookups,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinassignments and unsetting of the variable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindefined by the portion of the name before the last \fB\s+2.\s-2\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinApplications can create additional disciplines for variables
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthat are created as part of user defined built-ins.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe portion of the name before the last \fB\s+2.\s-2\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmust refer to the name of an existing variable.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThus, if \f5p\fP is a reference to \f5PATH\fP, then
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe function name \f5p.get\fP and \f5PATH.get\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinrefer to the same function.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe function is invoked either
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinby specifying
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I name
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas the command name
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand optionally following it with arguments
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor by using it as an option to the \fB\s+2.\s-2\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbuilt-in command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinPositional parameters are saved before each
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfunction call and restored when completed.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe arguments that follow the function name on the calling
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinline become positional parameters inside the function.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe \f5return\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbuilt-in can be used to cause the function to return to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe statement following
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe point of invocation.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFunctions can also be defined with the System V notation,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in +.5i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.nf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fIname\fP \f5()\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.br
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B {
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.br
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin any shell script
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.br
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.fi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFunctions defined with this syntax cannot be used as the first
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinargument to a \fB\s+2.\s-2\fP procedure.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP accepts this notation for compatibility only.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThere is no need to use this notation when writing
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP scripts.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFunctions defined with the \f5function\fP\ \fIname\fP syntax
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand invoked by name
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare executed in the current shell environment
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand can share named variables with the calling program.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOptions, other than execution trace
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR \-x ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinset by the calling program are
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinpassed down to a function.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe options are
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinnot shared with
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe function so that any options set within a function are
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinrestored when the function exits.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinTraps ignored by the caller are ignored within the function
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand cannot be enabled.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinTraps caught by the calling program are reset to their
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindefault action within the function.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn most instances, the default action is
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto cause the function to terminate.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA trap on
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1EXIT\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindefined within a function executes after the function
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincompletes but
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbefore the caller resumes.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinTherefore,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinany variable assignments and
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinany options set as part of a trap action will be effective
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinafter the caller resumes.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBy default, variables are inherited by the function and shared
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinby the calling program.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinHowever,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfor functions defined with the \f5function\fP\ \fIname\fP syntax
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthat are invoked by name,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinenvironment substitutions preceding the function call
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinapply only to the scope of the function call.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAlso, variables whose names do not contain a \fB\s+2.\s-2\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthat are defined with the \f5typeset\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbuilt-in command are local to the function that they are declared in.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThus, for the function defined
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.nf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in .5i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5function name
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin typeset -i x=10
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin let z=x+y
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin print $z
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.fi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ta
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chininvoked as
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5y=13\ name\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5x\fP and \f5y\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare local variables with respect to the function
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5name\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhile
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5z\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis global.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFunctions defined with the \fIname\fP\f5()\fP syntax,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand functions invoked as an argument to the \fB\s+2.\s-2\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinshare everything other than positional parameters with the caller.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAssignments that precede the call remain in effect after the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfunction completes.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAlias and function names are not passed down to shell scripts
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor carried across separate
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chininvocations of \f5ksh\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B $\s-1FPATH\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable gives a colon separated list of directories that
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis searched for function definitions when trying to resolve
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe command name.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhenever a file name contained in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B $\s-1FPATH\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis found, the complete file is read and all functions
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincontained within become defined.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinCalls that reference functions can be recursive.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinExcept for special built-ins,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfunction names take precedence over built-in names and names
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinof programs when used as command names.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinTo write a replacement function that invokes the command that
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinyou wish to replace,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinyou can use the \f5command\fP built-in command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe arguments to \f5command\fP are the name and arguments
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinof the program you want to execute.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor example to write a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B cd
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfunction which changes the directory and prints out the directory name,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinyou can write
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.nf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in .5i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5function cd
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin{
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if command cd "$@"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin then print -r -- $PWD
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin fi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin}\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.fi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ta
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1FPATH\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable is a colon separated list that \f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinuses to search for function definitions.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhen
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinencounters an autoload function,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinit runs the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand on the script containing the function,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand then executes the function.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor interactive shells,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfunction definitions may also be placed in the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1ENV\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfile.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinHowever, this
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincauses the shell to take longer to begin executing.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Process Substitution"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis feature is only available
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinon versions of the UNIX operating system which support the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B /dev/fd
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindirectory for naming open files.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinEach command argument of the form
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB<(\fP\fIlist\^\fP\fB)\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB>(\fP\fIlist\^\fP\fB)\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwill run process
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I list
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinasynchronously connected to some file in the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B /dev/fd
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindirectory.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe name of this file will become the argument to the command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf the form with
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B >
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis selected then writing on this file will provide input for
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IR list .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B <
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis used,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen the file passed as an argument will contain the output of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I list
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprocess.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor example,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.nf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in .5i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5paste <(cut \-f1 \fP\fIfile1\fP\f5) <(cut \-f2 \fP\fIfile2\fP\f5) | tee >(\fP\fIprocess1\fP\f5) >(\fP\fIprocess2\fP\f5)\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.fi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ta
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.sp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextracts
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfields 1 and 3 from
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe files
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I file1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I file2
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinrespectively,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinplaces the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinresults side by side, and
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsends it
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto the processes
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I process1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IR process2 ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas well as putting it onto the standard output.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinNote that the file which is passed as an argument to the command is
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china UNIX system
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IR pipe (2)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinso that the programs that expect to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IR lseek (2)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinon the file will not work.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Finding Commands"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe addition of aliases, functions,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand more built-ins
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinhas made it substantially more difficult to know what
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china given command name really means.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinCommands that begin with reserved words
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare an integral part of the shell language itself
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand typically define the control flow of the language.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSome control flow commands are not reserved words in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe language but are \fIspecial\fP built-ins.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSpecial built-ins are built-ins that are considered a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinpart of the language rather than user definable commands.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe best examples of commands that fit this description
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare \f5break\fP and \f5continue\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBecause they are not reserved words, they can be the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinresult of shell expansions and are not effected by quoting.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThese commands have the following special properties:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BL
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.LI
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAssignments that precede them apply to the current shell process,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinnot just to the given command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.LI
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAn error in the format of these commands cause a shell script
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor function that contains them to abort.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.LI
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThey cannot be overridden by shell functions.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.LE
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOther commands are built-in because they perform side effects
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinon the current environment that would be nearly impossible
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto implement otherwise.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBuilt-ins such as \f5cd\fP and \f5read\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare examples of such built-ins.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThese built-ins are semantically equivalent to commands that
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare not built-in except that they don't take a path search
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto locate.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA third reason to have a command built-in is so that
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinit will be unaffected by the setting of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1PATH\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe \f5print\fP command fits this category.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinScripts that use \f5print\fP will be portable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto all sites that run \f5ksh\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe final reason for having a command be a built-in is
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfor performance.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOn most systems it is more than an order of magnitude
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfaster to initiate a command that is built-in than
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto create a separate process to run the command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinExamples that fit this category are \f5test\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand \f5pwd\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinGiven a command name \f5ksh\fP decides what it means using
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe following order:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BL
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.LI
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinReserved words define commands that form part of the shell
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chingrammar.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThey cannot be quoted.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.LI
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAlias substitutions occur first as part of the reading of commands.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinUsing quotes in the command name will prevent alias substitutions.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.LI
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSpecial built-ins.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.LI
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFunctions.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.LI
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinCommands that are built-in that are not associated with a pathname
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsuch as \f5cd\fP and \f5print\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.LI
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf the command name contains a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR / ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe program or script corresponding to the given name is executed.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.LI
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA path search locates the pathname corresponding to the command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf the pathname where it is found matches the pathname associated
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwith a built-in command, the built-in command is executed.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf the directory where the command is found is listed in the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1FPATH\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable, the file is read into the shell
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinlike a dot script, and a function by that name is invoked.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOnce a pathname is found, \f5ksh\fP remembers its location
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand only checks relative directories in \fB\s-1PATH\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe next time the command name is used.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAssigning a value to \fB\s-1PATH\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincauses \f5ksh\fP to forget the location of all command names.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.LI
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1FPATH\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable is searched and files found are treated as described above.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.LE
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe first argument of the \f5command\fP built-in, described earlier,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinskips the checks for reserved words and for function definitions.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn all other ways, \f5command\fP behaves like a built-in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthat is not associated with a pathname.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAs a result, if the first argument of \f5command\fP is
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china special built-in, the special properties of this built-in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindo not apply.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor example, whereas, \f5exec\ 3<\ foo\fP will cause a script containing
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinit to abort if the open fails, \f5command\ exec\ 3<\ foo\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinresults in a non-zero exit status but does not abort the script.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinYou can get a complete list of the special built-in commands
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwith \f5builtin\ -s\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn addition \f5builtin\fP without arguments gives a list of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe current built-ins and the pathname that they are associated with.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA built-in can be bound to another pathname by giving
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe pathname for the built-in. The basename of this path must
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbe the name of an existing built-in for this to succeed.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSpecifying the name of the built-in without a pathname causes
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthis built-in to be found before a path search.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA built-in can be deleted with the \fB\-d\fP option.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOn systems with run time loading of libraries, built-in commands
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincan be added with the \f5builtin\fP command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinEach command that is to be built-in must be written as a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinC function whose name is of the form \f5b_\fP\fIname\fP, where
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fIname\fP is the name of the built-in that is to be added.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe function has the same argument calling convention as
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5main\fP. The lower eight bits of the return value become
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe exit status for this built-in.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBuiltins are added by specifying the pathname of the library
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas an argument to the \fB\-f\fP option of \f5builtin\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe built-in command,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5whence\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhen used with the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-v
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption, tells how a given command is bound.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA line is printed for each argument to \f5whence\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintelling what would happen if this argument were used as a command name.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIt reports on reserved words, aliases, built-ins, and
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfunctions.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf the command is none of the above,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinit follows the path search rules and prints the full path-name,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinif any, otherwise it prints an error message.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Symbolic Names"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinTo avoid implementation dependencies, \f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinaccepts and generates symbolic names
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfor built-ins that use numerical values in the Bourne shell.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-S
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5umask\fP built-in command
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinaccepts and displays
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindefault file creation permissions
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsymbolically.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIt uses the same symbolic notation as the \f5chmod\fP command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe \f5trap\fP and \f5kill\fP built-in commands
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinallows the signal names to be given symbolically.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe names of signals and traps
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincorresponding to signals are the same as the signal name with
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1SIG\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprefix removed.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe trap
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B 0
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis named
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1EXIT\s+1\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Additional Variables"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn addition to the variables discussed earlier, \f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinhas other variables that it handles specially.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe variable \fB\s-1RANDOM\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinproduces a random number in the range 0 to 32767 each time it is referenced.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAssignment to this variable sets the seed for the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinrandom number generator.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe parameter \fB\s-1PPID\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis used to generate the process id of the process which invoked this shell.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Added Traps"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA new trap named
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1ERR\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinhas been added.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis trap is invoked whenever the shell would exit if the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-e
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption were set.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis trap is used by
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFourth Generation Make\*(Rf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinG. S. Fowler,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I "The Fourth Generation Make,"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinProceedings of the Portland USENIX meeting, pp. 159-174, 1985.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhich runs \f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas a co-process.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA trap named
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1DEBUG\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chingets executed after each command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis trap can be used for debugging and other purposes.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1KEYBD\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintrap was described earlier.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 Debugging
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe primary method for debugging Bourne shell scripts is to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinuse the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-x
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption to enable the execution trace.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAfter all
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe expansions have been performed,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbut before each command is executed,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe trace writes to standard error the name and arguments
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinof each command preceded by a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR + .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhile the trace is very useful, there is no way
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto find out what line of source a given trace line
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincorresponds to.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWith
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1PS4\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis evaluated for parameter expansion and
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis displayed before each command,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chininstead of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR + .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1LINENO\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable is set to the current line number relative to the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbeginning of the current script or function.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIt is most useful as part of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1PS4\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprompt.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fB\s-1DEBUG\s+1\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintrap can be used to write a break point shell
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindebugger in \f5ksh\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAn example of such a debugger is \f5kshdb\fP.\*(Rf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBill Rosenblatt,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IR "Debugging Shell Scripts with \f5kshdb\fP" ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinUnix World, Volume X, No. 5, 1993.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 2 "Timing Commands"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFinding the time it takes to execute commands
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinhas been a serious problem with the Bourne shell.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSince the \f5time\fP command is not part of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinlanguage, it is necessary to write a script
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinin order to time a \f5for\fP or \f5while\fP loop.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe extra time in invoking the shell and processing
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe script is accumulated along with the time
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto execute the script.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinMore seriously, the Bourne shell does not give correct
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintimes for pipelines.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe reason for this is that the times for some members
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinof a pipeline are not counted when computing the time.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAs an extreme example,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinrunning \f5time\fP on the script
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ce
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5cat < /dev/null | sort -u bigfile | wc\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwith the Bourne shell will show very little
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinuser and system time no matter how
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinlarge \f5bigfile\fP is.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinTo correct these problems,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china reserved word \f5time\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinhas been added to replace
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe \f5time\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommand.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAny function, command or pipeline can be preceded by this reserved word
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto obtain information about the elapsed, user, and system times.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSince I/O redirections bind to the command, not to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5time\fP,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinparentheses should be used to redirect the timing information which
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis normally printed on file descriptor 2.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 1 SECURITY
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThere are several documented problems associated with the security of
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinshell procedures\*(Rf.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinF. T. Grampp and R. H. Morris,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I "UNIX Operating System Security,"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAT&T Bell Labs Tech. Journal, Vol. 63, No. 8, Part 2, pp. 1649-1671, 1984.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThese security holes occur primarily because a user can manipulate the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I environment
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto subvert the intent of a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I setuid
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinshell procedure.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSometimes, shell procedures are initiated from
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbinary programs, without the author's
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinawareness, by library routines which invoke shells to carry out
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintheir tasks.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhen the binary program is run
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I setuid
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen the shell procedure runs with the permissions afforded to the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinowner of the binary file.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn the Bourne shell,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1IFS\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinparameter is used to split each word into separate command arguments.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf a user knows that some
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I setuid
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprogram will run
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5sh\ -c\ /bin/pwd\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin(or any other command in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR /bin )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthen the user sets and exports
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.BR \s-1IFS\s+1=\^/ .
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinInstead of running
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B /bin/pwd
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe shell will run
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B bin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwith
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B pwd
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas an argument.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe user puts his or her own \f5bin\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprogram into the current directory.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis program can
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincreate a copy of the shell,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmake this shell
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IR setuid ,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand then run the \f5/bin/pwd\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprogram so that the original program continues to run successfully.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis kind of penetration is not possible with
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsince the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1IFS\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinparameter only splits arguments that result from command or parameter
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsubstitution.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSome
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I setuid
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprograms run programs using
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I system()
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwithout giving the full pathname.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinuser sets the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1PATH\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable so that the desired command will be found
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinin his or her local bin, then the same technique described above can
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbe employed to compromise the security of the system.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinTo close up this and other security holes,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinresets the effective user id to the real user id and the effective
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chingroup id to the real group id unless the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I privileged
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinoption
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RB ( \-p\^ )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis specified at invocation.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIn
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthis mode, the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B privileged
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmode, the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B .profile
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1ENV\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfiles are not processed.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinInstead, the file
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B /etc/suid_profile
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis read and executed.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis gives an administrator control over the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinenvironment to set the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \s-1PATH\s+1
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinvariable or to log setuid shell invocations.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinClearly security of the system is compromised if
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B /etc
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor this file is publicly writable.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSome versions of the UNIX operating system look for the characters
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5#!\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas the first two characters of an executable file.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf these characters are found, then the next word on this line is taken
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinas the interpreter to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chininvoke
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfor this command and the interpreter is
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IR exec ed
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwith the name of the script as argument zero and argument one.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIf the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I setuid
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I setgid
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbits are on for this file, then the interpreter
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinis run with the effective uid and/or gid set accordingly.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis scheme has three major drawbacks.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFirst of all,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinputting the pathname of the interpreter into the script
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmakes the script less portable since the interpreter
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmay be installed in a different directory on another system.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSecondly, using the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5#!\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinnotation forces an
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B exec
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinof the interpreter even when the call is invoked from the interpreter
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhich it must exec. This is inefficient since
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP can handle a failed exec much faster than starting up
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinagain.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinMore importantly,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I setuid
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I setgid
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprocedures provide an easy target for intrusion.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinBy linking a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I setuid
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinor
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I setgid
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprocedure to a name beginning with a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B \-
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe interpreter is fooled into thinking that it is being invoked with
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china command line option rather than the name of a file.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhen the interpreter is the shell, the user gets a privileged
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chininteractive shell.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThere is code in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto guard against this simple form of intrusion.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA more reliable way to handle
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I setuid
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I setgid
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprocedures is provided with
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe technique does not require any changes to the operating system
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand provides better security.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAnother advantage to this method is that it also allows scripts which
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinhave execute permission but no read permission to run. Taking away read
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinpermission makes scripts more secure.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe method relies on a setuid
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B root
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprogram to authenticate the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinrequest and exec the shell with the correct mode bits to carry out
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe task. This shell is invoked with the requested file already open
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfor reading. A script which cannot be opened for reading or which
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinhas its setuid and/or setgid bits turned on causes this setuid
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.B root
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprogram to get \fBexec\fPed.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinFor security reasons, this program is given the full
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinpathname
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5/etc/suid_exec\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinA description of the implementation of the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5/etc/suid_exec\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprogram can be found in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china separate paper\*(Rf.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinD. G Korn
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I "Parlez-vous Kanji?"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinTM-59554-860602-03, 1986.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 1 "CODE CHANGES"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP is written in ANSI-C as a reusable library.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe code can be compiled with C++ and older K&R C as well.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe code uses the IEEE POSIX 1003.1 and ISO 9945-1 standard\*(Rf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.I "POSIX \- Part 1: System Application Program Interface,"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIEEE Std 1003.1-1990, ISO/IEC 9945-1:1990.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwherever possible so that \f5ksh\fP should be able to run
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinon any POSIX compliant system. In addition, it is possible
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto compile \f5ksh\fP for older systems.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinUnlike earlier version of the Bourne shell,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP treats eight bit characters transparently
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwithout stripping off the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinleading bit.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThere is also a compile time switch to enable handling multi-byte
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand multi-width characters sets.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinOn systems with dynamic libraries, it is possible to add built-in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chincommands at run time with the built-in command \f5builtin\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chindescribed earlier.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIt is also possible to embed \f5ksh\fP in applications in
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968china manner analogous to \f5tcl\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 1 "EXAMPLE"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAn example of a \f5ksh\fP script is included
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinin the Appendix.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis one page program is a variant of the UNIX system
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5grep\fP(1) program.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinPattern matching for this version of \f5grep\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmeans shell patterns.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe first half uses the \f5getopts\fP command to
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfind the option flags.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinNearly all options have been implemented.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe second half goes through each line of each file
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto look for a pattern match.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThis program is not intended to serve as a
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinreplacement for \f5grep\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinwhich has been highly tuned for performance.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIt does
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinillustrate the programming power of \f5ksh\fP.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinNote that no auxiliary processes are spawned by this script.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIt was written and debugged in under two hours.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinWhile performance is acceptable for small files,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthis program runs at only one tenth
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthe speed of \f5grep\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinfor large files.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 1 "PERFORMANCE"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP executes many scripts faster than the System V Bourne shell;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinin some cases more than 10 times as fast.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe primary reason for this is that \f5ksh\fP creates fewer
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprocesses.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe time to execute a built-in command or a function is one or two
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinorders of magnitude faster than performing a \f5fork\fP() and
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5exec\fP() to create a separate process.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinCommand substitution and commands inside parentheses
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinare performed without creating another process, unless necessary
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto preserve correct behavior.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinAnother reason for improved performance is the use of the \fBsfio\fP\*(Rf,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinDavid Korn and Kiem-Phong Vo,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IR "SFIO - A Safe/Fast String/File I/O,"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinProceedings of the Summer Usenix,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinpp. 235-255, 1991.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinlibrary for I/O. The \fBsfio\fP library buffers all I/O
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand buffers are flushed only when required.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe algorithms used in \fBsfio\fP perform better than
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chintraditional versions of standard I/O so that programs that
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinspend most of their time
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinformatting output may actually perform better
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthan versions written in C.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSeveral of the internal algorithms have been changed
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinso that the number of subroutine calls has been
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinsubstantially reduced.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5ksh\fP uses variable sized hash tables for variables.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinScripts that rely heavily on referencing variables execute faster.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinMore processing is performed while reading the script
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinso that execution time is saved while running loops.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThese changes are not noticeable for scripts that \f5fork()\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand run processes,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbut they reduce the time that it takes to interpret commands by
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinmore than a factor of two.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinMost importantly, \f5ksh\fP provide mechanisms to write applications
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinthat do not require as many processes.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe arithmetic provided by the shell eliminates the need for the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5expr\fP command.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe pattern matching and substring capabilities eliminate the
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinneed to use \f5sed\fP or \f5awk\fP to process strings.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe architecture of \f5ksh\fP makes it easy to make commands
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinbuilt-ins without changing the semantics at all.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinSystems that have run-time binding of libraries allow
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinapplications to be sped up by supplying the critical
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinprograms as shell built-in commands.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinImplementations on other systems can add built-in commands
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinat compile time.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe procedure for writing built-in commands that can be loaded
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinat run time is in a separate document.\*(Rf,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RS
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinDavid Korn,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.IR "Guidelines for writing \f5ksh-93\fP built-in commands,"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinto be published, 1994.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.RF
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.H 1 "CONCLUSION"
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.P
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe 1988 version of \f5ksh\fP has tens of thousands of regular users
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand is a suitable replacement for the Bourne shell.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe 1993 version of \f5ksh\fP is essentially upward compatible with
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinboth the 1988 version of \f5ksh\fP and with the recent IEEE POSIX
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand ISO shell standard.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinThe 1993 version offers many advantages for programming applications,
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinand it has been rewritten so that it can be used in embedded applications.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinIt also offers improved performance.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.SG dgk \" signature typist initials
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\" .CS 14 24 38 0 0 16 \" cover sheet for TM
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.bp
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ce
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fIAPPENDIX\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.nf
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\f5
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.ta .66i 1.33i 2i 2.66i 3.33i 4i 4.66i 5.33i 6i 6.66i 7.33i 8i
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.so grep.mm
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin.fi
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin\fP
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin