19ddb49cbSWarner Losh.\"- 24b88c807SRodney W. Grimes.\" Copyright (c) 1991, 1993 34b88c807SRodney W. Grimes.\" The Regents of the University of California. All rights reserved. 44b88c807SRodney W. Grimes.\" 54b88c807SRodney W. Grimes.\" This code is derived from software contributed to Berkeley by 64b88c807SRodney W. Grimes.\" Kenneth Almquist. 74b88c807SRodney W. Grimes.\" 84b88c807SRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without 94b88c807SRodney W. Grimes.\" modification, are permitted provided that the following conditions 104b88c807SRodney W. Grimes.\" are met: 114b88c807SRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright 124b88c807SRodney W. Grimes.\" notice, this list of conditions and the following disclaimer. 134b88c807SRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright 144b88c807SRodney W. Grimes.\" notice, this list of conditions and the following disclaimer in the 154b88c807SRodney W. Grimes.\" documentation and/or other materials provided with the distribution. 164b88c807SRodney W. Grimes.\" 4. Neither the name of the University nor the names of its contributors 174b88c807SRodney W. Grimes.\" may be used to endorse or promote products derived from this software 184b88c807SRodney W. Grimes.\" without specific prior written permission. 194b88c807SRodney W. Grimes.\" 204b88c807SRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 214b88c807SRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 224b88c807SRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 234b88c807SRodney W. Grimes.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 244b88c807SRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 254b88c807SRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 264b88c807SRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 274b88c807SRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 284b88c807SRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 294b88c807SRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 304b88c807SRodney W. Grimes.\" SUCH DAMAGE. 314b88c807SRodney W. Grimes.\" 32ab72124fSSteve Price.\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95 332a456239SPeter Wemm.\" $FreeBSD$ 344b88c807SRodney W. Grimes.\" 35*d6ee26adSJilles Tjoelker.Dd May 20, 2011 36ab72124fSSteve Price.Dt SH 1 37caa2db3bSRuslan Ermilov.Os 38ab72124fSSteve Price.Sh NAME 39ab72124fSSteve Price.Nm sh 40ab72124fSSteve Price.Nd command interpreter (shell) 41ab72124fSSteve Price.Sh SYNOPSIS 42ab72124fSSteve Price.Nm 4357a40f7dSJilles Tjoelker.Op Fl /+abCEefIimnPpTuVvx 44ab72124fSSteve Price.Op Fl /+o Ar longname 4557a40f7dSJilles Tjoelker.Oo 4657a40f7dSJilles Tjoelker.Ar script 4757a40f7dSJilles Tjoelker.Op Ar arg ... 4857a40f7dSJilles Tjoelker.Oc 4957a40f7dSJilles Tjoelker.Nm 5057a40f7dSJilles Tjoelker.Op Fl /+abCEefIimnPpTuVvx 5157a40f7dSJilles Tjoelker.Op Fl /+o Ar longname 5257a40f7dSJilles Tjoelker.Fl c Ar string 5357a40f7dSJilles Tjoelker.Oo 5457a40f7dSJilles Tjoelker.Ar name 5557a40f7dSJilles Tjoelker.Op Ar arg ... 5657a40f7dSJilles Tjoelker.Oc 5757a40f7dSJilles Tjoelker.Nm 5857a40f7dSJilles Tjoelker.Op Fl /+abCEefIimnPpTuVvx 5957a40f7dSJilles Tjoelker.Op Fl /+o Ar longname 6057a40f7dSJilles Tjoelker.Fl s 6181e9cda2SRuslan Ermilov.Op Ar arg ... 62ab72124fSSteve Price.Sh DESCRIPTION 63e918fc8fSSheldon HearnThe 64e918fc8fSSheldon Hearn.Nm 65e918fc8fSSheldon Hearnutility is the standard command interpreter for the system. 66ab72124fSSteve PriceThe current version of 67ab72124fSSteve Price.Nm 683fdfd0a4SJilles Tjoelkeris close to the 693fdfd0a4SJilles Tjoelker.St -p1003.1 70eccea571SRuslan Ermilovspecification for the shell. 713fdfd0a4SJilles TjoelkerIt only supports features 72dfe302abSRuslan Ermilovdesignated by 73dfe302abSRuslan Ermilov.Tn POSIX , 743fdfd0a4SJilles Tjoelkerplus a few Berkeley extensions. 7549e11e3aSSheldon HearnThis man page is not intended to be a tutorial nor a complete 764b88c807SRodney W. Grimesspecification of the shell. 77ab72124fSSteve Price.Ss Overview 784b88c807SRodney W. GrimesThe shell is a command that reads lines from 794b88c807SRodney W. Grimeseither a file or the terminal, interprets them, and 80e6d3cf26SSheldon Hearngenerally executes other commands. 8149e11e3aSSheldon HearnIt is the program that is started when a user logs into the system, 8249e11e3aSSheldon Hearnalthough a user can select a different shell with the 83c644db6aSSheldon Hearn.Xr chsh 1 8449e11e3aSSheldon Hearncommand. 854b88c807SRodney W. GrimesThe shell 864b88c807SRodney W. Grimesimplements a language that has flow control constructs, 874b88c807SRodney W. Grimesa macro facility that provides a variety of features in 888eed22b1SJens Schweikhardtaddition to data storage, along with built-in history and line 89eccea571SRuslan Ermilovediting capabilities. 90eccea571SRuslan ErmilovIt incorporates many features to 914b88c807SRodney W. Grimesaid interactive use and has the advantage that the interpretative 924b88c807SRodney W. Grimeslanguage is common to both interactive and non-interactive 93eccea571SRuslan Ermilovuse (shell scripts). 94eccea571SRuslan ErmilovThat is, commands can be typed directly 9549e11e3aSSheldon Hearnto the running shell or can be put into a file, 9649e11e3aSSheldon Hearnwhich can be executed directly by the shell. 97ab72124fSSteve Price.Ss Invocation 9849e11e3aSSheldon Hearn.\" 9949e11e3aSSheldon Hearn.\" XXX This next sentence is incredibly confusing. 10049e11e3aSSheldon Hearn.\" 10149e11e3aSSheldon HearnIf no arguments are present and if the standard input of the shell 10249e11e3aSSheldon Hearnis connected to a terminal 10349e11e3aSSheldon Hearn(or if the 10449e11e3aSSheldon Hearn.Fl i 10549e11e3aSSheldon Hearnoption is set), 106eccea571SRuslan Ermilovthe shell is considered an interactive shell. 107eccea571SRuslan ErmilovAn interactive shell 1084b88c807SRodney W. Grimesgenerally prompts before each command and handles programming 1094b88c807SRodney W. Grimesand command errors differently (as described below). 1104b88c807SRodney W. GrimesWhen first starting, the shell inspects argument 0, and 11149e11e3aSSheldon Hearnif it begins with a dash 112a762afe5SGiorgos Keramidas.Pq Ql - , 11349e11e3aSSheldon Hearnthe shell is also considered a login shell. 11449e11e3aSSheldon HearnThis is normally done automatically by the system 115eccea571SRuslan Ermilovwhen the user first logs in. 116eccea571SRuslan ErmilovA login shell first reads commands 117ab72124fSSteve Pricefrom the files 118ab72124fSSteve Price.Pa /etc/profile 11949e11e3aSSheldon Hearnand then 120ab72124fSSteve Price.Pa .profile 121dfe302abSRuslan Ermilovin a user's home directory, 122eccea571SRuslan Ermilovif they exist. 123eccea571SRuslan ErmilovIf the environment variable 124ab72124fSSteve Price.Ev ENV 125ab72124fSSteve Priceis set on entry to a shell, or is set in the 126ab72124fSSteve Price.Pa .profile 12749e11e3aSSheldon Hearnof a login shell, the shell then reads commands from the file named in 128ab72124fSSteve Price.Ev ENV . 129ab72124fSSteve PriceTherefore, a user should place commands that are to be executed only 130ab72124fSSteve Priceat login time in the 131ab72124fSSteve Price.Pa .profile 132ab72124fSSteve Pricefile, and commands that are executed for every shell inside the 133ab72124fSSteve Price.Ev ENV 134e6d3cf26SSheldon Hearnfile. 13549e11e3aSSheldon HearnThe user can set the 136ab72124fSSteve Price.Ev ENV 13749e11e3aSSheldon Hearnvariable to some file by placing the following line in the file 138ab72124fSSteve Price.Pa .profile 13949e11e3aSSheldon Hearnin the home directory, 140ab72124fSSteve Pricesubstituting for 141ab72124fSSteve Price.Pa .shinit 14249e11e3aSSheldon Hearnthe filename desired: 14349e11e3aSSheldon Hearn.Pp 144dfe302abSRuslan Ermilov.Dl "ENV=$HOME/.shinit; export ENV" 14549e11e3aSSheldon Hearn.Pp 14649e11e3aSSheldon HearnThe first non-option argument specified on the command line 14749e11e3aSSheldon Hearnwill be treated as the 1484b88c807SRodney W. Grimesname of a file from which to read commands (a shell script), and 1494b88c807SRodney W. Grimesthe remaining arguments are set as the positional parameters 150dfe302abSRuslan Ermilovof the shell 151dfe302abSRuslan Ermilov.Li ( $1 , $2 , 152dfe302abSRuslan Ermilovetc.). 153eccea571SRuslan ErmilovOtherwise, the shell reads commands 1544b88c807SRodney W. Grimesfrom its standard input. 155ea76be72SSteve Price.Pp 156ea76be72SSteve PriceUnlike older versions of 157ea76be72SSteve Price.Nm 158ea76be72SSteve Pricethe 159ea76be72SSteve Price.Ev ENV 160eccea571SRuslan Ermilovscript is only sourced on invocation of interactive shells. 161eccea571SRuslan ErmilovThis 162ea76be72SSteve Pricecloses a well-known, and sometimes easily exploitable security 163ea76be72SSteve Pricehole related to poorly thought out 164ea76be72SSteve Price.Ev ENV 165ea76be72SSteve Pricescripts. 166ab72124fSSteve Price.Ss Argument List Processing 167ab72124fSSteve PriceAll of the single letter options to 168ab72124fSSteve Price.Nm 16949e11e3aSSheldon Hearnhave a corresponding long name, 17049e11e3aSSheldon Hearnwith the exception of 17149e11e3aSSheldon Hearn.Fl c 17249e11e3aSSheldon Hearnand 17349e11e3aSSheldon Hearn.Fl /+o . 17449e11e3aSSheldon HearnThese long names are provided next to the single letter options 17549e11e3aSSheldon Hearnin the descriptions below. 17649e11e3aSSheldon HearnThe long name for an option may be specified as an argument to the 17749e11e3aSSheldon Hearn.Fl /+o 17849e11e3aSSheldon Hearnoption of 179facc6767SRuslan Ermilov.Nm . 18049e11e3aSSheldon HearnOnce the shell is running, 18149e11e3aSSheldon Hearnthe long name for an option may be specified as an argument to the 18249e11e3aSSheldon Hearn.Fl /+o 18349e11e3aSSheldon Hearnoption of the 184c644db6aSSheldon Hearn.Ic set 1858eed22b1SJens Schweikhardtbuilt-in command 18649e11e3aSSheldon Hearn(described later in the section called 1878eed22b1SJens Schweikhardt.Sx Built-in Commands ) . 18849e11e3aSSheldon HearnIntroducing an option with a dash 189a762afe5SGiorgos Keramidas.Pq Ql - 19049e11e3aSSheldon Hearnenables the option, 19149e11e3aSSheldon Hearnwhile using a plus 192a762afe5SGiorgos Keramidas.Pq Ql + 19349e11e3aSSheldon Hearndisables the option. 19449e11e3aSSheldon HearnA 19549e11e3aSSheldon Hearn.Dq Li -- 196fab26805SJoseph Koshyor plain 197dfe302abSRuslan Ermilov.Ql - 198fab26805SJoseph Koshywill stop option processing and will force the remaining 199fab26805SJoseph Koshywords on the command line to be treated as arguments. 20049e11e3aSSheldon HearnThe 20149e11e3aSSheldon Hearn.Fl /+o 20249e11e3aSSheldon Hearnand 20349e11e3aSSheldon Hearn.Fl c 20449e11e3aSSheldon Hearnoptions do not have long names. 20549e11e3aSSheldon HearnThey take arguments and are described after the single letter options. 20681e9cda2SRuslan Ermilov.Bl -tag -width indent 207ab72124fSSteve Price.It Fl a Li allexport 20849e11e3aSSheldon HearnFlag variables for export when assignments are made to them. 209ab72124fSSteve Price.It Fl b Li notify 210ab72124fSSteve PriceEnable asynchronous notification of background job 211ab72124fSSteve Pricecompletion. 212c4d9468eSRuslan Ermilov(UNIMPLEMENTED) 213ab72124fSSteve Price.It Fl C Li noclobber 214c644db6aSSheldon HearnDo not overwrite existing files with 215dfe302abSRuslan Ermilov.Ql > . 216ab72124fSSteve Price.It Fl E Li emacs 2178eed22b1SJens SchweikhardtEnable the built-in 218ab72124fSSteve Price.Xr emacs 1 21949e11e3aSSheldon Hearncommand line editor (disables the 220ab72124fSSteve Price.Fl V 221d5f6b530SJilles Tjoelkeroption if it has been set; 222d5f6b530SJilles Tjoelkerset automatically when interactive on terminals). 223ab72124fSSteve Price.It Fl e Li errexit 22449e11e3aSSheldon HearnExit immediately if any untested command fails in non-interactive mode. 2254b88c807SRodney W. GrimesThe exit status of a command is considered to be 22674053659SStefan Farfelederexplicitly tested if the command is part of the list used to control 2278af11131SRuslan Ermilovan 2288af11131SRuslan Ermilov.Ic if , elif , while , 2298af11131SRuslan Ermilovor 2308af11131SRuslan Ermilov.Ic until ; 2318af11131SRuslan Ermilovif the command is the left 232ab72124fSSteve Pricehand operand of an 23349e11e3aSSheldon Hearn.Dq Li && 234ab72124fSSteve Priceor 23549e11e3aSSheldon Hearn.Dq Li || 23674053659SStefan Farfelederoperator; or if the command is a pipeline preceded by the 23774053659SStefan Farfeleder.Ic !\& 238ab72124fSSteve Priceoperator. 23974053659SStefan FarfelederIf a shell function is executed and its exit status is explicitly 24074053659SStefan Farfeledertested, all commands of the function are considered to be tested as 24174053659SStefan Farfelederwell. 242ab72124fSSteve Price.It Fl f Li noglob 2434b88c807SRodney W. GrimesDisable pathname expansion. 244ab72124fSSteve Price.It Fl I Li ignoreeof 24549e11e3aSSheldon HearnIgnore 246dfe302abSRuslan Ermilov.Dv EOF Ap s 24749e11e3aSSheldon Hearnfrom input when in interactive mode. 248ab72124fSSteve Price.It Fl i Li interactive 249ab72124fSSteve PriceForce the shell to behave interactively. 250ab72124fSSteve Price.It Fl m Li monitor 251ab72124fSSteve PriceTurn on job control (set automatically when interactive). 252ab72124fSSteve Price.It Fl n Li noexec 2534b88c807SRodney W. GrimesIf not interactive, read commands but do not 254eccea571SRuslan Ermilovexecute them. 255eccea571SRuslan ErmilovThis is useful for checking the 2564b88c807SRodney W. Grimessyntax of shell scripts. 2577e1975c2STim J. Robbins.It Fl P Li physical 2587e1975c2STim J. RobbinsChange the default for the 2597e1975c2STim J. Robbins.Ic cd 2607e1975c2STim J. Robbinsand 2617e1975c2STim J. Robbins.Ic pwd 2627e1975c2STim J. Robbinscommands from 2637e1975c2STim J. Robbins.Fl L 2647e1975c2STim J. Robbins(logical directory layout) 2657e1975c2STim J. Robbinsto 2667e1975c2STim J. Robbins.Fl P 2677e1975c2STim J. Robbins(physical directory layout). 268ab72124fSSteve Price.It Fl p Li privileged 269eccea571SRuslan ErmilovTurn on privileged mode. 270eccea571SRuslan ErmilovThis mode is enabled on startup 271dfe302abSRuslan Ermilovif either the effective user or group ID is not equal to the 272dfe302abSRuslan Ermilovreal user or group ID. 273eccea571SRuslan ErmilovTurning this mode off sets the 274dfe302abSRuslan Ermiloveffective user and group IDs to the real user and group IDs. 27549e11e3aSSheldon HearnWhen this mode is enabled for interactive shells, the file 276ab72124fSSteve Price.Pa /etc/suid_profile 27749e11e3aSSheldon Hearnis sourced instead of 27849e11e3aSSheldon Hearn.Pa ~/.profile 279ab72124fSSteve Priceafter 280ab72124fSSteve Price.Pa /etc/profile 28149e11e3aSSheldon Hearnis sourced, and the contents of the 282ab72124fSSteve Price.Ev ENV 28349e11e3aSSheldon Hearnvariable are ignored. 284ab72124fSSteve Price.It Fl s Li stdin 2854b88c807SRodney W. GrimesRead commands from standard input (set automatically 286eccea571SRuslan Ermilovif no file arguments are present). 287eccea571SRuslan ErmilovThis option has 2884b88c807SRodney W. Grimesno effect when set after the shell has already started 289eccea571SRuslan Ermilovrunning (i.e., when set with the 290c644db6aSSheldon Hearn.Ic set 2916b15476eSSheldon Hearncommand). 292304199ceSTom Rhodes.It Fl T Li trapsasync 293e6d3cf26SSheldon HearnWhen waiting for a child, execute traps immediately. 294e6d3cf26SSheldon HearnIf this option is not set, 295e6d3cf26SSheldon Hearntraps are executed after the child exits, 296e6d3cf26SSheldon Hearnas specified in 297af5eb6fcSMaxim Konovalov.St -p1003.2 . 2986b15476eSSheldon HearnThis nonstandard option is useful for putting guarding shells around 299eccea571SRuslan Ermilovchildren that block signals. 300eccea571SRuslan ErmilovThe surrounding shell may kill the child 3016b15476eSSheldon Hearnor it may just return control to the tty and leave the child alone, 3026b15476eSSheldon Hearnlike this: 303c1c72a3cSMartin Cracauer.Bd -literal -offset indent 304c1c72a3cSMartin Cracauersh -T -c "trap 'exit 1' 2 ; some-blocking-program" 305c1c72a3cSMartin Cracauer.Ed 306ab72124fSSteve Price.It Fl u Li nounset 307ab72124fSSteve PriceWrite a message to standard error when attempting 30864254a66SJilles Tjoelkerto expand a variable, a positional parameter or 30964254a66SJilles Tjoelkerthe special parameter 31064254a66SJilles Tjoelker.Va \&! 31164254a66SJilles Tjoelkerthat is not set, and if the 312ab72124fSSteve Priceshell is not interactive, exit immediately. 313ab72124fSSteve Price.It Fl V Li vi 3148eed22b1SJens SchweikhardtEnable the built-in 315ab72124fSSteve Price.Xr vi 1 316ab72124fSSteve Pricecommand line editor (disables 317ab72124fSSteve Price.Fl E 318ab72124fSSteve Priceif it has been set). 319ab72124fSSteve Price.It Fl v Li verbose 320ab72124fSSteve PriceThe shell writes its input to standard error 321eccea571SRuslan Ermilovas it is read. 322eccea571SRuslan ErmilovUseful for debugging. 323ab72124fSSteve Price.It Fl x Li xtrace 32449e11e3aSSheldon HearnWrite each command 325120c8e6cSStefan Farfeleder(preceded by the value of the 326dfe302abSRuslan Ermilov.Va PS4 327120c8e6cSStefan Farfeledervariable) 32849e11e3aSSheldon Hearnto standard error before it is executed. 32949e11e3aSSheldon HearnUseful for debugging. 33049e11e3aSSheldon Hearn.El 33149e11e3aSSheldon Hearn.Pp 33249e11e3aSSheldon HearnThe 333fd50df30SRuslan Ermilov.Fl c 334fd50df30SRuslan Ermilovoption causes the commands to be read from the 335fd50df30SRuslan Ermilov.Ar string 336fd50df30SRuslan Ermilovoperand instead of from the standard input. 337fd50df30SRuslan ErmilovKeep in mind that this option only accepts a single string as its 338fd50df30SRuslan Ermilovargument, hence multi-word strings must be quoted. 339fd50df30SRuslan Ermilov.Pp 340fd50df30SRuslan ErmilovThe 34149e11e3aSSheldon Hearn.Fl /+o 34249e11e3aSSheldon Hearnoption takes as its only argument the long name of an option 34349e11e3aSSheldon Hearnto be enabled or disabled. 34449e11e3aSSheldon HearnFor example, the following two invocations of 34549e11e3aSSheldon Hearn.Nm 3468eed22b1SJens Schweikhardtboth enable the built-in 34749e11e3aSSheldon Hearn.Xr emacs 1 34849e11e3aSSheldon Hearncommand line editor: 34949e11e3aSSheldon Hearn.Bd -literal -offset indent 35049e11e3aSSheldon Hearnset -E 35149e11e3aSSheldon Hearnset -o emacs 35249e11e3aSSheldon Hearn.Ed 353d513af6aSTim J. Robbins.Pp 354d513af6aSTim J. RobbinsIf used without an argument, the 355d513af6aSTim J. Robbins.Fl o 356d513af6aSTim J. Robbinsoption displays the current option settings in a human-readable format. 357d513af6aSTim J. RobbinsIf 358d513af6aSTim J. Robbins.Cm +o 359d513af6aSTim J. Robbinsis used without an argument, the current option settings are output 360d513af6aSTim J. Robbinsin a format suitable for re-input into the shell. 361ab72124fSSteve Price.Ss Lexical Structure 3624b88c807SRodney W. GrimesThe shell reads input in terms of lines from a file and breaks 3634b88c807SRodney W. Grimesit up into words at whitespace (blanks and tabs), and at 3644b88c807SRodney W. Grimescertain sequences of 3656b15476eSSheldon Hearncharacters called 3666b15476eSSheldon Hearn.Dq operators , 3676b15476eSSheldon Hearnwhich are special to the shell. 3684b88c807SRodney W. GrimesThere are two types of operators: control operators and 3694b88c807SRodney W. Grimesredirection operators (their meaning is discussed later). 370ab72124fSSteve PriceThe following is a list of valid operators: 37181e9cda2SRuslan Ermilov.Bl -tag -width indent 37249e11e3aSSheldon Hearn.It Control operators: 37349e11e3aSSheldon Hearn.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact 37481e9cda2SRuslan Ermilov.It Li & Ta Li && Ta Li ( Ta Li ) Ta Li \en 37581e9cda2SRuslan Ermilov.It Li ;; Ta Li ; Ta Li | Ta Li || 37649e11e3aSSheldon Hearn.El 37749e11e3aSSheldon Hearn.It Redirection operators: 3784682f420SBrian Somers.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact 37981e9cda2SRuslan Ermilov.It Li < Ta Li > Ta Li << Ta Li >> Ta Li <> 38081e9cda2SRuslan Ermilov.It Li <& Ta Li >& Ta Li <<- Ta Li >| 38149e11e3aSSheldon Hearn.El 382ab72124fSSteve Price.El 3837d1a55fcSStefan Farfeleder.Pp 3847d1a55fcSStefan FarfelederThe character 3857d1a55fcSStefan Farfeleder.Ql # 3867d1a55fcSStefan Farfelederintroduces a comment if used at the beginning of a word. 3877d1a55fcSStefan FarfelederThe word starting with 3887d1a55fcSStefan Farfeleder.Ql # 3897d1a55fcSStefan Farfelederand the rest of the line are ignored. 3909764aa41SJilles Tjoelker.Pp 3919764aa41SJilles Tjoelker.Tn ASCII 3929764aa41SJilles Tjoelker.Dv NUL 3939764aa41SJilles Tjoelkercharacters (character code 0) are not allowed in shell input. 394ab72124fSSteve Price.Ss Quoting 3954b88c807SRodney W. GrimesQuoting is used to remove the special meaning of certain characters 396a886288eSJesus R. Camouor words to the shell, such as operators, whitespace, keywords, 397a886288eSJesus R. Camouor alias names. 398a886288eSJesus R. Camou.Pp 399a62ab027SJilles TjoelkerThere are four types of quoting: matched single quotes, 400a62ab027SJilles Tjoelkerdollar-single quotes, 4014b88c807SRodney W. Grimesmatched double quotes, and backslash. 40281e9cda2SRuslan Ermilov.Bl -tag -width indent 403ab72124fSSteve Price.It Single Quotes 4044b88c807SRodney W. GrimesEnclosing characters in single quotes preserves the literal 405ab0a2172SSteve Pricemeaning of all the characters (except single quotes, making 406ab0a2172SSteve Priceit impossible to put single-quotes in a single-quoted string). 407a62ab027SJilles Tjoelker.It Dollar-Single Quotes 408a62ab027SJilles TjoelkerEnclosing characters between 409a62ab027SJilles Tjoelker.Li $' 410a62ab027SJilles Tjoelkerand 411a62ab027SJilles Tjoelker.Li ' 412a62ab027SJilles Tjoelkerpreserves the literal meaning of all characters 413a62ab027SJilles Tjoelkerexcept backslashes and single quotes. 414a62ab027SJilles TjoelkerA backslash introduces a C-style escape sequence: 415a62ab027SJilles Tjoelker.Bl -tag -width xUnnnnnnnn 416a62ab027SJilles Tjoelker.It \ea 417a62ab027SJilles TjoelkerAlert (ring the terminal bell) 418a62ab027SJilles Tjoelker.It \eb 419a62ab027SJilles TjoelkerBackspace 420a62ab027SJilles Tjoelker.It \ec Ns Ar c 421a62ab027SJilles TjoelkerThe control character denoted by 422a62ab027SJilles Tjoelker.Li ^ Ns Ar c 423a62ab027SJilles Tjoelkerin 424a62ab027SJilles Tjoelker.Xr stty 1 . 425a62ab027SJilles TjoelkerIf 426a62ab027SJilles Tjoelker.Ar c 427a62ab027SJilles Tjoelkeris a backslash, it must be doubled. 428a62ab027SJilles Tjoelker.It \ee 429a62ab027SJilles TjoelkerThe ESC character 430a62ab027SJilles Tjoelker.Tn ( ASCII 431a62ab027SJilles Tjoelker0x1b) 432a62ab027SJilles Tjoelker.It \ef 433a62ab027SJilles TjoelkerFormfeed 434a62ab027SJilles Tjoelker.It \en 435a62ab027SJilles TjoelkerNewline 436a62ab027SJilles Tjoelker.It \er 437a62ab027SJilles TjoelkerCarriage return 438a62ab027SJilles Tjoelker.It \et 439a62ab027SJilles TjoelkerHorizontal tab 440a62ab027SJilles Tjoelker.It \ev 441a62ab027SJilles TjoelkerVertical tab 442a62ab027SJilles Tjoelker.It \e\e 443a62ab027SJilles TjoelkerLiteral backslash 444a62ab027SJilles Tjoelker.It \e\&' 445a62ab027SJilles TjoelkerLiteral single-quote 446a62ab027SJilles Tjoelker.It \e\&" 447a62ab027SJilles TjoelkerLiteral double-quote 448a62ab027SJilles Tjoelker.It \e Ns Ar nnn 449a62ab027SJilles TjoelkerThe byte whose octal value is 450a62ab027SJilles Tjoelker.Ar nnn 451a62ab027SJilles Tjoelker(one to three digits) 452a62ab027SJilles Tjoelker.It \ex Ns Ar nn 453a62ab027SJilles TjoelkerThe byte whose hexadecimal value is 454a62ab027SJilles Tjoelker.Ar nn 455a62ab027SJilles Tjoelker(one or more digits only the last two of which are used) 456a62ab027SJilles Tjoelker.It \eu Ns Ar nnnn 457a62ab027SJilles TjoelkerThe Unicode code point 458a62ab027SJilles Tjoelker.Ar nnnn 459a62ab027SJilles Tjoelker(four hexadecimal digits) 460a62ab027SJilles Tjoelker.It \eU Ns Ar nnnnnnnn 461a62ab027SJilles TjoelkerThe Unicode code point 462a62ab027SJilles Tjoelker.Ar nnnnnnnn 463a62ab027SJilles Tjoelker(eight hexadecimal digits) 464a62ab027SJilles Tjoelker.El 465a62ab027SJilles Tjoelker.Pp 46607eb7033SJilles TjoelkerThe sequences for Unicode code points are currently only useful with 46707eb7033SJilles TjoelkerUTF-8 locales. 468a62ab027SJilles TjoelkerThey reject code point 0 and UTF-16 surrogates. 469a62ab027SJilles Tjoelker.Pp 470a62ab027SJilles TjoelkerIf an escape sequence would produce a byte with value 0, 471a62ab027SJilles Tjoelkerthat byte and the rest of the string until the matching single-quote 472a62ab027SJilles Tjoelkerare ignored. 473a62ab027SJilles Tjoelker.Pp 474a62ab027SJilles TjoelkerAny other string starting with a backslash is an error. 475ab72124fSSteve Price.It Double Quotes 4764b88c807SRodney W. GrimesEnclosing characters within double quotes preserves the literal 47749e11e3aSSheldon Hearnmeaning of all characters except dollar sign 478a762afe5SGiorgos Keramidas.Pq Ql $ , 47949e11e3aSSheldon Hearnbackquote 480a762afe5SGiorgos Keramidas.Pq Ql ` , 48149e11e3aSSheldon Hearnand backslash 482a762afe5SGiorgos Keramidas.Pq Ql \e . 48349e11e3aSSheldon HearnThe backslash inside double quotes is historically weird. 48449e11e3aSSheldon HearnIt remains literal unless it precedes the following characters, 48549e11e3aSSheldon Hearnwhich it serves to quote: 48649e11e3aSSheldon Hearn.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact 48781e9cda2SRuslan Ermilov.It Li $ Ta Li ` Ta Li \&" Ta Li \e\ Ta Li \en 48849e11e3aSSheldon Hearn.El 489ab72124fSSteve Price.It Backslash 490ab72124fSSteve PriceA backslash preserves the literal meaning of the following 49149e11e3aSSheldon Hearncharacter, with the exception of the newline character 492a762afe5SGiorgos Keramidas.Pq Ql \en . 49349e11e3aSSheldon HearnA backslash preceding a newline is treated as a line continuation. 494ab72124fSSteve Price.El 495d806a030SJilles Tjoelker.Ss Keywords 496d806a030SJilles TjoelkerKeywords or reserved words are words that have special meaning to the 4974b88c807SRodney W. Grimesshell and are recognized at the beginning of a line and 498eccea571SRuslan Ermilovafter a control operator. 499d806a030SJilles TjoelkerThe following are keywords: 50049e11e3aSSheldon Hearn.Bl -column "doneXX" "elifXX" "elseXX" "untilXX" "whileX" -offset center 50181e9cda2SRuslan Ermilov.It Li \&! Ta { Ta } Ta Ic case Ta Ic do 50281e9cda2SRuslan Ermilov.It Ic done Ta Ic elif Ta Ic else Ta Ic esac Ta Ic fi 50381e9cda2SRuslan Ermilov.It Ic for Ta Ic if Ta Ic then Ta Ic until Ta Ic while 50449e11e3aSSheldon Hearn.El 505ab72124fSSteve Price.Ss Aliases 506ab72124fSSteve PriceAn alias is a name and corresponding value set using the 507c644db6aSSheldon Hearn.Ic alias 508eccea571SRuslan Ermilovbuilt-in command. 509d806a030SJilles TjoelkerWhenever a keyword may occur (see above), 510d806a030SJilles Tjoelkerand after checking for keywords, the shell 511e6d3cf26SSheldon Hearnchecks the word to see if it matches an alias. 512e6d3cf26SSheldon HearnIf it does, it replaces it in the input stream with its value. 513e6d3cf26SSheldon HearnFor example, if there is an alias called 51449e11e3aSSheldon Hearn.Dq Li lf 5156b15476eSSheldon Hearnwith the value 516dfe302abSRuslan Ermilov.Dq Li "ls -F" , 5174b88c807SRodney W. Grimesthen the input 518dfe302abSRuslan Ermilov.Pp 519dfe302abSRuslan Ermilov.Dl "lf foobar" 520ab72124fSSteve Price.Pp 5214b88c807SRodney W. Grimeswould become 522dfe302abSRuslan Ermilov.Pp 523dfe302abSRuslan Ermilov.Dl "ls -F foobar" 524ab72124fSSteve Price.Pp 5254b88c807SRodney W. GrimesAliases provide a convenient way for naive users to 5264b88c807SRodney W. Grimescreate shorthands for commands without having to learn how 527eccea571SRuslan Ermilovto create functions with arguments. 52812dfb7a5SJilles TjoelkerUsing aliases in scripts is discouraged 52912dfb7a5SJilles Tjoelkerbecause the command that defines them must be executed 53012dfb7a5SJilles Tjoelkerbefore the code that uses them is parsed. 53112dfb7a5SJilles TjoelkerThis is fragile and not portable. 532a886288eSJesus R. Camou.Pp 533a886288eSJesus R. CamouAn alias name may be escaped in a command line, so that it is not 534a886288eSJesus R. Camoureplaced by its alias value, by using quoting characters within or 53519162ab9SJesus R. Camouadjacent to the alias name. 53619162ab9SJesus R. CamouThis is most often done by prefixing 537a886288eSJesus R. Camouan alias name with a backslash to execute a function, built-in, or 538a886288eSJesus R. Camounormal program with the same name. 539a886288eSJesus R. CamouSee the 540a886288eSJesus R. Camou.Sx Quoting 541a886288eSJesus R. Camousubsection. 542ab72124fSSteve Price.Ss Commands 5434b88c807SRodney W. GrimesThe shell interprets the words it reads according to a 5444b88c807SRodney W. Grimeslanguage, the specification of which is outside the scope 545ab72124fSSteve Priceof this man page (refer to the BNF in the 546ab72124fSSteve Price.St -p1003.2 547eccea571SRuslan Ermilovdocument). 548eccea571SRuslan ErmilovEssentially though, a line is read and if 5494b88c807SRodney W. Grimesthe first word of the line (or after a control operator) 550d806a030SJilles Tjoelkeris not a keyword, then the shell has recognized a 551eccea571SRuslan Ermilovsimple command. 552eccea571SRuslan ErmilovOtherwise, a complex command or some 5534b88c807SRodney W. Grimesother special construct may have been recognized. 554ab72124fSSteve Price.Ss Simple Commands 5554b88c807SRodney W. GrimesIf a simple command has been recognized, the shell performs 5564b88c807SRodney W. Grimesthe following actions: 557ab72124fSSteve Price.Bl -enum 558ab72124fSSteve Price.It 5596b15476eSSheldon HearnLeading words of the form 56049e11e3aSSheldon Hearn.Dq Li name=value 5616b15476eSSheldon Hearnare stripped off and assigned to the environment of 562eccea571SRuslan Ermilovthe simple command. 563eccea571SRuslan ErmilovRedirection operators and 5644b88c807SRodney W. Grimestheir arguments (as described below) are stripped 5654b88c807SRodney W. Grimesoff and saved for processing. 566ab72124fSSteve Price.It 567ab72124fSSteve PriceThe remaining words are expanded as described in 5686b15476eSSheldon Hearnthe section called 5696b15476eSSheldon Hearn.Sx Word Expansions , 5706b15476eSSheldon Hearnand the first remaining word is considered the command 571eccea571SRuslan Ermilovname and the command is located. 572eccea571SRuslan ErmilovThe remaining 5734b88c807SRodney W. Grimeswords are considered the arguments of the command. 5746b15476eSSheldon HearnIf no command name resulted, then the 57549e11e3aSSheldon Hearn.Dq Li name=value 5764b88c807SRodney W. Grimesvariable assignments recognized in 1) affect the 5774b88c807SRodney W. Grimescurrent shell. 578ab72124fSSteve Price.It 579ab72124fSSteve PriceRedirections are performed as described in 5804b88c807SRodney W. Grimesthe next section. 581ab72124fSSteve Price.El 582ab72124fSSteve Price.Ss Redirections 5834b88c807SRodney W. GrimesRedirections are used to change where a command reads its input 584eccea571SRuslan Ermilovor sends its output. 585eccea571SRuslan ErmilovIn general, redirections open, close, or 586eccea571SRuslan Ermilovduplicate an existing reference to a file. 587eccea571SRuslan ErmilovThe overall format 5884b88c807SRodney W. Grimesused for redirection is: 58949e11e3aSSheldon Hearn.Pp 590dfe302abSRuslan Ermilov.D1 Oo Ar n Oc Ar redir-op file 59149e11e3aSSheldon Hearn.Pp 59249e11e3aSSheldon HearnThe 593dfe302abSRuslan Ermilov.Ar redir-op 59449e11e3aSSheldon Hearnis one of the redirection operators mentioned 595eccea571SRuslan Ermilovpreviously. 596eccea571SRuslan ErmilovThe following gives some examples of how these 59749e11e3aSSheldon Hearnoperators can be used. 59849e11e3aSSheldon HearnNote that stdin and stdout are commonly used abbreviations 59949e11e3aSSheldon Hearnfor standard input and standard output respectively. 60049e11e3aSSheldon Hearn.Bl -tag -width "1234567890XX" -offset indent 601dfe302abSRuslan Ermilov.It Oo Ar n Oc Ns Li > Ar file 602dfe302abSRuslan Ermilovredirect stdout (or file descriptor 603dfe302abSRuslan Ermilov.Ar n ) 604dfe302abSRuslan Ermilovto 605dfe302abSRuslan Ermilov.Ar file 606dfe302abSRuslan Ermilov.It Oo Ar n Oc Ns Li >| Ar file 60749e11e3aSSheldon Hearnsame as above, but override the 60849e11e3aSSheldon Hearn.Fl C 60949e11e3aSSheldon Hearnoption 610dfe302abSRuslan Ermilov.It Oo Ar n Oc Ns Li >> Ar file 611dfe302abSRuslan Ermilovappend stdout (or file descriptor 612dfe302abSRuslan Ermilov.Ar n ) 613dfe302abSRuslan Ermilovto 614dfe302abSRuslan Ermilov.Ar file 615dfe302abSRuslan Ermilov.It Oo Ar n Oc Ns Li < Ar file 616dfe302abSRuslan Ermilovredirect stdin (or file descriptor 617dfe302abSRuslan Ermilov.Ar n ) 618dfe302abSRuslan Ermilovfrom 619dfe302abSRuslan Ermilov.Ar file 620dfe302abSRuslan Ermilov.It Oo Ar n Oc Ns Li <> Ar file 621dfe302abSRuslan Ermilovredirect stdin (or file descriptor 622dfe302abSRuslan Ermilov.Ar n ) 623dfe302abSRuslan Ermilovto and from 624dfe302abSRuslan Ermilov.Ar file 625dfe302abSRuslan Ermilov.It Oo Ar n1 Oc Ns Li <& Ns Ar n2 626dfe302abSRuslan Ermilovduplicate stdin (or file descriptor 627dfe302abSRuslan Ermilov.Ar n1 ) 628dfe302abSRuslan Ermilovfrom file descriptor 629dfe302abSRuslan Ermilov.Ar n2 630dfe302abSRuslan Ermilov.It Oo Ar n Oc Ns Li <&- 631dfe302abSRuslan Ermilovclose stdin (or file descriptor 632dfe302abSRuslan Ermilov.Ar n ) 633dfe302abSRuslan Ermilov.It Oo Ar n1 Oc Ns Li >& Ns Ar n2 634dfe302abSRuslan Ermilovduplicate stdout (or file descriptor 635dfe302abSRuslan Ermilov.Ar n1 ) 636dfe302abSRuslan Ermilovto file descriptor 637dfe302abSRuslan Ermilov.Ar n2 638dfe302abSRuslan Ermilov.It Oo Ar n Oc Ns Li >&- 639dfe302abSRuslan Ermilovclose stdout (or file descriptor 640dfe302abSRuslan Ermilov.Ar n ) 641ab72124fSSteve Price.El 642ab72124fSSteve Price.Pp 6436b15476eSSheldon HearnThe following redirection is often called a 6446b15476eSSheldon Hearn.Dq here-document . 645dfe302abSRuslan Ermilov.Bd -unfilled -offset indent 646dfe302abSRuslan Ermilov.Oo Ar n Oc Ns Li << Ar delimiter 647dfe302abSRuslan Ermilov.D1 Ar here-doc-text 648dfe302abSRuslan Ermilov.D1 ... 649dfe302abSRuslan Ermilov.Ar delimiter 650ab72124fSSteve Price.Ed 651ab72124fSSteve Price.Pp 6524b88c807SRodney W. GrimesAll the text on successive lines up to the delimiter is 6534b88c807SRodney W. Grimessaved away and made available to the command on standard 654dfe302abSRuslan Ermilovinput, or file descriptor 655dfe302abSRuslan Ermilov.Ar n 656dfe302abSRuslan Ermilovif it is specified. 657dfe302abSRuslan ErmilovIf the 658dfe302abSRuslan Ermilov.Ar delimiter 659dfe302abSRuslan Ermilovas specified on the initial line is quoted, then the 660dfe302abSRuslan Ermilov.Ar here-doc-text 6614b88c807SRodney W. Grimesis treated literally, otherwise the text is subjected to 6624b88c807SRodney W. Grimesparameter expansion, command substitution, and arithmetic 6636b15476eSSheldon Hearnexpansion (as described in the section on 6646b15476eSSheldon Hearn.Sx Word Expansions ) . 6656b15476eSSheldon HearnIf the operator is 66649e11e3aSSheldon Hearn.Dq Li <<- 6676b15476eSSheldon Hearninstead of 66849e11e3aSSheldon Hearn.Dq Li << , 6696b15476eSSheldon Hearnthen leading tabs 670dfe302abSRuslan Ermilovin the 671dfe302abSRuslan Ermilov.Ar here-doc-text 672dfe302abSRuslan Ermilovare stripped. 673ab72124fSSteve Price.Ss Search and Execution 674ab0a2172SSteve PriceThere are three types of commands: shell functions, 6758eed22b1SJens Schweikhardtbuilt-in commands, and normal programs. 67649e11e3aSSheldon HearnThe command is searched for (by name) in that order. 67749e11e3aSSheldon HearnThe three types of commands are all executed in a different way. 678ab72124fSSteve Price.Pp 679ab0a2172SSteve PriceWhen a shell function is executed, all of the shell positional 680dfe302abSRuslan Ermilovparameters (except 681dfe302abSRuslan Ermilov.Li $0 , 682dfe302abSRuslan Ermilovwhich remains unchanged) are 6834b88c807SRodney W. Grimesset to the arguments of the shell function. 6844b88c807SRodney W. GrimesThe variables which are explicitly placed in the environment of 6854b88c807SRodney W. Grimesthe command (by placing assignments to them before the 6864b88c807SRodney W. Grimesfunction name) are made local to the function and are set 687e6d3cf26SSheldon Hearnto the values given. 688e6d3cf26SSheldon HearnThen the command given in the function definition is executed. 689e6d3cf26SSheldon HearnThe positional parameters are restored to their original values 690e6d3cf26SSheldon Hearnwhen the command completes. 691ab0a2172SSteve PriceThis all occurs within the current shell. 692ab72124fSSteve Price.Pp 6938eed22b1SJens SchweikhardtShell built-in commands are executed internally to the shell, without 694ab0a2172SSteve Pricespawning a new process. 69530268dfaSJilles TjoelkerThere are two kinds of built-in commands: regular and special. 69630268dfaSJilles TjoelkerAssignments before special builtins persist after they finish 69730268dfaSJilles Tjoelkerexecuting and assignment errors, redirection errors and certain 69830268dfaSJilles Tjoelkeroperand errors cause a script to be aborted. 6994b985a89SJilles TjoelkerSpecial builtins cannot be overridden with a function. 70030268dfaSJilles TjoelkerBoth regular and special builtins can affect the shell in ways 70130268dfaSJilles Tjoelkernormal programs cannot. 702ab72124fSSteve Price.Pp 703c644db6aSSheldon HearnOtherwise, if the command name does not match a function 7048eed22b1SJens Schweikhardtor built-in command, the command is searched for as a normal 7054b88c807SRodney W. Grimesprogram in the file system (as described in the next section). 7064b88c807SRodney W. GrimesWhen a normal program is executed, the shell runs the program, 707e6d3cf26SSheldon Hearnpassing the arguments and the environment to the program. 708e6d3cf26SSheldon HearnIf the program is not a normal executable file 709eccea571SRuslan Ermilov(i.e., if it does not begin with the 710dfe302abSRuslan Ermilov.Dq "magic number" 7116b15476eSSheldon Hearnwhose 712ab72124fSSteve Price.Tn ASCII 7136b15476eSSheldon Hearnrepresentation is 714dfe302abSRuslan Ermilov.Dq Li #! , 71549e11e3aSSheldon Hearnresulting in an 716b4f0f4aaSRuslan Ermilov.Er ENOEXEC 71749e11e3aSSheldon Hearnreturn value from 71849e11e3aSSheldon Hearn.Xr execve 2 ) 719604e8224SJilles Tjoelkerbut appears to be a text file, 7203835f47cSJilles Tjoelkerthe shell will run a new instance of 7213835f47cSJilles Tjoelker.Nm 7223835f47cSJilles Tjoelkerto interpret it. 723ab72124fSSteve Price.Pp 724ab0a2172SSteve PriceNote that previous versions of this document 725ab0a2172SSteve Priceand the source code itself misleadingly and sporadically 726ab0a2172SSteve Pricerefer to a shell script without a magic number 7276b15476eSSheldon Hearnas a 728dfe302abSRuslan Ermilov.Dq "shell procedure" . 729ab72124fSSteve Price.Ss Path Search 7304b88c807SRodney W. GrimesWhen locating a command, the shell first looks to see if 731eccea571SRuslan Ermilovit has a shell function by that name. 732eccea571SRuslan ErmilovThen it looks for a 733eccea571SRuslan Ermilovbuilt-in command by that name. 734eccea571SRuslan ErmilovIf a built-in command is not found, 735ab0a2172SSteve Priceone of two things happen: 736ab72124fSSteve Price.Bl -enum 737ab72124fSSteve Price.It 738ab72124fSSteve PriceCommand names containing a slash are simply executed without 739ab0a2172SSteve Priceperforming any searches. 740ab72124fSSteve Price.It 741dfe302abSRuslan ErmilovThe shell searches each entry in the 7421788b7ffSJilles Tjoelker.Va PATH 7431788b7ffSJilles Tjoelkervariable 744eccea571SRuslan Ermilovin turn for the command. 745eccea571SRuslan ErmilovThe value of the 7461788b7ffSJilles Tjoelker.Va PATH 747ab72124fSSteve Pricevariable should be a series of 748eccea571SRuslan Ermiloventries separated by colons. 749eccea571SRuslan ErmilovEach entry consists of a 7504b88c807SRodney W. Grimesdirectory name. 7514b88c807SRodney W. GrimesThe current directory 752ab0a2172SSteve Pricemay be indicated implicitly by an empty directory name, 753ab0a2172SSteve Priceor explicitly by a single period. 754ab72124fSSteve Price.El 755ab72124fSSteve Price.Ss Command Exit Status 7564b88c807SRodney W. GrimesEach command has an exit status that can influence the behavior 757eccea571SRuslan Ermilovof other shell commands. 758eccea571SRuslan ErmilovThe paradigm is that a command exits 7594b88c807SRodney W. Grimeswith zero for normal or success, and non-zero for failure, 760eccea571SRuslan Ermiloverror, or a false indication. 761eccea571SRuslan ErmilovThe man page for each command 7624b88c807SRodney W. Grimesshould indicate the various exit codes and what they mean. 7638eed22b1SJens SchweikhardtAdditionally, the built-in commands return exit codes, as does 764ab0a2172SSteve Pricean executed shell function. 765c351e089SJohn Polstra.Pp 766c351e089SJohn PolstraIf a command is terminated by a signal, its exit status is 128 plus 767eccea571SRuslan Ermilovthe signal number. 768eccea571SRuslan ErmilovSignal numbers are defined in the header file 769fe08efe6SRuslan Ermilov.In sys/signal.h . 770ab72124fSSteve Price.Ss Complex Commands 7714b88c807SRodney W. GrimesComplex commands are combinations of simple commands 772d806a030SJilles Tjoelkerwith control operators or keywords, together creating a larger complex 773eccea571SRuslan Ermilovcommand. 774eccea571SRuslan ErmilovMore generally, a command is one of the following: 775ab72124fSSteve Price.Bl -item -offset indent 776ab72124fSSteve Price.It 777ab72124fSSteve Pricesimple command 778ab72124fSSteve Price.It 779ab72124fSSteve Pricepipeline 780ab72124fSSteve Price.It 781ab72124fSSteve Pricelist or compound-list 782ab72124fSSteve Price.It 783ab72124fSSteve Pricecompound command 784ab72124fSSteve Price.It 785ab72124fSSteve Pricefunction definition 786ab72124fSSteve Price.El 787ab72124fSSteve Price.Pp 7884b88c807SRodney W. GrimesUnless otherwise stated, the exit status of a command is 7894b88c807SRodney W. Grimesthat of the last simple command executed by the command. 790ab72124fSSteve Price.Ss Pipelines 7914b88c807SRodney W. GrimesA pipeline is a sequence of one or more commands separated 792dfe302abSRuslan Ermilovby the control operator 793dfe302abSRuslan Ermilov.Ql \&| . 794eccea571SRuslan ErmilovThe standard output of all but 7954b88c807SRodney W. Grimesthe last command is connected to the standard input 796eccea571SRuslan Ermilovof the next command. 797eccea571SRuslan ErmilovThe standard output of the last 798ab0a2172SSteve Pricecommand is inherited from the shell, as usual. 799ab72124fSSteve Price.Pp 8004b88c807SRodney W. GrimesThe format for a pipeline is: 80149e11e3aSSheldon Hearn.Pp 802dfe302abSRuslan Ermilov.D1 Oo Li \&! Oc Ar command1 Op Li \&| Ar command2 ... 803ab72124fSSteve Price.Pp 804dfe302abSRuslan ErmilovThe standard output of 805dfe302abSRuslan Ermilov.Ar command1 806dfe302abSRuslan Ermilovis connected to the standard input of 807dfe302abSRuslan Ermilov.Ar command2 . 808eccea571SRuslan ErmilovThe standard input, standard output, or 8094b88c807SRodney W. Grimesboth of a command is considered to be assigned by the 8104b88c807SRodney W. Grimespipeline before any redirection specified by redirection 8114b88c807SRodney W. Grimesoperators that are part of the command. 812ab72124fSSteve Price.Pp 813f1ec0581SJilles TjoelkerNote that unlike some other shells, 814f1ec0581SJilles Tjoelker.Nm 815f1ec0581SJilles Tjoelkerexecutes each process in a pipeline with more than one command 816f1ec0581SJilles Tjoelkerin a subshell environment and as a child of the 817f1ec0581SJilles Tjoelker.Nm 818f1ec0581SJilles Tjoelkerprocess. 819f1ec0581SJilles Tjoelker.Pp 8204b88c807SRodney W. GrimesIf the pipeline is not in the background (discussed later), 8214b88c807SRodney W. Grimesthe shell waits for all commands to complete. 822ab72124fSSteve Price.Pp 823d806a030SJilles TjoelkerIf the keyword 824eccea571SRuslan Ermilov.Ic !\& 825eccea571SRuslan Ermilovdoes not precede the pipeline, the 8264b88c807SRodney W. Grimesexit status is the exit status of the last command specified 827eccea571SRuslan Ermilovin the pipeline. 828eccea571SRuslan ErmilovOtherwise, the exit status is the logical 829eccea571SRuslan ErmilovNOT of the exit status of the last command. 830eccea571SRuslan ErmilovThat is, if 8314b88c807SRodney W. Grimesthe last command returns zero, the exit status is 1; if 8324b88c807SRodney W. Grimesthe last command returns greater than zero, the exit status 8334b88c807SRodney W. Grimesis zero. 834ab72124fSSteve Price.Pp 8354b88c807SRodney W. GrimesBecause pipeline assignment of standard input or standard 8364b88c807SRodney W. Grimesoutput or both takes place before redirection, it can be 837eccea571SRuslan Ermilovmodified by redirection. 838eccea571SRuslan ErmilovFor example: 839ab72124fSSteve Price.Pp 840dfe302abSRuslan Ermilov.Dl "command1 2>&1 | command2" 841ab72124fSSteve Price.Pp 84249e11e3aSSheldon Hearnsends both the standard output and standard error of 843dfe302abSRuslan Ermilov.Ar command1 84449e11e3aSSheldon Hearnto the standard input of 845dfe302abSRuslan Ermilov.Ar command2 . 84649e11e3aSSheldon Hearn.Pp 84749e11e3aSSheldon HearnA 848dfe302abSRuslan Ermilov.Ql \&; 84949e11e3aSSheldon Hearnor newline terminator causes the preceding 850e918fc8fSSheldon HearnAND-OR-list 851e918fc8fSSheldon Hearn(described below in the section called 852e918fc8fSSheldon Hearn.Sx Short-Circuit List Operators ) 853e918fc8fSSheldon Hearnto be executed sequentially; 854e918fc8fSSheldon Hearnan 855dfe302abSRuslan Ermilov.Ql & 856e918fc8fSSheldon Hearncauses asynchronous execution of the preceding AND-OR-list. 85749e11e3aSSheldon Hearn.Ss Background Commands (&) 8584b88c807SRodney W. GrimesIf a command is terminated by the control operator ampersand 859a762afe5SGiorgos Keramidas.Pq Ql & , 86049e11e3aSSheldon Hearnthe shell executes the command asynchronously; 86149e11e3aSSheldon Hearnthe shell does not wait for the command to finish 86249e11e3aSSheldon Hearnbefore executing the next command. 863ab72124fSSteve Price.Pp 8644b88c807SRodney W. GrimesThe format for running a command in background is: 865dfe302abSRuslan Ermilov.Pp 866dfe302abSRuslan Ermilov.D1 Ar command1 Li & Op Ar command2 Li & Ar ... 867ab72124fSSteve Price.Pp 8684b88c807SRodney W. GrimesIf the shell is not interactive, the standard input of an 869dfe302abSRuslan Ermilovasynchronous command is set to 870dfe302abSRuslan Ermilov.Pa /dev/null . 87149e11e3aSSheldon Hearn.Ss Lists (Generally Speaking) 8724b88c807SRodney W. GrimesA list is a sequence of zero or more commands separated by 8734b88c807SRodney W. Grimesnewlines, semicolons, or ampersands, 8744b88c807SRodney W. Grimesand optionally terminated by one of these three characters. 8754b88c807SRodney W. GrimesThe commands in a 8764b88c807SRodney W. Grimeslist are executed in the order they are written. 8774b88c807SRodney W. GrimesIf command is followed by an ampersand, the shell starts the 8788eed22b1SJens Schweikhardtcommand and immediately proceeds onto the next command; 8794b88c807SRodney W. Grimesotherwise it waits for the command to terminate before 8804b88c807SRodney W. Grimesproceeding to the next one. 881ab72124fSSteve Price.Ss Short-Circuit List Operators 88249e11e3aSSheldon Hearn.Dq Li && 8836b15476eSSheldon Hearnand 88449e11e3aSSheldon Hearn.Dq Li || 8856b15476eSSheldon Hearnare AND-OR list operators. 88649e11e3aSSheldon Hearn.Dq Li && 8876b15476eSSheldon Hearnexecutes the first command, and then executes the second command 8886b15476eSSheldon Hearnif the exit status of the first command is zero. 88949e11e3aSSheldon Hearn.Dq Li || 890120ac26dSMasafumi Max NAKANEis similar, but executes the second command if the exit 8916b15476eSSheldon Hearnstatus of the first command is nonzero. 89249e11e3aSSheldon Hearn.Dq Li && 8936b15476eSSheldon Hearnand 89449e11e3aSSheldon Hearn.Dq Li || 8954b88c807SRodney W. Grimesboth have the same priority. 89649e11e3aSSheldon Hearn.Ss Flow-Control Constructs (if, while, for, case) 89749e11e3aSSheldon HearnThe syntax of the 89849e11e3aSSheldon Hearn.Ic if 89949e11e3aSSheldon Hearncommand is: 900dfe302abSRuslan Ermilov.Bd -unfilled -offset indent -compact 901dfe302abSRuslan Ermilov.Ic if Ar list 902dfe302abSRuslan Ermilov.Ic then Ar list 903dfe302abSRuslan Ermilov.Oo Ic elif Ar list 904dfe302abSRuslan Ermilov.Ic then Ar list Oc Ar ... 905dfe302abSRuslan Ermilov.Op Ic else Ar list 906dfe302abSRuslan Ermilov.Ic fi 907dfe302abSRuslan Ermilov.Ed 908ab72124fSSteve Price.Pp 90949e11e3aSSheldon HearnThe syntax of the 91049e11e3aSSheldon Hearn.Ic while 91149e11e3aSSheldon Hearncommand is: 912dfe302abSRuslan Ermilov.Bd -unfilled -offset indent -compact 913dfe302abSRuslan Ermilov.Ic while Ar list 914dfe302abSRuslan Ermilov.Ic do Ar list 915dfe302abSRuslan Ermilov.Ic done 916dfe302abSRuslan Ermilov.Ed 917ab72124fSSteve Price.Pp 918ab0a2172SSteve PriceThe two lists are executed repeatedly while the exit status of the 91949e11e3aSSheldon Hearnfirst list is zero. 92049e11e3aSSheldon HearnThe 92149e11e3aSSheldon Hearn.Ic until 92249e11e3aSSheldon Hearncommand is similar, but has the word 92349e11e3aSSheldon Hearn.Ic until 92449e11e3aSSheldon Hearnin place of 92549e11e3aSSheldon Hearn.Ic while , 92649e11e3aSSheldon Hearnwhich causes it to 9274b88c807SRodney W. Grimesrepeat until the exit status of the first list is zero. 928ab72124fSSteve Price.Pp 92949e11e3aSSheldon HearnThe syntax of the 93049e11e3aSSheldon Hearn.Ic for 93149e11e3aSSheldon Hearncommand is: 932dfe302abSRuslan Ermilov.Bd -unfilled -offset indent -compact 933dfe302abSRuslan Ermilov.Ic for Ar variable Op Ic in Ar word ... 934dfe302abSRuslan Ermilov.Ic do Ar list 935dfe302abSRuslan Ermilov.Ic done 936dfe302abSRuslan Ermilov.Ed 937ab72124fSSteve Price.Pp 938a01459dfSStefan FarfelederIf 939a01459dfSStefan Farfeleder.Ic in 9407b14d37fSRalf S. Engelschalland the following words are omitted, 941776e6b33SJilles Tjoelker.Ic in Li \&"$@\&" 942a01459dfSStefan Farfelederis used instead. 9434b88c807SRodney W. GrimesThe words are expanded, and then the list is executed 94449e11e3aSSheldon Hearnrepeatedly with the variable set to each word in turn. 94549e11e3aSSheldon HearnThe 94649e11e3aSSheldon Hearn.Ic do 9476b15476eSSheldon Hearnand 94849e11e3aSSheldon Hearn.Ic done 94949e11e3aSSheldon Hearncommands may be replaced with 950dfe302abSRuslan Ermilov.Ql { 95149e11e3aSSheldon Hearnand 952dfe302abSRuslan Ermilov.Ql } . 953ab72124fSSteve Price.Pp 95449e11e3aSSheldon HearnThe syntax of the 95549e11e3aSSheldon Hearn.Ic break 95649e11e3aSSheldon Hearnand 95749e11e3aSSheldon Hearn.Ic continue 95849e11e3aSSheldon Hearncommands is: 959dfe302abSRuslan Ermilov.D1 Ic break Op Ar num 960dfe302abSRuslan Ermilov.D1 Ic continue Op Ar num 961ab72124fSSteve Price.Pp 96249e11e3aSSheldon HearnThe 96349e11e3aSSheldon Hearn.Ic break 96449e11e3aSSheldon Hearncommand terminates the 96549e11e3aSSheldon Hearn.Ar num 96649e11e3aSSheldon Hearninnermost 96749e11e3aSSheldon Hearn.Ic for 96849e11e3aSSheldon Hearnor 96949e11e3aSSheldon Hearn.Ic while 97049e11e3aSSheldon Hearnloops. 97149e11e3aSSheldon HearnThe 97249e11e3aSSheldon Hearn.Ic continue 97349e11e3aSSheldon Hearncommand continues with the next iteration of the innermost loop. 97430268dfaSJilles TjoelkerThese are implemented as special built-in commands. 975ab72124fSSteve Price.Pp 97649e11e3aSSheldon HearnThe syntax of the 97749e11e3aSSheldon Hearn.Ic case 978dfe302abSRuslan Ermilovcommand is: 979dfe302abSRuslan Ermilov.Bd -unfilled -offset indent -compact 980dfe302abSRuslan Ermilov.Ic case Ar word Ic in 981dfe302abSRuslan Ermilov.Ar pattern Ns Li ) Ar list Li ;; 982dfe302abSRuslan Ermilov.Ar ... 983dfe302abSRuslan Ermilov.Ic esac 984dfe302abSRuslan Ermilov.Ed 985ab72124fSSteve Price.Pp 98649e11e3aSSheldon HearnThe pattern can actually be one or more patterns 98749e11e3aSSheldon Hearn(see 98849e11e3aSSheldon Hearn.Sx Shell Patterns 98949e11e3aSSheldon Hearndescribed later), 99049e11e3aSSheldon Hearnseparated by 991dfe302abSRuslan Ermilov.Ql \&| 9926b15476eSSheldon Hearncharacters. 993aafd6a87SStefan FarfelederThe exit code of the 994aafd6a87SStefan Farfeleder.Ic case 995aafd6a87SStefan Farfeledercommand is the exit code of the last command executed in the list or 996aafd6a87SStefan Farfelederzero if no patterns were matched. 997ab72124fSSteve Price.Ss Grouping Commands Together 9984b88c807SRodney W. GrimesCommands may be grouped by writing either 999dfe302abSRuslan Ermilov.Pp 1000dfe302abSRuslan Ermilov.D1 Li \&( Ns Ar list Ns Li \%) 1001ab72124fSSteve Price.Pp 10024b88c807SRodney W. Grimesor 1003dfe302abSRuslan Ermilov.Pp 1004dfe302abSRuslan Ermilov.D1 Li { Ar list Ns Li \&; } 1005ab72124fSSteve Price.Pp 1006ef89d04fSJilles TjoelkerThe first form executes the commands in a subshell environment. 10078eed22b1SJens SchweikhardtNote that built-in commands thus executed do not affect the current shell. 1008ef89d04fSJilles TjoelkerThe second form never forks another shell, 100949e11e3aSSheldon Hearnso it is slightly more efficient. 101049e11e3aSSheldon HearnGrouping commands together this way allows the user to 1011ab0a2172SSteve Priceredirect their output as though they were one program: 1012ab72124fSSteve Price.Bd -literal -offset indent 1013ee7dc416SSteve Price{ echo -n "hello"; echo " world"; } > greeting 1014ab72124fSSteve Price.Ed 1015ab72124fSSteve Price.Ss Functions 10164b88c807SRodney W. GrimesThe syntax of a function definition is 1017dfe302abSRuslan Ermilov.Pp 1018dfe302abSRuslan Ermilov.D1 Ar name Li \&( \&) Ar command 1019ab72124fSSteve Price.Pp 10204b88c807SRodney W. GrimesA function definition is an executable statement; when 1021dfe302abSRuslan Ermilovexecuted it installs a function named 1022dfe302abSRuslan Ermilov.Ar name 1023dfe302abSRuslan Ermilovand returns an 1024eccea571SRuslan Ermilovexit status of zero. 1025dfe302abSRuslan ErmilovThe 1026dfe302abSRuslan Ermilov.Ar command 1027dfe302abSRuslan Ermilovis normally a list 10286b15476eSSheldon Hearnenclosed between 1029dfe302abSRuslan Ermilov.Ql { 10306b15476eSSheldon Hearnand 1031dfe302abSRuslan Ermilov.Ql } . 1032ab72124fSSteve Price.Pp 10334b88c807SRodney W. GrimesVariables may be declared to be local to a function by 103449e11e3aSSheldon Hearnusing the 103549e11e3aSSheldon Hearn.Ic local 103649e11e3aSSheldon Hearncommand. 103749e11e3aSSheldon HearnThis should appear as the first statement of a function, 103849e11e3aSSheldon Hearnand the syntax is: 1039dfe302abSRuslan Ermilov.Pp 1040dfe302abSRuslan Ermilov.D1 Ic local Oo Ar variable ... Oc Op Fl 1041ab72124fSSteve Price.Pp 104249e11e3aSSheldon HearnThe 104349e11e3aSSheldon Hearn.Ic local 10448eed22b1SJens Schweikhardtcommand is implemented as a built-in command. 1045ab72124fSSteve Price.Pp 10464b88c807SRodney W. GrimesWhen a variable is made local, it inherits the initial 10474b88c807SRodney W. Grimesvalue and exported and readonly flags from the variable 10484b88c807SRodney W. Grimeswith the same name in the surrounding scope, if there is 1049eccea571SRuslan Ermilovone. 1050eccea571SRuslan ErmilovOtherwise, the variable is initially unset. 1051eccea571SRuslan ErmilovThe shell 1052e918fc8fSSheldon Hearnuses dynamic scoping, so that if the variable 1053dfe302abSRuslan Ermilov.Va x 1054e918fc8fSSheldon Hearnis made local to function 1055e918fc8fSSheldon Hearn.Em f , 1056e918fc8fSSheldon Hearnwhich then calls function 1057e918fc8fSSheldon Hearn.Em g , 1058e918fc8fSSheldon Hearnreferences to the variable 1059dfe302abSRuslan Ermilov.Va x 1060e918fc8fSSheldon Hearnmade inside 1061e918fc8fSSheldon Hearn.Em g 1062e918fc8fSSheldon Hearnwill refer to the variable 1063dfe302abSRuslan Ermilov.Va x 1064e918fc8fSSheldon Hearndeclared inside 1065e918fc8fSSheldon Hearn.Em f , 1066e918fc8fSSheldon Hearnnot to the global variable named 1067dfe302abSRuslan Ermilov.Va x . 1068ab72124fSSteve Price.Pp 10696b4bbd8aSCeri DaviesThe only special parameter that can be made local is 1070dfe302abSRuslan Ermilov.Ql - . 10716b15476eSSheldon HearnMaking 1072dfe302abSRuslan Ermilov.Ql - 10736b15476eSSheldon Hearnlocal causes any shell options that are 1074dfe302abSRuslan Ermilovchanged via the 1075dfe302abSRuslan Ermilov.Ic set 1076dfe302abSRuslan Ermilovcommand inside the function to be 10774b88c807SRodney W. Grimesrestored to their original values when the function 10784b88c807SRodney W. Grimesreturns. 1079ab72124fSSteve Price.Pp 108049e11e3aSSheldon HearnThe syntax of the 108149e11e3aSSheldon Hearn.Ic return 108249e11e3aSSheldon Hearncommand is 1083dfe302abSRuslan Ermilov.Pp 1084dfe302abSRuslan Ermilov.D1 Ic return Op Ar exitstatus 1085ab72124fSSteve Price.Pp 10864ad8968aSBrian FeldmanIt terminates the current executional scope, returning from the previous 10874ad8968aSBrian Feldmannested function, sourced script, or shell instance, in that order. 108849e11e3aSSheldon HearnThe 108949e11e3aSSheldon Hearn.Ic return 109030268dfaSJilles Tjoelkercommand is implemented as a special built-in command. 1091ab72124fSSteve Price.Ss Variables and Parameters 1092eccea571SRuslan ErmilovThe shell maintains a set of parameters. 1093eccea571SRuslan ErmilovA parameter 1094eccea571SRuslan Ermilovdenoted by a name is called a variable. 1095eccea571SRuslan ErmilovWhen starting up, 10964b88c807SRodney W. Grimesthe shell turns all the environment variables into shell 1097eccea571SRuslan Ermilovvariables. 1098eccea571SRuslan ErmilovNew variables can be set using the form 1099dfe302abSRuslan Ermilov.Pp 1100dfe302abSRuslan Ermilov.D1 Ar name Ns = Ns Ar value 1101ab72124fSSteve Price.Pp 11024b88c807SRodney W. GrimesVariables set by the user must have a name consisting solely 110349e11e3aSSheldon Hearnof alphabetics, numerics, and underscores. 110449e11e3aSSheldon HearnThe first letter of a variable name must not be numeric. 110549e11e3aSSheldon HearnA parameter can also be denoted by a number 11064b88c807SRodney W. Grimesor a special character as explained below. 1107ab72124fSSteve Price.Ss Positional Parameters 110849e11e3aSSheldon HearnA positional parameter is a parameter denoted by a number greater than zero. 1109ab72124fSSteve PriceThe shell sets these initially to the values of its command line 1110eccea571SRuslan Ermilovarguments that follow the name of the shell script. 1111eccea571SRuslan ErmilovThe 1112c644db6aSSheldon Hearn.Ic set 11138eed22b1SJens Schweikhardtbuilt-in command can also be used to set or reset them. 1114ab72124fSSteve Price.Ss Special Parameters 1115626a7b1dSJilles TjoelkerSpecial parameters are parameters denoted by a single special character 1116626a7b1dSJilles Tjoelkeror the digit zero. 1117626a7b1dSJilles TjoelkerThey are shown in the following list, exactly as they would appear in input 111843d6677bSAlfred Perlsteintyped by the user or in the source of a shell script. 1119ab72124fSSteve Price.Bl -hang 112043d6677bSAlfred Perlstein.It Li $* 1121eccea571SRuslan ErmilovExpands to the positional parameters, starting from one. 1122eccea571SRuslan ErmilovWhen 11234b88c807SRodney W. Grimesthe expansion occurs within a double-quoted string 11244b88c807SRodney W. Grimesit expands to a single field with the value of each parameter 112549e11e3aSSheldon Hearnseparated by the first character of the 1126dfe302abSRuslan Ermilov.Va IFS 112749e11e3aSSheldon Hearnvariable, 1128dfe302abSRuslan Ermilovor by a space if 1129dfe302abSRuslan Ermilov.Va IFS 113049e11e3aSSheldon Hearnis unset. 113143d6677bSAlfred Perlstein.It Li $@ 1132eccea571SRuslan ErmilovExpands to the positional parameters, starting from one. 1133eccea571SRuslan ErmilovWhen 11344b88c807SRodney W. Grimesthe expansion occurs within double-quotes, each positional 11354b88c807SRodney W. Grimesparameter expands as a separate argument. 11364b88c807SRodney W. GrimesIf there are no positional parameters, the 1137e918fc8fSSheldon Hearnexpansion of 1138e918fc8fSSheldon Hearn.Li @ 1139e918fc8fSSheldon Hearngenerates zero arguments, even when 1140e918fc8fSSheldon Hearn.Li @ 1141eccea571SRuslan Ermilovis double-quoted. 1142eccea571SRuslan ErmilovWhat this basically means, for example, is 1143dfe302abSRuslan Ermilovif 1144dfe302abSRuslan Ermilov.Li $1 1145dfe302abSRuslan Ermilovis 1146dfe302abSRuslan Ermilov.Dq Li abc 1147dfe302abSRuslan Ermilovand 1148dfe302abSRuslan Ermilov.Li $2 1149dfe302abSRuslan Ermilovis 1150dfe302abSRuslan Ermilov.Dq Li "def ghi" , 11516b15476eSSheldon Hearnthen 1152776e6b33SJilles Tjoelker.Li \&"$@\&" 11536b15476eSSheldon Hearnexpands to 11544b88c807SRodney W. Grimesthe two arguments: 1155ab72124fSSteve Price.Bd -literal -offset indent 11564b88c807SRodney W. Grimes"abc" "def ghi" 1157ab72124fSSteve Price.Ed 115843d6677bSAlfred Perlstein.It Li $# 11594b88c807SRodney W. GrimesExpands to the number of positional parameters. 1160dfe302abSRuslan Ermilov.It Li $? 11614b88c807SRodney W. GrimesExpands to the exit status of the most recent pipeline. 116243d6677bSAlfred Perlstein.It Li $- 1163ab72124fSSteve Price(hyphen) Expands to the current option flags (the single-letter 11644b88c807SRodney W. Grimesoption names concatenated into a string) as specified on 1165dfe302abSRuslan Ermilovinvocation, by the 1166dfe302abSRuslan Ermilov.Ic set 1167dfe302abSRuslan Ermilovbuilt-in command, or implicitly 11684b88c807SRodney W. Grimesby the shell. 116943d6677bSAlfred Perlstein.It Li $$ 1170eccea571SRuslan ErmilovExpands to the process ID of the invoked shell. 1171eccea571SRuslan ErmilovA subshell 1172dfe302abSRuslan Ermilovretains the same value of 1173dfe302abSRuslan Ermilov.Va $ 1174dfe302abSRuslan Ermilovas its parent. 1175dfe302abSRuslan Ermilov.It Li $! 11764b88c807SRodney W. GrimesExpands to the process ID of the most recent background 1177eccea571SRuslan Ermilovcommand executed from the current shell. 1178eccea571SRuslan ErmilovFor a 11794b88c807SRodney W. Grimespipeline, the process ID is that of the last command in the 11804b88c807SRodney W. Grimespipeline. 1181ed4c3b5fSJilles TjoelkerIf this parameter is referenced, the shell will remember 1182ed4c3b5fSJilles Tjoelkerthe process ID and its exit status until the 1183ed4c3b5fSJilles Tjoelker.Ic wait 1184ed4c3b5fSJilles Tjoelkerbuilt-in command reports completion of the process. 118543d6677bSAlfred Perlstein.It Li $0 118657a40f7dSJilles Tjoelker(zero) Expands to the name of the shell script if passed on the command line, 118757a40f7dSJilles Tjoelkerthe 118857a40f7dSJilles Tjoelker.Ar name 118957a40f7dSJilles Tjoelkeroperand if given (with 119057a40f7dSJilles Tjoelker.Fl c ) 119157a40f7dSJilles Tjoelkeror otherwise argument 0 passed to the shell. 1192ab72124fSSteve Price.El 11931788b7ffSJilles Tjoelker.Ss Special Variables 11941788b7ffSJilles TjoelkerThe following variables are set by the shell or 11951788b7ffSJilles Tjoelkerhave special meaning to it: 11961788b7ffSJilles Tjoelker.Bl -tag -width ".Va HISTSIZE" 11971788b7ffSJilles Tjoelker.It Va CDPATH 11981788b7ffSJilles TjoelkerThe search path used with the 11991788b7ffSJilles Tjoelker.Ic cd 12001788b7ffSJilles Tjoelkerbuilt-in. 12011788b7ffSJilles Tjoelker.It Va EDITOR 12021788b7ffSJilles TjoelkerThe fallback editor used with the 12031788b7ffSJilles Tjoelker.Ic fc 12041788b7ffSJilles Tjoelkerbuilt-in. 12051788b7ffSJilles TjoelkerIf not set, the default editor is 12061788b7ffSJilles Tjoelker.Xr ed 1 . 12071788b7ffSJilles Tjoelker.It Va FCEDIT 12081788b7ffSJilles TjoelkerThe default editor used with the 12091788b7ffSJilles Tjoelker.Ic fc 12101788b7ffSJilles Tjoelkerbuilt-in. 12111788b7ffSJilles Tjoelker.It Va HISTSIZE 12121788b7ffSJilles TjoelkerThe number of previous commands that are accessible. 12131788b7ffSJilles Tjoelker.It Va HOME 12141788b7ffSJilles TjoelkerThe user's home directory, 12151788b7ffSJilles Tjoelkerused in tilde expansion and as a default directory for the 12161788b7ffSJilles Tjoelker.Ic cd 12171788b7ffSJilles Tjoelkerbuilt-in. 12181788b7ffSJilles Tjoelker.It Va IFS 12191788b7ffSJilles TjoelkerInput Field Separators. 12201788b7ffSJilles TjoelkerThis is normally set to 12211788b7ffSJilles Tjoelker.Aq space , 12221788b7ffSJilles Tjoelker.Aq tab , 12231788b7ffSJilles Tjoelkerand 12241788b7ffSJilles Tjoelker.Aq newline . 12251788b7ffSJilles TjoelkerSee the 12261788b7ffSJilles Tjoelker.Sx White Space Splitting 12271788b7ffSJilles Tjoelkersection for more details. 12281788b7ffSJilles Tjoelker.It Va LINENO 12291788b7ffSJilles TjoelkerThe current line number in the script or function. 12301788b7ffSJilles Tjoelker.It Va MAIL 12311788b7ffSJilles TjoelkerThe name of a mail file, that will be checked for the arrival of new 12321788b7ffSJilles Tjoelkermail. 12331788b7ffSJilles TjoelkerOverridden by 12341788b7ffSJilles Tjoelker.Va MAILPATH . 12351788b7ffSJilles Tjoelker.It Va MAILPATH 12361788b7ffSJilles TjoelkerA colon 12371788b7ffSJilles Tjoelker.Pq Ql \&: 12381788b7ffSJilles Tjoelkerseparated list of file names, for the shell to check for incoming 12391788b7ffSJilles Tjoelkermail. 12401788b7ffSJilles TjoelkerThis variable overrides the 12411788b7ffSJilles Tjoelker.Va MAIL 12421788b7ffSJilles Tjoelkersetting. 12431788b7ffSJilles TjoelkerThere is a maximum of 10 mailboxes that can be monitored at once. 12441788b7ffSJilles Tjoelker.It Va PATH 12451788b7ffSJilles TjoelkerThe default search path for executables. 12461788b7ffSJilles TjoelkerSee the 12471788b7ffSJilles Tjoelker.Sx Path Search 12481788b7ffSJilles Tjoelkersection for details. 12491788b7ffSJilles Tjoelker.It Va PPID 12501788b7ffSJilles TjoelkerThe parent process ID of the invoked shell. 12511788b7ffSJilles TjoelkerThis is set at startup 12521788b7ffSJilles Tjoelkerunless this variable is in the environment. 12531788b7ffSJilles TjoelkerA later change of parent process ID is not reflected. 12541788b7ffSJilles TjoelkerA subshell retains the same value of 12551788b7ffSJilles Tjoelker.Va PPID . 12561788b7ffSJilles Tjoelker.It Va PS1 12571788b7ffSJilles TjoelkerThe primary prompt string, which defaults to 12581788b7ffSJilles Tjoelker.Dq Li "$ " , 12591788b7ffSJilles Tjoelkerunless you are the superuser, in which case it defaults to 12601788b7ffSJilles Tjoelker.Dq Li "# " . 12611788b7ffSJilles Tjoelker.It Va PS2 12621788b7ffSJilles TjoelkerThe secondary prompt string, which defaults to 12631788b7ffSJilles Tjoelker.Dq Li "> " . 12641788b7ffSJilles Tjoelker.It Va PS4 12651788b7ffSJilles TjoelkerThe prefix for the trace output (if 12661788b7ffSJilles Tjoelker.Fl x 12671788b7ffSJilles Tjoelkeris active). 12681788b7ffSJilles TjoelkerThe default is 12691788b7ffSJilles Tjoelker.Dq Li "+ " . 12701788b7ffSJilles Tjoelker.El 1271ab72124fSSteve Price.Ss Word Expansions 12724b88c807SRodney W. GrimesThis clause describes the various expansions that are 1273eccea571SRuslan Ermilovperformed on words. 1274eccea571SRuslan ErmilovNot all expansions are performed on 12754b88c807SRodney W. Grimesevery word, as explained later. 1276ab72124fSSteve Price.Pp 12774b88c807SRodney W. GrimesTilde expansions, parameter expansions, command substitutions, 12784b88c807SRodney W. Grimesarithmetic expansions, and quote removals that occur within 1279eccea571SRuslan Ermilova single word expand to a single field. 1280eccea571SRuslan ErmilovIt is only field 12814b88c807SRodney W. Grimessplitting or pathname expansion that can create multiple 1282e6d3cf26SSheldon Hearnfields from a single word. 1283e6d3cf26SSheldon HearnThe single exception to this rule is 1284e918fc8fSSheldon Hearnthe expansion of the special parameter 1285dfe302abSRuslan Ermilov.Va @ 1286e918fc8fSSheldon Hearnwithin double-quotes, 1287e6d3cf26SSheldon Hearnas was described above. 1288ab72124fSSteve Price.Pp 12894b88c807SRodney W. GrimesThe order of word expansion is: 1290ab72124fSSteve Price.Bl -enum 1291ab72124fSSteve Price.It 1292ab72124fSSteve PriceTilde Expansion, Parameter Expansion, Command Substitution, 12934b88c807SRodney W. GrimesArithmetic Expansion (these all occur at the same time). 1294ab72124fSSteve Price.It 129549e11e3aSSheldon HearnField Splitting is performed on fields generated by step (1) 129649e11e3aSSheldon Hearnunless the 1297dfe302abSRuslan Ermilov.Va IFS 129849e11e3aSSheldon Hearnvariable is null. 1299ab72124fSSteve Price.It 130049e11e3aSSheldon HearnPathname Expansion (unless the 130149e11e3aSSheldon Hearn.Fl f 130249e11e3aSSheldon Hearnoption is in effect). 1303ab72124fSSteve Price.It 1304ab72124fSSteve PriceQuote Removal. 1305ab72124fSSteve Price.El 1306ab72124fSSteve Price.Pp 130749e11e3aSSheldon HearnThe 1308dfe302abSRuslan Ermilov.Ql $ 130949e11e3aSSheldon Hearncharacter is used to introduce parameter expansion, command 131036cf3efeSJilles Tjoelkersubstitution, or arithmetic expansion. 1311ab72124fSSteve Price.Ss Tilde Expansion (substituting a user's home directory) 131249e11e3aSSheldon HearnA word beginning with an unquoted tilde character 1313a762afe5SGiorgos Keramidas.Pq Ql ~ 131449e11e3aSSheldon Hearnis 131549e11e3aSSheldon Hearnsubjected to tilde expansion. 131649e11e3aSSheldon HearnAll the characters up to a slash 1317a762afe5SGiorgos Keramidas.Pq Ql / 131849e11e3aSSheldon Hearnor the end of the word are treated as a username 1319eccea571SRuslan Ermilovand are replaced with the user's home directory. 1320eccea571SRuslan ErmilovIf the 1321dfe302abSRuslan Ermilovusername is missing (as in 1322dfe302abSRuslan Ermilov.Pa ~/foobar ) , 1323dfe302abSRuslan Ermilovthe tilde is replaced with the value of the 13241788b7ffSJilles Tjoelker.Va HOME 1325dfe302abSRuslan Ermilovvariable (the current user's home directory). 1326ab72124fSSteve Price.Ss Parameter Expansion 13274b88c807SRodney W. GrimesThe format for parameter expansion is as follows: 1328ab72124fSSteve Price.Pp 1329dfe302abSRuslan Ermilov.D1 Li ${ Ns Ar expression Ns Li } 1330dfe302abSRuslan Ermilov.Pp 1331dfe302abSRuslan Ermilovwhere 1332dfe302abSRuslan Ermilov.Ar expression 1333dfe302abSRuslan Ermilovconsists of all characters until the matching 1334dfe302abSRuslan Ermilov.Ql } . 133549e11e3aSSheldon HearnAny 1336dfe302abSRuslan Ermilov.Ql } 1337f35d74beSJilles Tjoelkerescaped by a backslash or within a single-quoted or double-quoted 1338f35d74beSJilles Tjoelkerstring, and characters in 13394b88c807SRodney W. Grimesembedded arithmetic expansions, command substitutions, and variable 134049e11e3aSSheldon Hearnexpansions, are not examined in determining the matching 1341dfe302abSRuslan Ermilov.Ql } . 1342f35d74beSJilles TjoelkerIf the variants with 1343227c8e2aSJilles Tjoelker.Ql + , 1344227c8e2aSJilles Tjoelker.Ql - , 1345227c8e2aSJilles Tjoelker.Ql = 1346227c8e2aSJilles Tjoelkeror 1347f35d74beSJilles Tjoelker.Ql ?\& 1348f35d74beSJilles Tjoelkeroccur within a double-quoted string, 1349f35d74beSJilles Tjoelkeras an extension there may be unquoted parts 1350f35d74beSJilles Tjoelker(via double-quotes inside the expansion); 1351227c8e2aSJilles Tjoelker.Ql } 1352f35d74beSJilles Tjoelkerwithin such parts are also not examined in determining the matching 1353227c8e2aSJilles Tjoelker.Ql } . 1354ab72124fSSteve Price.Pp 13554b88c807SRodney W. GrimesThe simplest form for parameter expansion is: 1356ab72124fSSteve Price.Pp 1357dfe302abSRuslan Ermilov.D1 Li ${ Ns Ar parameter Ns Li } 1358dfe302abSRuslan Ermilov.Pp 1359dfe302abSRuslan ErmilovThe value, if any, of 1360dfe302abSRuslan Ermilov.Ar parameter 1361dfe302abSRuslan Ermilovis substituted. 1362ab72124fSSteve Price.Pp 13634b88c807SRodney W. GrimesThe parameter name or symbol can be enclosed in braces, which are 13644b88c807SRodney W. Grimesoptional except for positional parameters with more than one digit or 13654b88c807SRodney W. Grimeswhen parameter is followed by a character that could be interpreted as 13664b88c807SRodney W. Grimespart of the name. 1367ab72124fSSteve PriceIf a parameter expansion occurs inside double-quotes: 1368ab72124fSSteve Price.Bl -enum 1369ab72124fSSteve Price.It 1370ab72124fSSteve PricePathname expansion is not performed on the results of the 13714b88c807SRodney W. Grimesexpansion. 1372ab72124fSSteve Price.It 1373ab72124fSSteve PriceField splitting is not performed on the results of the 1374e918fc8fSSheldon Hearnexpansion, with the exception of the special parameter 1375dfe302abSRuslan Ermilov.Va @ . 1376ab72124fSSteve Price.El 1377ab72124fSSteve Price.Pp 13784b88c807SRodney W. GrimesIn addition, a parameter expansion can be modified by using one of the 13794b88c807SRodney W. Grimesfollowing formats. 138081e9cda2SRuslan Ermilov.Bl -tag -width indent 1381dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li :- Ns Ar word Ns Li } 1382eccea571SRuslan ErmilovUse Default Values. 1383dfe302abSRuslan ErmilovIf 1384dfe302abSRuslan Ermilov.Ar parameter 1385dfe302abSRuslan Ermilovis unset or null, the expansion of 1386dfe302abSRuslan Ermilov.Ar word 1387dfe302abSRuslan Ermilovis substituted; otherwise, the value of 1388dfe302abSRuslan Ermilov.Ar parameter 1389dfe302abSRuslan Ermilovis substituted. 1390dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li := Ns Ar word Ns Li } 1391eccea571SRuslan ErmilovAssign Default Values. 1392dfe302abSRuslan ErmilovIf 1393dfe302abSRuslan Ermilov.Ar parameter 1394dfe302abSRuslan Ermilovis unset or null, the expansion of 1395dfe302abSRuslan Ermilov.Ar word 1396dfe302abSRuslan Ermilovis assigned to 1397dfe302abSRuslan Ermilov.Ar parameter . 1398eccea571SRuslan ErmilovIn all cases, the 1399dfe302abSRuslan Ermilovfinal value of 1400dfe302abSRuslan Ermilov.Ar parameter 1401dfe302abSRuslan Ermilovis substituted. 1402b139165cSJilles TjoelkerQuoting inside 1403b139165cSJilles Tjoelker.Ar word 1404b139165cSJilles Tjoelkerdoes not prevent field splitting or pathname expansion. 1405eccea571SRuslan ErmilovOnly variables, not positional 14064b88c807SRodney W. Grimesparameters or special parameters, can be 14074b88c807SRodney W. Grimesassigned in this way. 1408dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li :? Ns Oo Ar word Oc Ns Li } 1409eccea571SRuslan ErmilovIndicate Error if Null or Unset. 1410eccea571SRuslan ErmilovIf 1411dfe302abSRuslan Ermilov.Ar parameter 1412dfe302abSRuslan Ermilovis unset or null, the expansion of 1413dfe302abSRuslan Ermilov.Ar word 1414dfe302abSRuslan Ermilov(or a message indicating it is unset if 1415dfe302abSRuslan Ermilov.Ar word 1416dfe302abSRuslan Ermilovis omitted) is written to standard 14174b88c807SRodney W. Grimeserror and the shell exits with a nonzero 1418e6d3cf26SSheldon Hearnexit status. 1419e6d3cf26SSheldon HearnOtherwise, the value of 1420dfe302abSRuslan Ermilov.Ar parameter 1421dfe302abSRuslan Ermilovis substituted. 1422eccea571SRuslan ErmilovAn 14234b88c807SRodney W. Grimesinteractive shell need not exit. 1424dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li :+ Ns Ar word Ns Li } 1425eccea571SRuslan ErmilovUse Alternate Value. 1426dfe302abSRuslan ErmilovIf 1427dfe302abSRuslan Ermilov.Ar parameter 1428dfe302abSRuslan Ermilovis unset or null, null is substituted; 1429dfe302abSRuslan Ermilovotherwise, the expansion of 1430dfe302abSRuslan Ermilov.Ar word 1431dfe302abSRuslan Ermilovis substituted. 14321413f7deSTony Finch.El 1433ab72124fSSteve Price.Pp 14344b88c807SRodney W. GrimesIn the parameter expansions shown previously, use of the colon in the 14354b88c807SRodney W. Grimesformat results in a test for a parameter that is unset or null; omission 14364b88c807SRodney W. Grimesof the colon results in a test for a parameter that is only unset. 1437227c8e2aSJilles Tjoelker.Pp 1438227c8e2aSJilles TjoelkerThe 1439227c8e2aSJilles Tjoelker.Ar word 1440227c8e2aSJilles Tjoelkerinherits the type of quoting 1441227c8e2aSJilles Tjoelker(unquoted, double-quoted or here-document) 1442227c8e2aSJilles Tjoelkerfrom the surroundings, 1443227c8e2aSJilles Tjoelkerwith the exception that a backslash that quotes a closing brace is removed 1444227c8e2aSJilles Tjoelkerduring quote removal. 14451413f7deSTony Finch.Bl -tag -width indent 1446dfe302abSRuslan Ermilov.It Li ${# Ns Ar parameter Ns Li } 1447eccea571SRuslan ErmilovString Length. 1448eccea571SRuslan ErmilovThe length in characters of 1449dfe302abSRuslan Ermilovthe value of 1450dfe302abSRuslan Ermilov.Ar parameter . 145171337c33SSheldon Hearn.El 1452ab72124fSSteve Price.Pp 14534b88c807SRodney W. GrimesThe following four varieties of parameter expansion provide for substring 145449e11e3aSSheldon Hearnprocessing. 145549e11e3aSSheldon HearnIn each case, pattern matching notation 145649e11e3aSSheldon Hearn(see 145749e11e3aSSheldon Hearn.Sx Shell Patterns ) , 145849e11e3aSSheldon Hearnrather than regular expression notation, 145949e11e3aSSheldon Hearnis used to evaluate the patterns. 1460e918fc8fSSheldon HearnIf parameter is one of the special parameters 1461dfe302abSRuslan Ermilov.Va * 146249e11e3aSSheldon Hearnor 1463dfe302abSRuslan Ermilov.Va @ , 146449e11e3aSSheldon Hearnthe result of the expansion is unspecified. 14654b88c807SRodney W. GrimesEnclosing the full parameter expansion string in double-quotes does not 14664b88c807SRodney W. Grimescause the following four varieties of pattern characters to be quoted, 14674b88c807SRodney W. Grimeswhereas quoting characters within the braces has this effect. 146881e9cda2SRuslan Ermilov.Bl -tag -width indent 1469dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li % Ns Ar word Ns Li } 1470eccea571SRuslan ErmilovRemove Smallest Suffix Pattern. 1471dfe302abSRuslan ErmilovThe 1472dfe302abSRuslan Ermilov.Ar word 1473eccea571SRuslan Ermilovis expanded to produce a pattern. 1474eccea571SRuslan ErmilovThe 14754b88c807SRodney W. Grimesparameter expansion then results in 1476dfe302abSRuslan Ermilov.Ar parameter , 1477dfe302abSRuslan Ermilovwith the smallest portion of the 14784b88c807SRodney W. Grimessuffix matched by the pattern deleted. 1479dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li %% Ns Ar word Ns Li } 1480eccea571SRuslan ErmilovRemove Largest Suffix Pattern. 1481dfe302abSRuslan ErmilovThe 1482dfe302abSRuslan Ermilov.Ar word 1483eccea571SRuslan Ermilovis expanded to produce a pattern. 1484eccea571SRuslan ErmilovThe 14854b88c807SRodney W. Grimesparameter expansion then results in 1486dfe302abSRuslan Ermilov.Ar parameter , 1487dfe302abSRuslan Ermilovwith the largest portion of the 14884b88c807SRodney W. Grimessuffix matched by the pattern deleted. 1489dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li # Ns Ar word Ns Li } 1490eccea571SRuslan ErmilovRemove Smallest Prefix Pattern. 1491dfe302abSRuslan ErmilovThe 1492dfe302abSRuslan Ermilov.Ar word 1493eccea571SRuslan Ermilovis expanded to produce a pattern. 1494eccea571SRuslan ErmilovThe 14954b88c807SRodney W. Grimesparameter expansion then results in 1496dfe302abSRuslan Ermilov.Ar parameter , 1497dfe302abSRuslan Ermilovwith the smallest portion of the 14984b88c807SRodney W. Grimesprefix matched by the pattern deleted. 1499dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li ## Ns Ar word Ns Li } 1500eccea571SRuslan ErmilovRemove Largest Prefix Pattern. 1501dfe302abSRuslan ErmilovThe 1502dfe302abSRuslan Ermilov.Ar word 1503eccea571SRuslan Ermilovis expanded to produce a pattern. 1504eccea571SRuslan ErmilovThe 15054b88c807SRodney W. Grimesparameter expansion then results in 1506dfe302abSRuslan Ermilov.Ar parameter , 1507dfe302abSRuslan Ermilovwith the largest portion of the 15084b88c807SRodney W. Grimesprefix matched by the pattern deleted. 1509ab72124fSSteve Price.El 1510ab72124fSSteve Price.Ss Command Substitution 15114b88c807SRodney W. GrimesCommand substitution allows the output of a command to be substituted in 1512eccea571SRuslan Ermilovplace of the command name itself. 1513eccea571SRuslan ErmilovCommand substitution occurs when 15144b88c807SRodney W. Grimesthe command is enclosed as follows: 1515dfe302abSRuslan Ermilov.Pp 1516dfe302abSRuslan Ermilov.D1 Li $( Ns Ar command Ns Li )\& 1517ab72124fSSteve Price.Pp 151849e11e3aSSheldon Hearnor the backquoted version: 1519dfe302abSRuslan Ermilov.Pp 1520dfe302abSRuslan Ermilov.D1 Li ` Ns Ar command Ns Li ` 1521ab72124fSSteve Price.Pp 1522ef89d04fSJilles TjoelkerThe shell expands the command substitution by executing command 1523ef89d04fSJilles Tjoelkerand replacing the command substitution 152449e11e3aSSheldon Hearnwith the standard output of the command, 152549e11e3aSSheldon Hearnremoving sequences of one or more newlines at the end of the substitution. 152649e11e3aSSheldon HearnEmbedded newlines before the end of the output are not removed; 152749e11e3aSSheldon Hearnhowever, during field splitting, they may be translated into spaces 152849e11e3aSSheldon Hearndepending on the value of 1529dfe302abSRuslan Ermilov.Va IFS 153049e11e3aSSheldon Hearnand the quoting that is in effect. 1531ef89d04fSJilles TjoelkerThe command is executed in a subshell environment, 1532ef89d04fSJilles Tjoelkerexcept that the built-in commands 1533ef89d04fSJilles Tjoelker.Ic jobid , 1534ef89d04fSJilles Tjoelker.Ic jobs , 1535ef89d04fSJilles Tjoelker.Ic times 1536ef89d04fSJilles Tjoelkerand 1537ef89d04fSJilles Tjoelker.Ic trap 1538ef89d04fSJilles Tjoelkerreturn information about the main shell environment 1539ef89d04fSJilles Tjoelkerif they are the only command in a command substitution 1540ef89d04fSJilles Tjoelkerand the substitutions in the command cannot cause side effects 1541ef89d04fSJilles Tjoelker(such as from assigning values to variables or referencing 1542ef89d04fSJilles Tjoelker.Li $! ). 1543ab72124fSSteve Price.Ss Arithmetic Expansion 15444b88c807SRodney W. GrimesArithmetic expansion provides a mechanism for evaluating an arithmetic 1545e6d3cf26SSheldon Hearnexpression and substituting its value. 1546e6d3cf26SSheldon HearnThe format for arithmetic expansion is as follows: 1547ab72124fSSteve Price.Pp 1548dfe302abSRuslan Ermilov.D1 Li $(( Ns Ar expression Ns Li )) 1549dfe302abSRuslan Ermilov.Pp 1550dfe302abSRuslan ErmilovThe 1551dfe302abSRuslan Ermilov.Ar expression 1552dfe302abSRuslan Ermilovis treated as if it were in double-quotes, except 1553eccea571SRuslan Ermilovthat a double-quote inside the expression is not treated specially. 1554eccea571SRuslan ErmilovThe 1555dfe302abSRuslan Ermilovshell expands all tokens in the 1556dfe302abSRuslan Ermilov.Ar expression 1557dfe302abSRuslan Ermilovfor parameter expansion, 155836cf3efeSJilles Tjoelkercommand substitution, 155936cf3efeSJilles Tjoelkerarithmetic expansion 156036cf3efeSJilles Tjoelkerand quote removal. 1561ab72124fSSteve Price.Pp 156236cf3efeSJilles TjoelkerThe allowed expressions are a subset of C expressions, 156336cf3efeSJilles Tjoelkersummarized below. 156436cf3efeSJilles Tjoelker.Bl -tag -width "Variables" -offset indent 156536cf3efeSJilles Tjoelker.It Values 156636cf3efeSJilles TjoelkerAll values are of type 156736cf3efeSJilles Tjoelker.Ft intmax_t . 156836cf3efeSJilles Tjoelker.It Constants 156936cf3efeSJilles TjoelkerDecimal, octal (starting with 157036cf3efeSJilles Tjoelker.Li 0 ) 157136cf3efeSJilles Tjoelkerand hexadecimal (starting with 157236cf3efeSJilles Tjoelker.Li 0x ) 157336cf3efeSJilles Tjoelkerinteger constants. 157436cf3efeSJilles Tjoelker.It Variables 157536cf3efeSJilles TjoelkerShell variables can be read and written 157636cf3efeSJilles Tjoelkerand contain integer constants. 157736cf3efeSJilles Tjoelker.It Unary operators 157836cf3efeSJilles Tjoelker.Li "! ~ + -" 157936cf3efeSJilles Tjoelker.It Binary operators 158036cf3efeSJilles Tjoelker.Li "* / % + - << >> < <= > >= == != & ^ | && ||" 158136cf3efeSJilles Tjoelker.It Assignment operators 158236cf3efeSJilles Tjoelker.Li "= += -= *= /= %= <<= >>= &= ^= |=" 1583075b72efSJilles Tjoelker.It Conditional operator 1584075b72efSJilles Tjoelker.Li "? :" 158536cf3efeSJilles Tjoelker.El 158636cf3efeSJilles Tjoelker.Pp 158736cf3efeSJilles TjoelkerThe result of the expression is substituted in decimal. 1588ab72124fSSteve Price.Ss White Space Splitting (Field Splitting) 15894b88c807SRodney W. GrimesAfter parameter expansion, command substitution, and 15904b88c807SRodney W. Grimesarithmetic expansion the shell scans the results of 15914b88c807SRodney W. Grimesexpansions and substitutions that did not occur in double-quotes for 15924b88c807SRodney W. Grimesfield splitting and multiple fields can result. 1593ab72124fSSteve Price.Pp 159449e11e3aSSheldon HearnThe shell treats each character of the 1595dfe302abSRuslan Ermilov.Va IFS 1596dfe302abSRuslan Ermilovvariable as a delimiter and uses 15974b88c807SRodney W. Grimesthe delimiters to split the results of parameter expansion and command 15984b88c807SRodney W. Grimessubstitution into fields. 1599ab72124fSSteve Price.Ss Pathname Expansion (File Name Generation) 160049e11e3aSSheldon HearnUnless the 160149e11e3aSSheldon Hearn.Fl f 160249e11e3aSSheldon Hearnoption is set, 160349e11e3aSSheldon Hearnfile name generation is performed 1604eccea571SRuslan Ermilovafter word splitting is complete. 1605eccea571SRuslan ErmilovEach word is 1606eccea571SRuslan Ermilovviewed as a series of patterns, separated by slashes. 1607eccea571SRuslan ErmilovThe 16084b88c807SRodney W. Grimesprocess of expansion replaces the word with the names of 16094b88c807SRodney W. Grimesall existing files whose names can be formed by replacing 16104b88c807SRodney W. Grimeseach pattern with a string that matches the specified pattern. 1611ab0a2172SSteve PriceThere are two restrictions on this: first, a pattern cannot match 1612ab0a2172SSteve Pricea string containing a slash, and second, 16134b88c807SRodney W. Grimesa pattern cannot match a string starting with a period 16144b88c807SRodney W. Grimesunless the first character of the pattern is a period. 16154b88c807SRodney W. GrimesThe next section describes the patterns used for both 1616ab72124fSSteve PricePathname Expansion and the 1617c644db6aSSheldon Hearn.Ic case 1618ab72124fSSteve Pricecommand. 1619ab72124fSSteve Price.Ss Shell Patterns 1620f434d1dfSAdam DavidA pattern consists of normal characters, which match themselves, 162149e11e3aSSheldon Hearnand meta-characters. 162249e11e3aSSheldon HearnThe meta-characters are 1623dfe302abSRuslan Ermilov.Ql \&! , 1624dfe302abSRuslan Ermilov.Ql * , 1625dfe302abSRuslan Ermilov.Ql \&? , 16266b15476eSSheldon Hearnand 1627dfe302abSRuslan Ermilov.Ql \&[ . 16286b15476eSSheldon HearnThese characters lose their special meanings if they are quoted. 16296b15476eSSheldon HearnWhen command or variable substitution is performed and the dollar sign 16306b15476eSSheldon Hearnor back quotes are not double-quoted, the value of the 16314b88c807SRodney W. Grimesvariable or the output of the command is scanned for these 16324b88c807SRodney W. Grimescharacters and they are turned into meta-characters. 1633ab72124fSSteve Price.Pp 16346b15476eSSheldon HearnAn asterisk 1635a762afe5SGiorgos Keramidas.Pq Ql * 1636e918fc8fSSheldon Hearnmatches any string of characters. 1637e918fc8fSSheldon HearnA question mark 1638a762afe5SGiorgos Keramidas.Pq Ql \&? 1639e918fc8fSSheldon Hearnmatches any single character. 1640e6d3cf26SSheldon HearnA left bracket 1641dfe302abSRuslan Ermilov.Pq Ql \&[ 1642e6d3cf26SSheldon Hearnintroduces a character class. 1643e6d3cf26SSheldon HearnThe end of the character class is indicated by a 1644dfe302abSRuslan Ermilov.Ql \&] ; 16456b15476eSSheldon Hearnif the 1646dfe302abSRuslan Ermilov.Ql \&] 16476b15476eSSheldon Hearnis missing then the 1648dfe302abSRuslan Ermilov.Ql \&[ 16496b15476eSSheldon Hearnmatches a 1650dfe302abSRuslan Ermilov.Ql \&[ 1651e6d3cf26SSheldon Hearnrather than introducing a character class. 1652e6d3cf26SSheldon HearnA character class matches any of the characters between the square brackets. 1653e6d3cf26SSheldon HearnA range of characters may be specified using a minus sign. 1654e918fc8fSSheldon HearnThe character class may be complemented by making an exclamation point 1655a762afe5SGiorgos Keramidas.Pq Ql !\& 1656e918fc8fSSheldon Hearnthe first character of the character class. 1657ab72124fSSteve Price.Pp 16586b15476eSSheldon HearnTo include a 1659dfe302abSRuslan Ermilov.Ql \&] 1660e6d3cf26SSheldon Hearnin a character class, make it the first character listed 1661e6d3cf26SSheldon Hearn(after the 1662dfe302abSRuslan Ermilov.Ql \&! , 1663e6d3cf26SSheldon Hearnif any). 1664e918fc8fSSheldon HearnTo include a 1665dfe302abSRuslan Ermilov.Ql - , 1666e918fc8fSSheldon Hearnmake it the first or last character listed. 16678eed22b1SJens Schweikhardt.Ss Built-in Commands 166825f6b31fSJilles TjoelkerThis section lists the built-in commands. 166981e9cda2SRuslan Ermilov.Bl -tag -width indent 1670d0353b83SRuslan Ermilov.It Ic \&: 1671aa9caaf6SPeter WemmA null command that returns a 0 (true) exit value. 1672d0353b83SRuslan Ermilov.It Ic \&. Ar file 1673aa9caaf6SPeter WemmThe commands in the specified file are read and executed by the shell. 16744ad8968aSBrian FeldmanThe 16754ad8968aSBrian Feldman.Ic return 16764ad8968aSBrian Feldmancommand may be used to return to the 16774ad8968aSBrian Feldman.Ic \&. 16784ad8968aSBrian Feldmancommand's caller. 16796466945aSJohn PolstraIf 16806466945aSJohn Polstra.Ar file 16816466945aSJohn Polstracontains any 1682dfe302abSRuslan Ermilov.Ql / 1683eccea571SRuslan Ermilovcharacters, it is used as is. 1684eccea571SRuslan ErmilovOtherwise, the shell searches the 16851788b7ffSJilles Tjoelker.Va PATH 1686eccea571SRuslan Ermilovfor the file. 1687eccea571SRuslan ErmilovIf it is not found in the 16881788b7ffSJilles Tjoelker.Va PATH , 16896466945aSJohn Polstrait is sought in the current working directory. 169010845922SYaroslav Tykhiy.It Ic \&[ 169110845922SYaroslav TykhiyA built-in equivalent of 169210845922SYaroslav Tykhiy.Xr test 1 . 1693a886288eSJesus R. Camou.It Ic alias Oo Ar name Ns Oo = Ns Ar string Oc ... Oc 169449e11e3aSSheldon HearnIf 1695d0353b83SRuslan Ermilov.Ar name Ns = Ns Ar string 169649e11e3aSSheldon Hearnis specified, the shell defines the alias 169749e11e3aSSheldon Hearn.Ar name 16986b15476eSSheldon Hearnwith value 169949e11e3aSSheldon Hearn.Ar string . 17006b15476eSSheldon HearnIf just 170149e11e3aSSheldon Hearn.Ar name 17026b15476eSSheldon Hearnis specified, the value of the alias 170349e11e3aSSheldon Hearn.Ar name 17046b15476eSSheldon Hearnis printed. 170549e11e3aSSheldon HearnWith no arguments, the 170649e11e3aSSheldon Hearn.Ic alias 17078eed22b1SJens Schweikhardtbuilt-in command prints the names and values of all defined aliases 170849e11e3aSSheldon Hearn(see 170949e11e3aSSheldon Hearn.Ic unalias ) . 1710e5341cbbSTim J. RobbinsAlias values are written with appropriate quoting so that they are 17119ba31ca0SRuslan Ermilovsuitable for re-input to the shell. 1712a886288eSJesus R. CamouAlso see the 1713a886288eSJesus R. Camou.Sx Aliases 1714a886288eSJesus R. Camousubsection. 171549e11e3aSSheldon Hearn.It Ic bg Op Ar job ... 171649e11e3aSSheldon HearnContinue the specified jobs 171749e11e3aSSheldon Hearn(or the current job if no jobs are given) 171849e11e3aSSheldon Hearnin the background. 171925f6b31fSJilles Tjoelker.It Ic bind Oo Fl aeklrsv Oc Oo Ar key Oo Ar command Oc Oc 172025f6b31fSJilles TjoelkerList or alter key bindings for the line editor. 172125f6b31fSJilles TjoelkerThis command is documented in 172225f6b31fSJilles Tjoelker.Xr editrc 5 . 172325f6b31fSJilles Tjoelker.It Ic break Op Ar num 172425f6b31fSJilles TjoelkerSee the 172525f6b31fSJilles Tjoelker.Sx Flow-Control Constructs 172625f6b31fSJilles Tjoelkersubsection. 1727df9e587bSTim J. Robbins.It Ic builtin Ar cmd Op Ar arg ... 17288eed22b1SJens SchweikhardtExecute the specified built-in command, 172949e11e3aSSheldon Hearn.Ar cmd . 173049e11e3aSSheldon HearnThis is useful when the user wishes to override a shell function 17318eed22b1SJens Schweikhardtwith the same name as a built-in command. 1732*d6ee26adSJilles Tjoelker.It Ic cd Oo Fl L | P Oc Oo Fl e Oc Op Ar directory 173349e11e3aSSheldon HearnSwitch to the specified 173449e11e3aSSheldon Hearn.Ar directory , 173549e11e3aSSheldon Hearnor to the directory specified in the 17361788b7ffSJilles Tjoelker.Va HOME 173749e11e3aSSheldon Hearnenvironment variable if no 173849e11e3aSSheldon Hearn.Ar directory 173949e11e3aSSheldon Hearnis specified. 174049e11e3aSSheldon HearnIf 174149e11e3aSSheldon Hearn.Ar directory 1742029c8ee4STim J. Robbinsdoes not begin with 17439ba31ca0SRuslan Ermilov.Pa / , \&. , 174449e11e3aSSheldon Hearnor 1745029c8ee4STim J. Robbins.Pa .. , 174649e11e3aSSheldon Hearnthen the directories listed in the 17471788b7ffSJilles Tjoelker.Va CDPATH 174849e11e3aSSheldon Hearnvariable will be 174949e11e3aSSheldon Hearnsearched for the specified 175049e11e3aSSheldon Hearn.Ar directory . 1751029c8ee4STim J. RobbinsIf 17521788b7ffSJilles Tjoelker.Va CDPATH 1753029c8ee4STim J. Robbinsis unset, the current directory is searched. 175449e11e3aSSheldon HearnThe format of 17551788b7ffSJilles Tjoelker.Va CDPATH 175649e11e3aSSheldon Hearnis the same as that of 17571788b7ffSJilles Tjoelker.Va PATH . 1758e6d3cf26SSheldon HearnIn an interactive shell, 175949e11e3aSSheldon Hearnthe 176049e11e3aSSheldon Hearn.Ic cd 176149e11e3aSSheldon Hearncommand will print out the name of the directory 1762e6d3cf26SSheldon Hearnthat it actually switched to 1763e6d3cf26SSheldon Hearnif this is different from the name that the user gave. 176449e11e3aSSheldon HearnThese may be different either because the 17651788b7ffSJilles Tjoelker.Va CDPATH 176649e11e3aSSheldon Hearnmechanism was used or because a symbolic link was crossed. 1767178897f1STim J. Robbins.Pp 1768178897f1STim J. RobbinsIf the 1769178897f1STim J. Robbins.Fl P 1770178897f1STim J. Robbinsoption is specified, 177181e9cda2SRuslan Ermilov.Pa .. 1772178897f1STim J. Robbinsis handled physically and symbolic links are resolved before 177381e9cda2SRuslan Ermilov.Pa .. 1774178897f1STim J. Robbinscomponents are processed. 1775178897f1STim J. RobbinsIf the 1776178897f1STim J. Robbins.Fl L 1777178897f1STim J. Robbinsoption is specified, 177881e9cda2SRuslan Ermilov.Pa .. 1779178897f1STim J. Robbinsis handled logically. 1780178897f1STim J. RobbinsThis is the default. 1781*d6ee26adSJilles Tjoelker.Pp 1782*d6ee26adSJilles TjoelkerThe 1783*d6ee26adSJilles Tjoelker.Fl e 1784*d6ee26adSJilles Tjoelkeroption causes 1785*d6ee26adSJilles Tjoelker.Ic cd 1786*d6ee26adSJilles Tjoelkerto return exit status 1 if the full pathname of the new directory 1787*d6ee26adSJilles Tjoelkercannot be determined reliably or at all. 1788*d6ee26adSJilles TjoelkerNormally this is not considered an error, 1789*d6ee26adSJilles Tjoelkeralthough a warning is printed. 17909434a1c7SDima Dorfman.It Ic chdir 17919434a1c7SDima DorfmanA synonym for the 17929434a1c7SDima Dorfman.Ic cd 17938eed22b1SJens Schweikhardtbuilt-in command. 17949ba31ca0SRuslan Ermilov.It Ic command Oo Fl p Oc Op Ar utility Op Ar argument ... 1795b97d13b3SJilles Tjoelker.It Ic command Oo Fl p Oc Fl v Ar utility 1796b97d13b3SJilles Tjoelker.It Ic command Oo Fl p Oc Fl V Ar utility 179733b222b9SStefan FarfelederThe first form of invocation executes the specified 1798c848bc18SJilles Tjoelker.Ar utility , 1799c848bc18SJilles Tjoelkerignoring shell functions in the search. 1800c848bc18SJilles TjoelkerIf 18012babaf74STim J. Robbins.Ar utility 1802c848bc18SJilles Tjoelkeris a special builtin, 1803c848bc18SJilles Tjoelkerit is executed as if it were a regular builtin. 18042babaf74STim J. Robbins.Pp 18052babaf74STim J. RobbinsIf the 18062babaf74STim J. Robbins.Fl p 18072babaf74STim J. Robbinsoption is specified, the command search is performed using a 18082babaf74STim J. Robbinsdefault value of 18091788b7ffSJilles Tjoelker.Va PATH 18102babaf74STim J. Robbinsthat is guaranteed to find all of the standard utilities. 181133b222b9SStefan Farfeleder.Pp 181233b222b9SStefan FarfelederIf the 181333b222b9SStefan Farfeleder.Fl v 181433b222b9SStefan Farfelederoption is specified, 181533b222b9SStefan Farfeleder.Ar utility 181633b222b9SStefan Farfelederis not executed but a description of its interpretation by the shell is 181733b222b9SStefan Farfelederprinted. 181833b222b9SStefan FarfelederFor ordinary commands the output is the path name; for shell built-in 181933b222b9SStefan Farfeledercommands, shell functions and keywords only the name is written. 182033b222b9SStefan FarfelederAliases are printed as 182133b222b9SStefan Farfeleder.Dq Ic alias Ar name Ns = Ns Ar value . 182233b222b9SStefan Farfeleder.Pp 182333b222b9SStefan FarfelederThe 182433b222b9SStefan Farfeleder.Fl V 182533b222b9SStefan Farfelederoption is identical to 182633b222b9SStefan Farfeleder.Fl v 182733b222b9SStefan Farfelederexcept for the output. 182833b222b9SStefan FarfelederIt prints 182933b222b9SStefan Farfeleder.Dq Ar utility Ic is Ar description 183033b222b9SStefan Farfelederwhere 183133b222b9SStefan Farfeleder.Ar description 183233b222b9SStefan Farfelederis either 183333b222b9SStefan Farfelederthe path name to 183433b222b9SStefan Farfeleder.Ar utility , 183530268dfaSJilles Tjoelkera special shell builtin, 18368af11131SRuslan Ermilova shell builtin, 18378af11131SRuslan Ermilova shell function, 18388af11131SRuslan Ermilova shell keyword 183933b222b9SStefan Farfelederor 18408af11131SRuslan Ermilovan alias for 18418af11131SRuslan Ermilov.Ar value . 184225f6b31fSJilles Tjoelker.It Ic continue Op Ar num 184325f6b31fSJilles TjoelkerSee the 184425f6b31fSJilles Tjoelker.Sx Flow-Control Constructs 184525f6b31fSJilles Tjoelkersubsection. 184673313421SStefan Farfeleder.It Ic echo Oo Fl e | n Oc Op Ar string ... 184773313421SStefan FarfelederPrint a space-separated list of the arguments to the standard output 184873313421SStefan Farfelederand append a newline character. 184981e9cda2SRuslan Ermilov.Bl -tag -width indent 1850501e74b7SMark Ovens.It Fl n 1851501e74b7SMark OvensSuppress the output of the trailing newline. 1852501e74b7SMark Ovens.It Fl e 1853501e74b7SMark OvensProcess C-style backslash escape sequences. 1854dfe302abSRuslan ErmilovThe 1855501e74b7SMark Ovens.Ic echo 1856dfe302abSRuslan Ermilovcommand understands the following character escapes: 185781e9cda2SRuslan Ermilov.Bl -tag -width indent 1858501e74b7SMark Ovens.It \ea 1859501e74b7SMark OvensAlert (ring the terminal bell) 1860501e74b7SMark Ovens.It \eb 1861501e74b7SMark OvensBackspace 1862501e74b7SMark Ovens.It \ec 1863501e74b7SMark OvensSuppress the trailing newline (this has the side-effect of truncating the 1864501e74b7SMark Ovensline if it is not the last character) 1865501e74b7SMark Ovens.It \ee 1866dfe302abSRuslan ErmilovThe ESC character 1867dfe302abSRuslan Ermilov.Tn ( ASCII 1868dfe302abSRuslan Ermilov0x1b) 1869501e74b7SMark Ovens.It \ef 1870501e74b7SMark OvensFormfeed 1871501e74b7SMark Ovens.It \en 1872501e74b7SMark OvensNewline 1873501e74b7SMark Ovens.It \er 1874501e74b7SMark OvensCarriage return 1875501e74b7SMark Ovens.It \et 1876501e74b7SMark OvensHorizontal tab 1877501e74b7SMark Ovens.It \ev 1878501e74b7SMark OvensVertical tab 1879501e74b7SMark Ovens.It \e\e 1880501e74b7SMark OvensLiteral backslash 1881501e74b7SMark Ovens.It \e0nnn 1882dfe302abSRuslan Ermilov(Zero) The character whose octal value is 1883dfe302abSRuslan Ermilov.Ar nnn 1884501e74b7SMark Ovens.El 1885501e74b7SMark Ovens.Pp 1886501e74b7SMark OvensIf 1887501e74b7SMark Ovens.Ar string 1888501e74b7SMark Ovensis not enclosed in quotes then the backslash itself must be escaped 1889eccea571SRuslan Ermilovwith a backslash to protect it from the shell. 1890eccea571SRuslan ErmilovFor example 1891501e74b7SMark Ovens.Bd -literal -offset indent 1892501e74b7SMark Ovens$ echo -e "a\evb" 1893501e74b7SMark Ovensa 1894501e74b7SMark Ovens b 1895501e74b7SMark Ovens$ echo -e a\e\evb 1896501e74b7SMark Ovensa 1897501e74b7SMark Ovens b 1898501e74b7SMark Ovens$ echo -e "a\e\eb" 1899501e74b7SMark Ovensa\eb 1900501e74b7SMark Ovens$ echo -e a\e\e\e\eb 1901501e74b7SMark Ovensa\eb 1902501e74b7SMark Ovens.Ed 1903501e74b7SMark Ovens.El 1904558175caSTim J. Robbins.Pp 1905558175caSTim J. RobbinsOnly one of the 1906558175caSTim J. Robbins.Fl e 1907558175caSTim J. Robbinsand 1908558175caSTim J. Robbins.Fl n 1909558175caSTim J. Robbinsoptions may be specified. 191049e11e3aSSheldon Hearn.It Ic eval Ar string ... 1911e6d3cf26SSheldon HearnConcatenate all the arguments with spaces. 1912e6d3cf26SSheldon HearnThen re-parse and execute the command. 191349e11e3aSSheldon Hearn.It Ic exec Op Ar command Op arg ... 191449e11e3aSSheldon HearnUnless 191549e11e3aSSheldon Hearn.Ar command 191649e11e3aSSheldon Hearnis omitted, 191749e11e3aSSheldon Hearnthe shell process is replaced with the specified program 19188eed22b1SJens Schweikhardt(which must be a real program, not a shell built-in command or function). 191949e11e3aSSheldon HearnAny redirections on the 192049e11e3aSSheldon Hearn.Ic exec 192149e11e3aSSheldon Hearncommand are marked as permanent, 192249e11e3aSSheldon Hearnso that they are not undone when the 192349e11e3aSSheldon Hearn.Ic exec 192449e11e3aSSheldon Hearncommand finishes. 192549e11e3aSSheldon Hearn.It Ic exit Op Ar exitstatus 192649e11e3aSSheldon HearnTerminate the shell process. 192749e11e3aSSheldon HearnIf 192849e11e3aSSheldon Hearn.Ar exitstatus 192949e11e3aSSheldon Hearnis given 1930ea381e69SJilles Tjoelkerit is used as the exit status of the shell. 1931ea381e69SJilles TjoelkerOtherwise, if the shell is executing an 1932421fb021SJilles Tjoelker.Cm EXIT 1933421fb021SJilles Tjoelkertrap, the exit status of the last command before the trap is used; 1934421fb021SJilles Tjoelkerif the shell is executing a trap for a signal, 1935ea381e69SJilles Tjoelkerthe shell exits by resending the signal to itself. 1936ea381e69SJilles TjoelkerOtherwise, the exit status of the preceding command is used. 1937a68fbc44SJilles TjoelkerThe exit status should be an integer between 0 and 255. 1938ef9791a3SStefan Farfeleder.It Ic export Ar name ... 1939ef9791a3SStefan Farfeleder.It Ic export Op Fl p 19404b88c807SRodney W. GrimesThe specified names are exported so that they will 19414b88c807SRodney W. Grimesappear in the environment of subsequent commands. 194249e11e3aSSheldon HearnThe only way to un-export a variable is to 194349e11e3aSSheldon Hearn.Ic unset 194449e11e3aSSheldon Hearnit. 194549e11e3aSSheldon HearnThe shell allows the value of a variable to be set 194649e11e3aSSheldon Hearnat the same time as it is exported by writing 1947ab72124fSSteve Price.Pp 1948dfe302abSRuslan Ermilov.D1 Ic export Ar name Ns = Ns Ar value 1949dfe302abSRuslan Ermilov.Pp 1950dfe302abSRuslan ErmilovWith no arguments the 1951dfe302abSRuslan Ermilov.Ic export 1952dfe302abSRuslan Ermilovcommand lists the names 19534b88c807SRodney W. Grimesof all exported variables. 195445086f8cSTim J. RobbinsIf the 195545086f8cSTim J. Robbins.Fl p 195645086f8cSTim J. Robbinsoption is specified, the exported variables are printed as 195745086f8cSTim J. Robbins.Dq Ic export Ar name Ns = Ns Ar value 195845086f8cSTim J. Robbinslines, suitable for re-input to the shell. 1959a6557dcbSYaroslav Tykhiy.It Ic false 1960a6557dcbSYaroslav TykhiyA null command that returns a non-zero (false) exit value. 196181e9cda2SRuslan Ermilov.It Ic fc Oo Fl e Ar editor Oc Op Ar first Op Ar last 196281e9cda2SRuslan Ermilov.It Ic fc Fl l Oo Fl nr Oc Op Ar first Op Ar last 196381e9cda2SRuslan Ermilov.It Ic fc Fl s Oo Ar old Ns = Ns Ar new Oc Op Ar first 196449e11e3aSSheldon HearnThe 196549e11e3aSSheldon Hearn.Ic fc 19668eed22b1SJens Schweikhardtbuilt-in command lists, or edits and re-executes, 196749e11e3aSSheldon Hearncommands previously entered to an interactive shell. 196881e9cda2SRuslan Ermilov.Bl -tag -width indent 196949e11e3aSSheldon Hearn.It Fl e Ar editor 197049e11e3aSSheldon HearnUse the editor named by 197149e11e3aSSheldon Hearn.Ar editor 197249e11e3aSSheldon Hearnto edit the commands. 1973dfe302abSRuslan ErmilovThe 1974dfe302abSRuslan Ermilov.Ar editor 1975dfe302abSRuslan Ermilovstring is a command name, 197649e11e3aSSheldon Hearnsubject to search via the 19771788b7ffSJilles Tjoelker.Va PATH 197849e11e3aSSheldon Hearnvariable. 197949e11e3aSSheldon HearnThe value in the 19801788b7ffSJilles Tjoelker.Va FCEDIT 198149e11e3aSSheldon Hearnvariable is used as a default when 198249e11e3aSSheldon Hearn.Fl e 198349e11e3aSSheldon Hearnis not specified. 198449e11e3aSSheldon HearnIf 19851788b7ffSJilles Tjoelker.Va FCEDIT 198649e11e3aSSheldon Hearnis null or unset, the value of the 19871788b7ffSJilles Tjoelker.Va EDITOR 198849e11e3aSSheldon Hearnvariable is used. 198949e11e3aSSheldon HearnIf 19901788b7ffSJilles Tjoelker.Va EDITOR 199149e11e3aSSheldon Hearnis null or unset, 1992ab72124fSSteve Price.Xr ed 1 1993ab72124fSSteve Priceis used as the editor. 199449e11e3aSSheldon Hearn.It Fl l No (ell) 19954b88c807SRodney W. GrimesList the commands rather than invoking 1996eccea571SRuslan Ermilovan editor on them. 1997eccea571SRuslan ErmilovThe commands are written in the 1998dfe302abSRuslan Ermilovsequence indicated by the 1999dfe302abSRuslan Ermilov.Ar first 2000dfe302abSRuslan Ermilovand 2001dfe302abSRuslan Ermilov.Ar last 2002dfe302abSRuslan Ermilovoperands, as affected by 200349e11e3aSSheldon Hearn.Fl r , 200449e11e3aSSheldon Hearnwith each command preceded by the command number. 200549e11e3aSSheldon Hearn.It Fl n 200649e11e3aSSheldon HearnSuppress command numbers when listing with 200749e11e3aSSheldon Hearn.Fl l . 200849e11e3aSSheldon Hearn.It Fl r 200949e11e3aSSheldon HearnReverse the order of the commands listed 201049e11e3aSSheldon Hearn(with 201149e11e3aSSheldon Hearn.Fl l ) 201249e11e3aSSheldon Hearnor edited 201349e11e3aSSheldon Hearn(with neither 201449e11e3aSSheldon Hearn.Fl l 201549e11e3aSSheldon Hearnnor 201649e11e3aSSheldon Hearn.Fl s ) . 201749e11e3aSSheldon Hearn.It Fl s 20184b88c807SRodney W. GrimesRe-execute the command without invoking an editor. 201949e11e3aSSheldon Hearn.It Ar first 202049e11e3aSSheldon Hearn.It Ar last 202149e11e3aSSheldon HearnSelect the commands to list or edit. 202249e11e3aSSheldon HearnThe number of previous commands that can be accessed 202349e11e3aSSheldon Hearnare determined by the value of the 20241788b7ffSJilles Tjoelker.Va HISTSIZE 2025d0353b83SRuslan Ermilovvariable. 202649e11e3aSSheldon HearnThe value of 202749e11e3aSSheldon Hearn.Ar first 202849e11e3aSSheldon Hearnor 202949e11e3aSSheldon Hearn.Ar last 203049e11e3aSSheldon Hearnor both are one of the following: 203181e9cda2SRuslan Ermilov.Bl -tag -width indent 2032dfe302abSRuslan Ermilov.It Oo Cm + Oc Ns Ar num 203349e11e3aSSheldon HearnA positive number representing a command number; 203449e11e3aSSheldon Hearncommand numbers can be displayed with the 203549e11e3aSSheldon Hearn.Fl l 203649e11e3aSSheldon Hearnoption. 2037dfe302abSRuslan Ermilov.It Fl Ar num 203849e11e3aSSheldon HearnA negative decimal number representing the 203949e11e3aSSheldon Hearncommand that was executed 204049e11e3aSSheldon Hearn.Ar num 204149e11e3aSSheldon Hearnof 204249e11e3aSSheldon Hearncommands previously. 2043dfe302abSRuslan ErmilovFor example, \-1 is the immediately previous command. 204449e11e3aSSheldon Hearn.It Ar string 204549e11e3aSSheldon HearnA string indicating the most recently entered command 204649e11e3aSSheldon Hearnthat begins with that string. 204749e11e3aSSheldon HearnIf the 2048dfe302abSRuslan Ermilov.Ar old Ns = Ns Ar new 204949e11e3aSSheldon Hearnoperand is not also specified with 205049e11e3aSSheldon Hearn.Fl s , 205149e11e3aSSheldon Hearnthe string form of the first operand cannot contain an embedded equal sign. 205249e11e3aSSheldon Hearn.El 205349e11e3aSSheldon Hearn.El 205449e11e3aSSheldon Hearn.Pp 20551788b7ffSJilles TjoelkerThe following variables affect the execution of 205649e11e3aSSheldon Hearn.Ic fc : 20571788b7ffSJilles Tjoelker.Bl -tag -width ".Va HISTSIZE" 20581788b7ffSJilles Tjoelker.It Va FCEDIT 205938afdcbcSGiorgos KeramidasName of the editor to use for history editing. 20601788b7ffSJilles Tjoelker.It Va HISTSIZE 20613d7b5b93SPhilippe CharnierThe number of previous commands that are accessible. 2062ab72124fSSteve Price.El 206349e11e3aSSheldon Hearn.It Ic fg Op Ar job 206449e11e3aSSheldon HearnMove the specified 206549e11e3aSSheldon Hearn.Ar job 206649e11e3aSSheldon Hearnor the current job to the foreground. 2067dfe302abSRuslan Ermilov.It Ic getopts Ar optstring var 2068dfe302abSRuslan ErmilovThe 2069dfe302abSRuslan Ermilov.Tn POSIX 207049e11e3aSSheldon Hearn.Ic getopts 207149e11e3aSSheldon Hearncommand. 207249e11e3aSSheldon HearnThe 207349e11e3aSSheldon Hearn.Ic getopts 207449e11e3aSSheldon Hearncommand deprecates the older 207549e11e3aSSheldon Hearn.Xr getopt 1 207649e11e3aSSheldon Hearncommand. 2077ab0a2172SSteve PriceThe first argument should be a series of letters, each possibly 2078ab0a2172SSteve Pricefollowed by a colon which indicates that the option takes an argument. 2079eccea571SRuslan ErmilovThe specified variable is set to the parsed option. 2080eccea571SRuslan ErmilovThe index of 208149e11e3aSSheldon Hearnthe next argument is placed into the shell variable 2082dfe302abSRuslan Ermilov.Va OPTIND . 2083ab0a2172SSteve PriceIf an option takes an argument, it is placed into the shell variable 2084dfe302abSRuslan Ermilov.Va OPTARG . 208549e11e3aSSheldon HearnIf an invalid option is encountered, 2086dfe302abSRuslan Ermilov.Ar var 208749e11e3aSSheldon Hearnis set to 2088dfe302abSRuslan Ermilov.Ql \&? . 2089ab0a2172SSteve PriceIt returns a false value (1) when it encounters the end of the options. 209081e9cda2SRuslan Ermilov.It Ic hash Oo Fl rv Oc Op Ar command ... 209149e11e3aSSheldon HearnThe shell maintains a hash table which remembers the locations of commands. 209249e11e3aSSheldon HearnWith no arguments whatsoever, the 209349e11e3aSSheldon Hearn.Ic hash 209449e11e3aSSheldon Hearncommand prints out the contents of this table. 209549e11e3aSSheldon HearnEntries which have not been looked at since the last 209649e11e3aSSheldon Hearn.Ic cd 209749e11e3aSSheldon Hearncommand are marked with an asterisk; 209849e11e3aSSheldon Hearnit is possible for these entries to be invalid. 2099ab72124fSSteve Price.Pp 210049e11e3aSSheldon HearnWith arguments, the 210149e11e3aSSheldon Hearn.Ic hash 210249e11e3aSSheldon Hearncommand removes each specified 210349e11e3aSSheldon Hearn.Ar command 210449e11e3aSSheldon Hearnfrom the hash table (unless they are functions) and then locates it. 210549e11e3aSSheldon HearnWith the 210649e11e3aSSheldon Hearn.Fl v 210749e11e3aSSheldon Hearnoption, 210849e11e3aSSheldon Hearn.Ic hash 210949e11e3aSSheldon Hearnprints the locations of the commands as it finds them. 211049e11e3aSSheldon HearnThe 211149e11e3aSSheldon Hearn.Fl r 211249e11e3aSSheldon Hearnoption causes the 211349e11e3aSSheldon Hearn.Ic hash 211449e11e3aSSheldon Hearncommand to delete all the entries in the hash table except for functions. 211549e11e3aSSheldon Hearn.It Ic jobid Op Ar job 2116dfe302abSRuslan ErmilovPrint the process IDs of the processes in the specified 211749e11e3aSSheldon Hearn.Ar job . 211849e11e3aSSheldon HearnIf the 211949e11e3aSSheldon Hearn.Ar job 212049e11e3aSSheldon Hearnargument is omitted, use the current job. 2121de37e41cSStefan Farfeleder.It Ic jobs Oo Fl lps Oc Op Ar job ... 2122ad8a0759STim J. RobbinsPrint information about the specified jobs, or all jobs if no 2123ad8a0759STim J. Robbins.Ar job 2124ad8a0759STim J. Robbinsargument is given. 2125ad8a0759STim J. RobbinsThe information printed includes job ID, status and command name. 2126ad8a0759STim J. Robbins.Pp 2127ad8a0759STim J. RobbinsIf the 2128ad8a0759STim J. Robbins.Fl l 2129ad8a0759STim J. Robbinsoption is specified, the PID of each job is also printed. 2130ad8a0759STim J. RobbinsIf the 2131de37e41cSStefan Farfeleder.Fl p 2132de37e41cSStefan Farfelederoption is specified, only the process IDs for the process group leaders 2133de37e41cSStefan Farfelederare printed, one per line. 2134de37e41cSStefan FarfelederIf the 2135ad8a0759STim J. Robbins.Fl s 2136de37e41cSStefan Farfelederoption is specified, only the PIDs of the job commands are printed, one per 2137de37e41cSStefan Farfelederline. 21380a62a9caSJilles Tjoelker.It Ic kill 21390a62a9caSJilles TjoelkerA built-in equivalent of 21400a62a9caSJilles Tjoelker.Xr kill 1 21410a62a9caSJilles Tjoelkerthat additionally supports sending signals to jobs. 21428af11131SRuslan Ermilov.It Ic local Oo Ar variable ... Oc Op Fl 2143d2f90294SJesus R. CamouSee the 2144d2f90294SJesus R. Camou.Sx Functions 2145d2f90294SJesus R. Camousubsection. 21469897c45fSJilles Tjoelker.It Ic printf 21479897c45fSJilles TjoelkerA built-in equivalent of 21489897c45fSJilles Tjoelker.Xr printf 1 . 2149d74e011fSRuslan Ermilov.It Ic pwd Op Fl L | P 2150eccea571SRuslan ErmilovPrint the path of the current directory. 2151eccea571SRuslan ErmilovThe built-in command may 21524b88c807SRodney W. Grimesdiffer from the program of the same name because the 21538eed22b1SJens Schweikhardtbuilt-in command remembers what the current directory 2154eccea571SRuslan Ermilovis rather than recomputing it each time. 2155eccea571SRuslan ErmilovThis makes 2156eccea571SRuslan Ermilovit faster. 2157eccea571SRuslan ErmilovHowever, if the current directory is 215849e11e3aSSheldon Hearnrenamed, 21598eed22b1SJens Schweikhardtthe built-in version of 216049e11e3aSSheldon Hearn.Xr pwd 1 216149e11e3aSSheldon Hearnwill continue to print the old name for the directory. 2162178897f1STim J. Robbins.Pp 2163178897f1STim J. RobbinsIf the 2164178897f1STim J. Robbins.Fl P 2165178897f1STim J. Robbinsoption is specified, symbolic links are resolved. 2166178897f1STim J. RobbinsIf the 2167178897f1STim J. Robbins.Fl L 2168178897f1STim J. Robbinsoption is specified, the shell's notion of the current directory 2169178897f1STim J. Robbinsis printed (symbolic links are not resolved). 2170178897f1STim J. RobbinsThis is the default. 2171dfe302abSRuslan Ermilov.It Ic read Oo Fl p Ar prompt Oc Oo 2172dfe302abSRuslan Ermilov.Fl t Ar timeout Oc Oo Fl er Oc Ar variable ... 217349e11e3aSSheldon HearnThe 217449e11e3aSSheldon Hearn.Ar prompt 217549e11e3aSSheldon Hearnis printed if the 217649e11e3aSSheldon Hearn.Fl p 217749e11e3aSSheldon Hearnoption is specified 2178eccea571SRuslan Ermilovand the standard input is a terminal. 2179eccea571SRuslan ErmilovThen a line is 2180eccea571SRuslan Ermilovread from the standard input. 2181eccea571SRuslan ErmilovThe trailing newline 21824b88c807SRodney W. Grimesis deleted from the line and the line is split as 218349e11e3aSSheldon Hearndescribed in the section on 218449e11e3aSSheldon Hearn.Sx White Space Splitting (Field Splitting) 218549e11e3aSSheldon Hearnabove, and 21864b88c807SRodney W. Grimesthe pieces are assigned to the variables in order. 2187f434d1dfSAdam DavidIf there are more pieces than variables, the remaining 218849e11e3aSSheldon Hearnpieces (along with the characters in 2189dfe302abSRuslan Ermilov.Va IFS 219049e11e3aSSheldon Hearnthat separated them) 219149e11e3aSSheldon Hearnare assigned to the last variable. 2192f434d1dfSAdam DavidIf there are more variables than pieces, the remaining 2193f434d1dfSAdam Davidvariables are assigned the null string. 2194ab72124fSSteve Price.Pp 219549e11e3aSSheldon HearnBackslashes are treated specially, unless the 219649e11e3aSSheldon Hearn.Fl r 219749e11e3aSSheldon Hearnoption is 2198eccea571SRuslan Ermilovspecified. 2199eccea571SRuslan ErmilovIf a backslash is followed by 22008f0561ccSThomas Gellekuma newline, the backslash and the newline will be 2201eccea571SRuslan Ermilovdeleted. 2202eccea571SRuslan ErmilovIf a backslash is followed by any other 22038f0561ccSThomas Gellekumcharacter, the backslash will be deleted and the following 220449e11e3aSSheldon Hearncharacter will be treated as though it were not in 2205dfe302abSRuslan Ermilov.Va IFS , 220649e11e3aSSheldon Hearneven if it is. 22078f0561ccSThomas Gellekum.Pp 220849e11e3aSSheldon HearnIf the 220949e11e3aSSheldon Hearn.Fl t 221049e11e3aSSheldon Hearnoption is specified and the 221149e11e3aSSheldon Hearn.Ar timeout 2212fe40d6d3SJilles Tjoelkerelapses before a complete line of input is supplied, 221349e11e3aSSheldon Hearnthe 221449e11e3aSSheldon Hearn.Ic read 22151b161559SStefan Farfeledercommand will return an exit status of 1 without assigning any values. 221649e11e3aSSheldon HearnThe 221749e11e3aSSheldon Hearn.Ar timeout 221849e11e3aSSheldon Hearnvalue may optionally be followed by one of 2219dfe302abSRuslan Ermilov.Ql s , 2220dfe302abSRuslan Ermilov.Ql m 222149e11e3aSSheldon Hearnor 2222dfe302abSRuslan Ermilov.Ql h 2223a910f192SDima Dorfmanto explicitly specify seconds, minutes or hours. 222449e11e3aSSheldon HearnIf none is supplied, 2225dfe302abSRuslan Ermilov.Ql s 222649e11e3aSSheldon Hearnis assumed. 2227afa53c8dSMike Smith.Pp 222849e11e3aSSheldon HearnThe 222949e11e3aSSheldon Hearn.Fl e 223049e11e3aSSheldon Hearnoption exists only for backward compatibility with older scripts. 22319ba31ca0SRuslan Ermilov.It Ic readonly Oo Fl p Oc Op Ar name ... 223249e11e3aSSheldon HearnEach specified 223349e11e3aSSheldon Hearn.Ar name 223449e11e3aSSheldon Hearnis marked as read only, 223549e11e3aSSheldon Hearnso that it cannot be subsequently modified or unset. 223649e11e3aSSheldon HearnThe shell allows the value of a variable to be set 223749e11e3aSSheldon Hearnat the same time as it is marked read only 223849e11e3aSSheldon Hearnby using the following form: 2239dfe302abSRuslan Ermilov.Pp 2240dfe302abSRuslan Ermilov.D1 Ic readonly Ar name Ns = Ns Ar value 2241ab72124fSSteve Price.Pp 224249e11e3aSSheldon HearnWith no arguments the 224349e11e3aSSheldon Hearn.Ic readonly 224449e11e3aSSheldon Hearncommand lists the names of all read only variables. 224545086f8cSTim J. RobbinsIf the 224645086f8cSTim J. Robbins.Fl p 224745086f8cSTim J. Robbinsoption is specified, the read-only variables are printed as 224845086f8cSTim J. Robbins.Dq Ic readonly Ar name Ns = Ns Ar value 224945086f8cSTim J. Robbinslines, suitable for re-input to the shell. 2250d2f90294SJesus R. Camou.It Ic return Op Ar exitstatus 2251d2f90294SJesus R. CamouSee the 2252d2f90294SJesus R. Camou.Sx Functions 2253d2f90294SJesus R. Camousubsection. 225481e9cda2SRuslan Ermilov.It Ic set Oo Fl /+abCEefIimnpTuVvx Oc Oo Fl /+o Ar longname Oc Oo 225581e9cda2SRuslan Ermilov.Fl c Ar string Oc Op Fl - Ar arg ... 2256e918fc8fSSheldon HearnThe 2257e918fc8fSSheldon Hearn.Ic set 2258e918fc8fSSheldon Hearncommand performs three different functions: 2259ab72124fSSteve Price.Bl -item 2260ab72124fSSteve Price.It 226149e11e3aSSheldon HearnWith no arguments, it lists the values of all shell variables. 2262ab72124fSSteve Price.It 226349e11e3aSSheldon HearnIf options are given, 226449e11e3aSSheldon Hearneither in short form or using the long 226549e11e3aSSheldon Hearn.Dq Fl /+o Ar longname 226649e11e3aSSheldon Hearnform, 226749e11e3aSSheldon Hearnit sets or clears the specified options as described in the section called 22686b15476eSSheldon Hearn.Sx Argument List Processing . 2269ab72124fSSteve Price.It 227049e11e3aSSheldon HearnIf the 227149e11e3aSSheldon Hearn.Dq Fl - 227249e11e3aSSheldon Hearnoption is specified, 227349e11e3aSSheldon Hearn.Ic set 227449e11e3aSSheldon Hearnwill replace the shell's positional parameters with the subsequent 227549e11e3aSSheldon Hearnarguments. 227649e11e3aSSheldon HearnIf no arguments follow the 227749e11e3aSSheldon Hearn.Dq Fl - 227849e11e3aSSheldon Hearnoption, 227949e11e3aSSheldon Hearnall the positional parameters will be cleared, 228049e11e3aSSheldon Hearnwhich is equivalent to executing the command 2281dfe302abSRuslan Ermilov.Dq Li "shift $#" . 228249e11e3aSSheldon HearnThe 228349e11e3aSSheldon Hearn.Dq Fl - 22848eed22b1SJens Schweikhardtflag may be omitted when specifying arguments to be used 228549e11e3aSSheldon Hearnas positional replacement parameters. 228649e11e3aSSheldon HearnThis is not recommended, 228749e11e3aSSheldon Hearnbecause the first argument may begin with a dash 2288a762afe5SGiorgos Keramidas.Pq Ql - 228949e11e3aSSheldon Hearnor a plus 2290a762afe5SGiorgos Keramidas.Pq Ql + , 229149e11e3aSSheldon Hearnwhich the 229249e11e3aSSheldon Hearn.Ic set 229349e11e3aSSheldon Hearncommand will interpret as a request to enable or disable options. 2294ab72124fSSteve Price.El 2295dfe302abSRuslan Ermilov.It Ic setvar Ar variable value 229649e11e3aSSheldon HearnAssigns the specified 229749e11e3aSSheldon Hearn.Ar value 229849e11e3aSSheldon Hearnto the specified 229949e11e3aSSheldon Hearn.Ar variable . 2300dfe302abSRuslan ErmilovThe 2301dfe302abSRuslan Ermilov.Ic setvar 2302dfe302abSRuslan Ermilovcommand is intended to be used in functions that 2303e6d3cf26SSheldon Hearnassign values to variables whose names are passed as parameters. 230449e11e3aSSheldon HearnIn general it is better to write 2305dfe302abSRuslan Ermilov.Dq Ar variable Ns = Ns Ar value 230649e11e3aSSheldon Hearnrather than using 230749e11e3aSSheldon Hearn.Ic setvar . 230849e11e3aSSheldon Hearn.It Ic shift Op Ar n 230949e11e3aSSheldon HearnShift the positional parameters 231049e11e3aSSheldon Hearn.Ar n 2311e918fc8fSSheldon Hearntimes, or once if 231249e11e3aSSheldon Hearn.Ar n 231349e11e3aSSheldon Hearnis not specified. 2314dfe302abSRuslan ErmilovA shift sets the value of 2315dfe302abSRuslan Ermilov.Li $1 2316dfe302abSRuslan Ermilovto the value of 2317dfe302abSRuslan Ermilov.Li $2 , 2318dfe302abSRuslan Ermilovthe value of 2319dfe302abSRuslan Ermilov.Li $2 2320dfe302abSRuslan Ermilovto the value of 2321dfe302abSRuslan Ermilov.Li $3 , 2322dfe302abSRuslan Ermilovand so on, 2323dfe302abSRuslan Ermilovdecreasing the value of 2324dfe302abSRuslan Ermilov.Li $# 2325dfe302abSRuslan Ermilovby one. 232649e11e3aSSheldon HearnIf there are zero positional parameters, shifting does not do anything. 232710845922SYaroslav Tykhiy.It Ic test 232810845922SYaroslav TykhiyA built-in equivalent of 232910845922SYaroslav Tykhiy.Xr test 1 . 23301974986aSStefan Farfeleder.It Ic times 23311974986aSStefan FarfelederPrint the amount of time spent executing the shell and its children. 23321974986aSStefan FarfelederThe first output line shows the user and system times for the shell 23331974986aSStefan Farfelederitself, the second one contains the user and system times for the 23341974986aSStefan Farfelederchildren. 233581e9cda2SRuslan Ermilov.It Ic trap Oo Ar action Oc Ar signal ... 23360673e800SStefan Farfeleder.It Ic trap Fl l 233749e11e3aSSheldon HearnCause the shell to parse and execute 233849e11e3aSSheldon Hearn.Ar action 233949e11e3aSSheldon Hearnwhen any specified 234049e11e3aSSheldon Hearn.Ar signal 234149e11e3aSSheldon Hearnis received. 2342162ae3aeSTony FinchThe signals are specified by name or number. 2343162ae3aeSTony FinchIn addition, the pseudo-signal 2344f5d1157fSRuslan Ermilov.Cm EXIT 2345dfe302abSRuslan Ermilovmay be used to specify an 2346dfe302abSRuslan Ermilov.Ar action 2347dfe302abSRuslan Ermilovthat is performed when the shell terminates. 234849e11e3aSSheldon HearnThe 234949e11e3aSSheldon Hearn.Ar action 23500673e800SStefan Farfeledermay be an empty string or a dash 23519badf57fSRuslan Ermilov.Pq Ql - ; 235249e11e3aSSheldon Hearnthe former causes the specified signal to be ignored 235349e11e3aSSheldon Hearnand the latter causes the default action to be taken. 23540673e800SStefan FarfelederOmitting the 23550673e800SStefan Farfeleder.Ar action 23560673e800SStefan Farfelederis another way to request the default action, for compatibility reasons this 23570673e800SStefan Farfelederusage is not recommended though. 2358ef89d04fSJilles TjoelkerIn a subshell environment, 2359ef89d04fSJilles Tjoelkerthe shell resets trapped (but not ignored) signals to the default action. 236049e11e3aSSheldon HearnThe 236149e11e3aSSheldon Hearn.Ic trap 236249e11e3aSSheldon Hearncommand has no effect on signals that were ignored on entry to the shell. 23630673e800SStefan Farfeleder.Pp 23640673e800SStefan FarfelederOption 23650673e800SStefan Farfeleder.Fl l 23660673e800SStefan Farfeledercauses the 23670673e800SStefan Farfeleder.Ic trap 23680673e800SStefan Farfeledercommand to display a list of valid signal names. 2369a6557dcbSYaroslav Tykhiy.It Ic true 2370a6557dcbSYaroslav TykhiyA null command that returns a 0 (true) exit value. 237149e11e3aSSheldon Hearn.It Ic type Op Ar name ... 237249e11e3aSSheldon HearnInterpret each 237349e11e3aSSheldon Hearn.Ar name 237449e11e3aSSheldon Hearnas a command and print the resolution of the command search. 2375e6d3cf26SSheldon HearnPossible resolutions are: 237630268dfaSJilles Tjoelkershell keyword, alias, special shell builtin, shell builtin, command, 237730268dfaSJilles Tjoelkertracked alias 237849e11e3aSSheldon Hearnand not found. 237949e11e3aSSheldon HearnFor aliases the alias expansion is printed; 238049e11e3aSSheldon Hearnfor commands and tracked aliases 238149e11e3aSSheldon Hearnthe complete pathname of the command is printed. 2382c8054a61SJilles Tjoelker.It Ic ulimit Oo Fl HSabcdflmnpstuvw Oc Op Ar limit 2383ab72124fSSteve PriceSet or display resource limits (see 2384ab72124fSSteve Price.Xr getrlimit 2 ) . 23856b15476eSSheldon HearnIf 238649e11e3aSSheldon Hearn.Ar limit 23876b15476eSSheldon Hearnis specified, the named resource will be set; 23887a2afe64SJoerg Wunschotherwise the current resource value will be displayed. 2389ab72124fSSteve Price.Pp 23906b15476eSSheldon HearnIf 239149e11e3aSSheldon Hearn.Fl H 239249e11e3aSSheldon Hearnis specified, the hard limits will be set or displayed. 239349e11e3aSSheldon HearnWhile everybody is allowed to reduce a hard limit, 239449e11e3aSSheldon Hearnonly the superuser can increase it. 239549e11e3aSSheldon HearnThe 239649e11e3aSSheldon Hearn.Fl S 239749e11e3aSSheldon Hearnoption 2398eccea571SRuslan Ermilovspecifies the soft limits instead. 2399eccea571SRuslan ErmilovWhen displaying limits, 24006b15476eSSheldon Hearnonly one of 240149e11e3aSSheldon Hearn.Fl S 24026b15476eSSheldon Hearnor 240349e11e3aSSheldon Hearn.Fl H 240449e11e3aSSheldon Hearncan be given. 240549e11e3aSSheldon HearnThe default is to display the soft limits, 240649e11e3aSSheldon Hearnand to set both the hard and the soft limits. 2407ab72124fSSteve Price.Pp 24086b15476eSSheldon HearnOption 240949e11e3aSSheldon Hearn.Fl a 241049e11e3aSSheldon Hearncauses the 241149e11e3aSSheldon Hearn.Ic ulimit 241249e11e3aSSheldon Hearncommand to display all resources. 241349e11e3aSSheldon HearnThe parameter 241449e11e3aSSheldon Hearn.Ar limit 24156b15476eSSheldon Hearnis not acceptable in this mode. 2416ab72124fSSteve Price.Pp 24177a2afe64SJoerg WunschThe remaining options specify which resource value is to be 241849e11e3aSSheldon Hearndisplayed or modified. 241949e11e3aSSheldon HearnThey are mutually exclusive. 242081e9cda2SRuslan Ermilov.Bl -tag -width indent 24213652a236SMartin Cracauer.It Fl b Ar sbsize 24223652a236SMartin CracauerThe maximum size of socket buffer usage, in bytes. 242349e11e3aSSheldon Hearn.It Fl c Ar coredumpsize 24247a2afe64SJoerg WunschThe maximal size of core dump files, in 512-byte blocks. 242549e11e3aSSheldon Hearn.It Fl d Ar datasize 24267a2afe64SJoerg WunschThe maximal size of the data segment of a process, in kilobytes. 242749e11e3aSSheldon Hearn.It Fl f Ar filesize 242849e11e3aSSheldon HearnThe maximal size of a file, in 512-byte blocks. 242949e11e3aSSheldon Hearn.It Fl l Ar lockedmem 24307a2afe64SJoerg WunschThe maximal size of memory that can be locked by a process, in 24317a2afe64SJoerg Wunschkilobytes. 243249e11e3aSSheldon Hearn.It Fl m Ar memoryuse 24337a2afe64SJoerg WunschThe maximal resident set size of a process, in kilobytes. 243449e11e3aSSheldon Hearn.It Fl n Ar nofiles 24357a2afe64SJoerg WunschThe maximal number of descriptors that could be opened by a process. 2436331773cdSEd Schouten.It Fl p Ar pseudoterminals 2437331773cdSEd SchoutenThe maximal number of pseudo-terminals for this user ID. 243849e11e3aSSheldon Hearn.It Fl s Ar stacksize 24397a2afe64SJoerg WunschThe maximal size of the stack segment, in kilobytes. 244049e11e3aSSheldon Hearn.It Fl t Ar time 24417a2afe64SJoerg WunschThe maximal amount of CPU time to be used by each process, in seconds. 244249e11e3aSSheldon Hearn.It Fl u Ar userproc 2443ab72124fSSteve PriceThe maximal number of simultaneous processes for this user ID. 244462df9b62SSheldon Hearn.It Fl v Ar virtualmem 24452d41ef4bSMatthew DillonThe maximal virtual size of a process, in kilobytes. 2446c8054a61SJilles Tjoelker.It Fl w Ar swapuse 2447c8054a61SJilles TjoelkerThe maximum amount of swap space reserved or used for this user ID, 2448c8054a61SJilles Tjoelkerin kilobytes. 2449ab72124fSSteve Price.El 2450faa1ed35SStefan Farfeleder.It Ic umask Oo Fl S Oc Op Ar mask 245149e11e3aSSheldon HearnSet the file creation mask (see 2452ab72124fSSteve Price.Xr umask 2 ) 2453faa1ed35SStefan Farfelederto the octal or symbolic (see 2454faa1ed35SStefan Farfeleder.Xr chmod 1 ) 2455faa1ed35SStefan Farfeledervalue specified by 24565d3b843bSSheldon Hearn.Ar mask . 245749e11e3aSSheldon HearnIf the argument is omitted, the current mask value is printed. 2458faa1ed35SStefan FarfelederIf the 2459faa1ed35SStefan Farfeleder.Fl S 2460faa1ed35SStefan Farfelederoption is specified, the output is symbolic, otherwise the output is octal. 2461de4ad382SStefan Farfeleder.It Ic unalias Oo Fl a Oc Op Ar name ... 2462de4ad382SStefan FarfelederThe specified alias names are removed. 24636b15476eSSheldon HearnIf 24641b9735e6SSheldon Hearn.Fl a 24656b15476eSSheldon Hearnis specified, all aliases are removed. 24669ba31ca0SRuslan Ermilov.It Ic unset Oo Fl fv Oc Ar name ... 24678b34ad92STim J. RobbinsThe specified variables or functions are unset and unexported. 24688b34ad92STim J. RobbinsIf the 24698b34ad92STim J. Robbins.Fl v 24708b34ad92STim J. Robbinsoption is specified or no options are given, the 247149e11e3aSSheldon Hearn.Ar name 24728b34ad92STim J. Robbinsarguments are treated as variable names. 24738b34ad92STim J. RobbinsIf the 24748b34ad92STim J. Robbins.Fl f 24758b34ad92STim J. Robbinsoption is specified, the 24768b34ad92STim J. Robbins.Ar name 24778b34ad92STim J. Robbinsarguments are treated as function names. 247849e11e3aSSheldon Hearn.It Ic wait Op Ar job 247949e11e3aSSheldon HearnWait for the specified 248049e11e3aSSheldon Hearn.Ar job 248149e11e3aSSheldon Hearnto complete and return the exit status of the last process in the 248249e11e3aSSheldon Hearn.Ar job . 2483e6d3cf26SSheldon HearnIf the argument is omitted, wait for all jobs to complete 2484e6d3cf26SSheldon Hearnand return an exit status of zero. 2485ab72124fSSteve Price.El 2486ab72124fSSteve Price.Ss Commandline Editing 2487ab72124fSSteve PriceWhen 2488ab72124fSSteve Price.Nm 2489ab72124fSSteve Priceis being used interactively from a terminal, the current command 249049e11e3aSSheldon Hearnand the command history 249149e11e3aSSheldon Hearn(see 249249e11e3aSSheldon Hearn.Ic fc 249349e11e3aSSheldon Hearnin 24948eed22b1SJens Schweikhardt.Sx Built-in Commands ) 2495dfe302abSRuslan Ermilovcan be edited using 2496dfe302abSRuslan Ermilov.Nm vi Ns -mode 2497dfe302abSRuslan Ermilovcommand line editing. 249849e11e3aSSheldon HearnThis mode uses commands similar 2499dfe302abSRuslan Ermilovto a subset of those described in the 2500dfe302abSRuslan Ermilov.Xr vi 1 2501dfe302abSRuslan Ermilovman page. 250249e11e3aSSheldon HearnThe command 2503dfe302abSRuslan Ermilov.Dq Li "set -o vi" 250449e11e3aSSheldon Hearn(or 2505dfe302abSRuslan Ermilov.Dq Li "set -V" ) 2506dfe302abSRuslan Ermilovenables 2507dfe302abSRuslan Ermilov.Nm vi Ns -mode 2508dfe302abSRuslan Ermilovediting and places 2509ab72124fSSteve Price.Nm 2510dfe302abSRuslan Ermilovinto 2511dfe302abSRuslan Ermilov.Nm vi 2512dfe302abSRuslan Ermilovinsert mode. 2513dfe302abSRuslan ErmilovWith 2514dfe302abSRuslan Ermilov.Nm vi Ns -mode 2515dfe302abSRuslan Ermilovenabled, 2516ab72124fSSteve Price.Nm 251749e11e3aSSheldon Hearncan be switched between insert mode and command mode by typing 251849e11e3aSSheldon Hearn.Aq ESC . 251949e11e3aSSheldon HearnHitting 252049e11e3aSSheldon Hearn.Aq return 252149e11e3aSSheldon Hearnwhile in command mode will pass the line to the shell. 2522ab72124fSSteve Price.Pp 252349e11e3aSSheldon HearnSimilarly, the 2524dfe302abSRuslan Ermilov.Dq Li "set -o emacs" 252549e11e3aSSheldon Hearn(or 2526dfe302abSRuslan Ermilov.Dq Li "set -E" ) 252749e11e3aSSheldon Hearncommand can be used to enable a subset of 2528dfe302abSRuslan Ermilov.Nm emacs Ns -style 2529dfe302abSRuslan Ermilovcommand line editing features. 253038afdcbcSGiorgos Keramidas.Sh ENVIRONMENT 253138afdcbcSGiorgos KeramidasThe following environment variables affect the execution of 253238afdcbcSGiorgos Keramidas.Nm : 25331788b7ffSJilles Tjoelker.Bl -tag -width ".Ev LANGXXXXXX" 25341788b7ffSJilles Tjoelker.It Ev ENV 25351788b7ffSJilles TjoelkerInitialization file for interactive shells. 25361788b7ffSJilles Tjoelker.It Ev LANG , Ev LC_* 25371788b7ffSJilles TjoelkerLocale settings. 25381788b7ffSJilles TjoelkerThese are inherited by children of the shell, 25391788b7ffSJilles Tjoelkerand is used in a limited manner by the shell itself. 25401788b7ffSJilles Tjoelker.It Ev PWD 25411788b7ffSJilles TjoelkerAn absolute pathname for the current directory, 25421788b7ffSJilles Tjoelkerpossibly containing symbolic links. 25431788b7ffSJilles TjoelkerThis is used and updated by the shell. 254438afdcbcSGiorgos Keramidas.It Ev TERM 254538afdcbcSGiorgos KeramidasThe default terminal setting for the shell. 254638afdcbcSGiorgos KeramidasThis is inherited by children of the shell, and is used in the history 254738afdcbcSGiorgos Keramidasediting modes. 254838afdcbcSGiorgos Keramidas.El 25491788b7ffSJilles Tjoelker.Pp 25501788b7ffSJilles TjoelkerAdditionally, all environment variables are turned into shell variables 25511788b7ffSJilles Tjoelkerat startup, 25521788b7ffSJilles Tjoelkerwhich may affect the shell as described under 25531788b7ffSJilles Tjoelker.Sx Special Variables . 2554fb039b55SRuslan Ermilov.Sh EXIT STATUS 2555fb039b55SRuslan ErmilovErrors that are detected by the shell, such as a syntax error, will 2556fb039b55SRuslan Ermilovcause the shell to exit with a non-zero exit status. 2557fb039b55SRuslan ErmilovIf the shell is not an interactive shell, the execution of the shell 2558fb039b55SRuslan Ermilovfile will be aborted. 2559fb039b55SRuslan ErmilovOtherwise the shell will return the exit status of the last command 2560dfe302abSRuslan Ermilovexecuted, or if the 2561dfe302abSRuslan Ermilov.Ic exit 2562dfe302abSRuslan Ermilovbuiltin is used with a numeric argument, it 2563fb039b55SRuslan Ermilovwill return the argument. 2564c83e7c50SJoseph Koshy.Sh SEE ALSO 2565c644db6aSSheldon Hearn.Xr builtin 1 , 256638afdcbcSGiorgos Keramidas.Xr chsh 1 , 2567c644db6aSSheldon Hearn.Xr echo 1 , 256838afdcbcSGiorgos Keramidas.Xr ed 1 , 256938afdcbcSGiorgos Keramidas.Xr emacs 1 , 25700a62a9caSJilles Tjoelker.Xr kill 1 , 25719897c45fSJilles Tjoelker.Xr printf 1 , 25725521ff5aSRuslan Ermilov.Xr pwd 1 , 257338afdcbcSGiorgos Keramidas.Xr test 1 , 257438afdcbcSGiorgos Keramidas.Xr vi 1 , 257538afdcbcSGiorgos Keramidas.Xr execve 2 , 257638afdcbcSGiorgos Keramidas.Xr getrlimit 2 , 2577fd50df30SRuslan Ermilov.Xr umask 2 , 257838afdcbcSGiorgos Keramidas.Xr editrc 5 2579ab72124fSSteve Price.Sh HISTORY 2580b8923d4cSWolfram SchneiderA 2581ab72124fSSteve Price.Nm 25829cdd1e3fSYaroslav Tykhiycommand, the Thompson shell, appeared in 2583fc482908STim J. Robbins.At v1 . 25849cdd1e3fSYaroslav TykhiyIt was superseded in 25859cdd1e3fSYaroslav Tykhiy.At v7 25869cdd1e3fSYaroslav Tykhiyby the Bourne shell, which inherited the name 25879cdd1e3fSYaroslav Tykhiy.Nm . 25889cdd1e3fSYaroslav Tykhiy.Pp 25899cdd1e3fSYaroslav TykhiyThis version of 25909cdd1e3fSYaroslav Tykhiy.Nm 25919cdd1e3fSYaroslav Tykhiywas rewritten in 1989 under the 25929cdd1e3fSYaroslav Tykhiy.Bx 25939cdd1e3fSYaroslav Tykhiylicense after the Bourne shell from 25949cdd1e3fSYaroslav Tykhiy.At V.4 . 25959cdd1e3fSYaroslav Tykhiy.Sh AUTHORS 25969cdd1e3fSYaroslav TykhiyThis version of 25979cdd1e3fSYaroslav Tykhiy.Nm 25989badf57fSRuslan Ermilovwas originally written by 25999badf57fSRuslan Ermilov.An Kenneth Almquist . 2600128dc4a2STim J. Robbins.Sh BUGS 2601128dc4a2STim J. RobbinsThe 2602128dc4a2STim J. Robbins.Nm 260350df3424SJilles Tjoelkerutility does not recognize multibyte characters other than UTF-8. 260450df3424SJilles TjoelkerSplitting using 260550df3424SJilles Tjoelker.Va IFS 260650df3424SJilles Tjoelkerand the line editing library 260750df3424SJilles Tjoelker.Xr editline 3 260850df3424SJilles Tjoelkerdo not recognize multibyte characters. 2609