14b88c807SRodney W. Grimes.\" Copyright (c) 1991, 1993 24b88c807SRodney W. Grimes.\" The Regents of the University of California. All rights reserved. 34b88c807SRodney W. Grimes.\" 44b88c807SRodney W. Grimes.\" This code is derived from software contributed to Berkeley by 54b88c807SRodney W. Grimes.\" Kenneth Almquist. 64b88c807SRodney W. Grimes.\" 74b88c807SRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without 84b88c807SRodney W. Grimes.\" modification, are permitted provided that the following conditions 94b88c807SRodney W. Grimes.\" are met: 104b88c807SRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright 114b88c807SRodney W. Grimes.\" notice, this list of conditions and the following disclaimer. 124b88c807SRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright 134b88c807SRodney W. Grimes.\" notice, this list of conditions and the following disclaimer in the 144b88c807SRodney W. Grimes.\" documentation and/or other materials provided with the distribution. 154b88c807SRodney W. Grimes.\" 3. All advertising materials mentioning features or use of this software 164b88c807SRodney W. Grimes.\" must display the following acknowledgement: 174b88c807SRodney W. Grimes.\" This product includes software developed by the University of 184b88c807SRodney W. Grimes.\" California, Berkeley and its contributors. 194b88c807SRodney W. Grimes.\" 4. Neither the name of the University nor the names of its contributors 204b88c807SRodney W. Grimes.\" may be used to endorse or promote products derived from this software 214b88c807SRodney W. Grimes.\" without specific prior written permission. 224b88c807SRodney W. Grimes.\" 234b88c807SRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 244b88c807SRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 254b88c807SRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 264b88c807SRodney W. Grimes.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 274b88c807SRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 284b88c807SRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 294b88c807SRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 304b88c807SRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 314b88c807SRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 324b88c807SRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 334b88c807SRodney W. Grimes.\" SUCH DAMAGE. 344b88c807SRodney W. Grimes.\" 35ab72124fSSteve Price.\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95 362a456239SPeter Wemm.\" $FreeBSD$ 374b88c807SRodney W. Grimes.\" 38ab72124fSSteve Price.Dd May 5, 1995 39ab72124fSSteve Price.Dt SH 1 40ab72124fSSteve Price.Os BSD 4 41ab72124fSSteve Price.Sh NAME 42ab72124fSSteve Price.Nm sh 43ab72124fSSteve Price.Nd command interpreter (shell) 44ab72124fSSteve Price.Sh SYNOPSIS 45ab72124fSSteve Price.Nm 462870f7a8SMartin Cracauer.Op Fl /+abCEefIimnpsTuVvx 47ab72124fSSteve Price.Op Fl /+o Ar longname 48c7a197e1SWolfram Schneider.Op Fl c Ar string 49ab72124fSSteve Price.Op Ar arg ... 50ab72124fSSteve Price.Sh DESCRIPTION 51e918fc8fSSheldon HearnThe 52e918fc8fSSheldon Hearn.Nm 53e918fc8fSSheldon Hearnutility is the standard command interpreter for the system. 54ab72124fSSteve PriceThe current version of 55ab72124fSSteve Price.Nm 56ab72124fSSteve Priceis in the process of being changed to 57ab72124fSSteve Priceconform with the 58ab72124fSSteve Price.St -p1003.2 59ab72124fSSteve Pricespecification for the shell. This version has many features which make 60ab72124fSSteve Priceit appear 614b88c807SRodney W. Grimessimilar in some respects to the Korn shell, but it is not a Korn 62150c3a33SSheldon Hearnshell clone like 63150c3a33SSheldon Hearn.Xr pdksh 1 . 6449e11e3aSSheldon HearnOnly features 654b88c807SRodney W. Grimesdesignated by POSIX, plus a few Berkeley extensions, are being 66ab72124fSSteve Priceincorporated into this shell. 6749e11e3aSSheldon HearnThis man page is not intended to be a tutorial nor a complete 684b88c807SRodney W. Grimesspecification of the shell. 69ab72124fSSteve Price.Ss Overview 704b88c807SRodney W. GrimesThe shell is a command that reads lines from 714b88c807SRodney W. Grimeseither a file or the terminal, interprets them, and 72e6d3cf26SSheldon Hearngenerally executes other commands. 7349e11e3aSSheldon HearnIt is the program that is started when a user logs into the system, 7449e11e3aSSheldon Hearnalthough a user can select a different shell with the 75c644db6aSSheldon Hearn.Xr chsh 1 7649e11e3aSSheldon Hearncommand. 774b88c807SRodney W. GrimesThe shell 784b88c807SRodney W. Grimesimplements a language that has flow control constructs, 794b88c807SRodney W. Grimesa macro facility that provides a variety of features in 804b88c807SRodney W. Grimesaddition to data storage, along with builtin history and line 814b88c807SRodney W. Grimesediting capabilities. It incorporates many features to 824b88c807SRodney W. Grimesaid interactive use and has the advantage that the interpretative 834b88c807SRodney W. Grimeslanguage is common to both interactive and non-interactive 844b88c807SRodney W. Grimesuse (shell scripts). That is, commands can be typed directly 8549e11e3aSSheldon Hearnto the running shell or can be put into a file, 8649e11e3aSSheldon Hearnwhich can be executed directly by the shell. 87ab72124fSSteve Price.Ss Invocation 8849e11e3aSSheldon Hearn.\" 8949e11e3aSSheldon Hearn.\" XXX This next sentence is incredibly confusing. 9049e11e3aSSheldon Hearn.\" 9149e11e3aSSheldon HearnIf no arguments are present and if the standard input of the shell 9249e11e3aSSheldon Hearnis connected to a terminal 9349e11e3aSSheldon Hearn(or if the 9449e11e3aSSheldon Hearn.Fl i 9549e11e3aSSheldon Hearnoption is set), 9649e11e3aSSheldon Hearnthe shell is considered an interactive shell. An interactive shell 974b88c807SRodney W. Grimesgenerally prompts before each command and handles programming 984b88c807SRodney W. Grimesand command errors differently (as described below). 994b88c807SRodney W. GrimesWhen first starting, the shell inspects argument 0, and 10049e11e3aSSheldon Hearnif it begins with a dash 10149e11e3aSSheldon Hearn.Pq Li - , 10249e11e3aSSheldon Hearnthe shell is also considered a login shell. 10349e11e3aSSheldon HearnThis is normally done automatically by the system 1044b88c807SRodney W. Grimeswhen the user first logs in. A login shell first reads commands 105ab72124fSSteve Pricefrom the files 106ab72124fSSteve Price.Pa /etc/profile 10749e11e3aSSheldon Hearnand then 108ab72124fSSteve Price.Pa .profile 109ab72124fSSteve Priceif they exist. If the environment variable 110ab72124fSSteve Price.Ev ENV 111ab72124fSSteve Priceis set on entry to a shell, or is set in the 112ab72124fSSteve Price.Pa .profile 11349e11e3aSSheldon Hearnof a login shell, the shell then reads commands from the file named in 114ab72124fSSteve Price.Ev ENV . 115ab72124fSSteve PriceTherefore, a user should place commands that are to be executed only 116ab72124fSSteve Priceat login time in the 117ab72124fSSteve Price.Pa .profile 118ab72124fSSteve Pricefile, and commands that are executed for every shell inside the 119ab72124fSSteve Price.Ev ENV 120e6d3cf26SSheldon Hearnfile. 12149e11e3aSSheldon HearnThe user can set the 122ab72124fSSteve Price.Ev ENV 12349e11e3aSSheldon Hearnvariable to some file by placing the following line in the file 124ab72124fSSteve Price.Pa .profile 12549e11e3aSSheldon Hearnin the home directory, 126ab72124fSSteve Pricesubstituting for 127ab72124fSSteve Price.Pa .shinit 12849e11e3aSSheldon Hearnthe filename desired: 12949e11e3aSSheldon Hearn.Pp 13049e11e3aSSheldon Hearn.Dl ENV=$HOME/.shinit; export ENV 13149e11e3aSSheldon Hearn.Pp 13249e11e3aSSheldon HearnThe first non-option argument specified on the command line 13349e11e3aSSheldon Hearnwill be treated as the 1344b88c807SRodney W. Grimesname of a file from which to read commands (a shell script), and 1354b88c807SRodney W. Grimesthe remaining arguments are set as the positional parameters 1364b88c807SRodney W. Grimesof the shell ($1, $2, etc). Otherwise, the shell reads commands 1374b88c807SRodney W. Grimesfrom its standard input. 138ea76be72SSteve Price.Pp 139ea76be72SSteve PriceUnlike older versions of 140ea76be72SSteve Price.Nm 141ea76be72SSteve Pricethe 142ea76be72SSteve Price.Ev ENV 143ea76be72SSteve Pricescript is only sourced on invocation of interactive shells. This 144ea76be72SSteve Pricecloses a well-known, and sometimes easily exploitable security 145ea76be72SSteve Pricehole related to poorly thought out 146ea76be72SSteve Price.Ev ENV 147ea76be72SSteve Pricescripts. 148ab72124fSSteve Price.Ss Argument List Processing 149ab72124fSSteve PriceAll of the single letter options to 150ab72124fSSteve Price.Nm 15149e11e3aSSheldon Hearnhave a corresponding long name, 15249e11e3aSSheldon Hearnwith the exception of 15349e11e3aSSheldon Hearn.Fl c 15449e11e3aSSheldon Hearnand 15549e11e3aSSheldon Hearn.Fl /+o . 15649e11e3aSSheldon HearnThese long names are provided next to the single letter options 15749e11e3aSSheldon Hearnin the descriptions below. 15849e11e3aSSheldon HearnThe long name for an option may be specified as an argument to the 15949e11e3aSSheldon Hearn.Fl /+o 16049e11e3aSSheldon Hearnoption of 16149e11e3aSSheldon Hearn.Xr sh 1 . 16249e11e3aSSheldon HearnOnce the shell is running, 16349e11e3aSSheldon Hearnthe long name for an option may be specified as an argument to the 16449e11e3aSSheldon Hearn.Fl /+o 16549e11e3aSSheldon Hearnoption of the 166c644db6aSSheldon Hearn.Ic set 16749e11e3aSSheldon Hearnbuiltin command 16849e11e3aSSheldon Hearn(described later in the section called 16949e11e3aSSheldon Hearn.Sx Builtin Commands ) . 17049e11e3aSSheldon HearnIntroducing an option with a dash 17149e11e3aSSheldon Hearn.Pq Li - 17249e11e3aSSheldon Hearnenables the option, 17349e11e3aSSheldon Hearnwhile using a plus 17449e11e3aSSheldon Hearn.Pq Li + 17549e11e3aSSheldon Hearndisables the option. 17649e11e3aSSheldon HearnA 17749e11e3aSSheldon Hearn.Dq Li -- 178fab26805SJoseph Koshyor plain 17949e11e3aSSheldon Hearn.Dq Li - 180fab26805SJoseph Koshywill stop option processing and will force the remaining 181fab26805SJoseph Koshywords on the command line to be treated as arguments. 18249e11e3aSSheldon HearnThe 18349e11e3aSSheldon Hearn.Fl /+o 18449e11e3aSSheldon Hearnand 18549e11e3aSSheldon Hearn.Fl c 18649e11e3aSSheldon Hearnoptions do not have long names. 18749e11e3aSSheldon HearnThey take arguments and are described after the single letter options. 188ab72124fSSteve Price.Bl -tag -width Ds 189ab72124fSSteve Price.It Fl a Li allexport 19049e11e3aSSheldon HearnFlag variables for export when assignments are made to them. 191ab72124fSSteve Price.It Fl b Li notify 192ab72124fSSteve PriceEnable asynchronous notification of background job 193ab72124fSSteve Pricecompletion. 194ab72124fSSteve Price.Pq UNIMPLEMENTED 195ab72124fSSteve Price.It Fl C Li noclobber 196c644db6aSSheldon HearnDo not overwrite existing files with 19749e11e3aSSheldon Hearn.Dq Li > . 198ab72124fSSteve Price.Pq UNIMPLEMENTED 199ab72124fSSteve Price.It Fl E Li emacs 200c644db6aSSheldon HearnEnable the builtin 201ab72124fSSteve Price.Xr emacs 1 20249e11e3aSSheldon Hearncommand line editor (disables the 203ab72124fSSteve Price.Fl V 20449e11e3aSSheldon Hearnoption if it has been set). 205ab72124fSSteve Price.It Fl e Li errexit 20649e11e3aSSheldon HearnExit immediately if any untested command fails in non-interactive mode. 2074b88c807SRodney W. GrimesThe exit status of a command is considered to be 2084b88c807SRodney W. Grimesexplicitly tested if the command is used to control 2094b88c807SRodney W. Grimesan if, elif, while, or until; or if the command is the left 210ab72124fSSteve Pricehand operand of an 21149e11e3aSSheldon Hearn.Dq Li && 212ab72124fSSteve Priceor 21349e11e3aSSheldon Hearn.Dq Li || 214ab72124fSSteve Priceoperator. 215ab72124fSSteve Price.It Fl f Li noglob 2164b88c807SRodney W. GrimesDisable pathname expansion. 217ab72124fSSteve Price.It Fl I Li ignoreeof 21849e11e3aSSheldon HearnIgnore 21949e11e3aSSheldon Hearn.Dv EOF Ns ' Ns s 22049e11e3aSSheldon Hearnfrom input when in interactive mode. 221ab72124fSSteve Price.It Fl i Li interactive 222ab72124fSSteve PriceForce the shell to behave interactively. 223ab72124fSSteve Price.It Fl m Li monitor 224ab72124fSSteve PriceTurn on job control (set automatically when interactive). 225ab72124fSSteve Price.It Fl n Li noexec 2264b88c807SRodney W. GrimesIf not interactive, read commands but do not 2274b88c807SRodney W. Grimesexecute them. This is useful for checking the 2284b88c807SRodney W. Grimessyntax of shell scripts. 229ab72124fSSteve Price.It Fl p Li privileged 230621a31c6SSteve PriceTurn on privileged mode. This mode is enabled on startup 231621a31c6SSteve Priceif either the effective user or group id is not equal to the 232621a31c6SSteve Pricereal user or group id. Turning this mode off sets the 233621a31c6SSteve Priceeffective user and group ids to the real user and group ids. 23449e11e3aSSheldon HearnWhen this mode is enabled for interactive shells, the file 235ab72124fSSteve Price.Pa /etc/suid_profile 23649e11e3aSSheldon Hearnis sourced instead of 23749e11e3aSSheldon Hearn.Pa ~/.profile 238ab72124fSSteve Priceafter 239ab72124fSSteve Price.Pa /etc/profile 24049e11e3aSSheldon Hearnis sourced, and the contents of the 241ab72124fSSteve Price.Ev ENV 24249e11e3aSSheldon Hearnvariable are ignored. 243ab72124fSSteve Price.It Fl s Li stdin 2444b88c807SRodney W. GrimesRead commands from standard input (set automatically 2454b88c807SRodney W. Grimesif no file arguments are present). This option has 2464b88c807SRodney W. Grimesno effect when set after the shell has already started 2476b15476eSSheldon Hearnrunning (i.e. when set with the 248c644db6aSSheldon Hearn.Ic set 2496b15476eSSheldon Hearncommand). 250c1c72a3cSMartin Cracauer.It Fl T Li asynctraps 251e6d3cf26SSheldon HearnWhen waiting for a child, execute traps immediately. 252e6d3cf26SSheldon HearnIf this option is not set, 253e6d3cf26SSheldon Hearntraps are executed after the child exits, 254e6d3cf26SSheldon Hearnas specified in 255c1c72a3cSMartin Cracauer.St -p1003.2 2566b15476eSSheldon HearnThis nonstandard option is useful for putting guarding shells around 2576b15476eSSheldon Hearnchildren that block signals. The surrounding shell may kill the child 2586b15476eSSheldon Hearnor it may just return control to the tty and leave the child alone, 2596b15476eSSheldon Hearnlike this: 260c1c72a3cSMartin Cracauer.Bd -literal -offset indent 261c1c72a3cSMartin Cracauersh -T -c "trap 'exit 1' 2 ; some-blocking-program" 262c1c72a3cSMartin Cracauer.Ed 263c1c72a3cSMartin Cracauer.Pp 264ab72124fSSteve Price.It Fl u Li nounset 265ab72124fSSteve PriceWrite a message to standard error when attempting 266ab72124fSSteve Priceto expand a variable that is not set, and if the 267ab72124fSSteve Priceshell is not interactive, exit immediately. 268ab72124fSSteve Price.Pq UNIMPLEMENTED 269ab72124fSSteve Price.It Fl V Li vi 270c644db6aSSheldon HearnEnable the builtin 271ab72124fSSteve Price.Xr vi 1 272ab72124fSSteve Pricecommand line editor (disables 273ab72124fSSteve Price.Fl E 274ab72124fSSteve Priceif it has been set). 275ab72124fSSteve Price.It Fl v Li verbose 276ab72124fSSteve PriceThe shell writes its input to standard error 277ab72124fSSteve Priceas it is read. Useful for debugging. 278ab72124fSSteve Price.It Fl x Li xtrace 27949e11e3aSSheldon HearnWrite each command 28049e11e3aSSheldon Hearn(preceded by 28149e11e3aSSheldon Hearn.Dq Li +\ ) 28249e11e3aSSheldon Hearnto standard error before it is executed. 28349e11e3aSSheldon HearnUseful for debugging. 28449e11e3aSSheldon Hearn.El 28549e11e3aSSheldon Hearn.Pp 28649e11e3aSSheldon HearnThe 28749e11e3aSSheldon Hearn.Fl c 28849e11e3aSSheldon Hearnoption may be used to pass its string argument to the shell 28949e11e3aSSheldon Hearnto be interpreted as input. 290c7a197e1SWolfram SchneiderKeep in mind that this option only accepts a single string as its 291c7a197e1SWolfram Schneiderargument, hence multi-word strings must be quoted. 29249e11e3aSSheldon Hearn.Pp 29349e11e3aSSheldon HearnThe 29449e11e3aSSheldon Hearn.Fl /+o 29549e11e3aSSheldon Hearnoption takes as its only argument the long name of an option 29649e11e3aSSheldon Hearnto be enabled or disabled. 29749e11e3aSSheldon HearnFor example, the following two invocations of 29849e11e3aSSheldon Hearn.Nm 29949e11e3aSSheldon Hearnboth enable the builtin 30049e11e3aSSheldon Hearn.Xr emacs 1 30149e11e3aSSheldon Hearncommand line editor: 30249e11e3aSSheldon Hearn.Bd -literal -offset indent 30349e11e3aSSheldon Hearnset -E 30449e11e3aSSheldon Hearnset -o emacs 30549e11e3aSSheldon Hearn.Ed 306ab72124fSSteve Price.Ss Lexical Structure 3074b88c807SRodney W. GrimesThe shell reads input in terms of lines from a file and breaks 3084b88c807SRodney W. Grimesit up into words at whitespace (blanks and tabs), and at 3094b88c807SRodney W. Grimescertain sequences of 3106b15476eSSheldon Hearncharacters called 3116b15476eSSheldon Hearn.Dq operators , 3126b15476eSSheldon Hearnwhich are special to the shell. 3134b88c807SRodney W. GrimesThere are two types of operators: control operators and 3144b88c807SRodney W. Grimesredirection operators (their meaning is discussed later). 315ab72124fSSteve PriceThe following is a list of valid operators: 316ab72124fSSteve Price.Bl -tag -width Ds 31749e11e3aSSheldon Hearn.It Control operators: 31849e11e3aSSheldon Hearn.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact 31949e11e3aSSheldon Hearn.It Xo 32049e11e3aSSheldon Hearn.Li & Ta Xo 32149e11e3aSSheldon Hearn.Li && Ta Xo 32249e11e3aSSheldon Hearn.Li ( Ta Xo 32349e11e3aSSheldon Hearn.Li ) Ta Xo 32449e11e3aSSheldon Hearn.Li \en 32549e11e3aSSheldon Hearn.Xc Xc Xc Xc Xc 32649e11e3aSSheldon Hearn.It Xo 32749e11e3aSSheldon Hearn.Li ;; Ta Xo 32849e11e3aSSheldon Hearn.Li ; Ta Xo 32949e11e3aSSheldon Hearn.Li | Ta Xo 33049e11e3aSSheldon Hearn.Li || 33149e11e3aSSheldon Hearn.Xc Xc Xc Xc 33249e11e3aSSheldon Hearn.El 33349e11e3aSSheldon Hearn.It Redirection operators: 33449e11e3aSSheldon Hearn.Bl -column "XXX" "XXX" "XXX" "XXX" -offset center -compact 33549e11e3aSSheldon Hearn.It Xo 33649e11e3aSSheldon Hearn.Li < Ta Xo 33749e11e3aSSheldon Hearn.Li > Ta Xo 33849e11e3aSSheldon Hearn.Li << Ta Xo 33949e11e3aSSheldon Hearn.Li >> 34049e11e3aSSheldon Hearn.Xc Xc Xc Xc 34149e11e3aSSheldon Hearn.It Xo 34249e11e3aSSheldon Hearn.Li <& Ta Xo 34349e11e3aSSheldon Hearn.Li >& Ta Xo 34449e11e3aSSheldon Hearn.Li <<- Ta Xo 34549e11e3aSSheldon Hearn.Li >| 34649e11e3aSSheldon Hearn.Xc Xc Xc Xc 34749e11e3aSSheldon Hearn.El 348ab72124fSSteve Price.El 349ab72124fSSteve Price.Ss Quoting 3504b88c807SRodney W. GrimesQuoting is used to remove the special meaning of certain characters 3514b88c807SRodney W. Grimesor words to the shell, such as operators, whitespace, or 3524b88c807SRodney W. Grimeskeywords. There are three types of quoting: matched single quotes, 3534b88c807SRodney W. Grimesmatched double quotes, and backslash. 354ab72124fSSteve Price.Bl -tag -width Ds 355ab72124fSSteve Price.It Single Quotes 3564b88c807SRodney W. GrimesEnclosing characters in single quotes preserves the literal 357ab0a2172SSteve Pricemeaning of all the characters (except single quotes, making 358ab0a2172SSteve Priceit impossible to put single-quotes in a single-quoted string). 359ab72124fSSteve Price.It Double Quotes 3604b88c807SRodney W. GrimesEnclosing characters within double quotes preserves the literal 36149e11e3aSSheldon Hearnmeaning of all characters except dollarsign 36249e11e3aSSheldon Hearn.Pq Li $ , 36349e11e3aSSheldon Hearnbackquote 36449e11e3aSSheldon Hearn.Pq Li ` , 36549e11e3aSSheldon Hearnand backslash 36649e11e3aSSheldon Hearn.Po Li \e\" 36749e11e3aSSheldon Hearn.Pc . 36849e11e3aSSheldon HearnThe backslash inside double quotes is historically weird. 36949e11e3aSSheldon HearnIt remains literal unless it precedes the following characters, 37049e11e3aSSheldon Hearnwhich it serves to quote: 37149e11e3aSSheldon Hearn.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact 37249e11e3aSSheldon Hearn.It Xo 37349e11e3aSSheldon Hearn.Li $ Ta Xo 37449e11e3aSSheldon Hearn.Li ` Ta Xo 37549e11e3aSSheldon Hearn.Li \&" Ta Xo 37649e11e3aSSheldon Hearn.Li \e\ Ta Xo 37749e11e3aSSheldon Hearn.Li \en 37849e11e3aSSheldon Hearn.Xc Xc Xc Xc Xc 37949e11e3aSSheldon Hearn.El 380ab72124fSSteve Price.It Backslash 381ab72124fSSteve PriceA backslash preserves the literal meaning of the following 38249e11e3aSSheldon Hearncharacter, with the exception of the newline character 38349e11e3aSSheldon Hearn.Pq Li \en . 38449e11e3aSSheldon HearnA backslash preceding a newline is treated as a line continuation. 385ab72124fSSteve Price.El 386ab72124fSSteve Price.Ss Reserved Words 3874b88c807SRodney W. GrimesReserved words are words that have special meaning to the 3884b88c807SRodney W. Grimesshell and are recognized at the beginning of a line and 3894b88c807SRodney W. Grimesafter a control operator. The following are reserved words: 39049e11e3aSSheldon Hearn.Bl -column "doneXX" "elifXX" "elseXX" "untilXX" "whileX" -offset center 39149e11e3aSSheldon Hearn.It Xo 39249e11e3aSSheldon Hearn.Li ! Ta Xo 39349e11e3aSSheldon Hearn.Li { Ta Xo 39449e11e3aSSheldon Hearn.Li } Ta Xo 39549e11e3aSSheldon Hearn.Ic case Ta Xo 39649e11e3aSSheldon Hearn.Ic do 39749e11e3aSSheldon Hearn.Xc Xc Xc Xc Xc 39849e11e3aSSheldon Hearn.It Xo 39949e11e3aSSheldon Hearn.Ic done Ta Xo 40049e11e3aSSheldon Hearn.Ic elif Ta Xo 40149e11e3aSSheldon Hearn.Ic else Ta Xo 40249e11e3aSSheldon Hearn.Ic esac Ta Xo 40349e11e3aSSheldon Hearn.Ic fi 40449e11e3aSSheldon Hearn.Xc Xc Xc Xc Xc 40549e11e3aSSheldon Hearn.It Xo 40649e11e3aSSheldon Hearn.Ic for Ta Xo 40749e11e3aSSheldon Hearn.Ic if Ta Xo 40849e11e3aSSheldon Hearn.Ic then Ta Xo 40949e11e3aSSheldon Hearn.Ic until Ta Xo 41049e11e3aSSheldon Hearn.Ic while 41149e11e3aSSheldon Hearn.Xc Xc Xc Xc Xc 41249e11e3aSSheldon Hearn.El 413ab72124fSSteve Price.Ss Aliases 414ab72124fSSteve PriceAn alias is a name and corresponding value set using the 415c644db6aSSheldon Hearn.Ic alias 4164b88c807SRodney W. Grimesbuiltin command. Whenever a reserved word may occur (see above), 4174b88c807SRodney W. Grimesand after checking for reserved words, the shell 418e6d3cf26SSheldon Hearnchecks the word to see if it matches an alias. 419e6d3cf26SSheldon HearnIf it does, it replaces it in the input stream with its value. 420e6d3cf26SSheldon HearnFor example, if there is an alias called 42149e11e3aSSheldon Hearn.Dq Li lf 4226b15476eSSheldon Hearnwith the value 42349e11e3aSSheldon Hearn.Dq Li ls -F , 4244b88c807SRodney W. Grimesthen the input 425ab72124fSSteve Price.Bd -literal -offset indent 42649e11e3aSSheldon Hearnlf foobar 427ab72124fSSteve Price.Ed 428ab72124fSSteve Price.Pp 4294b88c807SRodney W. Grimeswould become 430ab72124fSSteve Price.Bd -literal -offset indent 43149e11e3aSSheldon Hearnls -F foobar 432ab72124fSSteve Price.Ed 433ab72124fSSteve Price.Pp 4344b88c807SRodney W. GrimesAliases provide a convenient way for naive users to 4354b88c807SRodney W. Grimescreate shorthands for commands without having to learn how 4364b88c807SRodney W. Grimesto create functions with arguments. They can also be 4374b88c807SRodney W. Grimesused to create lexically obscure code. This use is discouraged. 438ab72124fSSteve Price.Ss Commands 4394b88c807SRodney W. GrimesThe shell interprets the words it reads according to a 4404b88c807SRodney W. Grimeslanguage, the specification of which is outside the scope 441ab72124fSSteve Priceof this man page (refer to the BNF in the 442ab72124fSSteve Price.St -p1003.2 4434b88c807SRodney W. Grimesdocument). Essentially though, a line is read and if 4444b88c807SRodney W. Grimesthe first word of the line (or after a control operator) 4454b88c807SRodney W. Grimesis not a reserved word, then the shell has recognized a 4464b88c807SRodney W. Grimessimple command. Otherwise, a complex command or some 4474b88c807SRodney W. Grimesother special construct may have been recognized. 448ab72124fSSteve Price.Ss Simple Commands 4494b88c807SRodney W. GrimesIf a simple command has been recognized, the shell performs 4504b88c807SRodney W. Grimesthe following actions: 451ab72124fSSteve Price.Bl -enum 452ab72124fSSteve Price.It 4536b15476eSSheldon HearnLeading words of the form 45449e11e3aSSheldon Hearn.Dq Li name=value 4556b15476eSSheldon Hearnare stripped off and assigned to the environment of 4564b88c807SRodney W. Grimesthe simple command. Redirection operators and 4574b88c807SRodney W. Grimestheir arguments (as described below) are stripped 4584b88c807SRodney W. Grimesoff and saved for processing. 459ab72124fSSteve Price.It 460ab72124fSSteve PriceThe remaining words are expanded as described in 4616b15476eSSheldon Hearnthe section called 4626b15476eSSheldon Hearn.Sx Word Expansions , 4636b15476eSSheldon Hearnand the first remaining word is considered the command 4644b88c807SRodney W. Grimesname and the command is located. The remaining 4654b88c807SRodney W. Grimeswords are considered the arguments of the command. 4666b15476eSSheldon HearnIf no command name resulted, then the 46749e11e3aSSheldon Hearn.Dq Li name=value 4684b88c807SRodney W. Grimesvariable assignments recognized in 1) affect the 4694b88c807SRodney W. Grimescurrent shell. 470ab72124fSSteve Price.It 471ab72124fSSteve PriceRedirections are performed as described in 4724b88c807SRodney W. Grimesthe next section. 473ab72124fSSteve Price.El 474ab72124fSSteve Price.Ss Redirections 4754b88c807SRodney W. GrimesRedirections are used to change where a command reads its input 4764b88c807SRodney W. Grimesor sends its output. In general, redirections open, close, or 4774b88c807SRodney W. Grimesduplicate an existing reference to a file. The overall format 4784b88c807SRodney W. Grimesused for redirection is: 47949e11e3aSSheldon Hearn.Pp 480ab72124fSSteve Price.Dl [n] redir-op file 48149e11e3aSSheldon Hearn.Pp 48249e11e3aSSheldon HearnThe 48349e11e3aSSheldon Hearn.Ql redir-op 48449e11e3aSSheldon Hearnis one of the redirection operators mentioned 485ab72124fSSteve Pricepreviously. The following gives some examples of how these 48649e11e3aSSheldon Hearnoperators can be used. 48749e11e3aSSheldon HearnNote that stdin and stdout are commonly used abbreviations 48849e11e3aSSheldon Hearnfor standard input and standard output respectively. 48949e11e3aSSheldon Hearn.Bl -tag -width "1234567890XX" -offset indent 49049e11e3aSSheldon Hearn.It Li [n]> file 49149e11e3aSSheldon Hearnredirect stdout (or file descriptor n) to file 49249e11e3aSSheldon Hearn.It Li [n]>| file 49349e11e3aSSheldon Hearnsame as above, but override the 49449e11e3aSSheldon Hearn.Fl C 49549e11e3aSSheldon Hearnoption 49649e11e3aSSheldon Hearn.It Li [n]>> file 49749e11e3aSSheldon Hearnappend stdout (or file descriptor n) to file 49849e11e3aSSheldon Hearn.It Li [n]< file 49949e11e3aSSheldon Hearnredirect stdin (or file descriptor n) from file 50049e11e3aSSheldon Hearn.It Li [n1]<&n2 50149e11e3aSSheldon Hearnduplicate stdin (or file descriptor n1) from file descriptor n2 50249e11e3aSSheldon Hearn.It Li [n]<&- 50349e11e3aSSheldon Hearnclose stdin (or file descriptor n) 50449e11e3aSSheldon Hearn.It Li [n1]>&n2 50549e11e3aSSheldon Hearnduplicate stdout (or file descriptor n1) to file descriptor n2 50649e11e3aSSheldon Hearn.It Li [n]>&- 50749e11e3aSSheldon Hearnclose stdout (or file descriptor n) 508ab72124fSSteve Price.El 509ab72124fSSteve Price.Pp 5106b15476eSSheldon HearnThe following redirection is often called a 5116b15476eSSheldon Hearn.Dq here-document . 512ab72124fSSteve Price.Bd -literal -offset indent 5134b88c807SRodney W. Grimes[n]<< delimiter 51449e11e3aSSheldon Hearn here-doc-text 51549e11e3aSSheldon Hearn ... 5164b88c807SRodney W. Grimesdelimiter 517ab72124fSSteve Price.Ed 518ab72124fSSteve Price.Pp 5194b88c807SRodney W. GrimesAll the text on successive lines up to the delimiter is 5204b88c807SRodney W. Grimessaved away and made available to the command on standard 5214b88c807SRodney W. Grimesinput, or file descriptor n if it is specified. If the delimiter 5224b88c807SRodney W. Grimesas specified on the initial line is quoted, then the here-doc-text 5234b88c807SRodney W. Grimesis treated literally, otherwise the text is subjected to 5244b88c807SRodney W. Grimesparameter expansion, command substitution, and arithmetic 5256b15476eSSheldon Hearnexpansion (as described in the section on 5266b15476eSSheldon Hearn.Sx Word Expansions ) . 5276b15476eSSheldon HearnIf the operator is 52849e11e3aSSheldon Hearn.Dq Li <<- 5296b15476eSSheldon Hearninstead of 53049e11e3aSSheldon Hearn.Dq Li << , 5316b15476eSSheldon Hearnthen leading tabs 5324b88c807SRodney W. Grimesin the here-doc-text are stripped. 533ab72124fSSteve Price.Ss Search and Execution 534ab0a2172SSteve PriceThere are three types of commands: shell functions, 53549e11e3aSSheldon Hearnbuiltin commands, and normal programs. 53649e11e3aSSheldon HearnThe command is searched for (by name) in that order. 53749e11e3aSSheldon HearnThe three types of commands are all executed in a different way. 538ab72124fSSteve Price.Pp 539ab0a2172SSteve PriceWhen a shell function is executed, all of the shell positional 540ab0a2172SSteve Priceparameters (except $0, which remains unchanged) are 5414b88c807SRodney W. Grimesset to the arguments of the shell function. 5424b88c807SRodney W. GrimesThe variables which are explicitly placed in the environment of 5434b88c807SRodney W. Grimesthe command (by placing assignments to them before the 5444b88c807SRodney W. Grimesfunction name) are made local to the function and are set 545e6d3cf26SSheldon Hearnto the values given. 546e6d3cf26SSheldon HearnThen the command given in the function definition is executed. 547e6d3cf26SSheldon HearnThe positional parameters are restored to their original values 548e6d3cf26SSheldon Hearnwhen the command completes. 549ab0a2172SSteve PriceThis all occurs within the current shell. 550ab72124fSSteve Price.Pp 551c644db6aSSheldon HearnShell builtin commands are executed internally to the shell, without 552ab0a2172SSteve Pricespawning a new process. 553ab72124fSSteve Price.Pp 554c644db6aSSheldon HearnOtherwise, if the command name does not match a function 555c644db6aSSheldon Hearnor builtin command, the command is searched for as a normal 5564b88c807SRodney W. Grimesprogram in the filesystem (as described in the next section). 5574b88c807SRodney W. GrimesWhen a normal program is executed, the shell runs the program, 558e6d3cf26SSheldon Hearnpassing the arguments and the environment to the program. 559e6d3cf26SSheldon HearnIf the program is not a normal executable file 5606b15476eSSheldon Hearn(i.e. if it does not begin with the 5616b15476eSSheldon Hearn.Qq magic number 5626b15476eSSheldon Hearnwhose 563ab72124fSSteve Price.Tn ASCII 5646b15476eSSheldon Hearnrepresentation is 5656b15476eSSheldon Hearn.Qq #! , 56649e11e3aSSheldon Hearnresulting in an 56749e11e3aSSheldon Hearn.Dv ENOEXEC 56849e11e3aSSheldon Hearnreturn value from 56949e11e3aSSheldon Hearn.Xr execve 2 ) 570e6d3cf26SSheldon Hearnthe shell will interpret the program in a subshell. 571e6d3cf26SSheldon HearnThe child shell will reinitialize itself in this case, 572e6d3cf26SSheldon Hearnso that the effect will be 573e6d3cf26SSheldon Hearnas if a new shell had been invoked to handle the ad-hoc shell script, 574e6d3cf26SSheldon Hearnexcept that the location of hashed commands located in 5754b88c807SRodney W. Grimesthe parent shell will be remembered by the child. 576ab72124fSSteve Price.Pp 577ab0a2172SSteve PriceNote that previous versions of this document 578ab0a2172SSteve Priceand the source code itself misleadingly and sporadically 579ab0a2172SSteve Pricerefer to a shell script without a magic number 5806b15476eSSheldon Hearnas a 5816b15476eSSheldon Hearn.Qq shell procedure . 582ab72124fSSteve Price.Ss Path Search 5834b88c807SRodney W. GrimesWhen locating a command, the shell first looks to see if 5844b88c807SRodney W. Grimesit has a shell function by that name. Then it looks for a 585ab0a2172SSteve Pricebuiltin command by that name. If a builtin command is not found, 586ab0a2172SSteve Priceone of two things happen: 587ab72124fSSteve Price.Bl -enum 588ab72124fSSteve Price.It 589ab72124fSSteve PriceCommand names containing a slash are simply executed without 590ab0a2172SSteve Priceperforming any searches. 591ab72124fSSteve Price.It 592ab72124fSSteve PriceThe shell searches each entry in 593ab72124fSSteve Price.Ev PATH 594ab72124fSSteve Pricein turn for the command. The value of the 595ab72124fSSteve Price.Ev PATH 596ab72124fSSteve Pricevariable should be a series of 5974b88c807SRodney W. Grimesentries separated by colons. Each entry consists of a 5984b88c807SRodney W. Grimesdirectory name. 5994b88c807SRodney W. GrimesThe current directory 600ab0a2172SSteve Pricemay be indicated implicitly by an empty directory name, 601ab0a2172SSteve Priceor explicitly by a single period. 602ab72124fSSteve Price.El 603ab72124fSSteve Price.Ss Command Exit Status 6044b88c807SRodney W. GrimesEach command has an exit status that can influence the behavior 6054b88c807SRodney W. Grimesof other shell commands. The paradigm is that a command exits 6064b88c807SRodney W. Grimeswith zero for normal or success, and non-zero for failure, 6074b88c807SRodney W. Grimeserror, or a false indication. The man page for each command 6084b88c807SRodney W. Grimesshould indicate the various exit codes and what they mean. 6094b88c807SRodney W. GrimesAdditionally, the builtin commands return exit codes, as does 610ab0a2172SSteve Pricean executed shell function. 611c351e089SJohn Polstra.Pp 612c351e089SJohn PolstraIf a command is terminated by a signal, its exit status is 128 plus 613c351e089SJohn Polstrathe signal number. Signal numbers are defined in the header file 614c351e089SJohn Polstra.Aq Pa sys/signal.h . 615ab72124fSSteve Price.Ss Complex Commands 6164b88c807SRodney W. GrimesComplex commands are combinations of simple commands 6174b88c807SRodney W. Grimeswith control operators or reserved words, together creating a larger complex 6184b88c807SRodney W. Grimescommand. More generally, a command is one of the following: 619ab72124fSSteve Price.Bl -item -offset indent 620ab72124fSSteve Price.It 621ab72124fSSteve Pricesimple command 622ab72124fSSteve Price.It 623ab72124fSSteve Pricepipeline 624ab72124fSSteve Price.It 625ab72124fSSteve Pricelist or compound-list 626ab72124fSSteve Price.It 627ab72124fSSteve Pricecompound command 628ab72124fSSteve Price.It 629ab72124fSSteve Pricefunction definition 630ab72124fSSteve Price.El 631ab72124fSSteve Price.Pp 6324b88c807SRodney W. GrimesUnless otherwise stated, the exit status of a command is 6334b88c807SRodney W. Grimesthat of the last simple command executed by the command. 634ab72124fSSteve Price.Ss Pipelines 6354b88c807SRodney W. GrimesA pipeline is a sequence of one or more commands separated 6364b88c807SRodney W. Grimesby the control operator |. The standard output of all but 6374b88c807SRodney W. Grimesthe last command is connected to the standard input 638ab0a2172SSteve Priceof the next command. The standard output of the last 639ab0a2172SSteve Pricecommand is inherited from the shell, as usual. 640ab72124fSSteve Price.Pp 6414b88c807SRodney W. GrimesThe format for a pipeline is: 64249e11e3aSSheldon Hearn.Pp 64349e11e3aSSheldon Hearn.Dl [!] command1 [ | command2 ...] 644ab72124fSSteve Price.Pp 6454b88c807SRodney W. GrimesThe standard output of command1 is connected to the standard 6464b88c807SRodney W. Grimesinput of command2. The standard input, standard output, or 6474b88c807SRodney W. Grimesboth of a command is considered to be assigned by the 6484b88c807SRodney W. Grimespipeline before any redirection specified by redirection 6494b88c807SRodney W. Grimesoperators that are part of the command. 650ab72124fSSteve Price.Pp 6514b88c807SRodney W. GrimesIf the pipeline is not in the background (discussed later), 6524b88c807SRodney W. Grimesthe shell waits for all commands to complete. 653ab72124fSSteve Price.Pp 6544b88c807SRodney W. GrimesIf the reserved word ! does not precede the pipeline, the 6554b88c807SRodney W. Grimesexit status is the exit status of the last command specified 6564b88c807SRodney W. Grimesin the pipeline. Otherwise, the exit status is the logical 6574b88c807SRodney W. GrimesNOT of the exit status of the last command. That is, if 6584b88c807SRodney W. Grimesthe last command returns zero, the exit status is 1; if 6594b88c807SRodney W. Grimesthe last command returns greater than zero, the exit status 6604b88c807SRodney W. Grimesis zero. 661ab72124fSSteve Price.Pp 6624b88c807SRodney W. GrimesBecause pipeline assignment of standard input or standard 6634b88c807SRodney W. Grimesoutput or both takes place before redirection, it can be 6644b88c807SRodney W. Grimesmodified by redirection. For example: 665ab72124fSSteve Price.Pp 66649e11e3aSSheldon Hearn.Dl $ command1 2>&1 | command2 667ab72124fSSteve Price.Pp 66849e11e3aSSheldon Hearnsends both the standard output and standard error of 66949e11e3aSSheldon Hearn.Ql command1 67049e11e3aSSheldon Hearnto the standard input of 67149e11e3aSSheldon Hearn.Ql command2 . 67249e11e3aSSheldon Hearn.Pp 67349e11e3aSSheldon HearnA 674e918fc8fSSheldon Hearn.Dq Li \; 67549e11e3aSSheldon Hearnor newline terminator causes the preceding 676e918fc8fSSheldon HearnAND-OR-list 677e918fc8fSSheldon Hearn(described below in the section called 678e918fc8fSSheldon Hearn.Sx Short-Circuit List Operators ) 679e918fc8fSSheldon Hearnto be executed sequentially; 680e918fc8fSSheldon Hearnan 681e918fc8fSSheldon Hearn.Dq Li & 682e918fc8fSSheldon Hearncauses asynchronous execution of the preceding AND-OR-list. 683ab72124fSSteve Price.Pp 68449e11e3aSSheldon HearnNote that unlike some other shells, 68549e11e3aSSheldon Hearn.Nm 68649e11e3aSSheldon Hearnexecutes each process in the pipeline as a child of the 68749e11e3aSSheldon Hearn.Nm 68849e11e3aSSheldon Hearnprocess. 68949e11e3aSSheldon HearnShell builtin commands are the exception to this rule. 69049e11e3aSSheldon HearnThey are executed in the current shell, although they do not affect its 69149e11e3aSSheldon Hearnenvironment when used in pipelines. 69249e11e3aSSheldon Hearn.Ss Background Commands (&) 6934b88c807SRodney W. GrimesIf a command is terminated by the control operator ampersand 69449e11e3aSSheldon Hearn.Pq Li & , 69549e11e3aSSheldon Hearnthe shell executes the command asynchronously; 69649e11e3aSSheldon Hearnthe shell does not wait for the command to finish 69749e11e3aSSheldon Hearnbefore executing the next command. 698ab72124fSSteve Price.Pp 6994b88c807SRodney W. GrimesThe format for running a command in background is: 700ab72124fSSteve Price.Bd -literal -offset indent 7014b88c807SRodney W. Grimescommand1 & [command2 & ...] 702ab72124fSSteve Price.Ed 703ab72124fSSteve Price.Pp 7044b88c807SRodney W. GrimesIf the shell is not interactive, the standard input of an 7054b88c807SRodney W. Grimesasynchronous command is set to /dev/null. 70649e11e3aSSheldon Hearn.Ss Lists (Generally Speaking) 7074b88c807SRodney W. GrimesA list is a sequence of zero or more commands separated by 7084b88c807SRodney W. Grimesnewlines, semicolons, or ampersands, 7094b88c807SRodney W. Grimesand optionally terminated by one of these three characters. 7104b88c807SRodney W. GrimesThe commands in a 7114b88c807SRodney W. Grimeslist are executed in the order they are written. 7124b88c807SRodney W. GrimesIf command is followed by an ampersand, the shell starts the 7134b88c807SRodney W. Grimescommand and immediately proceed onto the next command; 7144b88c807SRodney W. Grimesotherwise it waits for the command to terminate before 7154b88c807SRodney W. Grimesproceeding to the next one. 716ab72124fSSteve Price.Ss Short-Circuit List Operators 71749e11e3aSSheldon Hearn.Dq Li && 7186b15476eSSheldon Hearnand 71949e11e3aSSheldon Hearn.Dq Li || 7206b15476eSSheldon Hearnare AND-OR list operators. 72149e11e3aSSheldon Hearn.Dq Li && 7226b15476eSSheldon Hearnexecutes the first command, and then executes the second command 7236b15476eSSheldon Hearnif the exit status of the first command is zero. 72449e11e3aSSheldon Hearn.Dq Li || 725120ac26dSMasafumi Max NAKANEis similar, but executes the second command if the exit 7266b15476eSSheldon Hearnstatus of the first command is nonzero. 72749e11e3aSSheldon Hearn.Dq Li && 7286b15476eSSheldon Hearnand 72949e11e3aSSheldon Hearn.Dq Li || 7304b88c807SRodney W. Grimesboth have the same priority. 73149e11e3aSSheldon Hearn.Ss Flow-Control Constructs (if, while, for, case) 73249e11e3aSSheldon HearnThe syntax of the 73349e11e3aSSheldon Hearn.Ic if 73449e11e3aSSheldon Hearncommand is: 73549e11e3aSSheldon Hearn.\" 73649e11e3aSSheldon Hearn.\" XXX Use .Dl to work around broken handling of .Ic inside .Bd and .Ed . 73749e11e3aSSheldon Hearn.\" 73849e11e3aSSheldon Hearn.Dl Ic if Ar list 73949e11e3aSSheldon Hearn.Dl Ic then Ar list 74049e11e3aSSheldon Hearn.Dl [ Ic elif Ar list 74149e11e3aSSheldon Hearn.Dl Ic then Ar list ] ... 74249e11e3aSSheldon Hearn.Dl [ Ic else Ar list ] 74349e11e3aSSheldon Hearn.Dl Ic fi 744ab72124fSSteve Price.Pp 74549e11e3aSSheldon HearnThe syntax of the 74649e11e3aSSheldon Hearn.Ic while 74749e11e3aSSheldon Hearncommand is: 74849e11e3aSSheldon Hearn.Dl Ic while Ar list 74949e11e3aSSheldon Hearn.Dl Ic do Ar list 75049e11e3aSSheldon Hearn.Dl Ic done 751ab72124fSSteve Price.Pp 752ab0a2172SSteve PriceThe two lists are executed repeatedly while the exit status of the 75349e11e3aSSheldon Hearnfirst list is zero. 75449e11e3aSSheldon HearnThe 75549e11e3aSSheldon Hearn.Ic until 75649e11e3aSSheldon Hearncommand is similar, but has the word 75749e11e3aSSheldon Hearn.Ic until 75849e11e3aSSheldon Hearnin place of 75949e11e3aSSheldon Hearn.Ic while , 76049e11e3aSSheldon Hearnwhich causes it to 7614b88c807SRodney W. Grimesrepeat until the exit status of the first list is zero. 762ab72124fSSteve Price.Pp 76349e11e3aSSheldon HearnThe syntax of the 76449e11e3aSSheldon Hearn.Ic for 76549e11e3aSSheldon Hearncommand is: 76649e11e3aSSheldon Hearn.Dl Ic for Ar variable Ic in Ar word ... 76749e11e3aSSheldon Hearn.Dl Ic do Ar list 76849e11e3aSSheldon Hearn.Dl Ic done 769ab72124fSSteve Price.Pp 7704b88c807SRodney W. GrimesThe words are expanded, and then the list is executed 77149e11e3aSSheldon Hearnrepeatedly with the variable set to each word in turn. 77249e11e3aSSheldon HearnThe 77349e11e3aSSheldon Hearn.Ic do 7746b15476eSSheldon Hearnand 77549e11e3aSSheldon Hearn.Ic done 77649e11e3aSSheldon Hearncommands may be replaced with 77749e11e3aSSheldon Hearn.Dq Li { 77849e11e3aSSheldon Hearnand 77949e11e3aSSheldon Hearn.Dq Li } . 780ab72124fSSteve Price.Pp 78149e11e3aSSheldon HearnThe syntax of the 78249e11e3aSSheldon Hearn.Ic break 78349e11e3aSSheldon Hearnand 78449e11e3aSSheldon Hearn.Ic continue 78549e11e3aSSheldon Hearncommands is: 78649e11e3aSSheldon Hearn.Dl Ic break Op Ar num 78749e11e3aSSheldon Hearn.Dl Ic continue Op Ar num 788ab72124fSSteve Price.Pp 78949e11e3aSSheldon HearnThe 79049e11e3aSSheldon Hearn.Ic break 79149e11e3aSSheldon Hearncommand terminates the 79249e11e3aSSheldon Hearn.Ar num 79349e11e3aSSheldon Hearninnermost 79449e11e3aSSheldon Hearn.Ic for 79549e11e3aSSheldon Hearnor 79649e11e3aSSheldon Hearn.Ic while 79749e11e3aSSheldon Hearnloops. 79849e11e3aSSheldon HearnThe 79949e11e3aSSheldon Hearn.Ic continue 80049e11e3aSSheldon Hearncommand continues with the next iteration of the innermost loop. 801ab0a2172SSteve PriceThese are implemented as builtin commands. 802ab72124fSSteve Price.Pp 80349e11e3aSSheldon HearnThe syntax of the 80449e11e3aSSheldon Hearn.Ic case 80549e11e3aSSheldon Hearncommand is 80649e11e3aSSheldon Hearn.Dl Ic case Ar word Ic in 80749e11e3aSSheldon Hearn.Dl pattern) list ;; 80849e11e3aSSheldon Hearn.Dl ... 80949e11e3aSSheldon Hearn.Dl Ic esac 810ab72124fSSteve Price.Pp 81149e11e3aSSheldon HearnThe pattern can actually be one or more patterns 81249e11e3aSSheldon Hearn(see 81349e11e3aSSheldon Hearn.Sx Shell Patterns 81449e11e3aSSheldon Hearndescribed later), 81549e11e3aSSheldon Hearnseparated by 81667135607SMartin Cracauer.Dq Li \&| 8176b15476eSSheldon Hearncharacters. 818ab72124fSSteve Price.Ss Grouping Commands Together 8194b88c807SRodney W. GrimesCommands may be grouped by writing either 820ab72124fSSteve Price.Bd -literal -offset indent 8214b88c807SRodney W. Grimes(list) 822ab72124fSSteve Price.Ed 823ab72124fSSteve Price.Pp 8244b88c807SRodney W. Grimesor 825ab72124fSSteve Price.Bd -literal -offset indent 8264b88c807SRodney W. Grimes{ list; } 827ab72124fSSteve Price.Ed 828ab72124fSSteve Price.Pp 82949e11e3aSSheldon HearnThe first form executes the commands in a subshell. 83049e11e3aSSheldon HearnNote that builtin commands thus executed do not affect the current shell. 83149e11e3aSSheldon HearnThe second form does not fork another shell, 83249e11e3aSSheldon Hearnso it is slightly more efficient. 83349e11e3aSSheldon HearnGrouping commands together this way allows the user to 834ab0a2172SSteve Priceredirect their output as though they were one program: 835ab72124fSSteve Price.Bd -literal -offset indent 836ee7dc416SSteve Price{ echo -n "hello"; echo " world"; } > greeting 837ab72124fSSteve Price.Ed 838ab72124fSSteve Price.Ss Functions 8394b88c807SRodney W. GrimesThe syntax of a function definition is 840ab72124fSSteve Price.Bd -literal -offset indent 8414b88c807SRodney W. Grimesname ( ) command 842ab72124fSSteve Price.Ed 843ab72124fSSteve Price.Pp 8444b88c807SRodney W. GrimesA function definition is an executable statement; when 8454b88c807SRodney W. Grimesexecuted it installs a function named name and returns an 8464b88c807SRodney W. Grimesexit status of zero. The command is normally a list 8476b15476eSSheldon Hearnenclosed between 84849e11e3aSSheldon Hearn.Dq Li { 8496b15476eSSheldon Hearnand 85049e11e3aSSheldon Hearn.Dq Li } . 851ab72124fSSteve Price.Pp 8524b88c807SRodney W. GrimesVariables may be declared to be local to a function by 85349e11e3aSSheldon Hearnusing the 85449e11e3aSSheldon Hearn.Ic local 85549e11e3aSSheldon Hearncommand. 85649e11e3aSSheldon HearnThis should appear as the first statement of a function, 85749e11e3aSSheldon Hearnand the syntax is: 85849e11e3aSSheldon Hearn.Bd -ragged -offset indent 85949e11e3aSSheldon Hearn.Ic local 86049e11e3aSSheldon Hearn.Op Ar variable ... 86149e11e3aSSheldon Hearn.Op Ar - 862ab72124fSSteve Price.Ed 863ab72124fSSteve Price.Pp 86449e11e3aSSheldon HearnThe 86549e11e3aSSheldon Hearn.Ic local 86649e11e3aSSheldon Hearncommand is implemented as a builtin command. 867ab72124fSSteve Price.Pp 8684b88c807SRodney W. GrimesWhen a variable is made local, it inherits the initial 8694b88c807SRodney W. Grimesvalue and exported and readonly flags from the variable 8704b88c807SRodney W. Grimeswith the same name in the surrounding scope, if there is 8714b88c807SRodney W. Grimesone. Otherwise, the variable is initially unset. The shell 872e918fc8fSSheldon Hearnuses dynamic scoping, so that if the variable 873e918fc8fSSheldon Hearn.Em x 874e918fc8fSSheldon Hearnis made local to function 875e918fc8fSSheldon Hearn.Em f , 876e918fc8fSSheldon Hearnwhich then calls function 877e918fc8fSSheldon Hearn.Em g , 878e918fc8fSSheldon Hearnreferences to the variable 879e918fc8fSSheldon Hearn.Em x 880e918fc8fSSheldon Hearnmade inside 881e918fc8fSSheldon Hearn.Em g 882e918fc8fSSheldon Hearnwill refer to the variable 883e918fc8fSSheldon Hearn.Em x 884e918fc8fSSheldon Hearndeclared inside 885e918fc8fSSheldon Hearn.Em f , 886e918fc8fSSheldon Hearnnot to the global variable named 887e918fc8fSSheldon Hearn.Em x . 888ab72124fSSteve Price.Pp 8894b88c807SRodney W. GrimesThe only special parameter than can be made local is 89049e11e3aSSheldon Hearn.Dq Li - . 8916b15476eSSheldon HearnMaking 89249e11e3aSSheldon Hearn.Dq Li - 8936b15476eSSheldon Hearnlocal causes any shell options that are 8944b88c807SRodney W. Grimeschanged via the set command inside the function to be 8954b88c807SRodney W. Grimesrestored to their original values when the function 8964b88c807SRodney W. Grimesreturns. 897ab72124fSSteve Price.Pp 89849e11e3aSSheldon HearnThe syntax of the 89949e11e3aSSheldon Hearn.Ic return 90049e11e3aSSheldon Hearncommand is 90149e11e3aSSheldon Hearn.Bd -ragged -offset indent 90249e11e3aSSheldon Hearn.Ic return 90349e11e3aSSheldon Hearn.Op Ar exitstatus 904ab72124fSSteve Price.Ed 905ab72124fSSteve Price.Pp 90649e11e3aSSheldon HearnIt terminates the currently executing function. 90749e11e3aSSheldon HearnThe 90849e11e3aSSheldon Hearn.Ic return 90949e11e3aSSheldon Hearncommand is implemented as a builtin command. 910ab72124fSSteve Price.Ss Variables and Parameters 9114b88c807SRodney W. GrimesThe shell maintains a set of parameters. A parameter 9124b88c807SRodney W. Grimesdenoted by a name is called a variable. When starting up, 9134b88c807SRodney W. Grimesthe shell turns all the environment variables into shell 9144b88c807SRodney W. Grimesvariables. New variables can be set using the form 915ab72124fSSteve Price.Bd -literal -offset indent 9164b88c807SRodney W. Grimesname=value 917ab72124fSSteve Price.Ed 918ab72124fSSteve Price.Pp 9194b88c807SRodney W. GrimesVariables set by the user must have a name consisting solely 92049e11e3aSSheldon Hearnof alphabetics, numerics, and underscores. 92149e11e3aSSheldon HearnThe first letter of a variable name must not be numeric. 92249e11e3aSSheldon HearnA parameter can also be denoted by a number 9234b88c807SRodney W. Grimesor a special character as explained below. 924ab72124fSSteve Price.Ss Positional Parameters 92549e11e3aSSheldon HearnA positional parameter is a parameter denoted by a number greater than zero. 926ab72124fSSteve PriceThe shell sets these initially to the values of its command line 927ab72124fSSteve Pricearguments that follow the name of the shell script. The 928c644db6aSSheldon Hearn.Ic set 929c644db6aSSheldon Hearnbuiltin command can also be used to set or reset them. 930ab72124fSSteve Price.Ss Special Parameters 9314b88c807SRodney W. GrimesA special parameter is a parameter denoted by one of the following 9324b88c807SRodney W. Grimesspecial characters. The value of the parameter is listed 9334b88c807SRodney W. Grimesnext to its character. 934ab72124fSSteve Price.Bl -hang 93549e11e3aSSheldon Hearn.It Li * 9364b88c807SRodney W. GrimesExpands to the positional parameters, starting from one. When 9374b88c807SRodney W. Grimesthe expansion occurs within a double-quoted string 9384b88c807SRodney W. Grimesit expands to a single field with the value of each parameter 93949e11e3aSSheldon Hearnseparated by the first character of the 94049e11e3aSSheldon Hearn.Ev IFS 94149e11e3aSSheldon Hearnvariable, 94249e11e3aSSheldon Hearnor by a 94349e11e3aSSheldon Hearn.Aq space 94449e11e3aSSheldon Hearnif 94549e11e3aSSheldon Hearn.Ev IFS 94649e11e3aSSheldon Hearnis unset. 94749e11e3aSSheldon Hearn.It Li @ 9484b88c807SRodney W. GrimesExpands to the positional parameters, starting from one. When 9494b88c807SRodney W. Grimesthe expansion occurs within double-quotes, each positional 9504b88c807SRodney W. Grimesparameter expands as a separate argument. 9514b88c807SRodney W. GrimesIf there are no positional parameters, the 952e918fc8fSSheldon Hearnexpansion of 953e918fc8fSSheldon Hearn.Li @ 954e918fc8fSSheldon Hearngenerates zero arguments, even when 955e918fc8fSSheldon Hearn.Li @ 956e918fc8fSSheldon Hearnis double-quoted. What this basically means, for example, is 9576b15476eSSheldon Hearnif $1 is 9586b15476eSSheldon Hearn.Dq abc 9596b15476eSSheldon Hearnand $2 is 9606b15476eSSheldon Hearn.Dq def ghi , 9616b15476eSSheldon Hearnthen 962e918fc8fSSheldon Hearn.Qq Li $@ 9636b15476eSSheldon Hearnexpands to 9644b88c807SRodney W. Grimesthe two arguments: 965ab72124fSSteve Price.Bd -literal -offset indent 9664b88c807SRodney W. Grimes"abc" "def ghi" 967ab72124fSSteve Price.Ed 96849e11e3aSSheldon Hearn.It Li # 9694b88c807SRodney W. GrimesExpands to the number of positional parameters. 97049e11e3aSSheldon Hearn.It Li ? 9714b88c807SRodney W. GrimesExpands to the exit status of the most recent pipeline. 97249e11e3aSSheldon Hearn.It Li - 973ab72124fSSteve Price(hyphen) Expands to the current option flags (the single-letter 9744b88c807SRodney W. Grimesoption names concatenated into a string) as specified on 9754b88c807SRodney W. Grimesinvocation, by the set builtin command, or implicitly 9764b88c807SRodney W. Grimesby the shell. 97749e11e3aSSheldon Hearn.It Li $ 9784b88c807SRodney W. GrimesExpands to the process ID of the invoked shell. A subshell 9794b88c807SRodney W. Grimesretains the same value of $ as its parent. 98049e11e3aSSheldon Hearn.It Li ! 9814b88c807SRodney W. GrimesExpands to the process ID of the most recent background 9824b88c807SRodney W. Grimescommand executed from the current shell. For a 9834b88c807SRodney W. Grimespipeline, the process ID is that of the last command in the 9844b88c807SRodney W. Grimespipeline. 98549e11e3aSSheldon Hearn.It Li 0 986ab72124fSSteve Price(zero) Expands to the name of the shell or shell script. 987ab72124fSSteve Price.El 988ab72124fSSteve Price.Ss Word Expansions 9894b88c807SRodney W. GrimesThis clause describes the various expansions that are 9904b88c807SRodney W. Grimesperformed on words. Not all expansions are performed on 9914b88c807SRodney W. Grimesevery word, as explained later. 992ab72124fSSteve Price.Pp 9934b88c807SRodney W. GrimesTilde expansions, parameter expansions, command substitutions, 9944b88c807SRodney W. Grimesarithmetic expansions, and quote removals that occur within 9954b88c807SRodney W. Grimesa single word expand to a single field. It is only field 9964b88c807SRodney W. Grimessplitting or pathname expansion that can create multiple 997e6d3cf26SSheldon Hearnfields from a single word. 998e6d3cf26SSheldon HearnThe single exception to this rule is 999e918fc8fSSheldon Hearnthe expansion of the special parameter 1000e918fc8fSSheldon Hearn.Li @ 1001e918fc8fSSheldon Hearnwithin double-quotes, 1002e6d3cf26SSheldon Hearnas was described above. 1003ab72124fSSteve Price.Pp 10044b88c807SRodney W. GrimesThe order of word expansion is: 1005ab72124fSSteve Price.Bl -enum 1006ab72124fSSteve Price.It 1007ab72124fSSteve PriceTilde Expansion, Parameter Expansion, Command Substitution, 10084b88c807SRodney W. GrimesArithmetic Expansion (these all occur at the same time). 1009ab72124fSSteve Price.It 101049e11e3aSSheldon HearnField Splitting is performed on fields generated by step (1) 101149e11e3aSSheldon Hearnunless the 101249e11e3aSSheldon Hearn.Ev IFS 101349e11e3aSSheldon Hearnvariable is null. 1014ab72124fSSteve Price.It 101549e11e3aSSheldon HearnPathname Expansion (unless the 101649e11e3aSSheldon Hearn.Fl f 101749e11e3aSSheldon Hearnoption is in effect). 1018ab72124fSSteve Price.It 1019ab72124fSSteve PriceQuote Removal. 1020ab72124fSSteve Price.El 1021ab72124fSSteve Price.Pp 102249e11e3aSSheldon HearnThe 102349e11e3aSSheldon Hearn.Dq Li $ 102449e11e3aSSheldon Hearncharacter is used to introduce parameter expansion, command 10254b88c807SRodney W. Grimessubstitution, or arithmetic evaluation. 1026ab72124fSSteve Price.Ss Tilde Expansion (substituting a user's home directory) 102749e11e3aSSheldon HearnA word beginning with an unquoted tilde character 102849e11e3aSSheldon Hearn.Pq Li ~ 102949e11e3aSSheldon Hearnis 103049e11e3aSSheldon Hearnsubjected to tilde expansion. 103149e11e3aSSheldon HearnAll the characters up to a slash 103249e11e3aSSheldon Hearn.Pq Li / 103349e11e3aSSheldon Hearnor the end of the word are treated as a username 10344b88c807SRodney W. Grimesand are replaced with the user's home directory. If the 10354b88c807SRodney W. Grimesusername is missing (as in ~/foobar), the tilde is replaced 10364b88c807SRodney W. Grimeswith the value of the HOME variable (the current user's 10374b88c807SRodney W. Grimeshome directory). 1038ab72124fSSteve Price.Ss Parameter Expansion 10394b88c807SRodney W. GrimesThe format for parameter expansion is as follows: 1040ab72124fSSteve Price.Bd -literal -offset indent 10414b88c807SRodney W. Grimes${expression} 1042ab72124fSSteve Price.Ed 1043ab72124fSSteve Price.Pp 104449e11e3aSSheldon Hearnwhere expression consists of all characters until the matching 104549e11e3aSSheldon Hearn.Dq Li } . 104649e11e3aSSheldon HearnAny 104749e11e3aSSheldon Hearn.Dq Li } 10484b88c807SRodney W. Grimesescaped by a backslash or within a quoted string, and characters in 10494b88c807SRodney W. Grimesembedded arithmetic expansions, command substitutions, and variable 105049e11e3aSSheldon Hearnexpansions, are not examined in determining the matching 105149e11e3aSSheldon Hearn.Dq Li } . 1052ab72124fSSteve Price.Pp 10534b88c807SRodney W. GrimesThe simplest form for parameter expansion is: 1054ab72124fSSteve Price.Bd -literal -offset indent 10554b88c807SRodney W. Grimes${parameter} 1056ab72124fSSteve Price.Ed 1057ab72124fSSteve Price.Pp 10584b88c807SRodney W. GrimesThe value, if any, of parameter is substituted. 1059ab72124fSSteve Price.Pp 10604b88c807SRodney W. GrimesThe parameter name or symbol can be enclosed in braces, which are 10614b88c807SRodney W. Grimesoptional except for positional parameters with more than one digit or 10624b88c807SRodney W. Grimeswhen parameter is followed by a character that could be interpreted as 10634b88c807SRodney W. Grimespart of the name. 1064ab72124fSSteve PriceIf a parameter expansion occurs inside double-quotes: 1065ab72124fSSteve Price.Bl -enum 1066ab72124fSSteve Price.It 1067ab72124fSSteve PricePathname expansion is not performed on the results of the 10684b88c807SRodney W. Grimesexpansion. 1069ab72124fSSteve Price.It 1070ab72124fSSteve PriceField splitting is not performed on the results of the 1071e918fc8fSSheldon Hearnexpansion, with the exception of the special parameter 1072e918fc8fSSheldon Hearn.Li @ . 1073ab72124fSSteve Price.El 1074ab72124fSSteve Price.Pp 10754b88c807SRodney W. GrimesIn addition, a parameter expansion can be modified by using one of the 10764b88c807SRodney W. Grimesfollowing formats. 1077ab72124fSSteve Price.Bl -tag -width Ds 1078ab72124fSSteve Price.It Li ${parameter:-word} 10794b88c807SRodney W. GrimesUse Default Values. If parameter is unset or 10804b88c807SRodney W. Grimesnull, the expansion of word is 10814b88c807SRodney W. Grimessubstituted; otherwise, the value of 10824b88c807SRodney W. Grimesparameter is substituted. 1083ab72124fSSteve Price.It Li ${parameter:=word} 10844b88c807SRodney W. GrimesAssign Default Values. If parameter is unset 10854b88c807SRodney W. Grimesor null, the expansion of word is 10864b88c807SRodney W. Grimesassigned to parameter. In all cases, the 10874b88c807SRodney W. Grimesfinal value of parameter is 10884b88c807SRodney W. Grimessubstituted. Only variables, not positional 10894b88c807SRodney W. Grimesparameters or special parameters, can be 10904b88c807SRodney W. Grimesassigned in this way. 1091ab72124fSSteve Price.It Li ${parameter:?[word]} 10924b88c807SRodney W. GrimesIndicate Error if Null or Unset. If 10934b88c807SRodney W. Grimesparameter is unset or null, the expansion of 10944b88c807SRodney W. Grimesword (or a message indicating it is unset if 10954b88c807SRodney W. Grimesword is omitted) is written to standard 10964b88c807SRodney W. Grimeserror and the shell exits with a nonzero 1097e6d3cf26SSheldon Hearnexit status. 1098e6d3cf26SSheldon HearnOtherwise, the value of 10994b88c807SRodney W. Grimesparameter is substituted. An 11004b88c807SRodney W. Grimesinteractive shell need not exit. 1101ab72124fSSteve Price.It Li ${parameter:+word} 11024b88c807SRodney W. GrimesUse Alternate Value. If parameter is unset 11034b88c807SRodney W. Grimesor null, null is substituted; 11044b88c807SRodney W. Grimesotherwise, the expansion of word is 11054b88c807SRodney W. Grimessubstituted. 1106ab72124fSSteve Price.Pp 11074b88c807SRodney W. GrimesIn the parameter expansions shown previously, use of the colon in the 11084b88c807SRodney W. Grimesformat results in a test for a parameter that is unset or null; omission 11094b88c807SRodney W. Grimesof the colon results in a test for a parameter that is only unset. 1110ab72124fSSteve Price.It Li ${#parameter} 11114b88c807SRodney W. GrimesString Length. The length in characters of 11124b88c807SRodney W. Grimesthe value of parameter. 1113ab72124fSSteve Price.Pp 11144b88c807SRodney W. GrimesThe following four varieties of parameter expansion provide for substring 111549e11e3aSSheldon Hearnprocessing. 111649e11e3aSSheldon HearnIn each case, pattern matching notation 111749e11e3aSSheldon Hearn(see 111849e11e3aSSheldon Hearn.Sx Shell Patterns ) , 111949e11e3aSSheldon Hearnrather than regular expression notation, 112049e11e3aSSheldon Hearnis used to evaluate the patterns. 1121e918fc8fSSheldon HearnIf parameter is one of the special parameters 1122e918fc8fSSheldon Hearn.Li * 112349e11e3aSSheldon Hearnor 1124e918fc8fSSheldon Hearn.Li @ , 112549e11e3aSSheldon Hearnthe result of the expansion is unspecified. 11264b88c807SRodney W. GrimesEnclosing the full parameter expansion string in double-quotes does not 11274b88c807SRodney W. Grimescause the following four varieties of pattern characters to be quoted, 11284b88c807SRodney W. Grimeswhereas quoting characters within the braces has this effect. 1129ab72124fSSteve Price.It Li ${parameter%word} 11304b88c807SRodney W. GrimesRemove Smallest Suffix Pattern. The word 11314b88c807SRodney W. Grimesis expanded to produce a pattern. The 11324b88c807SRodney W. Grimesparameter expansion then results in 11334b88c807SRodney W. Grimesparameter, with the smallest portion of the 11344b88c807SRodney W. Grimessuffix matched by the pattern deleted. 1135ab72124fSSteve Price.It Li ${parameter%%word} 11364b88c807SRodney W. GrimesRemove Largest Suffix Pattern. The word 11374b88c807SRodney W. Grimesis expanded to produce a pattern. The 11384b88c807SRodney W. Grimesparameter expansion then results in 11394b88c807SRodney W. Grimesparameter, with the largest portion of the 11404b88c807SRodney W. Grimessuffix matched by the pattern deleted. 1141ab72124fSSteve Price.It Li ${parameter#word} 11424b88c807SRodney W. GrimesRemove Smallest Prefix Pattern. The word 11434b88c807SRodney W. Grimesis expanded to produce a pattern. The 11444b88c807SRodney W. Grimesparameter expansion then results in 11454b88c807SRodney W. Grimesparameter, with the smallest portion of the 11464b88c807SRodney W. Grimesprefix matched by the pattern deleted. 1147ab72124fSSteve Price.It Li ${parameter##word} 11484b88c807SRodney W. GrimesRemove Largest Prefix Pattern. The word 11494b88c807SRodney W. Grimesis expanded to produce a pattern. The 11504b88c807SRodney W. Grimesparameter expansion then results in 11514b88c807SRodney W. Grimesparameter, with the largest portion of the 11524b88c807SRodney W. Grimesprefix matched by the pattern deleted. 1153ab72124fSSteve Price.El 1154ab72124fSSteve Price.Ss Command Substitution 11554b88c807SRodney W. GrimesCommand substitution allows the output of a command to be substituted in 11564b88c807SRodney W. Grimesplace of the command name itself. Command substitution occurs when 11574b88c807SRodney W. Grimesthe command is enclosed as follows: 1158ab72124fSSteve Price.Bd -literal -offset indent 11594b88c807SRodney W. Grimes$(command) 1160ab72124fSSteve Price.Ed 1161ab72124fSSteve Price.Pp 116249e11e3aSSheldon Hearnor the backquoted version: 1163ab72124fSSteve Price.Bd -literal -offset indent 11644b88c807SRodney W. Grimes`command` 1165ab72124fSSteve Price.Ed 1166ab72124fSSteve Price.Pp 11674b88c807SRodney W. GrimesThe shell expands the command substitution by executing command in a 11684b88c807SRodney W. Grimessubshell environment and replacing the command substitution 116949e11e3aSSheldon Hearnwith the standard output of the command, 117049e11e3aSSheldon Hearnremoving sequences of one or more newlines at the end of the substitution. 117149e11e3aSSheldon HearnEmbedded newlines before the end of the output are not removed; 117249e11e3aSSheldon Hearnhowever, during field splitting, they may be translated into spaces 117349e11e3aSSheldon Hearndepending on the value of 117449e11e3aSSheldon Hearn.Ev IFS 117549e11e3aSSheldon Hearnand the quoting that is in effect. 1176ab72124fSSteve Price.Ss Arithmetic Expansion 11774b88c807SRodney W. GrimesArithmetic expansion provides a mechanism for evaluating an arithmetic 1178e6d3cf26SSheldon Hearnexpression and substituting its value. 1179e6d3cf26SSheldon HearnThe format for arithmetic expansion is as follows: 1180ab72124fSSteve Price.Bd -literal -offset indent 11814b88c807SRodney W. Grimes$((expression)) 1182ab72124fSSteve Price.Ed 1183ab72124fSSteve Price.Pp 11844b88c807SRodney W. GrimesThe expression is treated as if it were in double-quotes, except 11854b88c807SRodney W. Grimesthat a double-quote inside the expression is not treated specially. The 11864b88c807SRodney W. Grimesshell expands all tokens in the expression for parameter expansion, 11874b88c807SRodney W. Grimescommand substitution, and quote removal. 1188ab72124fSSteve Price.Pp 11894b88c807SRodney W. GrimesNext, the shell treats this as an arithmetic expression and 11904b88c807SRodney W. Grimessubstitutes the value of the expression. 1191ab72124fSSteve Price.Ss White Space Splitting (Field Splitting) 11924b88c807SRodney W. GrimesAfter parameter expansion, command substitution, and 11934b88c807SRodney W. Grimesarithmetic expansion the shell scans the results of 11944b88c807SRodney W. Grimesexpansions and substitutions that did not occur in double-quotes for 11954b88c807SRodney W. Grimesfield splitting and multiple fields can result. 1196ab72124fSSteve Price.Pp 119749e11e3aSSheldon HearnThe shell treats each character of the 119849e11e3aSSheldon Hearn.Ev IFS 119949e11e3aSSheldon Hearnas a delimiter and uses 12004b88c807SRodney W. Grimesthe delimiters to split the results of parameter expansion and command 12014b88c807SRodney W. Grimessubstitution into fields. 1202ab72124fSSteve Price.Ss Pathname Expansion (File Name Generation) 120349e11e3aSSheldon HearnUnless the 120449e11e3aSSheldon Hearn.Fl f 120549e11e3aSSheldon Hearnoption is set, 120649e11e3aSSheldon Hearnfile name generation is performed 1207ab0a2172SSteve Priceafter word splitting is complete. Each word is 12084b88c807SRodney W. Grimesviewed as a series of patterns, separated by slashes. The 12094b88c807SRodney W. Grimesprocess of expansion replaces the word with the names of 12104b88c807SRodney W. Grimesall existing files whose names can be formed by replacing 12114b88c807SRodney W. Grimeseach pattern with a string that matches the specified pattern. 1212ab0a2172SSteve PriceThere are two restrictions on this: first, a pattern cannot match 1213ab0a2172SSteve Pricea string containing a slash, and second, 12144b88c807SRodney W. Grimesa pattern cannot match a string starting with a period 12154b88c807SRodney W. Grimesunless the first character of the pattern is a period. 12164b88c807SRodney W. GrimesThe next section describes the patterns used for both 1217ab72124fSSteve PricePathname Expansion and the 1218c644db6aSSheldon Hearn.Ic case 1219ab72124fSSteve Pricecommand. 1220ab72124fSSteve Price.Ss Shell Patterns 1221f434d1dfSAdam DavidA pattern consists of normal characters, which match themselves, 122249e11e3aSSheldon Hearnand meta-characters. 122349e11e3aSSheldon HearnThe meta-characters are 122449e11e3aSSheldon Hearn.Dq Li ! , 122549e11e3aSSheldon Hearn.Dq Li * , 122649e11e3aSSheldon Hearn.Dq Li ? , 12276b15476eSSheldon Hearnand 122849e11e3aSSheldon Hearn.Dq Li [ . 12296b15476eSSheldon HearnThese characters lose their special meanings if they are quoted. 12306b15476eSSheldon HearnWhen command or variable substitution is performed and the dollar sign 12316b15476eSSheldon Hearnor back quotes are not double-quoted, the value of the 12324b88c807SRodney W. Grimesvariable or the output of the command is scanned for these 12334b88c807SRodney W. Grimescharacters and they are turned into meta-characters. 1234ab72124fSSteve Price.Pp 12356b15476eSSheldon HearnAn asterisk 123649e11e3aSSheldon Hearn.Pq Li * 1237e918fc8fSSheldon Hearnmatches any string of characters. 1238e918fc8fSSheldon HearnA question mark 1239e918fc8fSSheldon Hearn.Pq Li ? 1240e918fc8fSSheldon Hearnmatches any single character. 1241e6d3cf26SSheldon HearnA left bracket 124249e11e3aSSheldon Hearn.Pq Li [ 1243e6d3cf26SSheldon Hearnintroduces a character class. 1244e6d3cf26SSheldon HearnThe end of the character class is indicated by a 124549e11e3aSSheldon Hearn.Dq Li \&] ; 12466b15476eSSheldon Hearnif the 124749e11e3aSSheldon Hearn.Dq Li \&] 12486b15476eSSheldon Hearnis missing then the 124949e11e3aSSheldon Hearn.Dq Li [ 12506b15476eSSheldon Hearnmatches a 125149e11e3aSSheldon Hearn.Dq Li [ 1252e6d3cf26SSheldon Hearnrather than introducing a character class. 1253e6d3cf26SSheldon HearnA character class matches any of the characters between the square brackets. 1254e6d3cf26SSheldon HearnA range of characters may be specified using a minus sign. 1255e918fc8fSSheldon HearnThe character class may be complemented by making an exclamation point 1256e918fc8fSSheldon Hearn.Pq Li ! 1257e918fc8fSSheldon Hearnthe first character of the character class. 1258ab72124fSSteve Price.Pp 12596b15476eSSheldon HearnTo include a 1260e918fc8fSSheldon Hearn.Dq Li \] 1261e6d3cf26SSheldon Hearnin a character class, make it the first character listed 1262e6d3cf26SSheldon Hearn(after the 126349e11e3aSSheldon Hearn.Dq Li ! , 1264e6d3cf26SSheldon Hearnif any). 1265e918fc8fSSheldon HearnTo include a 1266e918fc8fSSheldon Hearn.Dq Li - , 1267e918fc8fSSheldon Hearnmake it the first or last character listed. 1268c644db6aSSheldon Hearn.Ss Builtin Commands 1269c644db6aSSheldon HearnThis section lists the commands which 12704b88c807SRodney W. Grimesare builtin because they need to perform some operation 1271c644db6aSSheldon Hearnthat can not be performed by a separate process. In addition to 1272501e74b7SMark Ovensthese, a builtin version of the 1273c644db6aSSheldon Hearn.Xr printf 1 1274501e74b7SMark Ovenscommand is provided for efficiency. 1275ab72124fSSteve Price.Bl -tag -width Ds 127649e11e3aSSheldon Hearn.It Ic : 1277aa9caaf6SPeter WemmA null command that returns a 0 (true) exit value. 127849e11e3aSSheldon Hearn.It Ic . Ar file 1279aa9caaf6SPeter WemmThe commands in the specified file are read and executed by the shell. 12806466945aSJohn PolstraIf 12816466945aSJohn Polstra.Ar file 12826466945aSJohn Polstracontains any 128349e11e3aSSheldon Hearn.Dq / 12846466945aSJohn Polstracharacters, it is used as is. Otherwise, the shell searches the 12856466945aSJohn Polstra.Ev PATH 12866466945aSJohn Polstrafor the file. If it is not found in the 12876466945aSJohn Polstra.Ev PATH , 12886466945aSJohn Polstrait is sought in the current working directory. 128949e11e3aSSheldon Hearn.It Ic alias Op Ar name ... 129049e11e3aSSheldon Hearn.It Ic alias Op Ar name=string ... 129149e11e3aSSheldon HearnIf 129249e11e3aSSheldon Hearn.Ar name=string 129349e11e3aSSheldon Hearnis specified, the shell defines the alias 129449e11e3aSSheldon Hearn.Ar name 12956b15476eSSheldon Hearnwith value 129649e11e3aSSheldon Hearn.Ar string . 12976b15476eSSheldon HearnIf just 129849e11e3aSSheldon Hearn.Ar name 12996b15476eSSheldon Hearnis specified, the value of the alias 130049e11e3aSSheldon Hearn.Ar name 13016b15476eSSheldon Hearnis printed. 130249e11e3aSSheldon HearnWith no arguments, the 130349e11e3aSSheldon Hearn.Ic alias 130449e11e3aSSheldon Hearnbuiltin command prints the names and values of all defined aliases 130549e11e3aSSheldon Hearn(see 130649e11e3aSSheldon Hearn.Ic unalias ) . 130749e11e3aSSheldon Hearn.It Ic bg Op Ar job ... 130849e11e3aSSheldon HearnContinue the specified jobs 130949e11e3aSSheldon Hearn(or the current job if no jobs are given) 131049e11e3aSSheldon Hearnin the background. 131149e11e3aSSheldon Hearn.It Ic command Ar cmd Op Ar arg ... 131249e11e3aSSheldon HearnExecute the specified builtin command, 131349e11e3aSSheldon Hearn.Ar cmd . 131449e11e3aSSheldon HearnThis is useful when the user wishes to override a shell function 131549e11e3aSSheldon Hearnwith the same name as a builtin command. 131649e11e3aSSheldon Hearn.It Ic cd Op Ar directory 131749e11e3aSSheldon HearnSwitch to the specified 131849e11e3aSSheldon Hearn.Ar directory , 131949e11e3aSSheldon Hearnor to the directory specified in the 132049e11e3aSSheldon Hearn.Ev HOME 132149e11e3aSSheldon Hearnenvironment variable if no 132249e11e3aSSheldon Hearn.Ar directory 132349e11e3aSSheldon Hearnis specified. 132449e11e3aSSheldon HearnIf 132549e11e3aSSheldon Hearn.Ar directory 132649e11e3aSSheldon Hearnis not found as a subdirectory of the current directory 132749e11e3aSSheldon Hearn(and does not begin with 132849e11e3aSSheldon Hearn.Dq Li / , 132949e11e3aSSheldon Hearn.Dq Li ./ , 133049e11e3aSSheldon Hearnor 133149e11e3aSSheldon Hearn.Dq Li ../ ) , 133249e11e3aSSheldon Hearnthen the directories listed in the 133349e11e3aSSheldon Hearn.Ev CDPATH 133449e11e3aSSheldon Hearnvariable will be 133549e11e3aSSheldon Hearnsearched for the specified 133649e11e3aSSheldon Hearn.Ar directory . 133749e11e3aSSheldon HearnThe format of 133849e11e3aSSheldon Hearn.Ar CDPATH 133949e11e3aSSheldon Hearnis the same as that of 134049e11e3aSSheldon Hearn.Ev PATH . 1341e6d3cf26SSheldon HearnIn an interactive shell, 134249e11e3aSSheldon Hearnthe 134349e11e3aSSheldon Hearn.Ic cd 134449e11e3aSSheldon Hearncommand will print out the name of the directory 1345e6d3cf26SSheldon Hearnthat it actually switched to 1346e6d3cf26SSheldon Hearnif this is different from the name that the user gave. 134749e11e3aSSheldon HearnThese may be different either because the 134849e11e3aSSheldon Hearn.Ev CDPATH 134949e11e3aSSheldon Hearnmechanism was used or because a symbolic link was crossed. 1350501e74b7SMark Ovens.It Xo 1351501e74b7SMark Ovens.Ic echo 1352501e74b7SMark Ovens.Op Fl en 1353501e74b7SMark Ovens.Ar string 1354501e74b7SMark Ovens.Xc 1355501e74b7SMark OvensPrint 1356501e74b7SMark Ovens.Ar string 1357501e74b7SMark Ovensto the standard output with a newline appended. 1358501e74b7SMark Ovens.Bl -tag -width Ds 1359501e74b7SMark Ovens.It Fl n 1360501e74b7SMark OvensSuppress the output of the trailing newline. 1361501e74b7SMark Ovens.It Fl e 1362501e74b7SMark OvensProcess C-style backslash escape sequences. 1363501e74b7SMark Ovens.Ic echo 1364501e74b7SMark Ovensunderstands the following character escapes: 1365501e74b7SMark Ovens.Bl -tag -width Ds 1366501e74b7SMark Ovens.It \ea 1367501e74b7SMark OvensAlert (ring the terminal bell) 1368501e74b7SMark Ovens.It \eb 1369501e74b7SMark OvensBackspace 1370501e74b7SMark Ovens.It \ec 1371501e74b7SMark OvensSuppress the trailing newline (this has the side-effect of truncating the 1372501e74b7SMark Ovensline if it is not the last character) 1373501e74b7SMark Ovens.It \ee 1374501e74b7SMark OvensThe ESC character (ASCII 0x1b) 1375501e74b7SMark Ovens.It \ef 1376501e74b7SMark OvensFormfeed 1377501e74b7SMark Ovens.It \en 1378501e74b7SMark OvensNewline 1379501e74b7SMark Ovens.It \er 1380501e74b7SMark OvensCarriage return 1381501e74b7SMark Ovens.It \et 1382501e74b7SMark OvensHorizontal tab 1383501e74b7SMark Ovens.It \ev 1384501e74b7SMark OvensVertical tab 1385501e74b7SMark Ovens.It \e\e 1386501e74b7SMark OvensLiteral backslash 1387501e74b7SMark Ovens.It \e0nnn 1388501e74b7SMark Ovens(Zero) The character whose octal value is nnn 1389501e74b7SMark Ovens.El 1390501e74b7SMark Ovens.Pp 1391501e74b7SMark OvensIf 1392501e74b7SMark Ovens.Ar string 1393501e74b7SMark Ovensis not enclosed in quotes then the backslash itself must be escaped 1394501e74b7SMark Ovenswith a backslash to protect it from the shell. For example 1395501e74b7SMark Ovens.Bd -literal -offset indent 1396501e74b7SMark Ovens$ echo -e "a\evb" 1397501e74b7SMark Ovensa 1398501e74b7SMark Ovens b 1399501e74b7SMark Ovens$ echo -e a\e\evb 1400501e74b7SMark Ovensa 1401501e74b7SMark Ovens b 1402501e74b7SMark Ovens$ echo -e "a\e\eb" 1403501e74b7SMark Ovensa\eb 1404501e74b7SMark Ovens$ echo -e a\e\e\e\eb 1405501e74b7SMark Ovensa\eb 1406501e74b7SMark Ovens.Ed 1407501e74b7SMark Ovens.El 140849e11e3aSSheldon Hearn.It Ic eval Ar string ... 1409e6d3cf26SSheldon HearnConcatenate all the arguments with spaces. 1410e6d3cf26SSheldon HearnThen re-parse and execute the command. 141149e11e3aSSheldon Hearn.It Ic exec Op Ar command Op arg ... 141249e11e3aSSheldon HearnUnless 141349e11e3aSSheldon Hearn.Ar command 141449e11e3aSSheldon Hearnis omitted, 141549e11e3aSSheldon Hearnthe shell process is replaced with the specified program 141649e11e3aSSheldon Hearn(which must be a real program, not a shell builtin command or function). 141749e11e3aSSheldon HearnAny redirections on the 141849e11e3aSSheldon Hearn.Ic exec 141949e11e3aSSheldon Hearncommand are marked as permanent, 142049e11e3aSSheldon Hearnso that they are not undone when the 142149e11e3aSSheldon Hearn.Ic exec 142249e11e3aSSheldon Hearncommand finishes. 142349e11e3aSSheldon Hearn.It Ic exit Op Ar exitstatus 142449e11e3aSSheldon HearnTerminate the shell process. 142549e11e3aSSheldon HearnIf 142649e11e3aSSheldon Hearn.Ar exitstatus 142749e11e3aSSheldon Hearnis given 142849e11e3aSSheldon Hearnit is used as the exit status of the shell; 142949e11e3aSSheldon Hearnotherwise the exit status of the preceding command is used. 143049e11e3aSSheldon Hearn.It Ic export Ar name ... 14314b88c807SRodney W. GrimesThe specified names are exported so that they will 14324b88c807SRodney W. Grimesappear in the environment of subsequent commands. 143349e11e3aSSheldon HearnThe only way to un-export a variable is to 143449e11e3aSSheldon Hearn.Ic unset 143549e11e3aSSheldon Hearnit. 143649e11e3aSSheldon HearnThe shell allows the value of a variable to be set 143749e11e3aSSheldon Hearnat the same time as it is exported by writing 1438ab72124fSSteve Price.Bd -literal -offset indent 14394b88c807SRodney W. Grimesexport name=value 1440ab72124fSSteve Price.Ed 1441ab72124fSSteve Price.Pp 14424b88c807SRodney W. GrimesWith no arguments the export command lists the names 14434b88c807SRodney W. Grimesof all exported variables. 144449e11e3aSSheldon Hearn.It Xo 144549e11e3aSSheldon Hearn.Ic fc 144649e11e3aSSheldon Hearn.Op Fl e Ar editor 144749e11e3aSSheldon Hearn.Op Ar first Op Ar last 144849e11e3aSSheldon Hearn.Xc 144949e11e3aSSheldon Hearn.It Xo 145049e11e3aSSheldon Hearn.Ic fc 145149e11e3aSSheldon Hearn.Fl l 145249e11e3aSSheldon Hearn.Op Fl nr 145349e11e3aSSheldon Hearn.Op Ar first Op Ar last 145449e11e3aSSheldon Hearn.Xc 145549e11e3aSSheldon Hearn.It Xo 145649e11e3aSSheldon Hearn.Ic fc 145749e11e3aSSheldon Hearn.Fl s 145849e11e3aSSheldon Hearn.Op Ar old=new 145949e11e3aSSheldon Hearn.Op Ar first 146049e11e3aSSheldon Hearn.Xc 146149e11e3aSSheldon HearnThe 146249e11e3aSSheldon Hearn.Ic fc 146349e11e3aSSheldon Hearnbuiltin command lists, or edits and re-executes, 146449e11e3aSSheldon Hearncommands previously entered to an interactive shell. 1465ab72124fSSteve Price.Bl -tag -width Ds 146649e11e3aSSheldon Hearn.It Fl e Ar editor 146749e11e3aSSheldon HearnUse the editor named by 146849e11e3aSSheldon Hearn.Ar editor 146949e11e3aSSheldon Hearnto edit the commands. 147049e11e3aSSheldon HearnThe editor string is a command name, 147149e11e3aSSheldon Hearnsubject to search via the 147249e11e3aSSheldon Hearn.Ev PATH 147349e11e3aSSheldon Hearnvariable. 147449e11e3aSSheldon HearnThe value in the 147549e11e3aSSheldon Hearn.Ev FCEDIT 147649e11e3aSSheldon Hearnvariable is used as a default when 147749e11e3aSSheldon Hearn.Fl e 147849e11e3aSSheldon Hearnis not specified. 147949e11e3aSSheldon HearnIf 148049e11e3aSSheldon Hearn.Ev FCEDIT 148149e11e3aSSheldon Hearnis null or unset, the value of the 148249e11e3aSSheldon Hearn.Ev EDITOR 148349e11e3aSSheldon Hearnvariable is used. 148449e11e3aSSheldon HearnIf 148549e11e3aSSheldon Hearn.Ev EDITOR 148649e11e3aSSheldon Hearnis null or unset, 1487ab72124fSSteve Price.Xr ed 1 1488ab72124fSSteve Priceis used as the editor. 148949e11e3aSSheldon Hearn.It Fl l No (ell) 14904b88c807SRodney W. GrimesList the commands rather than invoking 14914b88c807SRodney W. Grimesan editor on them. The commands are written in the 14924b88c807SRodney W. Grimessequence indicated by the first and last operands, as 149349e11e3aSSheldon Hearnaffected by 149449e11e3aSSheldon Hearn.Fl r , 149549e11e3aSSheldon Hearnwith each command preceded by the command number. 149649e11e3aSSheldon Hearn.It Fl n 149749e11e3aSSheldon HearnSuppress command numbers when listing with 149849e11e3aSSheldon Hearn.Fl l . 149949e11e3aSSheldon Hearn.It Fl r 150049e11e3aSSheldon HearnReverse the order of the commands listed 150149e11e3aSSheldon Hearn(with 150249e11e3aSSheldon Hearn.Fl l ) 150349e11e3aSSheldon Hearnor edited 150449e11e3aSSheldon Hearn(with neither 150549e11e3aSSheldon Hearn.Fl l 150649e11e3aSSheldon Hearnnor 150749e11e3aSSheldon Hearn.Fl s ) . 150849e11e3aSSheldon Hearn.It Fl s 15094b88c807SRodney W. GrimesRe-execute the command without invoking an editor. 151049e11e3aSSheldon Hearn.It Ar first 151149e11e3aSSheldon Hearn.It Ar last 151249e11e3aSSheldon HearnSelect the commands to list or edit. 151349e11e3aSSheldon HearnThe number of previous commands that can be accessed 151449e11e3aSSheldon Hearnare determined by the value of the 151549e11e3aSSheldon Hearn.Ev HISTSIZE variable. 151649e11e3aSSheldon HearnThe value of 151749e11e3aSSheldon Hearn.Ar first 151849e11e3aSSheldon Hearnor 151949e11e3aSSheldon Hearn.Ar last 152049e11e3aSSheldon Hearnor both are one of the following: 1521ab72124fSSteve Price.Bl -tag -width Ds 152249e11e3aSSheldon Hearn.It Ar [+]num 152349e11e3aSSheldon HearnA positive number representing a command number; 152449e11e3aSSheldon Hearncommand numbers can be displayed with the 152549e11e3aSSheldon Hearn.Fl l 152649e11e3aSSheldon Hearnoption. 152749e11e3aSSheldon Hearn.It Ar -num 152849e11e3aSSheldon HearnA negative decimal number representing the 152949e11e3aSSheldon Hearncommand that was executed 153049e11e3aSSheldon Hearn.Ar num 153149e11e3aSSheldon Hearnof 153249e11e3aSSheldon Hearncommands previously. 153349e11e3aSSheldon HearnFor example, -1 is the immediately previous command. 153449e11e3aSSheldon Hearn.It Ar string 153549e11e3aSSheldon HearnA string indicating the most recently entered command 153649e11e3aSSheldon Hearnthat begins with that string. 153749e11e3aSSheldon HearnIf the 153849e11e3aSSheldon Hearn.Ar old=new 153949e11e3aSSheldon Hearnoperand is not also specified with 154049e11e3aSSheldon Hearn.Fl s , 154149e11e3aSSheldon Hearnthe string form of the first operand cannot contain an embedded equal sign. 154249e11e3aSSheldon Hearn.El 154349e11e3aSSheldon Hearn.El 154449e11e3aSSheldon Hearn.Pp 154549e11e3aSSheldon HearnThe following environment variables affect the execution of 154649e11e3aSSheldon Hearn.Ic fc : 154749e11e3aSSheldon Hearn.Bl -tag -width Ds 154849e11e3aSSheldon Hearn.It Ev FCEDIT 15494b88c807SRodney W. GrimesName of the editor to use. 155049e11e3aSSheldon Hearn.It Ev HISTSIZE 15513d7b5b93SPhilippe CharnierThe number of previous commands that are accessible. 1552ab72124fSSteve Price.El 155349e11e3aSSheldon Hearn.It Ic fg Op Ar job 155449e11e3aSSheldon HearnMove the specified 155549e11e3aSSheldon Hearn.Ar job 155649e11e3aSSheldon Hearnor the current job to the foreground. 155749e11e3aSSheldon Hearn.It Ic getopts Ar optstring Ar var 155849e11e3aSSheldon HearnThe POSIX 155949e11e3aSSheldon Hearn.Ic getopts 156049e11e3aSSheldon Hearncommand. 156149e11e3aSSheldon HearnThe 156249e11e3aSSheldon Hearn.Ic getopts 156349e11e3aSSheldon Hearncommand deprecates the older 156449e11e3aSSheldon Hearn.Xr getopt 1 156549e11e3aSSheldon Hearncommand. 1566ab0a2172SSteve PriceThe first argument should be a series of letters, each possibly 1567ab0a2172SSteve Pricefollowed by a colon which indicates that the option takes an argument. 1568ab0a2172SSteve PriceThe specified variable is set to the parsed option. The index of 156949e11e3aSSheldon Hearnthe next argument is placed into the shell variable 157049e11e3aSSheldon Hearn.Ev OPTIND . 1571ab0a2172SSteve PriceIf an option takes an argument, it is placed into the shell variable 157249e11e3aSSheldon Hearn.Ev OPTARG . 157349e11e3aSSheldon HearnIf an invalid option is encountered, 157449e11e3aSSheldon Hearn.Ev var 157549e11e3aSSheldon Hearnis set to 157649e11e3aSSheldon Hearn.Dq Li ? . 1577ab0a2172SSteve PriceIt returns a false value (1) when it encounters the end of the options. 157849e11e3aSSheldon Hearn.It Xo 157949e11e3aSSheldon Hearn.Ic hash 158049e11e3aSSheldon Hearn.Op Fl rv 158149e11e3aSSheldon Hearn.Op Ar command ... 158249e11e3aSSheldon Hearn.Xc 158349e11e3aSSheldon HearnThe shell maintains a hash table which remembers the locations of commands. 158449e11e3aSSheldon HearnWith no arguments whatsoever, the 158549e11e3aSSheldon Hearn.Ic hash 158649e11e3aSSheldon Hearncommand prints out the contents of this table. 158749e11e3aSSheldon HearnEntries which have not been looked at since the last 158849e11e3aSSheldon Hearn.Ic cd 158949e11e3aSSheldon Hearncommand are marked with an asterisk; 159049e11e3aSSheldon Hearnit is possible for these entries to be invalid. 1591ab72124fSSteve Price.Pp 159249e11e3aSSheldon HearnWith arguments, the 159349e11e3aSSheldon Hearn.Ic hash 159449e11e3aSSheldon Hearncommand removes each specified 159549e11e3aSSheldon Hearn.Ar command 159649e11e3aSSheldon Hearnfrom the hash table (unless they are functions) and then locates it. 159749e11e3aSSheldon HearnWith the 159849e11e3aSSheldon Hearn.Fl v 159949e11e3aSSheldon Hearnoption, 160049e11e3aSSheldon Hearn.Ic hash 160149e11e3aSSheldon Hearnprints the locations of the commands as it finds them. 160249e11e3aSSheldon HearnThe 160349e11e3aSSheldon Hearn.Fl r 160449e11e3aSSheldon Hearnoption causes the 160549e11e3aSSheldon Hearn.Ic hash 160649e11e3aSSheldon Hearncommand to delete all the entries in the hash table except for functions. 160749e11e3aSSheldon Hearn.It Ic jobid Op Ar job 160849e11e3aSSheldon HearnPrint the process id's of the processes in the specified 160949e11e3aSSheldon Hearn.Ar job . 161049e11e3aSSheldon HearnIf the 161149e11e3aSSheldon Hearn.Ar job 161249e11e3aSSheldon Hearnargument is omitted, use the current job. 161349e11e3aSSheldon Hearn.It Ic jobs 16144b88c807SRodney W. GrimesThis command lists out all the background processes 16154b88c807SRodney W. Grimeswhich are children of the current shell process. 161649e11e3aSSheldon Hearn.It Ic pwd 161749e11e3aSSheldon HearnPrint the path of the current directory. The builtin command may 16184b88c807SRodney W. Grimesdiffer from the program of the same name because the 16194b88c807SRodney W. Grimesbuiltin command remembers what the current directory 16204b88c807SRodney W. Grimesis rather than recomputing it each time. This makes 16214b88c807SRodney W. Grimesit faster. However, if the current directory is 162249e11e3aSSheldon Hearnrenamed, 162349e11e3aSSheldon Hearnthe builtin version of 162449e11e3aSSheldon Hearn.Xr pwd 1 162549e11e3aSSheldon Hearnwill continue to print the old name for the directory. 162649e11e3aSSheldon Hearn.It Xo 162749e11e3aSSheldon Hearn.Ic read 162849e11e3aSSheldon Hearn.Op Fl p Ar prompt 162949e11e3aSSheldon Hearn.Op Fl t Ar timeout 163049e11e3aSSheldon Hearn.Op Fl er 163149e11e3aSSheldon Hearn.Ar variable ... 163249e11e3aSSheldon Hearn.Xc 163349e11e3aSSheldon HearnThe 163449e11e3aSSheldon Hearn.Ar prompt 163549e11e3aSSheldon Hearnis printed if the 163649e11e3aSSheldon Hearn.Fl p 163749e11e3aSSheldon Hearnoption is specified 16384b88c807SRodney W. Grimesand the standard input is a terminal. Then a line is 16394b88c807SRodney W. Grimesread from the standard input. The trailing newline 16404b88c807SRodney W. Grimesis deleted from the line and the line is split as 164149e11e3aSSheldon Hearndescribed in the section on 164249e11e3aSSheldon Hearn.Sx White Space Splitting (Field Splitting) 164349e11e3aSSheldon Hearnabove, and 16444b88c807SRodney W. Grimesthe pieces are assigned to the variables in order. 1645f434d1dfSAdam DavidIf there are more pieces than variables, the remaining 164649e11e3aSSheldon Hearnpieces (along with the characters in 164749e11e3aSSheldon Hearn.Ev IFS 164849e11e3aSSheldon Hearnthat separated them) 164949e11e3aSSheldon Hearnare assigned to the last variable. 1650f434d1dfSAdam DavidIf there are more variables than pieces, the remaining 1651f434d1dfSAdam Davidvariables are assigned the null string. 1652ab72124fSSteve Price.Pp 165349e11e3aSSheldon HearnBackslashes are treated specially, unless the 165449e11e3aSSheldon Hearn.Fl r 165549e11e3aSSheldon Hearnoption is 16568f0561ccSThomas Gellekumspecified. If a backslash is followed by 16578f0561ccSThomas Gellekuma newline, the backslash and the newline will be 16588f0561ccSThomas Gellekumdeleted. If a backslash is followed by any other 16598f0561ccSThomas Gellekumcharacter, the backslash will be deleted and the following 166049e11e3aSSheldon Hearncharacter will be treated as though it were not in 166149e11e3aSSheldon Hearn.Ev IFS , 166249e11e3aSSheldon Hearneven if it is. 16638f0561ccSThomas Gellekum.Pp 166449e11e3aSSheldon HearnIf the 166549e11e3aSSheldon Hearn.Fl t 166649e11e3aSSheldon Hearnoption is specified and the 166749e11e3aSSheldon Hearn.Ar timeout 166849e11e3aSSheldon Hearnelapses before any input is supplied, 166949e11e3aSSheldon Hearnthe 167049e11e3aSSheldon Hearn.Ic read 167149e11e3aSSheldon Hearncommand will return without assigning any values. 167249e11e3aSSheldon HearnThe 167349e11e3aSSheldon Hearn.Ar timeout 167449e11e3aSSheldon Hearnvalue may optionally be followed by one of 167549e11e3aSSheldon Hearn.Dq s , 167649e11e3aSSheldon Hearn.Dq m 167749e11e3aSSheldon Hearnor 167849e11e3aSSheldon Hearn.Dq h 167949e11e3aSSheldon Hearnto explicitly specify seconds, minutes or or hours. 168049e11e3aSSheldon HearnIf none is supplied, 168149e11e3aSSheldon Hearn.Dq s 168249e11e3aSSheldon Hearnis assumed. 1683afa53c8dSMike Smith.Pp 168449e11e3aSSheldon HearnThe 168549e11e3aSSheldon Hearn.Fl e 168649e11e3aSSheldon Hearnoption exists only for backward compatibility with older scripts. 168749e11e3aSSheldon Hearn.It Ic readonly Ar name ... 168849e11e3aSSheldon HearnEach specified 168949e11e3aSSheldon Hearn.Ar name 169049e11e3aSSheldon Hearnis marked as read only, 169149e11e3aSSheldon Hearnso that it cannot be subsequently modified or unset. 169249e11e3aSSheldon HearnThe shell allows the value of a variable to be set 169349e11e3aSSheldon Hearnat the same time as it is marked read only 169449e11e3aSSheldon Hearnby using the following form: 1695ab72124fSSteve Price.Bd -literal -offset indent 16964b88c807SRodney W. Grimesreadonly name=value 1697ab72124fSSteve Price.Ed 1698ab72124fSSteve Price.Pp 169949e11e3aSSheldon HearnWith no arguments the 170049e11e3aSSheldon Hearn.Ic readonly 170149e11e3aSSheldon Hearncommand lists the names of all read only variables. 170249e11e3aSSheldon Hearn.It Xo 170349e11e3aSSheldon Hearn.Ic set 170449e11e3aSSheldon Hearn.Op Fl /+abCEefIimnpTuVvx 170549e11e3aSSheldon Hearn.Op Fl /+o Ar longname 170649e11e3aSSheldon Hearn.Op Fl c Ar string 170749e11e3aSSheldon Hearn.Op Fl - Ar arg ... 170849e11e3aSSheldon Hearn.Xc 1709e918fc8fSSheldon HearnThe 1710e918fc8fSSheldon Hearn.Ic set 1711e918fc8fSSheldon Hearncommand performs three different functions: 1712ab72124fSSteve Price.Bl -item 1713ab72124fSSteve Price.It 171449e11e3aSSheldon HearnWith no arguments, it lists the values of all shell variables. 1715ab72124fSSteve Price.It 171649e11e3aSSheldon HearnIf options are given, 171749e11e3aSSheldon Hearneither in short form or using the long 171849e11e3aSSheldon Hearn.Dq Fl /+o Ar longname 171949e11e3aSSheldon Hearnform, 172049e11e3aSSheldon Hearnit sets or clears the specified options as described in the section called 17216b15476eSSheldon Hearn.Sx Argument List Processing . 1722ab72124fSSteve Price.It 172349e11e3aSSheldon HearnIf the 172449e11e3aSSheldon Hearn.Dq Fl - 172549e11e3aSSheldon Hearnoption is specified, 172649e11e3aSSheldon Hearn.Ic set 172749e11e3aSSheldon Hearnwill replace the shell's positional parameters with the subsequent 172849e11e3aSSheldon Hearnarguments. 172949e11e3aSSheldon HearnIf no arguments follow the 173049e11e3aSSheldon Hearn.Dq Fl - 173149e11e3aSSheldon Hearnoption, 173249e11e3aSSheldon Hearnall the positional parameters will be cleared, 173349e11e3aSSheldon Hearnwhich is equivalent to executing the command 173449e11e3aSSheldon Hearn.Dq Li shift $# . 173549e11e3aSSheldon HearnThe 173649e11e3aSSheldon Hearn.Dq Fl - 173749e11e3aSSheldon Hearnflag may be ommitted when specifying arguments to be used 173849e11e3aSSheldon Hearnas positional replacement parameters. 173949e11e3aSSheldon HearnThis is not recommended, 174049e11e3aSSheldon Hearnbecause the first argument may begin with a dash 174149e11e3aSSheldon Hearn.Pq Li - 174249e11e3aSSheldon Hearnor a plus 174349e11e3aSSheldon Hearn.Pq Li + , 174449e11e3aSSheldon Hearnwhich the 174549e11e3aSSheldon Hearn.Ic set 174649e11e3aSSheldon Hearncommand will interpret as a request to enable or disable options. 1747ab72124fSSteve Price.El 174849e11e3aSSheldon Hearn.It Ic setvar Ar variable Ar value 174949e11e3aSSheldon HearnAssigns the specified 175049e11e3aSSheldon Hearn.Ar value 175149e11e3aSSheldon Hearnto the specified 175249e11e3aSSheldon Hearn.Ar variable . 175349e11e3aSSheldon Hearn.Ic Setvar 175449e11e3aSSheldon Hearnis intended to be used in functions that 1755e6d3cf26SSheldon Hearnassign values to variables whose names are passed as parameters. 175649e11e3aSSheldon HearnIn general it is better to write 175749e11e3aSSheldon Hearn.Bd -literal -offset indent 175849e11e3aSSheldon Hearnvariable=value 175949e11e3aSSheldon Hearn.Ed 176049e11e3aSSheldon Hearnrather than using 176149e11e3aSSheldon Hearn.Ic setvar . 176249e11e3aSSheldon Hearn.It Ic shift Op Ar n 176349e11e3aSSheldon HearnShift the positional parameters 176449e11e3aSSheldon Hearn.Ar n 1765e918fc8fSSheldon Hearntimes, or once if 176649e11e3aSSheldon Hearn.Ar n 176749e11e3aSSheldon Hearnis not specified. 176849e11e3aSSheldon HearnA shift sets the value of $1 to the value of $2, 176949e11e3aSSheldon Hearnthe value of $2 to the value of $3, and so on, 177049e11e3aSSheldon Hearndecreasing the value of $# by one. 177149e11e3aSSheldon HearnIf there are zero positional parameters, shifting does not do anything. 177249e11e3aSSheldon Hearn.It Xo 177349e11e3aSSheldon Hearn.Ic trap 177449e11e3aSSheldon Hearn.Op Ar action 177549e11e3aSSheldon Hearn.Ar signal ... 177649e11e3aSSheldon Hearn.Xc 177749e11e3aSSheldon HearnCause the shell to parse and execute 177849e11e3aSSheldon Hearn.Ar action 177949e11e3aSSheldon Hearnwhen any specified 178049e11e3aSSheldon Hearn.Ar signal 178149e11e3aSSheldon Hearnis received. 178249e11e3aSSheldon HearnThe signals are specified by signal number. 178349e11e3aSSheldon HearnThe 178449e11e3aSSheldon Hearn.Ar action 178549e11e3aSSheldon Hearnmay be null or omitted; 178649e11e3aSSheldon Hearnthe former causes the specified signal to be ignored 178749e11e3aSSheldon Hearnand the latter causes the default action to be taken. 178849e11e3aSSheldon HearnWhen the shell forks off a subshell, 178949e11e3aSSheldon Hearnit resets trapped (but not ignored) signals to the default action. 179049e11e3aSSheldon HearnThe 179149e11e3aSSheldon Hearn.Ic trap 179249e11e3aSSheldon Hearncommand has no effect on signals that were ignored on entry to the shell. 179349e11e3aSSheldon Hearn.It Ic type Op Ar name ... 179449e11e3aSSheldon HearnInterpret each 179549e11e3aSSheldon Hearn.Ar name 179649e11e3aSSheldon Hearnas a command and print the resolution of the command search. 1797e6d3cf26SSheldon HearnPossible resolutions are: 1798c644db6aSSheldon Hearnshell keyword, alias, shell builtin command, command, tracked alias 179949e11e3aSSheldon Hearnand not found. 180049e11e3aSSheldon HearnFor aliases the alias expansion is printed; 180149e11e3aSSheldon Hearnfor commands and tracked aliases 180249e11e3aSSheldon Hearnthe complete pathname of the command is printed. 180349e11e3aSSheldon Hearn.It Xo 180449e11e3aSSheldon Hearn.Ic ulimit 18053652a236SMartin Cracauer.Op Fl HSabcdflmnust 180649e11e3aSSheldon Hearn.Op Ar limit 180749e11e3aSSheldon Hearn.Xc 1808ab72124fSSteve PriceSet or display resource limits (see 1809ab72124fSSteve Price.Xr getrlimit 2 ). 18106b15476eSSheldon HearnIf 181149e11e3aSSheldon Hearn.Ar limit 18126b15476eSSheldon Hearnis specified, the named resource will be set; 18137a2afe64SJoerg Wunschotherwise the current resource value will be displayed. 1814ab72124fSSteve Price.Pp 18156b15476eSSheldon HearnIf 181649e11e3aSSheldon Hearn.Fl H 181749e11e3aSSheldon Hearnis specified, the hard limits will be set or displayed. 181849e11e3aSSheldon HearnWhile everybody is allowed to reduce a hard limit, 181949e11e3aSSheldon Hearnonly the superuser can increase it. 182049e11e3aSSheldon HearnThe 182149e11e3aSSheldon Hearn.Fl S 182249e11e3aSSheldon Hearnoption 18237a2afe64SJoerg Wunschspecifies the soft limits instead. When displaying limits, 18246b15476eSSheldon Hearnonly one of 182549e11e3aSSheldon Hearn.Fl S 18266b15476eSSheldon Hearnor 182749e11e3aSSheldon Hearn.Fl H 182849e11e3aSSheldon Hearncan be given. 182949e11e3aSSheldon HearnThe default is to display the soft limits, 183049e11e3aSSheldon Hearnand to set both the hard and the soft limits. 1831ab72124fSSteve Price.Pp 18326b15476eSSheldon HearnOption 183349e11e3aSSheldon Hearn.Fl a 183449e11e3aSSheldon Hearncauses the 183549e11e3aSSheldon Hearn.Ic ulimit 183649e11e3aSSheldon Hearncommand to display all resources. 183749e11e3aSSheldon HearnThe parameter 183849e11e3aSSheldon Hearn.Ar limit 18396b15476eSSheldon Hearnis not acceptable in this mode. 1840ab72124fSSteve Price.Pp 18417a2afe64SJoerg WunschThe remaining options specify which resource value is to be 184249e11e3aSSheldon Hearndisplayed or modified. 184349e11e3aSSheldon HearnThey are mutually exclusive. 1844ab72124fSSteve Price.Bl -tag -width Ds 18453652a236SMartin Cracauer.It Fl b Ar sbsize 18463652a236SMartin CracauerThe maximum size of socket buffer usage, in bytes. 184749e11e3aSSheldon Hearn.It Fl c Ar coredumpsize 18487a2afe64SJoerg WunschThe maximal size of core dump files, in 512-byte blocks. 184949e11e3aSSheldon Hearn.It Fl d Ar datasize 18507a2afe64SJoerg WunschThe maximal size of the data segment of a process, in kilobytes. 185149e11e3aSSheldon Hearn.It Fl f Ar filesize 185249e11e3aSSheldon HearnThe maximal size of a file, in 512-byte blocks. 185349e11e3aSSheldon Hearn.It Fl l Ar lockedmem 18547a2afe64SJoerg WunschThe maximal size of memory that can be locked by a process, in 18557a2afe64SJoerg Wunschkilobytes. 185649e11e3aSSheldon Hearn.It Fl m Ar memoryuse 18577a2afe64SJoerg WunschThe maximal resident set size of a process, in kilobytes. 185849e11e3aSSheldon Hearn.It Fl n Ar nofiles 18597a2afe64SJoerg WunschThe maximal number of descriptors that could be opened by a process. 186049e11e3aSSheldon Hearn.It Fl s Ar stacksize 18617a2afe64SJoerg WunschThe maximal size of the stack segment, in kilobytes. 186249e11e3aSSheldon Hearn.It Fl t Ar time 18637a2afe64SJoerg WunschThe maximal amount of CPU time to be used by each process, in seconds. 186449e11e3aSSheldon Hearn.It Fl u Ar userproc 1865ab72124fSSteve PriceThe maximal number of simultaneous processes for this user ID. 1866ab72124fSSteve Price.El 186749e11e3aSSheldon Hearn.It Ic umask Op Ar mask 186849e11e3aSSheldon HearnSet the file creation mask (see 1869ab72124fSSteve Price.Xr umask 2 ) 187049e11e3aSSheldon Hearnto the octal value specified by 187149e11e3aSSheldon Hearn.Ar mask 187249e11e3aSSheldon HearnIf the argument is omitted, the current mask value is printed. 187349e11e3aSSheldon Hearn.It Xo 187449e11e3aSSheldon Hearn.Ic unalias 187549e11e3aSSheldon Hearn.Op Fl a 187649e11e3aSSheldon Hearn.Op Ar name 187749e11e3aSSheldon Hearn.Xc 18786b15476eSSheldon HearnIf 187949e11e3aSSheldon Hearn.Ar name 18806b15476eSSheldon Hearnis specified, the shell removes that alias. 18816b15476eSSheldon HearnIf 188249e11e3aSSheldon Hearn.Ar a 18836b15476eSSheldon Hearnis specified, all aliases are removed. 188449e11e3aSSheldon Hearn.It Ic unset Ar name ... 188549e11e3aSSheldon HearnThe specified variables and functions are unset and unexported. 188649e11e3aSSheldon HearnIf a given 188749e11e3aSSheldon Hearn.Ar name 188849e11e3aSSheldon Hearncorresponds to both a variable and a function, 1889e6d3cf26SSheldon Hearnboth the variable and the function are unset. 189049e11e3aSSheldon Hearn.It Ic wait Op Ar job 189149e11e3aSSheldon HearnWait for the specified 189249e11e3aSSheldon Hearn.Ar job 189349e11e3aSSheldon Hearnto complete and return the exit status of the last process in the 189449e11e3aSSheldon Hearn.Ar job . 1895e6d3cf26SSheldon HearnIf the argument is omitted, wait for all jobs to complete 1896e6d3cf26SSheldon Hearnand return an exit status of zero. 1897ab72124fSSteve Price.El 1898ab72124fSSteve Price.Ss Commandline Editing 1899ab72124fSSteve PriceWhen 1900ab72124fSSteve Price.Nm 1901ab72124fSSteve Priceis being used interactively from a terminal, the current command 190249e11e3aSSheldon Hearnand the command history 190349e11e3aSSheldon Hearn(see 190449e11e3aSSheldon Hearn.Ic fc 190549e11e3aSSheldon Hearnin 1906c644db6aSSheldon Hearn.Sx Builtin Commands ) 190749e11e3aSSheldon Hearncan be edited using vi-mode command line editing. 190849e11e3aSSheldon HearnThis mode uses commands similar 19094b88c807SRodney W. Grimesto a subset of those described in the vi man page. 191049e11e3aSSheldon HearnThe command 191149e11e3aSSheldon Hearn.Dq Li set -o vi 191249e11e3aSSheldon Hearn(or 1913e918fc8fSSheldon Hearn.Dq Li set -V ) 191449e11e3aSSheldon Hearnenables vi-mode editing and places 1915ab72124fSSteve Price.Nm 1916ab72124fSSteve Priceinto vi insert mode. With vi-mode enabled, 1917ab72124fSSteve Price.Nm 191849e11e3aSSheldon Hearncan be switched between insert mode and command mode by typing 191949e11e3aSSheldon Hearn.Aq ESC . 192049e11e3aSSheldon HearnHitting 192149e11e3aSSheldon Hearn.Aq return 192249e11e3aSSheldon Hearnwhile in command mode will pass the line to the shell. 1923ab72124fSSteve Price.Pp 192449e11e3aSSheldon HearnSimilarly, the 192549e11e3aSSheldon Hearn.Dq Li set -o emacs 192649e11e3aSSheldon Hearn(or 1927e918fc8fSSheldon Hearn.Dq Li set -E ) 192849e11e3aSSheldon Hearncommand can be used to enable a subset of 1929ab72124fSSteve Priceemacs-style command line editing features. 1930c83e7c50SJoseph Koshy.Sh SEE ALSO 1931c644db6aSSheldon Hearn.Xr builtin 1 , 1932c644db6aSSheldon Hearn.Xr echo 1 , 1933c83e7c50SJoseph Koshy.Xr expr 1 , 1934c644db6aSSheldon Hearn.Xr pwd 1 , 1935c644db6aSSheldon Hearn.Xr printf 1 , 1936c83e7c50SJoseph Koshy.Xr test 1 1937ab72124fSSteve Price.Sh HISTORY 1938b8923d4cSWolfram SchneiderA 1939ab72124fSSteve Price.Nm 1940b8923d4cSWolfram Schneidercommand appeared in 1941ab72124fSSteve Price.At V.1 . 1942