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 40caa2db3bSRuslan Ermilov.Os 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 4981e9cda2SRuslan Ermilov.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. 18881e9cda2SRuslan Ermilov.Bl -tag -width indent 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. 194c4d9468eSRuslan Ermilov(UNIMPLEMENTED) 195ab72124fSSteve Price.It Fl C Li noclobber 196c644db6aSSheldon HearnDo not overwrite existing files with 19749e11e3aSSheldon Hearn.Dq Li > . 198ab72124fSSteve Price.It Fl E Li emacs 199c644db6aSSheldon HearnEnable the builtin 200ab72124fSSteve Price.Xr emacs 1 20149e11e3aSSheldon Hearncommand line editor (disables the 202ab72124fSSteve Price.Fl V 20349e11e3aSSheldon Hearnoption if it has been set). 204ab72124fSSteve Price.It Fl e Li errexit 20549e11e3aSSheldon HearnExit immediately if any untested command fails in non-interactive mode. 2064b88c807SRodney W. GrimesThe exit status of a command is considered to be 2074b88c807SRodney W. Grimesexplicitly tested if the command is used to control 2084b88c807SRodney W. Grimesan if, elif, while, or until; or if the command is the left 209ab72124fSSteve Pricehand operand of an 21049e11e3aSSheldon Hearn.Dq Li && 211ab72124fSSteve Priceor 21249e11e3aSSheldon Hearn.Dq Li || 213ab72124fSSteve Priceoperator. 214ab72124fSSteve Price.It Fl f Li noglob 2154b88c807SRodney W. GrimesDisable pathname expansion. 216ab72124fSSteve Price.It Fl I Li ignoreeof 21749e11e3aSSheldon HearnIgnore 21849e11e3aSSheldon Hearn.Dv EOF Ns ' Ns s 21949e11e3aSSheldon Hearnfrom input when in interactive mode. 220ab72124fSSteve Price.It Fl i Li interactive 221ab72124fSSteve PriceForce the shell to behave interactively. 222ab72124fSSteve Price.It Fl m Li monitor 223ab72124fSSteve PriceTurn on job control (set automatically when interactive). 224ab72124fSSteve Price.It Fl n Li noexec 2254b88c807SRodney W. GrimesIf not interactive, read commands but do not 2264b88c807SRodney W. Grimesexecute them. This is useful for checking the 2274b88c807SRodney W. Grimessyntax of shell scripts. 228ab72124fSSteve Price.It Fl p Li privileged 229621a31c6SSteve PriceTurn on privileged mode. This mode is enabled on startup 230621a31c6SSteve Priceif either the effective user or group id is not equal to the 231621a31c6SSteve Pricereal user or group id. Turning this mode off sets the 232621a31c6SSteve Priceeffective user and group ids to the real user and group ids. 23349e11e3aSSheldon HearnWhen this mode is enabled for interactive shells, the file 234ab72124fSSteve Price.Pa /etc/suid_profile 23549e11e3aSSheldon Hearnis sourced instead of 23649e11e3aSSheldon Hearn.Pa ~/.profile 237ab72124fSSteve Priceafter 238ab72124fSSteve Price.Pa /etc/profile 23949e11e3aSSheldon Hearnis sourced, and the contents of the 240ab72124fSSteve Price.Ev ENV 24149e11e3aSSheldon Hearnvariable are ignored. 242ab72124fSSteve Price.It Fl s Li stdin 2434b88c807SRodney W. GrimesRead commands from standard input (set automatically 2444b88c807SRodney W. Grimesif no file arguments are present). This option has 2454b88c807SRodney W. Grimesno effect when set after the shell has already started 2466b15476eSSheldon Hearnrunning (i.e. when set with the 247c644db6aSSheldon Hearn.Ic set 2486b15476eSSheldon Hearncommand). 249c1c72a3cSMartin Cracauer.It Fl T Li asynctraps 250e6d3cf26SSheldon HearnWhen waiting for a child, execute traps immediately. 251e6d3cf26SSheldon HearnIf this option is not set, 252e6d3cf26SSheldon Hearntraps are executed after the child exits, 253e6d3cf26SSheldon Hearnas specified in 254c1c72a3cSMartin Cracauer.St -p1003.2 2556b15476eSSheldon HearnThis nonstandard option is useful for putting guarding shells around 2566b15476eSSheldon Hearnchildren that block signals. The surrounding shell may kill the child 2576b15476eSSheldon Hearnor it may just return control to the tty and leave the child alone, 2586b15476eSSheldon Hearnlike this: 259c1c72a3cSMartin Cracauer.Bd -literal -offset indent 260c1c72a3cSMartin Cracauersh -T -c "trap 'exit 1' 2 ; some-blocking-program" 261c1c72a3cSMartin Cracauer.Ed 262c1c72a3cSMartin Cracauer.Pp 263ab72124fSSteve Price.It Fl u Li nounset 264ab72124fSSteve PriceWrite a message to standard error when attempting 265ab72124fSSteve Priceto expand a variable that is not set, and if the 266ab72124fSSteve Priceshell is not interactive, exit immediately. 267ab72124fSSteve Price.It Fl V Li vi 268c644db6aSSheldon HearnEnable the builtin 269ab72124fSSteve Price.Xr vi 1 270ab72124fSSteve Pricecommand line editor (disables 271ab72124fSSteve Price.Fl E 272ab72124fSSteve Priceif it has been set). 273ab72124fSSteve Price.It Fl v Li verbose 274ab72124fSSteve PriceThe shell writes its input to standard error 275ab72124fSSteve Priceas it is read. Useful for debugging. 276ab72124fSSteve Price.It Fl x Li xtrace 27749e11e3aSSheldon HearnWrite each command 27849e11e3aSSheldon Hearn(preceded by 27949e11e3aSSheldon Hearn.Dq Li +\ ) 28049e11e3aSSheldon Hearnto standard error before it is executed. 28149e11e3aSSheldon HearnUseful for debugging. 28249e11e3aSSheldon Hearn.El 28349e11e3aSSheldon Hearn.Pp 28449e11e3aSSheldon HearnThe 28549e11e3aSSheldon Hearn.Fl c 28649e11e3aSSheldon Hearnoption may be used to pass its string argument to the shell 28749e11e3aSSheldon Hearnto be interpreted as input. 288c7a197e1SWolfram SchneiderKeep in mind that this option only accepts a single string as its 289c7a197e1SWolfram Schneiderargument, hence multi-word strings must be quoted. 29049e11e3aSSheldon Hearn.Pp 29149e11e3aSSheldon HearnThe 29249e11e3aSSheldon Hearn.Fl /+o 29349e11e3aSSheldon Hearnoption takes as its only argument the long name of an option 29449e11e3aSSheldon Hearnto be enabled or disabled. 29549e11e3aSSheldon HearnFor example, the following two invocations of 29649e11e3aSSheldon Hearn.Nm 29749e11e3aSSheldon Hearnboth enable the builtin 29849e11e3aSSheldon Hearn.Xr emacs 1 29949e11e3aSSheldon Hearncommand line editor: 30049e11e3aSSheldon Hearn.Bd -literal -offset indent 30149e11e3aSSheldon Hearnset -E 30249e11e3aSSheldon Hearnset -o emacs 30349e11e3aSSheldon Hearn.Ed 304d513af6aSTim J. Robbins.Pp 305d513af6aSTim J. RobbinsIf used without an argument, the 306d513af6aSTim J. Robbins.Fl o 307d513af6aSTim J. Robbinsoption displays the current option settings in a human-readable format. 308d513af6aSTim J. RobbinsIf 309d513af6aSTim J. Robbins.Cm +o 310d513af6aSTim J. Robbinsis used without an argument, the current option settings are output 311d513af6aSTim J. Robbinsin a format suitable for re-input into the shell. 312ab72124fSSteve Price.Ss Lexical Structure 3134b88c807SRodney W. GrimesThe shell reads input in terms of lines from a file and breaks 3144b88c807SRodney W. Grimesit up into words at whitespace (blanks and tabs), and at 3154b88c807SRodney W. Grimescertain sequences of 3166b15476eSSheldon Hearncharacters called 3176b15476eSSheldon Hearn.Dq operators , 3186b15476eSSheldon Hearnwhich are special to the shell. 3194b88c807SRodney W. GrimesThere are two types of operators: control operators and 3204b88c807SRodney W. Grimesredirection operators (their meaning is discussed later). 321ab72124fSSteve PriceThe following is a list of valid operators: 32281e9cda2SRuslan Ermilov.Bl -tag -width indent 32349e11e3aSSheldon Hearn.It Control operators: 32449e11e3aSSheldon Hearn.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact 32581e9cda2SRuslan Ermilov.It Li & Ta Li && Ta Li ( Ta Li ) Ta Li \en 32681e9cda2SRuslan Ermilov.It Li ;; Ta Li ; Ta Li | Ta Li || 32749e11e3aSSheldon Hearn.El 32849e11e3aSSheldon Hearn.It Redirection operators: 3294682f420SBrian Somers.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact 33081e9cda2SRuslan Ermilov.It Li < Ta Li > Ta Li << Ta Li >> Ta Li <> 33181e9cda2SRuslan Ermilov.It Li <& Ta Li >& Ta Li <<- Ta Li >| 33249e11e3aSSheldon Hearn.El 333ab72124fSSteve Price.El 334ab72124fSSteve Price.Ss Quoting 3354b88c807SRodney W. GrimesQuoting is used to remove the special meaning of certain characters 3364b88c807SRodney W. Grimesor words to the shell, such as operators, whitespace, or 3374b88c807SRodney W. Grimeskeywords. There are three types of quoting: matched single quotes, 3384b88c807SRodney W. Grimesmatched double quotes, and backslash. 33981e9cda2SRuslan Ermilov.Bl -tag -width indent 340ab72124fSSteve Price.It Single Quotes 3414b88c807SRodney W. GrimesEnclosing characters in single quotes preserves the literal 342ab0a2172SSteve Pricemeaning of all the characters (except single quotes, making 343ab0a2172SSteve Priceit impossible to put single-quotes in a single-quoted string). 344ab72124fSSteve Price.It Double Quotes 3454b88c807SRodney W. GrimesEnclosing characters within double quotes preserves the literal 34649e11e3aSSheldon Hearnmeaning of all characters except dollarsign 34749e11e3aSSheldon Hearn.Pq Li $ , 34849e11e3aSSheldon Hearnbackquote 34949e11e3aSSheldon Hearn.Pq Li ` , 35049e11e3aSSheldon Hearnand backslash 351c4d9468eSRuslan Ermilov.Pq Li \e . 35249e11e3aSSheldon HearnThe backslash inside double quotes is historically weird. 35349e11e3aSSheldon HearnIt remains literal unless it precedes the following characters, 35449e11e3aSSheldon Hearnwhich it serves to quote: 35549e11e3aSSheldon Hearn.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact 35681e9cda2SRuslan Ermilov.It Li $ Ta Li ` Ta Li \&" Ta Li \e\ Ta Li \en 35749e11e3aSSheldon Hearn.El 358ab72124fSSteve Price.It Backslash 359ab72124fSSteve PriceA backslash preserves the literal meaning of the following 36049e11e3aSSheldon Hearncharacter, with the exception of the newline character 36149e11e3aSSheldon Hearn.Pq Li \en . 36249e11e3aSSheldon HearnA backslash preceding a newline is treated as a line continuation. 363ab72124fSSteve Price.El 364ab72124fSSteve Price.Ss Reserved Words 3654b88c807SRodney W. GrimesReserved words are words that have special meaning to the 3664b88c807SRodney W. Grimesshell and are recognized at the beginning of a line and 3674b88c807SRodney W. Grimesafter a control operator. The following are reserved words: 36849e11e3aSSheldon Hearn.Bl -column "doneXX" "elifXX" "elseXX" "untilXX" "whileX" -offset center 36981e9cda2SRuslan Ermilov.It Li \&! Ta { Ta } Ta Ic case Ta Ic do 37081e9cda2SRuslan Ermilov.It Ic done Ta Ic elif Ta Ic else Ta Ic esac Ta Ic fi 37181e9cda2SRuslan Ermilov.It Ic for Ta Ic if Ta Ic then Ta Ic until Ta Ic while 37249e11e3aSSheldon Hearn.El 373ab72124fSSteve Price.Ss Aliases 374ab72124fSSteve PriceAn alias is a name and corresponding value set using the 375c644db6aSSheldon Hearn.Ic alias 3764b88c807SRodney W. Grimesbuiltin command. Whenever a reserved word may occur (see above), 3774b88c807SRodney W. Grimesand after checking for reserved words, the shell 378e6d3cf26SSheldon Hearnchecks the word to see if it matches an alias. 379e6d3cf26SSheldon HearnIf it does, it replaces it in the input stream with its value. 380e6d3cf26SSheldon HearnFor example, if there is an alias called 38149e11e3aSSheldon Hearn.Dq Li lf 3826b15476eSSheldon Hearnwith the value 38349e11e3aSSheldon Hearn.Dq Li ls -F , 3844b88c807SRodney W. Grimesthen the input 385ab72124fSSteve Price.Bd -literal -offset indent 38649e11e3aSSheldon Hearnlf foobar 387ab72124fSSteve Price.Ed 388ab72124fSSteve Price.Pp 3894b88c807SRodney W. Grimeswould become 390ab72124fSSteve Price.Bd -literal -offset indent 39149e11e3aSSheldon Hearnls -F foobar 392ab72124fSSteve Price.Ed 393ab72124fSSteve Price.Pp 3944b88c807SRodney W. GrimesAliases provide a convenient way for naive users to 3954b88c807SRodney W. Grimescreate shorthands for commands without having to learn how 3964b88c807SRodney W. Grimesto create functions with arguments. They can also be 3974b88c807SRodney W. Grimesused to create lexically obscure code. This use is discouraged. 398ab72124fSSteve Price.Ss Commands 3994b88c807SRodney W. GrimesThe shell interprets the words it reads according to a 4004b88c807SRodney W. Grimeslanguage, the specification of which is outside the scope 401ab72124fSSteve Priceof this man page (refer to the BNF in the 402ab72124fSSteve Price.St -p1003.2 4034b88c807SRodney W. Grimesdocument). Essentially though, a line is read and if 4044b88c807SRodney W. Grimesthe first word of the line (or after a control operator) 4054b88c807SRodney W. Grimesis not a reserved word, then the shell has recognized a 4064b88c807SRodney W. Grimessimple command. Otherwise, a complex command or some 4074b88c807SRodney W. Grimesother special construct may have been recognized. 408ab72124fSSteve Price.Ss Simple Commands 4094b88c807SRodney W. GrimesIf a simple command has been recognized, the shell performs 4104b88c807SRodney W. Grimesthe following actions: 411ab72124fSSteve Price.Bl -enum 412ab72124fSSteve Price.It 4136b15476eSSheldon HearnLeading words of the form 41449e11e3aSSheldon Hearn.Dq Li name=value 4156b15476eSSheldon Hearnare stripped off and assigned to the environment of 4164b88c807SRodney W. Grimesthe simple command. Redirection operators and 4174b88c807SRodney W. Grimestheir arguments (as described below) are stripped 4184b88c807SRodney W. Grimesoff and saved for processing. 419ab72124fSSteve Price.It 420ab72124fSSteve PriceThe remaining words are expanded as described in 4216b15476eSSheldon Hearnthe section called 4226b15476eSSheldon Hearn.Sx Word Expansions , 4236b15476eSSheldon Hearnand the first remaining word is considered the command 4244b88c807SRodney W. Grimesname and the command is located. The remaining 4254b88c807SRodney W. Grimeswords are considered the arguments of the command. 4266b15476eSSheldon HearnIf no command name resulted, then the 42749e11e3aSSheldon Hearn.Dq Li name=value 4284b88c807SRodney W. Grimesvariable assignments recognized in 1) affect the 4294b88c807SRodney W. Grimescurrent shell. 430ab72124fSSteve Price.It 431ab72124fSSteve PriceRedirections are performed as described in 4324b88c807SRodney W. Grimesthe next section. 433ab72124fSSteve Price.El 434ab72124fSSteve Price.Ss Redirections 4354b88c807SRodney W. GrimesRedirections are used to change where a command reads its input 4364b88c807SRodney W. Grimesor sends its output. In general, redirections open, close, or 4374b88c807SRodney W. Grimesduplicate an existing reference to a file. The overall format 4384b88c807SRodney W. Grimesused for redirection is: 43949e11e3aSSheldon Hearn.Pp 440ab72124fSSteve Price.Dl [n] redir-op file 44149e11e3aSSheldon Hearn.Pp 44249e11e3aSSheldon HearnThe 44349e11e3aSSheldon Hearn.Ql redir-op 44449e11e3aSSheldon Hearnis one of the redirection operators mentioned 445ab72124fSSteve Pricepreviously. The following gives some examples of how these 44649e11e3aSSheldon Hearnoperators can be used. 44749e11e3aSSheldon HearnNote that stdin and stdout are commonly used abbreviations 44849e11e3aSSheldon Hearnfor standard input and standard output respectively. 44949e11e3aSSheldon Hearn.Bl -tag -width "1234567890XX" -offset indent 45049e11e3aSSheldon Hearn.It Li [n]> file 45149e11e3aSSheldon Hearnredirect stdout (or file descriptor n) to file 45249e11e3aSSheldon Hearn.It Li [n]>| file 45349e11e3aSSheldon Hearnsame as above, but override the 45449e11e3aSSheldon Hearn.Fl C 45549e11e3aSSheldon Hearnoption 45649e11e3aSSheldon Hearn.It Li [n]>> file 45749e11e3aSSheldon Hearnappend stdout (or file descriptor n) to file 45849e11e3aSSheldon Hearn.It Li [n]< file 45949e11e3aSSheldon Hearnredirect stdin (or file descriptor n) from file 4604682f420SBrian Somers.It Li [n]<> file 4614682f420SBrian Somersredirect stdin (or file descriptor n) to and from file 46249e11e3aSSheldon Hearn.It Li [n1]<&n2 46349e11e3aSSheldon Hearnduplicate stdin (or file descriptor n1) from file descriptor n2 46449e11e3aSSheldon Hearn.It Li [n]<&- 46549e11e3aSSheldon Hearnclose stdin (or file descriptor n) 46649e11e3aSSheldon Hearn.It Li [n1]>&n2 46749e11e3aSSheldon Hearnduplicate stdout (or file descriptor n1) to file descriptor n2 46849e11e3aSSheldon Hearn.It Li [n]>&- 46949e11e3aSSheldon Hearnclose stdout (or file descriptor n) 470ab72124fSSteve Price.El 471ab72124fSSteve Price.Pp 4726b15476eSSheldon HearnThe following redirection is often called a 4736b15476eSSheldon Hearn.Dq here-document . 474ab72124fSSteve Price.Bd -literal -offset indent 4754b88c807SRodney W. Grimes[n]<< delimiter 47649e11e3aSSheldon Hearn here-doc-text 47749e11e3aSSheldon Hearn ... 4784b88c807SRodney W. Grimesdelimiter 479ab72124fSSteve Price.Ed 480ab72124fSSteve Price.Pp 4814b88c807SRodney W. GrimesAll the text on successive lines up to the delimiter is 4824b88c807SRodney W. Grimessaved away and made available to the command on standard 4834b88c807SRodney W. Grimesinput, or file descriptor n if it is specified. If the delimiter 4844b88c807SRodney W. Grimesas specified on the initial line is quoted, then the here-doc-text 4854b88c807SRodney W. Grimesis treated literally, otherwise the text is subjected to 4864b88c807SRodney W. Grimesparameter expansion, command substitution, and arithmetic 4876b15476eSSheldon Hearnexpansion (as described in the section on 4886b15476eSSheldon Hearn.Sx Word Expansions ) . 4896b15476eSSheldon HearnIf the operator is 49049e11e3aSSheldon Hearn.Dq Li <<- 4916b15476eSSheldon Hearninstead of 49249e11e3aSSheldon Hearn.Dq Li << , 4936b15476eSSheldon Hearnthen leading tabs 4944b88c807SRodney W. Grimesin the here-doc-text are stripped. 495ab72124fSSteve Price.Ss Search and Execution 496ab0a2172SSteve PriceThere are three types of commands: shell functions, 49749e11e3aSSheldon Hearnbuiltin commands, and normal programs. 49849e11e3aSSheldon HearnThe command is searched for (by name) in that order. 49949e11e3aSSheldon HearnThe three types of commands are all executed in a different way. 500ab72124fSSteve Price.Pp 501ab0a2172SSteve PriceWhen a shell function is executed, all of the shell positional 502ab0a2172SSteve Priceparameters (except $0, which remains unchanged) are 5034b88c807SRodney W. Grimesset to the arguments of the shell function. 5044b88c807SRodney W. GrimesThe variables which are explicitly placed in the environment of 5054b88c807SRodney W. Grimesthe command (by placing assignments to them before the 5064b88c807SRodney W. Grimesfunction name) are made local to the function and are set 507e6d3cf26SSheldon Hearnto the values given. 508e6d3cf26SSheldon HearnThen the command given in the function definition is executed. 509e6d3cf26SSheldon HearnThe positional parameters are restored to their original values 510e6d3cf26SSheldon Hearnwhen the command completes. 511ab0a2172SSteve PriceThis all occurs within the current shell. 512ab72124fSSteve Price.Pp 513c644db6aSSheldon HearnShell builtin commands are executed internally to the shell, without 514ab0a2172SSteve Pricespawning a new process. 515ab72124fSSteve Price.Pp 516c644db6aSSheldon HearnOtherwise, if the command name does not match a function 517c644db6aSSheldon Hearnor builtin command, the command is searched for as a normal 5184b88c807SRodney W. Grimesprogram in the filesystem (as described in the next section). 5194b88c807SRodney W. GrimesWhen a normal program is executed, the shell runs the program, 520e6d3cf26SSheldon Hearnpassing the arguments and the environment to the program. 521e6d3cf26SSheldon HearnIf the program is not a normal executable file 5226b15476eSSheldon Hearn(i.e. if it does not begin with the 5236b15476eSSheldon Hearn.Qq magic number 5246b15476eSSheldon Hearnwhose 525ab72124fSSteve Price.Tn ASCII 5266b15476eSSheldon Hearnrepresentation is 5276b15476eSSheldon Hearn.Qq #! , 52849e11e3aSSheldon Hearnresulting in an 529b4f0f4aaSRuslan Ermilov.Er ENOEXEC 53049e11e3aSSheldon Hearnreturn value from 53149e11e3aSSheldon Hearn.Xr execve 2 ) 532e6d3cf26SSheldon Hearnthe shell will interpret the program in a subshell. 533e6d3cf26SSheldon HearnThe child shell will reinitialize itself in this case, 534e6d3cf26SSheldon Hearnso that the effect will be 535e6d3cf26SSheldon Hearnas if a new shell had been invoked to handle the ad-hoc shell script, 536e6d3cf26SSheldon Hearnexcept that the location of hashed commands located in 5374b88c807SRodney W. Grimesthe parent shell will be remembered by the child. 538ab72124fSSteve Price.Pp 539ab0a2172SSteve PriceNote that previous versions of this document 540ab0a2172SSteve Priceand the source code itself misleadingly and sporadically 541ab0a2172SSteve Pricerefer to a shell script without a magic number 5426b15476eSSheldon Hearnas a 5436b15476eSSheldon Hearn.Qq shell procedure . 544ab72124fSSteve Price.Ss Path Search 5454b88c807SRodney W. GrimesWhen locating a command, the shell first looks to see if 5464b88c807SRodney W. Grimesit has a shell function by that name. Then it looks for a 547ab0a2172SSteve Pricebuiltin command by that name. If a builtin command is not found, 548ab0a2172SSteve Priceone of two things happen: 549ab72124fSSteve Price.Bl -enum 550ab72124fSSteve Price.It 551ab72124fSSteve PriceCommand names containing a slash are simply executed without 552ab0a2172SSteve Priceperforming any searches. 553ab72124fSSteve Price.It 554ab72124fSSteve PriceThe shell searches each entry in 555ab72124fSSteve Price.Ev PATH 556ab72124fSSteve Pricein turn for the command. The value of the 557ab72124fSSteve Price.Ev PATH 558ab72124fSSteve Pricevariable should be a series of 5594b88c807SRodney W. Grimesentries separated by colons. Each entry consists of a 5604b88c807SRodney W. Grimesdirectory name. 5614b88c807SRodney W. GrimesThe current directory 562ab0a2172SSteve Pricemay be indicated implicitly by an empty directory name, 563ab0a2172SSteve Priceor explicitly by a single period. 564ab72124fSSteve Price.El 565ab72124fSSteve Price.Ss Command Exit Status 5664b88c807SRodney W. GrimesEach command has an exit status that can influence the behavior 5674b88c807SRodney W. Grimesof other shell commands. The paradigm is that a command exits 5684b88c807SRodney W. Grimeswith zero for normal or success, and non-zero for failure, 5694b88c807SRodney W. Grimeserror, or a false indication. The man page for each command 5704b88c807SRodney W. Grimesshould indicate the various exit codes and what they mean. 5714b88c807SRodney W. GrimesAdditionally, the builtin commands return exit codes, as does 572ab0a2172SSteve Pricean executed shell function. 573c351e089SJohn Polstra.Pp 574c351e089SJohn PolstraIf a command is terminated by a signal, its exit status is 128 plus 575c351e089SJohn Polstrathe signal number. Signal numbers are defined in the header file 576c351e089SJohn Polstra.Aq Pa sys/signal.h . 577ab72124fSSteve Price.Ss Complex Commands 5784b88c807SRodney W. GrimesComplex commands are combinations of simple commands 5794b88c807SRodney W. Grimeswith control operators or reserved words, together creating a larger complex 5804b88c807SRodney W. Grimescommand. More generally, a command is one of the following: 581ab72124fSSteve Price.Bl -item -offset indent 582ab72124fSSteve Price.It 583ab72124fSSteve Pricesimple command 584ab72124fSSteve Price.It 585ab72124fSSteve Pricepipeline 586ab72124fSSteve Price.It 587ab72124fSSteve Pricelist or compound-list 588ab72124fSSteve Price.It 589ab72124fSSteve Pricecompound command 590ab72124fSSteve Price.It 591ab72124fSSteve Pricefunction definition 592ab72124fSSteve Price.El 593ab72124fSSteve Price.Pp 5944b88c807SRodney W. GrimesUnless otherwise stated, the exit status of a command is 5954b88c807SRodney W. Grimesthat of the last simple command executed by the command. 596ab72124fSSteve Price.Ss Pipelines 5974b88c807SRodney W. GrimesA pipeline is a sequence of one or more commands separated 5984b88c807SRodney W. Grimesby the control operator |. The standard output of all but 5994b88c807SRodney W. Grimesthe last command is connected to the standard input 600ab0a2172SSteve Priceof the next command. The standard output of the last 601ab0a2172SSteve Pricecommand is inherited from the shell, as usual. 602ab72124fSSteve Price.Pp 6034b88c807SRodney W. GrimesThe format for a pipeline is: 60449e11e3aSSheldon Hearn.Pp 60549e11e3aSSheldon Hearn.Dl [!] command1 [ | command2 ...] 606ab72124fSSteve Price.Pp 6074b88c807SRodney W. GrimesThe standard output of command1 is connected to the standard 6084b88c807SRodney W. Grimesinput of command2. The standard input, standard output, or 6094b88c807SRodney W. Grimesboth of a command is considered to be assigned by the 6104b88c807SRodney W. Grimespipeline before any redirection specified by redirection 6114b88c807SRodney W. Grimesoperators that are part of the command. 612ab72124fSSteve Price.Pp 6134b88c807SRodney W. GrimesIf the pipeline is not in the background (discussed later), 6144b88c807SRodney W. Grimesthe shell waits for all commands to complete. 615ab72124fSSteve Price.Pp 6164b88c807SRodney W. GrimesIf the reserved word ! does not precede the pipeline, the 6174b88c807SRodney W. Grimesexit status is the exit status of the last command specified 6184b88c807SRodney W. Grimesin the pipeline. Otherwise, the exit status is the logical 6194b88c807SRodney W. GrimesNOT of the exit status of the last command. That is, if 6204b88c807SRodney W. Grimesthe last command returns zero, the exit status is 1; if 6214b88c807SRodney W. Grimesthe last command returns greater than zero, the exit status 6224b88c807SRodney W. Grimesis zero. 623ab72124fSSteve Price.Pp 6244b88c807SRodney W. GrimesBecause pipeline assignment of standard input or standard 6254b88c807SRodney W. Grimesoutput or both takes place before redirection, it can be 6264b88c807SRodney W. Grimesmodified by redirection. For example: 627ab72124fSSteve Price.Pp 62849e11e3aSSheldon Hearn.Dl $ command1 2>&1 | command2 629ab72124fSSteve Price.Pp 63049e11e3aSSheldon Hearnsends both the standard output and standard error of 63149e11e3aSSheldon Hearn.Ql command1 63249e11e3aSSheldon Hearnto the standard input of 63349e11e3aSSheldon Hearn.Ql command2 . 63449e11e3aSSheldon Hearn.Pp 63549e11e3aSSheldon HearnA 63612e720d7SRuslan Ermilov.Dq Li \&; 63749e11e3aSSheldon Hearnor newline terminator causes the preceding 638e918fc8fSSheldon HearnAND-OR-list 639e918fc8fSSheldon Hearn(described below in the section called 640e918fc8fSSheldon Hearn.Sx Short-Circuit List Operators ) 641e918fc8fSSheldon Hearnto be executed sequentially; 642e918fc8fSSheldon Hearnan 643e918fc8fSSheldon Hearn.Dq Li & 644e918fc8fSSheldon Hearncauses asynchronous execution of the preceding AND-OR-list. 645ab72124fSSteve Price.Pp 64649e11e3aSSheldon HearnNote that unlike some other shells, 64749e11e3aSSheldon Hearn.Nm 64849e11e3aSSheldon Hearnexecutes each process in the pipeline as a child of the 64949e11e3aSSheldon Hearn.Nm 65049e11e3aSSheldon Hearnprocess. 65149e11e3aSSheldon HearnShell builtin commands are the exception to this rule. 65249e11e3aSSheldon HearnThey are executed in the current shell, although they do not affect its 65349e11e3aSSheldon Hearnenvironment when used in pipelines. 65449e11e3aSSheldon Hearn.Ss Background Commands (&) 6554b88c807SRodney W. GrimesIf a command is terminated by the control operator ampersand 65649e11e3aSSheldon Hearn.Pq Li & , 65749e11e3aSSheldon Hearnthe shell executes the command asynchronously; 65849e11e3aSSheldon Hearnthe shell does not wait for the command to finish 65949e11e3aSSheldon Hearnbefore executing the next command. 660ab72124fSSteve Price.Pp 6614b88c807SRodney W. GrimesThe format for running a command in background is: 662ab72124fSSteve Price.Bd -literal -offset indent 6634b88c807SRodney W. Grimescommand1 & [command2 & ...] 664ab72124fSSteve Price.Ed 665ab72124fSSteve Price.Pp 6664b88c807SRodney W. GrimesIf the shell is not interactive, the standard input of an 6674b88c807SRodney W. Grimesasynchronous command is set to /dev/null. 66849e11e3aSSheldon Hearn.Ss Lists (Generally Speaking) 6694b88c807SRodney W. GrimesA list is a sequence of zero or more commands separated by 6704b88c807SRodney W. Grimesnewlines, semicolons, or ampersands, 6714b88c807SRodney W. Grimesand optionally terminated by one of these three characters. 6724b88c807SRodney W. GrimesThe commands in a 6734b88c807SRodney W. Grimeslist are executed in the order they are written. 6744b88c807SRodney W. GrimesIf command is followed by an ampersand, the shell starts the 6754b88c807SRodney W. Grimescommand and immediately proceed onto the next command; 6764b88c807SRodney W. Grimesotherwise it waits for the command to terminate before 6774b88c807SRodney W. Grimesproceeding to the next one. 678ab72124fSSteve Price.Ss Short-Circuit List Operators 67949e11e3aSSheldon Hearn.Dq Li && 6806b15476eSSheldon Hearnand 68149e11e3aSSheldon Hearn.Dq Li || 6826b15476eSSheldon Hearnare AND-OR list operators. 68349e11e3aSSheldon Hearn.Dq Li && 6846b15476eSSheldon Hearnexecutes the first command, and then executes the second command 6856b15476eSSheldon Hearnif the exit status of the first command is zero. 68649e11e3aSSheldon Hearn.Dq Li || 687120ac26dSMasafumi Max NAKANEis similar, but executes the second command if the exit 6886b15476eSSheldon Hearnstatus of the first command is nonzero. 68949e11e3aSSheldon Hearn.Dq Li && 6906b15476eSSheldon Hearnand 69149e11e3aSSheldon Hearn.Dq Li || 6924b88c807SRodney W. Grimesboth have the same priority. 69349e11e3aSSheldon Hearn.Ss Flow-Control Constructs (if, while, for, case) 69449e11e3aSSheldon HearnThe syntax of the 69549e11e3aSSheldon Hearn.Ic if 69649e11e3aSSheldon Hearncommand is: 69749e11e3aSSheldon Hearn.\" 69849e11e3aSSheldon Hearn.\" XXX Use .Dl to work around broken handling of .Ic inside .Bd and .Ed . 69949e11e3aSSheldon Hearn.\" 70049e11e3aSSheldon Hearn.Dl Ic if Ar list 70149e11e3aSSheldon Hearn.Dl Ic then Ar list 70249e11e3aSSheldon Hearn.Dl [ Ic elif Ar list 70349e11e3aSSheldon Hearn.Dl Ic then Ar list ] ... 70449e11e3aSSheldon Hearn.Dl [ Ic else Ar list ] 70549e11e3aSSheldon Hearn.Dl Ic fi 706ab72124fSSteve Price.Pp 70749e11e3aSSheldon HearnThe syntax of the 70849e11e3aSSheldon Hearn.Ic while 70949e11e3aSSheldon Hearncommand is: 71049e11e3aSSheldon Hearn.Dl Ic while Ar list 71149e11e3aSSheldon Hearn.Dl Ic do Ar list 71249e11e3aSSheldon Hearn.Dl Ic done 713ab72124fSSteve Price.Pp 714ab0a2172SSteve PriceThe two lists are executed repeatedly while the exit status of the 71549e11e3aSSheldon Hearnfirst list is zero. 71649e11e3aSSheldon HearnThe 71749e11e3aSSheldon Hearn.Ic until 71849e11e3aSSheldon Hearncommand is similar, but has the word 71949e11e3aSSheldon Hearn.Ic until 72049e11e3aSSheldon Hearnin place of 72149e11e3aSSheldon Hearn.Ic while , 72249e11e3aSSheldon Hearnwhich causes it to 7234b88c807SRodney W. Grimesrepeat until the exit status of the first list is zero. 724ab72124fSSteve Price.Pp 72549e11e3aSSheldon HearnThe syntax of the 72649e11e3aSSheldon Hearn.Ic for 72749e11e3aSSheldon Hearncommand is: 72849e11e3aSSheldon Hearn.Dl Ic for Ar variable Ic in Ar word ... 72949e11e3aSSheldon Hearn.Dl Ic do Ar list 73049e11e3aSSheldon Hearn.Dl Ic done 731ab72124fSSteve Price.Pp 7324b88c807SRodney W. GrimesThe words are expanded, and then the list is executed 73349e11e3aSSheldon Hearnrepeatedly with the variable set to each word in turn. 73449e11e3aSSheldon HearnThe 73549e11e3aSSheldon Hearn.Ic do 7366b15476eSSheldon Hearnand 73749e11e3aSSheldon Hearn.Ic done 73849e11e3aSSheldon Hearncommands may be replaced with 73949e11e3aSSheldon Hearn.Dq Li { 74049e11e3aSSheldon Hearnand 74149e11e3aSSheldon Hearn.Dq Li } . 742ab72124fSSteve Price.Pp 74349e11e3aSSheldon HearnThe syntax of the 74449e11e3aSSheldon Hearn.Ic break 74549e11e3aSSheldon Hearnand 74649e11e3aSSheldon Hearn.Ic continue 74749e11e3aSSheldon Hearncommands is: 74849e11e3aSSheldon Hearn.Dl Ic break Op Ar num 74949e11e3aSSheldon Hearn.Dl Ic continue Op Ar num 750ab72124fSSteve Price.Pp 75149e11e3aSSheldon HearnThe 75249e11e3aSSheldon Hearn.Ic break 75349e11e3aSSheldon Hearncommand terminates the 75449e11e3aSSheldon Hearn.Ar num 75549e11e3aSSheldon Hearninnermost 75649e11e3aSSheldon Hearn.Ic for 75749e11e3aSSheldon Hearnor 75849e11e3aSSheldon Hearn.Ic while 75949e11e3aSSheldon Hearnloops. 76049e11e3aSSheldon HearnThe 76149e11e3aSSheldon Hearn.Ic continue 76249e11e3aSSheldon Hearncommand continues with the next iteration of the innermost loop. 763ab0a2172SSteve PriceThese are implemented as builtin commands. 764ab72124fSSteve Price.Pp 76549e11e3aSSheldon HearnThe syntax of the 76649e11e3aSSheldon Hearn.Ic case 76749e11e3aSSheldon Hearncommand is 76849e11e3aSSheldon Hearn.Dl Ic case Ar word Ic in 76949e11e3aSSheldon Hearn.Dl pattern) list ;; 77049e11e3aSSheldon Hearn.Dl ... 77149e11e3aSSheldon Hearn.Dl Ic esac 772ab72124fSSteve Price.Pp 77349e11e3aSSheldon HearnThe pattern can actually be one or more patterns 77449e11e3aSSheldon Hearn(see 77549e11e3aSSheldon Hearn.Sx Shell Patterns 77649e11e3aSSheldon Hearndescribed later), 77749e11e3aSSheldon Hearnseparated by 77867135607SMartin Cracauer.Dq Li \&| 7796b15476eSSheldon Hearncharacters. 780ab72124fSSteve Price.Ss Grouping Commands Together 7814b88c807SRodney W. GrimesCommands may be grouped by writing either 782ab72124fSSteve Price.Bd -literal -offset indent 7834b88c807SRodney W. Grimes(list) 784ab72124fSSteve Price.Ed 785ab72124fSSteve Price.Pp 7864b88c807SRodney W. Grimesor 787ab72124fSSteve Price.Bd -literal -offset indent 7884b88c807SRodney W. Grimes{ list; } 789ab72124fSSteve Price.Ed 790ab72124fSSteve Price.Pp 79149e11e3aSSheldon HearnThe first form executes the commands in a subshell. 79249e11e3aSSheldon HearnNote that builtin commands thus executed do not affect the current shell. 79349e11e3aSSheldon HearnThe second form does not fork another shell, 79449e11e3aSSheldon Hearnso it is slightly more efficient. 79549e11e3aSSheldon HearnGrouping commands together this way allows the user to 796ab0a2172SSteve Priceredirect their output as though they were one program: 797ab72124fSSteve Price.Bd -literal -offset indent 798ee7dc416SSteve Price{ echo -n "hello"; echo " world"; } > greeting 799ab72124fSSteve Price.Ed 800ab72124fSSteve Price.Ss Functions 8014b88c807SRodney W. GrimesThe syntax of a function definition is 802ab72124fSSteve Price.Bd -literal -offset indent 8034b88c807SRodney W. Grimesname ( ) command 804ab72124fSSteve Price.Ed 805ab72124fSSteve Price.Pp 8064b88c807SRodney W. GrimesA function definition is an executable statement; when 8074b88c807SRodney W. Grimesexecuted it installs a function named name and returns an 8084b88c807SRodney W. Grimesexit status of zero. The command is normally a list 8096b15476eSSheldon Hearnenclosed between 81049e11e3aSSheldon Hearn.Dq Li { 8116b15476eSSheldon Hearnand 81249e11e3aSSheldon Hearn.Dq Li } . 813ab72124fSSteve Price.Pp 8144b88c807SRodney W. GrimesVariables may be declared to be local to a function by 81549e11e3aSSheldon Hearnusing the 81649e11e3aSSheldon Hearn.Ic local 81749e11e3aSSheldon Hearncommand. 81849e11e3aSSheldon HearnThis should appear as the first statement of a function, 81949e11e3aSSheldon Hearnand the syntax is: 82049e11e3aSSheldon Hearn.Bd -ragged -offset indent 82149e11e3aSSheldon Hearn.Ic local 82281e9cda2SRuslan Ermilov.Op Ar variable ... 82349e11e3aSSheldon Hearn.Op Ar - 824ab72124fSSteve Price.Ed 825ab72124fSSteve Price.Pp 82649e11e3aSSheldon HearnThe 82749e11e3aSSheldon Hearn.Ic local 82849e11e3aSSheldon Hearncommand is implemented as a builtin command. 829ab72124fSSteve Price.Pp 8304b88c807SRodney W. GrimesWhen a variable is made local, it inherits the initial 8314b88c807SRodney W. Grimesvalue and exported and readonly flags from the variable 8324b88c807SRodney W. Grimeswith the same name in the surrounding scope, if there is 8334b88c807SRodney W. Grimesone. Otherwise, the variable is initially unset. The shell 834e918fc8fSSheldon Hearnuses dynamic scoping, so that if the variable 835e918fc8fSSheldon Hearn.Em x 836e918fc8fSSheldon Hearnis made local to function 837e918fc8fSSheldon Hearn.Em f , 838e918fc8fSSheldon Hearnwhich then calls function 839e918fc8fSSheldon Hearn.Em g , 840e918fc8fSSheldon Hearnreferences to the variable 841e918fc8fSSheldon Hearn.Em x 842e918fc8fSSheldon Hearnmade inside 843e918fc8fSSheldon Hearn.Em g 844e918fc8fSSheldon Hearnwill refer to the variable 845e918fc8fSSheldon Hearn.Em x 846e918fc8fSSheldon Hearndeclared inside 847e918fc8fSSheldon Hearn.Em f , 848e918fc8fSSheldon Hearnnot to the global variable named 849e918fc8fSSheldon Hearn.Em x . 850ab72124fSSteve Price.Pp 8514b88c807SRodney W. GrimesThe only special parameter than can be made local is 85249e11e3aSSheldon Hearn.Dq Li - . 8536b15476eSSheldon HearnMaking 85449e11e3aSSheldon Hearn.Dq Li - 8556b15476eSSheldon Hearnlocal causes any shell options that are 8564b88c807SRodney W. Grimeschanged via the set command inside the function to be 8574b88c807SRodney W. Grimesrestored to their original values when the function 8584b88c807SRodney W. Grimesreturns. 859ab72124fSSteve Price.Pp 86049e11e3aSSheldon HearnThe syntax of the 86149e11e3aSSheldon Hearn.Ic return 86249e11e3aSSheldon Hearncommand is 86349e11e3aSSheldon Hearn.Bd -ragged -offset indent 86449e11e3aSSheldon Hearn.Ic return 86549e11e3aSSheldon Hearn.Op Ar exitstatus 866ab72124fSSteve Price.Ed 867ab72124fSSteve Price.Pp 86849e11e3aSSheldon HearnIt terminates the currently executing function. 86949e11e3aSSheldon HearnThe 87049e11e3aSSheldon Hearn.Ic return 87149e11e3aSSheldon Hearncommand is implemented as a builtin command. 872ab72124fSSteve Price.Ss Variables and Parameters 8734b88c807SRodney W. GrimesThe shell maintains a set of parameters. A parameter 8744b88c807SRodney W. Grimesdenoted by a name is called a variable. When starting up, 8754b88c807SRodney W. Grimesthe shell turns all the environment variables into shell 8764b88c807SRodney W. Grimesvariables. New variables can be set using the form 877ab72124fSSteve Price.Bd -literal -offset indent 8784b88c807SRodney W. Grimesname=value 879ab72124fSSteve Price.Ed 880ab72124fSSteve Price.Pp 8814b88c807SRodney W. GrimesVariables set by the user must have a name consisting solely 88249e11e3aSSheldon Hearnof alphabetics, numerics, and underscores. 88349e11e3aSSheldon HearnThe first letter of a variable name must not be numeric. 88449e11e3aSSheldon HearnA parameter can also be denoted by a number 8854b88c807SRodney W. Grimesor a special character as explained below. 886ab72124fSSteve Price.Ss Positional Parameters 88749e11e3aSSheldon HearnA positional parameter is a parameter denoted by a number greater than zero. 888ab72124fSSteve PriceThe shell sets these initially to the values of its command line 889ab72124fSSteve Pricearguments that follow the name of the shell script. The 890c644db6aSSheldon Hearn.Ic set 891c644db6aSSheldon Hearnbuiltin command can also be used to set or reset them. 892ab72124fSSteve Price.Ss Special Parameters 8934b88c807SRodney W. GrimesA special parameter is a parameter denoted by one of the following 8944b88c807SRodney W. Grimesspecial characters. The value of the parameter is listed 8954b88c807SRodney W. Grimesnext to its character. 896ab72124fSSteve Price.Bl -hang 89749e11e3aSSheldon Hearn.It Li * 8984b88c807SRodney W. GrimesExpands to the positional parameters, starting from one. When 8994b88c807SRodney W. Grimesthe expansion occurs within a double-quoted string 9004b88c807SRodney W. Grimesit expands to a single field with the value of each parameter 90149e11e3aSSheldon Hearnseparated by the first character of the 90249e11e3aSSheldon Hearn.Ev IFS 90349e11e3aSSheldon Hearnvariable, 90449e11e3aSSheldon Hearnor by a 90549e11e3aSSheldon Hearn.Aq space 90649e11e3aSSheldon Hearnif 90749e11e3aSSheldon Hearn.Ev IFS 90849e11e3aSSheldon Hearnis unset. 90949e11e3aSSheldon Hearn.It Li @ 9104b88c807SRodney W. GrimesExpands to the positional parameters, starting from one. When 9114b88c807SRodney W. Grimesthe expansion occurs within double-quotes, each positional 9124b88c807SRodney W. Grimesparameter expands as a separate argument. 9134b88c807SRodney W. GrimesIf there are no positional parameters, the 914e918fc8fSSheldon Hearnexpansion of 915e918fc8fSSheldon Hearn.Li @ 916e918fc8fSSheldon Hearngenerates zero arguments, even when 917e918fc8fSSheldon Hearn.Li @ 918e918fc8fSSheldon Hearnis double-quoted. What this basically means, for example, is 9196b15476eSSheldon Hearnif $1 is 9206b15476eSSheldon Hearn.Dq abc 9216b15476eSSheldon Hearnand $2 is 9226b15476eSSheldon Hearn.Dq def ghi , 9236b15476eSSheldon Hearnthen 924e918fc8fSSheldon Hearn.Qq Li $@ 9256b15476eSSheldon Hearnexpands to 9264b88c807SRodney W. Grimesthe two arguments: 927ab72124fSSteve Price.Bd -literal -offset indent 9284b88c807SRodney W. Grimes"abc" "def ghi" 929ab72124fSSteve Price.Ed 93049e11e3aSSheldon Hearn.It Li # 9314b88c807SRodney W. GrimesExpands to the number of positional parameters. 9321586940eSRuslan Ermilov.It Li \&? 9334b88c807SRodney W. GrimesExpands to the exit status of the most recent pipeline. 93449e11e3aSSheldon Hearn.It Li - 935ab72124fSSteve Price(hyphen) Expands to the current option flags (the single-letter 9364b88c807SRodney W. Grimesoption names concatenated into a string) as specified on 9374b88c807SRodney W. Grimesinvocation, by the set builtin command, or implicitly 9384b88c807SRodney W. Grimesby the shell. 93949e11e3aSSheldon Hearn.It Li $ 9404b88c807SRodney W. GrimesExpands to the process ID of the invoked shell. A subshell 9414b88c807SRodney W. Grimesretains the same value of $ as its parent. 9421586940eSRuslan Ermilov.It Li \&! 9434b88c807SRodney W. GrimesExpands to the process ID of the most recent background 9444b88c807SRodney W. Grimescommand executed from the current shell. For a 9454b88c807SRodney W. Grimespipeline, the process ID is that of the last command in the 9464b88c807SRodney W. Grimespipeline. 94749e11e3aSSheldon Hearn.It Li 0 948ab72124fSSteve Price(zero) Expands to the name of the shell or shell script. 949ab72124fSSteve Price.El 950ab72124fSSteve Price.Ss Word Expansions 9514b88c807SRodney W. GrimesThis clause describes the various expansions that are 9524b88c807SRodney W. Grimesperformed on words. Not all expansions are performed on 9534b88c807SRodney W. Grimesevery word, as explained later. 954ab72124fSSteve Price.Pp 9554b88c807SRodney W. GrimesTilde expansions, parameter expansions, command substitutions, 9564b88c807SRodney W. Grimesarithmetic expansions, and quote removals that occur within 9574b88c807SRodney W. Grimesa single word expand to a single field. It is only field 9584b88c807SRodney W. Grimessplitting or pathname expansion that can create multiple 959e6d3cf26SSheldon Hearnfields from a single word. 960e6d3cf26SSheldon HearnThe single exception to this rule is 961e918fc8fSSheldon Hearnthe expansion of the special parameter 962e918fc8fSSheldon Hearn.Li @ 963e918fc8fSSheldon Hearnwithin double-quotes, 964e6d3cf26SSheldon Hearnas was described above. 965ab72124fSSteve Price.Pp 9664b88c807SRodney W. GrimesThe order of word expansion is: 967ab72124fSSteve Price.Bl -enum 968ab72124fSSteve Price.It 969ab72124fSSteve PriceTilde Expansion, Parameter Expansion, Command Substitution, 9704b88c807SRodney W. GrimesArithmetic Expansion (these all occur at the same time). 971ab72124fSSteve Price.It 97249e11e3aSSheldon HearnField Splitting is performed on fields generated by step (1) 97349e11e3aSSheldon Hearnunless the 97449e11e3aSSheldon Hearn.Ev IFS 97549e11e3aSSheldon Hearnvariable is null. 976ab72124fSSteve Price.It 97749e11e3aSSheldon HearnPathname Expansion (unless the 97849e11e3aSSheldon Hearn.Fl f 97949e11e3aSSheldon Hearnoption is in effect). 980ab72124fSSteve Price.It 981ab72124fSSteve PriceQuote Removal. 982ab72124fSSteve Price.El 983ab72124fSSteve Price.Pp 98449e11e3aSSheldon HearnThe 98549e11e3aSSheldon Hearn.Dq Li $ 98649e11e3aSSheldon Hearncharacter is used to introduce parameter expansion, command 9874b88c807SRodney W. Grimessubstitution, or arithmetic evaluation. 988ab72124fSSteve Price.Ss Tilde Expansion (substituting a user's home directory) 98949e11e3aSSheldon HearnA word beginning with an unquoted tilde character 99049e11e3aSSheldon Hearn.Pq Li ~ 99149e11e3aSSheldon Hearnis 99249e11e3aSSheldon Hearnsubjected to tilde expansion. 99349e11e3aSSheldon HearnAll the characters up to a slash 99449e11e3aSSheldon Hearn.Pq Li / 99549e11e3aSSheldon Hearnor the end of the word are treated as a username 9964b88c807SRodney W. Grimesand are replaced with the user's home directory. If the 9974b88c807SRodney W. Grimesusername is missing (as in ~/foobar), the tilde is replaced 9984b88c807SRodney W. Grimeswith the value of the HOME variable (the current user's 9994b88c807SRodney W. Grimeshome directory). 1000ab72124fSSteve Price.Ss Parameter Expansion 10014b88c807SRodney W. GrimesThe format for parameter expansion is as follows: 1002ab72124fSSteve Price.Bd -literal -offset indent 10034b88c807SRodney W. Grimes${expression} 1004ab72124fSSteve Price.Ed 1005ab72124fSSteve Price.Pp 100649e11e3aSSheldon Hearnwhere expression consists of all characters until the matching 100749e11e3aSSheldon Hearn.Dq Li } . 100849e11e3aSSheldon HearnAny 100949e11e3aSSheldon Hearn.Dq Li } 10104b88c807SRodney W. Grimesescaped by a backslash or within a quoted string, and characters in 10114b88c807SRodney W. Grimesembedded arithmetic expansions, command substitutions, and variable 101249e11e3aSSheldon Hearnexpansions, are not examined in determining the matching 101349e11e3aSSheldon Hearn.Dq Li } . 1014ab72124fSSteve Price.Pp 10154b88c807SRodney W. GrimesThe simplest form for parameter expansion is: 1016ab72124fSSteve Price.Bd -literal -offset indent 10174b88c807SRodney W. Grimes${parameter} 1018ab72124fSSteve Price.Ed 1019ab72124fSSteve Price.Pp 10204b88c807SRodney W. GrimesThe value, if any, of parameter is substituted. 1021ab72124fSSteve Price.Pp 10224b88c807SRodney W. GrimesThe parameter name or symbol can be enclosed in braces, which are 10234b88c807SRodney W. Grimesoptional except for positional parameters with more than one digit or 10244b88c807SRodney W. Grimeswhen parameter is followed by a character that could be interpreted as 10254b88c807SRodney W. Grimespart of the name. 1026ab72124fSSteve PriceIf a parameter expansion occurs inside double-quotes: 1027ab72124fSSteve Price.Bl -enum 1028ab72124fSSteve Price.It 1029ab72124fSSteve PricePathname expansion is not performed on the results of the 10304b88c807SRodney W. Grimesexpansion. 1031ab72124fSSteve Price.It 1032ab72124fSSteve PriceField splitting is not performed on the results of the 1033e918fc8fSSheldon Hearnexpansion, with the exception of the special parameter 1034e918fc8fSSheldon Hearn.Li @ . 1035ab72124fSSteve Price.El 1036ab72124fSSteve Price.Pp 10374b88c807SRodney W. GrimesIn addition, a parameter expansion can be modified by using one of the 10384b88c807SRodney W. Grimesfollowing formats. 103981e9cda2SRuslan Ermilov.Bl -tag -width indent 1040ab72124fSSteve Price.It Li ${parameter:-word} 10414b88c807SRodney W. GrimesUse Default Values. If parameter is unset or 10424b88c807SRodney W. Grimesnull, the expansion of word is 10434b88c807SRodney W. Grimessubstituted; otherwise, the value of 10444b88c807SRodney W. Grimesparameter is substituted. 1045ab72124fSSteve Price.It Li ${parameter:=word} 10464b88c807SRodney W. GrimesAssign Default Values. If parameter is unset 10474b88c807SRodney W. Grimesor null, the expansion of word is 10484b88c807SRodney W. Grimesassigned to parameter. In all cases, the 10494b88c807SRodney W. Grimesfinal value of parameter is 10504b88c807SRodney W. Grimessubstituted. Only variables, not positional 10514b88c807SRodney W. Grimesparameters or special parameters, can be 10524b88c807SRodney W. Grimesassigned in this way. 1053ab72124fSSteve Price.It Li ${parameter:?[word]} 10544b88c807SRodney W. GrimesIndicate Error if Null or Unset. If 10554b88c807SRodney W. Grimesparameter is unset or null, the expansion of 10564b88c807SRodney W. Grimesword (or a message indicating it is unset if 10574b88c807SRodney W. Grimesword is omitted) is written to standard 10584b88c807SRodney W. Grimeserror and the shell exits with a nonzero 1059e6d3cf26SSheldon Hearnexit status. 1060e6d3cf26SSheldon HearnOtherwise, the value of 10614b88c807SRodney W. Grimesparameter is substituted. An 10624b88c807SRodney W. Grimesinteractive shell need not exit. 1063ab72124fSSteve Price.It Li ${parameter:+word} 10644b88c807SRodney W. GrimesUse Alternate Value. If parameter is unset 10654b88c807SRodney W. Grimesor null, null is substituted; 10664b88c807SRodney W. Grimesotherwise, the expansion of word is 10674b88c807SRodney W. Grimessubstituted. 1068ab72124fSSteve Price.Pp 10694b88c807SRodney W. GrimesIn the parameter expansions shown previously, use of the colon in the 10704b88c807SRodney W. Grimesformat results in a test for a parameter that is unset or null; omission 10714b88c807SRodney W. Grimesof the colon results in a test for a parameter that is only unset. 1072ab72124fSSteve Price.It Li ${#parameter} 10734b88c807SRodney W. GrimesString Length. The length in characters of 10744b88c807SRodney W. Grimesthe value of parameter. 107571337c33SSheldon Hearn.El 1076ab72124fSSteve Price.Pp 10774b88c807SRodney W. GrimesThe following four varieties of parameter expansion provide for substring 107849e11e3aSSheldon Hearnprocessing. 107949e11e3aSSheldon HearnIn each case, pattern matching notation 108049e11e3aSSheldon Hearn(see 108149e11e3aSSheldon Hearn.Sx Shell Patterns ) , 108249e11e3aSSheldon Hearnrather than regular expression notation, 108349e11e3aSSheldon Hearnis used to evaluate the patterns. 1084e918fc8fSSheldon HearnIf parameter is one of the special parameters 1085e918fc8fSSheldon Hearn.Li * 108649e11e3aSSheldon Hearnor 1087e918fc8fSSheldon Hearn.Li @ , 108849e11e3aSSheldon Hearnthe result of the expansion is unspecified. 10894b88c807SRodney W. GrimesEnclosing the full parameter expansion string in double-quotes does not 10904b88c807SRodney W. Grimescause the following four varieties of pattern characters to be quoted, 10914b88c807SRodney W. Grimeswhereas quoting characters within the braces has this effect. 109281e9cda2SRuslan Ermilov.Bl -tag -width indent 1093ab72124fSSteve Price.It Li ${parameter%word} 10944b88c807SRodney W. GrimesRemove Smallest Suffix Pattern. The word 10954b88c807SRodney W. Grimesis expanded to produce a pattern. The 10964b88c807SRodney W. Grimesparameter expansion then results in 10974b88c807SRodney W. Grimesparameter, with the smallest portion of the 10984b88c807SRodney W. Grimessuffix matched by the pattern deleted. 1099ab72124fSSteve Price.It Li ${parameter%%word} 11004b88c807SRodney W. GrimesRemove Largest Suffix Pattern. The word 11014b88c807SRodney W. Grimesis expanded to produce a pattern. The 11024b88c807SRodney W. Grimesparameter expansion then results in 11034b88c807SRodney W. Grimesparameter, with the largest portion of the 11044b88c807SRodney W. Grimessuffix matched by the pattern deleted. 1105ab72124fSSteve Price.It Li ${parameter#word} 11064b88c807SRodney W. GrimesRemove Smallest Prefix Pattern. The word 11074b88c807SRodney W. Grimesis expanded to produce a pattern. The 11084b88c807SRodney W. Grimesparameter expansion then results in 11094b88c807SRodney W. Grimesparameter, with the smallest portion of the 11104b88c807SRodney W. Grimesprefix matched by the pattern deleted. 1111ab72124fSSteve Price.It Li ${parameter##word} 11124b88c807SRodney W. GrimesRemove Largest Prefix Pattern. The word 11134b88c807SRodney W. Grimesis expanded to produce a pattern. The 11144b88c807SRodney W. Grimesparameter expansion then results in 11154b88c807SRodney W. Grimesparameter, with the largest portion of the 11164b88c807SRodney W. Grimesprefix matched by the pattern deleted. 1117ab72124fSSteve Price.El 1118ab72124fSSteve Price.Ss Command Substitution 11194b88c807SRodney W. GrimesCommand substitution allows the output of a command to be substituted in 11204b88c807SRodney W. Grimesplace of the command name itself. Command substitution occurs when 11214b88c807SRodney W. Grimesthe command is enclosed as follows: 1122ab72124fSSteve Price.Bd -literal -offset indent 11234b88c807SRodney W. Grimes$(command) 1124ab72124fSSteve Price.Ed 1125ab72124fSSteve Price.Pp 112649e11e3aSSheldon Hearnor the backquoted version: 1127ab72124fSSteve Price.Bd -literal -offset indent 11284b88c807SRodney W. Grimes`command` 1129ab72124fSSteve Price.Ed 1130ab72124fSSteve Price.Pp 11314b88c807SRodney W. GrimesThe shell expands the command substitution by executing command in a 11324b88c807SRodney W. Grimessubshell environment and replacing the command substitution 113349e11e3aSSheldon Hearnwith the standard output of the command, 113449e11e3aSSheldon Hearnremoving sequences of one or more newlines at the end of the substitution. 113549e11e3aSSheldon HearnEmbedded newlines before the end of the output are not removed; 113649e11e3aSSheldon Hearnhowever, during field splitting, they may be translated into spaces 113749e11e3aSSheldon Hearndepending on the value of 113849e11e3aSSheldon Hearn.Ev IFS 113949e11e3aSSheldon Hearnand the quoting that is in effect. 1140ab72124fSSteve Price.Ss Arithmetic Expansion 11414b88c807SRodney W. GrimesArithmetic expansion provides a mechanism for evaluating an arithmetic 1142e6d3cf26SSheldon Hearnexpression and substituting its value. 1143e6d3cf26SSheldon HearnThe format for arithmetic expansion is as follows: 1144ab72124fSSteve Price.Bd -literal -offset indent 11454b88c807SRodney W. Grimes$((expression)) 1146ab72124fSSteve Price.Ed 1147ab72124fSSteve Price.Pp 11484b88c807SRodney W. GrimesThe expression is treated as if it were in double-quotes, except 11494b88c807SRodney W. Grimesthat a double-quote inside the expression is not treated specially. The 11504b88c807SRodney W. Grimesshell expands all tokens in the expression for parameter expansion, 11514b88c807SRodney W. Grimescommand substitution, and quote removal. 1152ab72124fSSteve Price.Pp 11534b88c807SRodney W. GrimesNext, the shell treats this as an arithmetic expression and 11544b88c807SRodney W. Grimessubstitutes the value of the expression. 1155ab72124fSSteve Price.Ss White Space Splitting (Field Splitting) 11564b88c807SRodney W. GrimesAfter parameter expansion, command substitution, and 11574b88c807SRodney W. Grimesarithmetic expansion the shell scans the results of 11584b88c807SRodney W. Grimesexpansions and substitutions that did not occur in double-quotes for 11594b88c807SRodney W. Grimesfield splitting and multiple fields can result. 1160ab72124fSSteve Price.Pp 116149e11e3aSSheldon HearnThe shell treats each character of the 116249e11e3aSSheldon Hearn.Ev IFS 116349e11e3aSSheldon Hearnas a delimiter and uses 11644b88c807SRodney W. Grimesthe delimiters to split the results of parameter expansion and command 11654b88c807SRodney W. Grimessubstitution into fields. 1166ab72124fSSteve Price.Ss Pathname Expansion (File Name Generation) 116749e11e3aSSheldon HearnUnless the 116849e11e3aSSheldon Hearn.Fl f 116949e11e3aSSheldon Hearnoption is set, 117049e11e3aSSheldon Hearnfile name generation is performed 1171ab0a2172SSteve Priceafter word splitting is complete. Each word is 11724b88c807SRodney W. Grimesviewed as a series of patterns, separated by slashes. The 11734b88c807SRodney W. Grimesprocess of expansion replaces the word with the names of 11744b88c807SRodney W. Grimesall existing files whose names can be formed by replacing 11754b88c807SRodney W. Grimeseach pattern with a string that matches the specified pattern. 1176ab0a2172SSteve PriceThere are two restrictions on this: first, a pattern cannot match 1177ab0a2172SSteve Pricea string containing a slash, and second, 11784b88c807SRodney W. Grimesa pattern cannot match a string starting with a period 11794b88c807SRodney W. Grimesunless the first character of the pattern is a period. 11804b88c807SRodney W. GrimesThe next section describes the patterns used for both 1181ab72124fSSteve PricePathname Expansion and the 1182c644db6aSSheldon Hearn.Ic case 1183ab72124fSSteve Pricecommand. 1184ab72124fSSteve Price.Ss Shell Patterns 1185f434d1dfSAdam DavidA pattern consists of normal characters, which match themselves, 118649e11e3aSSheldon Hearnand meta-characters. 118749e11e3aSSheldon HearnThe meta-characters are 11881586940eSRuslan Ermilov.Dq Li \&! , 118949e11e3aSSheldon Hearn.Dq Li * , 11901586940eSRuslan Ermilov.Dq Li \&? , 11916b15476eSSheldon Hearnand 119249e11e3aSSheldon Hearn.Dq Li [ . 11936b15476eSSheldon HearnThese characters lose their special meanings if they are quoted. 11946b15476eSSheldon HearnWhen command or variable substitution is performed and the dollar sign 11956b15476eSSheldon Hearnor back quotes are not double-quoted, the value of the 11964b88c807SRodney W. Grimesvariable or the output of the command is scanned for these 11974b88c807SRodney W. Grimescharacters and they are turned into meta-characters. 1198ab72124fSSteve Price.Pp 11996b15476eSSheldon HearnAn asterisk 120049e11e3aSSheldon Hearn.Pq Li * 1201e918fc8fSSheldon Hearnmatches any string of characters. 1202e918fc8fSSheldon HearnA question mark 12031586940eSRuslan Ermilov.Pq Li \&? 1204e918fc8fSSheldon Hearnmatches any single character. 1205e6d3cf26SSheldon HearnA left bracket 120649e11e3aSSheldon Hearn.Pq Li [ 1207e6d3cf26SSheldon Hearnintroduces a character class. 1208e6d3cf26SSheldon HearnThe end of the character class is indicated by a 120949e11e3aSSheldon Hearn.Dq Li \&] ; 12106b15476eSSheldon Hearnif the 121149e11e3aSSheldon Hearn.Dq Li \&] 12126b15476eSSheldon Hearnis missing then the 121349e11e3aSSheldon Hearn.Dq Li [ 12146b15476eSSheldon Hearnmatches a 121549e11e3aSSheldon Hearn.Dq Li [ 1216e6d3cf26SSheldon Hearnrather than introducing a character class. 1217e6d3cf26SSheldon HearnA character class matches any of the characters between the square brackets. 1218e6d3cf26SSheldon HearnA range of characters may be specified using a minus sign. 1219e918fc8fSSheldon HearnThe character class may be complemented by making an exclamation point 12201586940eSRuslan Ermilov.Pq Li !\& 1221e918fc8fSSheldon Hearnthe first character of the character class. 1222ab72124fSSteve Price.Pp 12236b15476eSSheldon HearnTo include a 122412e720d7SRuslan Ermilov.Dq Li \&] 1225e6d3cf26SSheldon Hearnin a character class, make it the first character listed 1226e6d3cf26SSheldon Hearn(after the 12271586940eSRuslan Ermilov.Dq Li \&! , 1228e6d3cf26SSheldon Hearnif any). 1229e918fc8fSSheldon HearnTo include a 1230e918fc8fSSheldon Hearn.Dq Li - , 1231e918fc8fSSheldon Hearnmake it the first or last character listed. 1232c644db6aSSheldon Hearn.Ss Builtin Commands 1233c644db6aSSheldon HearnThis section lists the commands which 12344b88c807SRodney W. Grimesare builtin because they need to perform some operation 1235c644db6aSSheldon Hearnthat cannot be performed by a separate process. In addition to 1236018d9f62SAkinori MUSHAthese, a builtin version of the 1237d90c5c4aSAkinori MUSHA.Xr test 1 1238018d9f62SAkinori MUSHAcommand is provided for efficiency. 123981e9cda2SRuslan Ermilov.Bl -tag -width indent 1240d0353b83SRuslan Ermilov.It Ic \&: 1241aa9caaf6SPeter WemmA null command that returns a 0 (true) exit value. 1242d0353b83SRuslan Ermilov.It Ic \&. Ar file 1243aa9caaf6SPeter WemmThe commands in the specified file are read and executed by the shell. 12446466945aSJohn PolstraIf 12456466945aSJohn Polstra.Ar file 12466466945aSJohn Polstracontains any 124749e11e3aSSheldon Hearn.Dq / 12486466945aSJohn Polstracharacters, it is used as is. Otherwise, the shell searches the 12496466945aSJohn Polstra.Ev PATH 12506466945aSJohn Polstrafor the file. If it is not found in the 12516466945aSJohn Polstra.Ev PATH , 12526466945aSJohn Polstrait is sought in the current working directory. 125349e11e3aSSheldon Hearn.It Ic alias Op Ar name ... 125481e9cda2SRuslan Ermilov.It Ic alias Op Ar name Ns = Ns Ar string ... 125549e11e3aSSheldon HearnIf 1256d0353b83SRuslan Ermilov.Ar name Ns = Ns Ar string 125749e11e3aSSheldon Hearnis specified, the shell defines the alias 125849e11e3aSSheldon Hearn.Ar name 12596b15476eSSheldon Hearnwith value 126049e11e3aSSheldon Hearn.Ar string . 12616b15476eSSheldon HearnIf just 126249e11e3aSSheldon Hearn.Ar name 12636b15476eSSheldon Hearnis specified, the value of the alias 126449e11e3aSSheldon Hearn.Ar name 12656b15476eSSheldon Hearnis printed. 126649e11e3aSSheldon HearnWith no arguments, the 126749e11e3aSSheldon Hearn.Ic alias 126849e11e3aSSheldon Hearnbuiltin command prints the names and values of all defined aliases 126949e11e3aSSheldon Hearn(see 127049e11e3aSSheldon Hearn.Ic unalias ) . 1271e5341cbbSTim J. RobbinsAlias values are written with appropriate quoting so that they are 1272e5341cbbSTim J. Robbinssuitable for reinput to the shell. 127349e11e3aSSheldon Hearn.It Ic bg Op Ar job ... 127449e11e3aSSheldon HearnContinue the specified jobs 127549e11e3aSSheldon Hearn(or the current job if no jobs are given) 127649e11e3aSSheldon Hearnin the background. 127749e11e3aSSheldon Hearn.It Ic command Ar cmd Op Ar arg ... 127849e11e3aSSheldon HearnExecute the specified builtin command, 127949e11e3aSSheldon Hearn.Ar cmd . 128049e11e3aSSheldon HearnThis is useful when the user wishes to override a shell function 128149e11e3aSSheldon Hearnwith the same name as a builtin command. 128281e9cda2SRuslan Ermilov.It Ic cd Oo Fl LP Oc Op Ar directory 128349e11e3aSSheldon HearnSwitch to the specified 128449e11e3aSSheldon Hearn.Ar directory , 128549e11e3aSSheldon Hearnor to the directory specified in the 128649e11e3aSSheldon Hearn.Ev HOME 128749e11e3aSSheldon Hearnenvironment variable if no 128849e11e3aSSheldon Hearn.Ar directory 128949e11e3aSSheldon Hearnis specified. 129049e11e3aSSheldon HearnIf 129149e11e3aSSheldon Hearn.Ar directory 1292029c8ee4STim J. Robbinsdoes not begin with 1293029c8ee4STim J. Robbins.Pa / , . , 129449e11e3aSSheldon Hearnor 1295029c8ee4STim J. Robbins.Pa .. , 129649e11e3aSSheldon Hearnthen the directories listed in the 129749e11e3aSSheldon Hearn.Ev CDPATH 129849e11e3aSSheldon Hearnvariable will be 129949e11e3aSSheldon Hearnsearched for the specified 130049e11e3aSSheldon Hearn.Ar directory . 1301029c8ee4STim J. RobbinsIf 1302029c8ee4STim J. Robbins.Ev CDPATH 1303029c8ee4STim J. Robbinsis unset, the current directory is searched. 130449e11e3aSSheldon HearnThe format of 130549e11e3aSSheldon Hearn.Ar CDPATH 130649e11e3aSSheldon Hearnis the same as that of 130749e11e3aSSheldon Hearn.Ev PATH . 1308e6d3cf26SSheldon HearnIn an interactive shell, 130949e11e3aSSheldon Hearnthe 131049e11e3aSSheldon Hearn.Ic cd 131149e11e3aSSheldon Hearncommand will print out the name of the directory 1312e6d3cf26SSheldon Hearnthat it actually switched to 1313e6d3cf26SSheldon Hearnif this is different from the name that the user gave. 131449e11e3aSSheldon HearnThese may be different either because the 131549e11e3aSSheldon Hearn.Ev CDPATH 131649e11e3aSSheldon Hearnmechanism was used or because a symbolic link was crossed. 1317178897f1STim J. Robbins.Pp 1318178897f1STim J. RobbinsIf the 1319178897f1STim J. Robbins.Fl P 1320178897f1STim J. Robbinsoption is specified, 132181e9cda2SRuslan Ermilov.Pa .. 1322178897f1STim J. Robbinsis handled physically and symbolic links are resolved before 132381e9cda2SRuslan Ermilov.Pa .. 1324178897f1STim J. Robbinscomponents are processed. 1325178897f1STim J. RobbinsIf the 1326178897f1STim J. Robbins.Fl L 1327178897f1STim J. Robbinsoption is specified, 132881e9cda2SRuslan Ermilov.Pa .. 1329178897f1STim J. Robbinsis handled logically. 1330178897f1STim J. RobbinsThis is the default. 13319434a1c7SDima Dorfman.It Ic chdir 13329434a1c7SDima DorfmanA synonym for the 13339434a1c7SDima Dorfman.Ic cd 13349434a1c7SDima Dorfmanbuiltin command. 133581e9cda2SRuslan Ermilov.It Ic echo Oo Fl en Oc Ar string 1336501e74b7SMark OvensPrint 1337501e74b7SMark Ovens.Ar string 1338501e74b7SMark Ovensto the standard output with a newline appended. 133981e9cda2SRuslan Ermilov.Bl -tag -width indent 1340501e74b7SMark Ovens.It Fl n 1341501e74b7SMark OvensSuppress the output of the trailing newline. 1342501e74b7SMark Ovens.It Fl e 1343501e74b7SMark OvensProcess C-style backslash escape sequences. 1344501e74b7SMark Ovens.Ic echo 1345501e74b7SMark Ovensunderstands the following character escapes: 134681e9cda2SRuslan Ermilov.Bl -tag -width indent 1347501e74b7SMark Ovens.It \ea 1348501e74b7SMark OvensAlert (ring the terminal bell) 1349501e74b7SMark Ovens.It \eb 1350501e74b7SMark OvensBackspace 1351501e74b7SMark Ovens.It \ec 1352501e74b7SMark OvensSuppress the trailing newline (this has the side-effect of truncating the 1353501e74b7SMark Ovensline if it is not the last character) 1354501e74b7SMark Ovens.It \ee 1355501e74b7SMark OvensThe ESC character (ASCII 0x1b) 1356501e74b7SMark Ovens.It \ef 1357501e74b7SMark OvensFormfeed 1358501e74b7SMark Ovens.It \en 1359501e74b7SMark OvensNewline 1360501e74b7SMark Ovens.It \er 1361501e74b7SMark OvensCarriage return 1362501e74b7SMark Ovens.It \et 1363501e74b7SMark OvensHorizontal tab 1364501e74b7SMark Ovens.It \ev 1365501e74b7SMark OvensVertical tab 1366501e74b7SMark Ovens.It \e\e 1367501e74b7SMark OvensLiteral backslash 1368501e74b7SMark Ovens.It \e0nnn 1369501e74b7SMark Ovens(Zero) The character whose octal value is nnn 1370501e74b7SMark Ovens.El 1371501e74b7SMark Ovens.Pp 1372501e74b7SMark OvensIf 1373501e74b7SMark Ovens.Ar string 1374501e74b7SMark Ovensis not enclosed in quotes then the backslash itself must be escaped 1375501e74b7SMark Ovenswith a backslash to protect it from the shell. For example 1376501e74b7SMark Ovens.Bd -literal -offset indent 1377501e74b7SMark Ovens$ echo -e "a\evb" 1378501e74b7SMark Ovensa 1379501e74b7SMark Ovens b 1380501e74b7SMark Ovens$ echo -e a\e\evb 1381501e74b7SMark Ovensa 1382501e74b7SMark Ovens b 1383501e74b7SMark Ovens$ echo -e "a\e\eb" 1384501e74b7SMark Ovensa\eb 1385501e74b7SMark Ovens$ echo -e a\e\e\e\eb 1386501e74b7SMark Ovensa\eb 1387501e74b7SMark Ovens.Ed 1388501e74b7SMark Ovens.El 138949e11e3aSSheldon Hearn.It Ic eval Ar string ... 1390e6d3cf26SSheldon HearnConcatenate all the arguments with spaces. 1391e6d3cf26SSheldon HearnThen re-parse and execute the command. 139249e11e3aSSheldon Hearn.It Ic exec Op Ar command Op arg ... 139349e11e3aSSheldon HearnUnless 139449e11e3aSSheldon Hearn.Ar command 139549e11e3aSSheldon Hearnis omitted, 139649e11e3aSSheldon Hearnthe shell process is replaced with the specified program 139749e11e3aSSheldon Hearn(which must be a real program, not a shell builtin command or function). 139849e11e3aSSheldon HearnAny redirections on the 139949e11e3aSSheldon Hearn.Ic exec 140049e11e3aSSheldon Hearncommand are marked as permanent, 140149e11e3aSSheldon Hearnso that they are not undone when the 140249e11e3aSSheldon Hearn.Ic exec 140349e11e3aSSheldon Hearncommand finishes. 140449e11e3aSSheldon Hearn.It Ic exit Op Ar exitstatus 140549e11e3aSSheldon HearnTerminate the shell process. 140649e11e3aSSheldon HearnIf 140749e11e3aSSheldon Hearn.Ar exitstatus 140849e11e3aSSheldon Hearnis given 140949e11e3aSSheldon Hearnit is used as the exit status of the shell; 141049e11e3aSSheldon Hearnotherwise the exit status of the preceding command is used. 141145086f8cSTim J. Robbins.It Xo 141245086f8cSTim J. Robbins.Ic export 141345086f8cSTim J. Robbins.Op Fl p 141445086f8cSTim J. Robbins.Op Ar name ... 141545086f8cSTim J. Robbins.Xc 14164b88c807SRodney W. GrimesThe specified names are exported so that they will 14174b88c807SRodney W. Grimesappear in the environment of subsequent commands. 141849e11e3aSSheldon HearnThe only way to un-export a variable is to 141949e11e3aSSheldon Hearn.Ic unset 142049e11e3aSSheldon Hearnit. 142149e11e3aSSheldon HearnThe shell allows the value of a variable to be set 142249e11e3aSSheldon Hearnat the same time as it is exported by writing 1423ab72124fSSteve Price.Bd -literal -offset indent 14244b88c807SRodney W. Grimesexport name=value 1425ab72124fSSteve Price.Ed 1426ab72124fSSteve Price.Pp 14274b88c807SRodney W. GrimesWith no arguments the export command lists the names 14284b88c807SRodney W. Grimesof all exported variables. 142945086f8cSTim J. RobbinsIf the 143045086f8cSTim J. Robbins.Fl p 143145086f8cSTim J. Robbinsoption is specified, the exported variables are printed as 143245086f8cSTim J. Robbins.Dq Ic export Ar name Ns = Ns Ar value 143345086f8cSTim J. Robbinslines, suitable for re-input to the shell. 143481e9cda2SRuslan Ermilov.It Ic fc Oo Fl e Ar editor Oc Op Ar first Op Ar last 143581e9cda2SRuslan Ermilov.It Ic fc Fl l Oo Fl nr Oc Op Ar first Op Ar last 143681e9cda2SRuslan Ermilov.It Ic fc Fl s Oo Ar old Ns = Ns Ar new Oc Op Ar first 143749e11e3aSSheldon HearnThe 143849e11e3aSSheldon Hearn.Ic fc 143949e11e3aSSheldon Hearnbuiltin command lists, or edits and re-executes, 144049e11e3aSSheldon Hearncommands previously entered to an interactive shell. 144181e9cda2SRuslan Ermilov.Bl -tag -width indent 144249e11e3aSSheldon Hearn.It Fl e Ar editor 144349e11e3aSSheldon HearnUse the editor named by 144449e11e3aSSheldon Hearn.Ar editor 144549e11e3aSSheldon Hearnto edit the commands. 144649e11e3aSSheldon HearnThe editor string is a command name, 144749e11e3aSSheldon Hearnsubject to search via the 144849e11e3aSSheldon Hearn.Ev PATH 144949e11e3aSSheldon Hearnvariable. 145049e11e3aSSheldon HearnThe value in the 145149e11e3aSSheldon Hearn.Ev FCEDIT 145249e11e3aSSheldon Hearnvariable is used as a default when 145349e11e3aSSheldon Hearn.Fl e 145449e11e3aSSheldon Hearnis not specified. 145549e11e3aSSheldon HearnIf 145649e11e3aSSheldon Hearn.Ev FCEDIT 145749e11e3aSSheldon Hearnis null or unset, the value of the 145849e11e3aSSheldon Hearn.Ev EDITOR 145949e11e3aSSheldon Hearnvariable is used. 146049e11e3aSSheldon HearnIf 146149e11e3aSSheldon Hearn.Ev EDITOR 146249e11e3aSSheldon Hearnis null or unset, 1463ab72124fSSteve Price.Xr ed 1 1464ab72124fSSteve Priceis used as the editor. 146549e11e3aSSheldon Hearn.It Fl l No (ell) 14664b88c807SRodney W. GrimesList the commands rather than invoking 14674b88c807SRodney W. Grimesan editor on them. The commands are written in the 14684b88c807SRodney W. Grimessequence indicated by the first and last operands, as 146949e11e3aSSheldon Hearnaffected by 147049e11e3aSSheldon Hearn.Fl r , 147149e11e3aSSheldon Hearnwith each command preceded by the command number. 147249e11e3aSSheldon Hearn.It Fl n 147349e11e3aSSheldon HearnSuppress command numbers when listing with 147449e11e3aSSheldon Hearn.Fl l . 147549e11e3aSSheldon Hearn.It Fl r 147649e11e3aSSheldon HearnReverse the order of the commands listed 147749e11e3aSSheldon Hearn(with 147849e11e3aSSheldon Hearn.Fl l ) 147949e11e3aSSheldon Hearnor edited 148049e11e3aSSheldon Hearn(with neither 148149e11e3aSSheldon Hearn.Fl l 148249e11e3aSSheldon Hearnnor 148349e11e3aSSheldon Hearn.Fl s ) . 148449e11e3aSSheldon Hearn.It Fl s 14854b88c807SRodney W. GrimesRe-execute the command without invoking an editor. 148649e11e3aSSheldon Hearn.It Ar first 148749e11e3aSSheldon Hearn.It Ar last 148849e11e3aSSheldon HearnSelect the commands to list or edit. 148949e11e3aSSheldon HearnThe number of previous commands that can be accessed 149049e11e3aSSheldon Hearnare determined by the value of the 1491d0353b83SRuslan Ermilov.Ev HISTSIZE 1492d0353b83SRuslan Ermilovvariable. 149349e11e3aSSheldon HearnThe value of 149449e11e3aSSheldon Hearn.Ar first 149549e11e3aSSheldon Hearnor 149649e11e3aSSheldon Hearn.Ar last 149749e11e3aSSheldon Hearnor both are one of the following: 149881e9cda2SRuslan Ermilov.Bl -tag -width indent 149949e11e3aSSheldon Hearn.It Ar [+]num 150049e11e3aSSheldon HearnA positive number representing a command number; 150149e11e3aSSheldon Hearncommand numbers can be displayed with the 150249e11e3aSSheldon Hearn.Fl l 150349e11e3aSSheldon Hearnoption. 150449e11e3aSSheldon Hearn.It Ar -num 150549e11e3aSSheldon HearnA negative decimal number representing the 150649e11e3aSSheldon Hearncommand that was executed 150749e11e3aSSheldon Hearn.Ar num 150849e11e3aSSheldon Hearnof 150949e11e3aSSheldon Hearncommands previously. 151049e11e3aSSheldon HearnFor example, -1 is the immediately previous command. 151149e11e3aSSheldon Hearn.It Ar string 151249e11e3aSSheldon HearnA string indicating the most recently entered command 151349e11e3aSSheldon Hearnthat begins with that string. 151449e11e3aSSheldon HearnIf the 151549e11e3aSSheldon Hearn.Ar old=new 151649e11e3aSSheldon Hearnoperand is not also specified with 151749e11e3aSSheldon Hearn.Fl s , 151849e11e3aSSheldon Hearnthe string form of the first operand cannot contain an embedded equal sign. 151949e11e3aSSheldon Hearn.El 152049e11e3aSSheldon Hearn.El 152149e11e3aSSheldon Hearn.Pp 152249e11e3aSSheldon HearnThe following environment variables affect the execution of 152349e11e3aSSheldon Hearn.Ic fc : 152481e9cda2SRuslan Ermilov.Bl -tag -width indent 152549e11e3aSSheldon Hearn.It Ev FCEDIT 15264b88c807SRodney W. GrimesName of the editor to use. 152749e11e3aSSheldon Hearn.It Ev HISTSIZE 15283d7b5b93SPhilippe CharnierThe number of previous commands that are accessible. 1529ab72124fSSteve Price.El 153049e11e3aSSheldon Hearn.It Ic fg Op Ar job 153149e11e3aSSheldon HearnMove the specified 153249e11e3aSSheldon Hearn.Ar job 153349e11e3aSSheldon Hearnor the current job to the foreground. 153449e11e3aSSheldon Hearn.It Ic getopts Ar optstring Ar var 153549e11e3aSSheldon HearnThe POSIX 153649e11e3aSSheldon Hearn.Ic getopts 153749e11e3aSSheldon Hearncommand. 153849e11e3aSSheldon HearnThe 153949e11e3aSSheldon Hearn.Ic getopts 154049e11e3aSSheldon Hearncommand deprecates the older 154149e11e3aSSheldon Hearn.Xr getopt 1 154249e11e3aSSheldon Hearncommand. 1543ab0a2172SSteve PriceThe first argument should be a series of letters, each possibly 1544ab0a2172SSteve Pricefollowed by a colon which indicates that the option takes an argument. 1545ab0a2172SSteve PriceThe specified variable is set to the parsed option. The index of 154649e11e3aSSheldon Hearnthe next argument is placed into the shell variable 154749e11e3aSSheldon Hearn.Ev OPTIND . 1548ab0a2172SSteve PriceIf an option takes an argument, it is placed into the shell variable 154949e11e3aSSheldon Hearn.Ev OPTARG . 155049e11e3aSSheldon HearnIf an invalid option is encountered, 155149e11e3aSSheldon Hearn.Ev var 155249e11e3aSSheldon Hearnis set to 15531586940eSRuslan Ermilov.Dq Li \&? . 1554ab0a2172SSteve PriceIt returns a false value (1) when it encounters the end of the options. 155581e9cda2SRuslan Ermilov.It Ic hash Oo Fl rv Oc Op Ar command ... 155649e11e3aSSheldon HearnThe shell maintains a hash table which remembers the locations of commands. 155749e11e3aSSheldon HearnWith no arguments whatsoever, the 155849e11e3aSSheldon Hearn.Ic hash 155949e11e3aSSheldon Hearncommand prints out the contents of this table. 156049e11e3aSSheldon HearnEntries which have not been looked at since the last 156149e11e3aSSheldon Hearn.Ic cd 156249e11e3aSSheldon Hearncommand are marked with an asterisk; 156349e11e3aSSheldon Hearnit is possible for these entries to be invalid. 1564ab72124fSSteve Price.Pp 156549e11e3aSSheldon HearnWith arguments, the 156649e11e3aSSheldon Hearn.Ic hash 156749e11e3aSSheldon Hearncommand removes each specified 156849e11e3aSSheldon Hearn.Ar command 156949e11e3aSSheldon Hearnfrom the hash table (unless they are functions) and then locates it. 157049e11e3aSSheldon HearnWith the 157149e11e3aSSheldon Hearn.Fl v 157249e11e3aSSheldon Hearnoption, 157349e11e3aSSheldon Hearn.Ic hash 157449e11e3aSSheldon Hearnprints the locations of the commands as it finds them. 157549e11e3aSSheldon HearnThe 157649e11e3aSSheldon Hearn.Fl r 157749e11e3aSSheldon Hearnoption causes the 157849e11e3aSSheldon Hearn.Ic hash 157949e11e3aSSheldon Hearncommand to delete all the entries in the hash table except for functions. 158049e11e3aSSheldon Hearn.It Ic jobid Op Ar job 158149e11e3aSSheldon HearnPrint the process id's of the processes in the specified 158249e11e3aSSheldon Hearn.Ar job . 158349e11e3aSSheldon HearnIf the 158449e11e3aSSheldon Hearn.Ar job 158549e11e3aSSheldon Hearnargument is omitted, use the current job. 1586ad8a0759STim J. Robbins.It Xo 1587ad8a0759STim J. Robbins.Ic jobs 1588ad8a0759STim J. Robbins.Op Fl ls 1589ad8a0759STim J. Robbins.Op Ar job ... 1590ad8a0759STim J. Robbins.Xc 1591ad8a0759STim J. RobbinsPrint information about the specified jobs, or all jobs if no 1592ad8a0759STim J. Robbins.Ar job 1593ad8a0759STim J. Robbinsargument is given. 1594ad8a0759STim J. RobbinsThe information printed includes job ID, status and command name. 1595ad8a0759STim J. Robbins.Pp 1596ad8a0759STim J. RobbinsIf the 1597ad8a0759STim J. Robbins.Fl l 1598ad8a0759STim J. Robbinsoption is specified, the PID of each job is also printed. 1599ad8a0759STim J. RobbinsIf the 1600ad8a0759STim J. Robbins.Fl s 1601ad8a0759STim J. Robbinsoption is specified, only the PID's of the jobs are printed, one per line. 160281e9cda2SRuslan Ermilov.It Ic pwd Op Fl LP 160349e11e3aSSheldon HearnPrint the path of the current directory. The builtin command may 16044b88c807SRodney W. Grimesdiffer from the program of the same name because the 16054b88c807SRodney W. Grimesbuiltin command remembers what the current directory 16064b88c807SRodney W. Grimesis rather than recomputing it each time. This makes 16074b88c807SRodney W. Grimesit faster. However, if the current directory is 160849e11e3aSSheldon Hearnrenamed, 160949e11e3aSSheldon Hearnthe builtin version of 161049e11e3aSSheldon Hearn.Xr pwd 1 161149e11e3aSSheldon Hearnwill continue to print the old name for the directory. 1612178897f1STim J. Robbins.Pp 1613178897f1STim J. RobbinsIf the 1614178897f1STim J. Robbins.Fl P 1615178897f1STim J. Robbinsoption is specified, symbolic links are resolved. 1616178897f1STim J. RobbinsIf the 1617178897f1STim J. Robbins.Fl L 1618178897f1STim J. Robbinsoption is specified, the shell's notion of the current directory 1619178897f1STim J. Robbinsis printed (symbolic links are not resolved). 1620178897f1STim J. RobbinsThis is the default. 162181e9cda2SRuslan Ermilov.It Ic read Oo Fl p Ar prompt Oc Oo Fl t Ar timeout Oc Oo Fl er Oc Ar variable ... 162249e11e3aSSheldon HearnThe 162349e11e3aSSheldon Hearn.Ar prompt 162449e11e3aSSheldon Hearnis printed if the 162549e11e3aSSheldon Hearn.Fl p 162649e11e3aSSheldon Hearnoption is specified 16274b88c807SRodney W. Grimesand the standard input is a terminal. Then a line is 16284b88c807SRodney W. Grimesread from the standard input. The trailing newline 16294b88c807SRodney W. Grimesis deleted from the line and the line is split as 163049e11e3aSSheldon Hearndescribed in the section on 163149e11e3aSSheldon Hearn.Sx White Space Splitting (Field Splitting) 163249e11e3aSSheldon Hearnabove, and 16334b88c807SRodney W. Grimesthe pieces are assigned to the variables in order. 1634f434d1dfSAdam DavidIf there are more pieces than variables, the remaining 163549e11e3aSSheldon Hearnpieces (along with the characters in 163649e11e3aSSheldon Hearn.Ev IFS 163749e11e3aSSheldon Hearnthat separated them) 163849e11e3aSSheldon Hearnare assigned to the last variable. 1639f434d1dfSAdam DavidIf there are more variables than pieces, the remaining 1640f434d1dfSAdam Davidvariables are assigned the null string. 1641ab72124fSSteve Price.Pp 164249e11e3aSSheldon HearnBackslashes are treated specially, unless the 164349e11e3aSSheldon Hearn.Fl r 164449e11e3aSSheldon Hearnoption is 16458f0561ccSThomas Gellekumspecified. If a backslash is followed by 16468f0561ccSThomas Gellekuma newline, the backslash and the newline will be 16478f0561ccSThomas Gellekumdeleted. If a backslash is followed by any other 16488f0561ccSThomas Gellekumcharacter, the backslash will be deleted and the following 164949e11e3aSSheldon Hearncharacter will be treated as though it were not in 165049e11e3aSSheldon Hearn.Ev IFS , 165149e11e3aSSheldon Hearneven if it is. 16528f0561ccSThomas Gellekum.Pp 165349e11e3aSSheldon HearnIf the 165449e11e3aSSheldon Hearn.Fl t 165549e11e3aSSheldon Hearnoption is specified and the 165649e11e3aSSheldon Hearn.Ar timeout 165749e11e3aSSheldon Hearnelapses before any input is supplied, 165849e11e3aSSheldon Hearnthe 165949e11e3aSSheldon Hearn.Ic read 166049e11e3aSSheldon Hearncommand will return without assigning any values. 166149e11e3aSSheldon HearnThe 166249e11e3aSSheldon Hearn.Ar timeout 166349e11e3aSSheldon Hearnvalue may optionally be followed by one of 166449e11e3aSSheldon Hearn.Dq s , 166549e11e3aSSheldon Hearn.Dq m 166649e11e3aSSheldon Hearnor 166749e11e3aSSheldon Hearn.Dq h 1668a910f192SDima Dorfmanto explicitly specify seconds, minutes or hours. 166949e11e3aSSheldon HearnIf none is supplied, 167049e11e3aSSheldon Hearn.Dq s 167149e11e3aSSheldon Hearnis assumed. 1672afa53c8dSMike Smith.Pp 167349e11e3aSSheldon HearnThe 167449e11e3aSSheldon Hearn.Fl e 167549e11e3aSSheldon Hearnoption exists only for backward compatibility with older scripts. 167645086f8cSTim J. Robbins.It Xo 167745086f8cSTim J. Robbins.Ic readonly 167845086f8cSTim J. Robbins.Op Fl p 167945086f8cSTim J. Robbins.Op Ar name ... 168045086f8cSTim J. Robbins.Xc 168149e11e3aSSheldon HearnEach specified 168249e11e3aSSheldon Hearn.Ar name 168349e11e3aSSheldon Hearnis marked as read only, 168449e11e3aSSheldon Hearnso that it cannot be subsequently modified or unset. 168549e11e3aSSheldon HearnThe shell allows the value of a variable to be set 168649e11e3aSSheldon Hearnat the same time as it is marked read only 168749e11e3aSSheldon Hearnby using the following form: 1688ab72124fSSteve Price.Bd -literal -offset indent 16894b88c807SRodney W. Grimesreadonly name=value 1690ab72124fSSteve Price.Ed 1691ab72124fSSteve Price.Pp 169249e11e3aSSheldon HearnWith no arguments the 169349e11e3aSSheldon Hearn.Ic readonly 169449e11e3aSSheldon Hearncommand lists the names of all read only variables. 169545086f8cSTim J. RobbinsIf the 169645086f8cSTim J. Robbins.Fl p 169745086f8cSTim J. Robbinsoption is specified, the read-only variables are printed as 169845086f8cSTim J. Robbins.Dq Ic readonly Ar name Ns = Ns Ar value 169945086f8cSTim J. Robbinslines, suitable for re-input to the shell. 170081e9cda2SRuslan Ermilov.It Ic set Oo Fl /+abCEefIimnpTuVvx Oc Oo Fl /+o Ar longname Oc Oo 170181e9cda2SRuslan Ermilov.Fl c Ar string Oc Op Fl - Ar arg ... 1702e918fc8fSSheldon HearnThe 1703e918fc8fSSheldon Hearn.Ic set 1704e918fc8fSSheldon Hearncommand performs three different functions: 1705ab72124fSSteve Price.Bl -item 1706ab72124fSSteve Price.It 170749e11e3aSSheldon HearnWith no arguments, it lists the values of all shell variables. 1708ab72124fSSteve Price.It 170949e11e3aSSheldon HearnIf options are given, 171049e11e3aSSheldon Hearneither in short form or using the long 171149e11e3aSSheldon Hearn.Dq Fl /+o Ar longname 171249e11e3aSSheldon Hearnform, 171349e11e3aSSheldon Hearnit sets or clears the specified options as described in the section called 17146b15476eSSheldon Hearn.Sx Argument List Processing . 1715ab72124fSSteve Price.It 171649e11e3aSSheldon HearnIf the 171749e11e3aSSheldon Hearn.Dq Fl - 171849e11e3aSSheldon Hearnoption is specified, 171949e11e3aSSheldon Hearn.Ic set 172049e11e3aSSheldon Hearnwill replace the shell's positional parameters with the subsequent 172149e11e3aSSheldon Hearnarguments. 172249e11e3aSSheldon HearnIf no arguments follow the 172349e11e3aSSheldon Hearn.Dq Fl - 172449e11e3aSSheldon Hearnoption, 172549e11e3aSSheldon Hearnall the positional parameters will be cleared, 172649e11e3aSSheldon Hearnwhich is equivalent to executing the command 172749e11e3aSSheldon Hearn.Dq Li shift $# . 172849e11e3aSSheldon HearnThe 172949e11e3aSSheldon Hearn.Dq Fl - 173049e11e3aSSheldon Hearnflag may be ommitted when specifying arguments to be used 173149e11e3aSSheldon Hearnas positional replacement parameters. 173249e11e3aSSheldon HearnThis is not recommended, 173349e11e3aSSheldon Hearnbecause the first argument may begin with a dash 173449e11e3aSSheldon Hearn.Pq Li - 173549e11e3aSSheldon Hearnor a plus 173649e11e3aSSheldon Hearn.Pq Li + , 173749e11e3aSSheldon Hearnwhich the 173849e11e3aSSheldon Hearn.Ic set 173949e11e3aSSheldon Hearncommand will interpret as a request to enable or disable options. 1740ab72124fSSteve Price.El 174149e11e3aSSheldon Hearn.It Ic setvar Ar variable Ar value 174249e11e3aSSheldon HearnAssigns the specified 174349e11e3aSSheldon Hearn.Ar value 174449e11e3aSSheldon Hearnto the specified 174549e11e3aSSheldon Hearn.Ar variable . 174649e11e3aSSheldon Hearn.Ic Setvar 174749e11e3aSSheldon Hearnis intended to be used in functions that 1748e6d3cf26SSheldon Hearnassign values to variables whose names are passed as parameters. 174949e11e3aSSheldon HearnIn general it is better to write 175049e11e3aSSheldon Hearn.Bd -literal -offset indent 175149e11e3aSSheldon Hearnvariable=value 175249e11e3aSSheldon Hearn.Ed 175349e11e3aSSheldon Hearnrather than using 175449e11e3aSSheldon Hearn.Ic setvar . 175549e11e3aSSheldon Hearn.It Ic shift Op Ar n 175649e11e3aSSheldon HearnShift the positional parameters 175749e11e3aSSheldon Hearn.Ar n 1758e918fc8fSSheldon Hearntimes, or once if 175949e11e3aSSheldon Hearn.Ar n 176049e11e3aSSheldon Hearnis not specified. 176149e11e3aSSheldon HearnA shift sets the value of $1 to the value of $2, 176249e11e3aSSheldon Hearnthe value of $2 to the value of $3, and so on, 176349e11e3aSSheldon Hearndecreasing the value of $# by one. 176449e11e3aSSheldon HearnIf there are zero positional parameters, shifting does not do anything. 176581e9cda2SRuslan Ermilov.It Ic trap Oo Ar action Oc Ar signal ... 176649e11e3aSSheldon HearnCause the shell to parse and execute 176749e11e3aSSheldon Hearn.Ar action 176849e11e3aSSheldon Hearnwhen any specified 176949e11e3aSSheldon Hearn.Ar signal 177049e11e3aSSheldon Hearnis received. 177149e11e3aSSheldon HearnThe signals are specified by signal number. 177249e11e3aSSheldon HearnThe 177349e11e3aSSheldon Hearn.Ar action 177449e11e3aSSheldon Hearnmay be null or omitted; 177549e11e3aSSheldon Hearnthe former causes the specified signal to be ignored 177649e11e3aSSheldon Hearnand the latter causes the default action to be taken. 177749e11e3aSSheldon HearnWhen the shell forks off a subshell, 177849e11e3aSSheldon Hearnit resets trapped (but not ignored) signals to the default action. 177949e11e3aSSheldon HearnThe 178049e11e3aSSheldon Hearn.Ic trap 178149e11e3aSSheldon Hearncommand has no effect on signals that were ignored on entry to the shell. 178249e11e3aSSheldon Hearn.It Ic type Op Ar name ... 178349e11e3aSSheldon HearnInterpret each 178449e11e3aSSheldon Hearn.Ar name 178549e11e3aSSheldon Hearnas a command and print the resolution of the command search. 1786e6d3cf26SSheldon HearnPossible resolutions are: 1787c644db6aSSheldon Hearnshell keyword, alias, shell builtin command, command, tracked alias 178849e11e3aSSheldon Hearnand not found. 178949e11e3aSSheldon HearnFor aliases the alias expansion is printed; 179049e11e3aSSheldon Hearnfor commands and tracked aliases 179149e11e3aSSheldon Hearnthe complete pathname of the command is printed. 179281e9cda2SRuslan Ermilov.It Ic ulimit Oo Fl HSabcdflmnust Oc Op Ar limit 1793ab72124fSSteve PriceSet or display resource limits (see 1794ab72124fSSteve Price.Xr getrlimit 2 ) . 17956b15476eSSheldon HearnIf 179649e11e3aSSheldon Hearn.Ar limit 17976b15476eSSheldon Hearnis specified, the named resource will be set; 17987a2afe64SJoerg Wunschotherwise the current resource value will be displayed. 1799ab72124fSSteve Price.Pp 18006b15476eSSheldon HearnIf 180149e11e3aSSheldon Hearn.Fl H 180249e11e3aSSheldon Hearnis specified, the hard limits will be set or displayed. 180349e11e3aSSheldon HearnWhile everybody is allowed to reduce a hard limit, 180449e11e3aSSheldon Hearnonly the superuser can increase it. 180549e11e3aSSheldon HearnThe 180649e11e3aSSheldon Hearn.Fl S 180749e11e3aSSheldon Hearnoption 18087a2afe64SJoerg Wunschspecifies the soft limits instead. When displaying limits, 18096b15476eSSheldon Hearnonly one of 181049e11e3aSSheldon Hearn.Fl S 18116b15476eSSheldon Hearnor 181249e11e3aSSheldon Hearn.Fl H 181349e11e3aSSheldon Hearncan be given. 181449e11e3aSSheldon HearnThe default is to display the soft limits, 181549e11e3aSSheldon Hearnand to set both the hard and the soft limits. 1816ab72124fSSteve Price.Pp 18176b15476eSSheldon HearnOption 181849e11e3aSSheldon Hearn.Fl a 181949e11e3aSSheldon Hearncauses the 182049e11e3aSSheldon Hearn.Ic ulimit 182149e11e3aSSheldon Hearncommand to display all resources. 182249e11e3aSSheldon HearnThe parameter 182349e11e3aSSheldon Hearn.Ar limit 18246b15476eSSheldon Hearnis not acceptable in this mode. 1825ab72124fSSteve Price.Pp 18267a2afe64SJoerg WunschThe remaining options specify which resource value is to be 182749e11e3aSSheldon Hearndisplayed or modified. 182849e11e3aSSheldon HearnThey are mutually exclusive. 182981e9cda2SRuslan Ermilov.Bl -tag -width indent 18303652a236SMartin Cracauer.It Fl b Ar sbsize 18313652a236SMartin CracauerThe maximum size of socket buffer usage, in bytes. 183249e11e3aSSheldon Hearn.It Fl c Ar coredumpsize 18337a2afe64SJoerg WunschThe maximal size of core dump files, in 512-byte blocks. 183449e11e3aSSheldon Hearn.It Fl d Ar datasize 18357a2afe64SJoerg WunschThe maximal size of the data segment of a process, in kilobytes. 183649e11e3aSSheldon Hearn.It Fl f Ar filesize 183749e11e3aSSheldon HearnThe maximal size of a file, in 512-byte blocks. 183849e11e3aSSheldon Hearn.It Fl l Ar lockedmem 18397a2afe64SJoerg WunschThe maximal size of memory that can be locked by a process, in 18407a2afe64SJoerg Wunschkilobytes. 184149e11e3aSSheldon Hearn.It Fl m Ar memoryuse 18427a2afe64SJoerg WunschThe maximal resident set size of a process, in kilobytes. 184349e11e3aSSheldon Hearn.It Fl n Ar nofiles 18447a2afe64SJoerg WunschThe maximal number of descriptors that could be opened by a process. 184549e11e3aSSheldon Hearn.It Fl s Ar stacksize 18467a2afe64SJoerg WunschThe maximal size of the stack segment, in kilobytes. 184749e11e3aSSheldon Hearn.It Fl t Ar time 18487a2afe64SJoerg WunschThe maximal amount of CPU time to be used by each process, in seconds. 184949e11e3aSSheldon Hearn.It Fl u Ar userproc 1850ab72124fSSteve PriceThe maximal number of simultaneous processes for this user ID. 1851ab72124fSSteve Price.El 185249e11e3aSSheldon Hearn.It Ic umask Op Ar mask 185349e11e3aSSheldon HearnSet the file creation mask (see 1854ab72124fSSteve Price.Xr umask 2 ) 185549e11e3aSSheldon Hearnto the octal value specified by 18565d3b843bSSheldon Hearn.Ar mask . 185749e11e3aSSheldon HearnIf the argument is omitted, the current mask value is printed. 185881e9cda2SRuslan Ermilov.It Ic unalias Oo Fl a Oc Op Ar name 18596b15476eSSheldon HearnIf 186049e11e3aSSheldon Hearn.Ar name 18616b15476eSSheldon Hearnis specified, the shell removes that alias. 18626b15476eSSheldon HearnIf 18631b9735e6SSheldon Hearn.Fl a 18646b15476eSSheldon Hearnis specified, all aliases are removed. 18658b34ad92STim J. Robbins.It Xo 18668b34ad92STim J. Robbins.Ic unset 18678b34ad92STim J. Robbins.Op Fl fv 18688b34ad92STim J. Robbins.Ar name ... 18698b34ad92STim J. Robbins.Xc 18708b34ad92STim J. RobbinsThe specified variables or functions are unset and unexported. 18718b34ad92STim J. RobbinsIf the 18728b34ad92STim J. Robbins.Fl v 18738b34ad92STim J. Robbinsoption is specified or no options are given, the 187449e11e3aSSheldon Hearn.Ar name 18758b34ad92STim J. Robbinsarguments are treated as variable names. 18768b34ad92STim J. RobbinsIf the 18778b34ad92STim J. Robbins.Fl f 18788b34ad92STim J. Robbinsoption is specified, the 18798b34ad92STim J. Robbins.Ar name 18808b34ad92STim J. Robbinsarguments are treated as function names. 188149e11e3aSSheldon Hearn.It Ic wait Op Ar job 188249e11e3aSSheldon HearnWait for the specified 188349e11e3aSSheldon Hearn.Ar job 188449e11e3aSSheldon Hearnto complete and return the exit status of the last process in the 188549e11e3aSSheldon Hearn.Ar job . 1886e6d3cf26SSheldon HearnIf the argument is omitted, wait for all jobs to complete 1887e6d3cf26SSheldon Hearnand return an exit status of zero. 1888ab72124fSSteve Price.El 1889ab72124fSSteve Price.Ss Commandline Editing 1890ab72124fSSteve PriceWhen 1891ab72124fSSteve Price.Nm 1892ab72124fSSteve Priceis being used interactively from a terminal, the current command 189349e11e3aSSheldon Hearnand the command history 189449e11e3aSSheldon Hearn(see 189549e11e3aSSheldon Hearn.Ic fc 189649e11e3aSSheldon Hearnin 1897c644db6aSSheldon Hearn.Sx Builtin Commands ) 189849e11e3aSSheldon Hearncan be edited using vi-mode command line editing. 189949e11e3aSSheldon HearnThis mode uses commands similar 19004b88c807SRodney W. Grimesto a subset of those described in the vi man page. 190149e11e3aSSheldon HearnThe command 190249e11e3aSSheldon Hearn.Dq Li set -o vi 190349e11e3aSSheldon Hearn(or 1904e918fc8fSSheldon Hearn.Dq Li set -V ) 190549e11e3aSSheldon Hearnenables vi-mode editing and places 1906ab72124fSSteve Price.Nm 1907ab72124fSSteve Priceinto vi insert mode. With vi-mode enabled, 1908ab72124fSSteve Price.Nm 190949e11e3aSSheldon Hearncan be switched between insert mode and command mode by typing 191049e11e3aSSheldon Hearn.Aq ESC . 191149e11e3aSSheldon HearnHitting 191249e11e3aSSheldon Hearn.Aq return 191349e11e3aSSheldon Hearnwhile in command mode will pass the line to the shell. 1914ab72124fSSteve Price.Pp 191549e11e3aSSheldon HearnSimilarly, the 191649e11e3aSSheldon Hearn.Dq Li set -o emacs 191749e11e3aSSheldon Hearn(or 1918e918fc8fSSheldon Hearn.Dq Li set -E ) 191949e11e3aSSheldon Hearncommand can be used to enable a subset of 1920ab72124fSSteve Priceemacs-style command line editing features. 1921c83e7c50SJoseph Koshy.Sh SEE ALSO 1922c644db6aSSheldon Hearn.Xr builtin 1 , 1923c644db6aSSheldon Hearn.Xr echo 1 , 1924c83e7c50SJoseph Koshy.Xr expr 1 , 19255521ff5aSRuslan Ermilov.Xr pwd 1 , 1926c83e7c50SJoseph Koshy.Xr test 1 1927ab72124fSSteve Price.Sh HISTORY 1928b8923d4cSWolfram SchneiderA 1929ab72124fSSteve Price.Nm 1930b8923d4cSWolfram Schneidercommand appeared in 1931fc482908STim J. Robbins.At v1 . 1932