xref: /freebsd/bin/sh/sh.1 (revision 7bd8da72c5814b486ae7f492286fe3ac0a5bf03d)
19ddb49cbSWarner Losh.\"-
2e2a72442SAlexander Ziaee.\" SPDX-License-Identifier: BSD-3-Clause
3e2a72442SAlexander Ziaee.\"
44b88c807SRodney W. Grimes.\" Copyright (c) 1991, 1993
54b88c807SRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
64b88c807SRodney W. Grimes.\"
74b88c807SRodney W. Grimes.\" This code is derived from software contributed to Berkeley by
84b88c807SRodney W. Grimes.\" Kenneth Almquist.
94b88c807SRodney W. Grimes.\"
104b88c807SRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
114b88c807SRodney W. Grimes.\" modification, are permitted provided that the following conditions
124b88c807SRodney W. Grimes.\" are met:
134b88c807SRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
144b88c807SRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
154b88c807SRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
164b88c807SRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
174b88c807SRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh.\" 3. Neither the name of the University nor the names of its contributors
194b88c807SRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
204b88c807SRodney W. Grimes.\"    without specific prior written permission.
214b88c807SRodney W. Grimes.\"
224b88c807SRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
234b88c807SRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
244b88c807SRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254b88c807SRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
264b88c807SRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
274b88c807SRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
284b88c807SRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
294b88c807SRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
304b88c807SRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
314b88c807SRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
324b88c807SRodney W. Grimes.\" SUCH DAMAGE.
334b88c807SRodney W. Grimes.\"
34*7bd8da72SMichael Osipov.Dd March 8, 2025
35ab72124fSSteve Price.Dt SH 1
36caa2db3bSRuslan Ermilov.Os
37ab72124fSSteve Price.Sh NAME
38ab72124fSSteve Price.Nm sh
39ab72124fSSteve Price.Nd command interpreter (shell)
40ab72124fSSteve Price.Sh SYNOPSIS
41ab72124fSSteve Price.Nm
4265f5dd42SBaptiste Daroussin.Op Fl /+abCEefhIilmnPpTuVvx
43ab72124fSSteve Price.Op Fl /+o Ar longname
4457a40f7dSJilles Tjoelker.Oo
4557a40f7dSJilles Tjoelker.Ar script
4657a40f7dSJilles Tjoelker.Op Ar arg ...
4757a40f7dSJilles Tjoelker.Oc
4857a40f7dSJilles Tjoelker.Nm
4965f5dd42SBaptiste Daroussin.Op Fl /+abCEefhIilmnPpTuVvx
5057a40f7dSJilles Tjoelker.Op Fl /+o Ar longname
5157a40f7dSJilles Tjoelker.Fl c Ar string
5257a40f7dSJilles Tjoelker.Oo
5357a40f7dSJilles Tjoelker.Ar name
5457a40f7dSJilles Tjoelker.Op Ar arg ...
5557a40f7dSJilles Tjoelker.Oc
5657a40f7dSJilles Tjoelker.Nm
5765f5dd42SBaptiste Daroussin.Op Fl /+abCEefhIilmnPpTuVvx
5857a40f7dSJilles Tjoelker.Op Fl /+o Ar longname
5957a40f7dSJilles Tjoelker.Fl s
6081e9cda2SRuslan Ermilov.Op Ar arg ...
61ab72124fSSteve Price.Sh DESCRIPTION
62e918fc8fSSheldon HearnThe
63e918fc8fSSheldon Hearn.Nm
64e918fc8fSSheldon Hearnutility is the standard command interpreter for the system.
65ab72124fSSteve PriceThe current version of
66ab72124fSSteve Price.Nm
673fdfd0a4SJilles Tjoelkeris close to the
683fdfd0a4SJilles Tjoelker.St -p1003.1
69eccea571SRuslan Ermilovspecification for the shell.
703fdfd0a4SJilles TjoelkerIt only supports features
7117e98da8SMateusz Piotrowskidesignated by POSIX,
723fdfd0a4SJilles Tjoelkerplus a few Berkeley extensions.
7349e11e3aSSheldon HearnThis man page is not intended to be a tutorial nor a complete
744b88c807SRodney W. Grimesspecification of the shell.
75ab72124fSSteve Price.Ss Overview
764b88c807SRodney W. GrimesThe shell is a command that reads lines from
774b88c807SRodney W. Grimeseither a file or the terminal, interprets them, and
78e6d3cf26SSheldon Hearngenerally executes other commands.
7949e11e3aSSheldon HearnIt is the program that is started when a user logs into the system,
8049e11e3aSSheldon Hearnalthough a user can select a different shell with the
81c644db6aSSheldon Hearn.Xr chsh 1
8249e11e3aSSheldon Hearncommand.
834b88c807SRodney W. GrimesThe shell
844b88c807SRodney W. Grimesimplements a language that has flow control constructs,
854b88c807SRodney W. Grimesa macro facility that provides a variety of features in
868eed22b1SJens Schweikhardtaddition to data storage, along with built-in history and line
87eccea571SRuslan Ermilovediting capabilities.
88eccea571SRuslan ErmilovIt incorporates many features to
894b88c807SRodney W. Grimesaid interactive use and has the advantage that the interpretative
904b88c807SRodney W. Grimeslanguage is common to both interactive and non-interactive
91eccea571SRuslan Ermilovuse (shell scripts).
92eccea571SRuslan ErmilovThat is, commands can be typed directly
9349e11e3aSSheldon Hearnto the running shell or can be put into a file,
9449e11e3aSSheldon Hearnwhich can be executed directly by the shell.
95ab72124fSSteve Price.Ss Invocation
9649e11e3aSSheldon Hearn.\"
9749e11e3aSSheldon Hearn.\" XXX This next sentence is incredibly confusing.
9849e11e3aSSheldon Hearn.\"
9949e11e3aSSheldon HearnIf no arguments are present and if the standard input of the shell
10049e11e3aSSheldon Hearnis connected to a terminal
10149e11e3aSSheldon Hearn(or if the
10249e11e3aSSheldon Hearn.Fl i
10349e11e3aSSheldon Hearnoption is set),
104eccea571SRuslan Ermilovthe shell is considered an interactive shell.
105eccea571SRuslan ErmilovAn interactive shell
1064b88c807SRodney W. Grimesgenerally prompts before each command and handles programming
1074b88c807SRodney W. Grimesand command errors differently (as described below).
1084b88c807SRodney W. GrimesWhen first starting, the shell inspects argument 0, and
10949e11e3aSSheldon Hearnif it begins with a dash
110a762afe5SGiorgos Keramidas.Pq Ql - ,
11149e11e3aSSheldon Hearnthe shell is also considered a login shell.
11249e11e3aSSheldon HearnThis is normally done automatically by the system
113eccea571SRuslan Ermilovwhen the user first logs in.
114eccea571SRuslan ErmilovA login shell first reads commands
115ab72124fSSteve Pricefrom the files
116ab72124fSSteve Price.Pa /etc/profile
11749e11e3aSSheldon Hearnand then
118ab72124fSSteve Price.Pa .profile
119dfe302abSRuslan Ermilovin a user's home directory,
120eccea571SRuslan Ermilovif they exist.
121eccea571SRuslan ErmilovIf the environment variable
122ab72124fSSteve Price.Ev ENV
123ab72124fSSteve Priceis set on entry to a shell, or is set in the
124ab72124fSSteve Price.Pa .profile
1251a62d884SJilles Tjoelkerof a login shell, the shell then subjects its value to parameter expansion
1261a62d884SJilles Tjoelkerand arithmetic expansion and reads commands from the named file.
127ab72124fSSteve PriceTherefore, a user should place commands that are to be executed only
128ab72124fSSteve Priceat login time in the
129ab72124fSSteve Price.Pa .profile
130ab72124fSSteve Pricefile, and commands that are executed for every shell inside the
131ab72124fSSteve Price.Ev ENV
132e6d3cf26SSheldon Hearnfile.
13349e11e3aSSheldon HearnThe user can set the
134ab72124fSSteve Price.Ev ENV
13549e11e3aSSheldon Hearnvariable to some file by placing the following line in the file
136ab72124fSSteve Price.Pa .profile
13749e11e3aSSheldon Hearnin the home directory,
138ab72124fSSteve Pricesubstituting for
1395dd78c9fSJoel Dahl.Pa .shrc
14049e11e3aSSheldon Hearnthe filename desired:
14149e11e3aSSheldon Hearn.Pp
1425dd78c9fSJoel Dahl.Dl "ENV=$HOME/.shrc; export ENV"
14349e11e3aSSheldon Hearn.Pp
14449e11e3aSSheldon HearnThe first non-option argument specified on the command line
14549e11e3aSSheldon Hearnwill be treated as the
1464b88c807SRodney W. Grimesname of a file from which to read commands (a shell script), and
1474b88c807SRodney W. Grimesthe remaining arguments are set as the positional parameters
148dfe302abSRuslan Ermilovof the shell
149dfe302abSRuslan Ermilov.Li ( $1 , $2 ,
150dfe302abSRuslan Ermilovetc.).
151eccea571SRuslan ErmilovOtherwise, the shell reads commands
1524b88c807SRodney W. Grimesfrom its standard input.
153ea76be72SSteve Price.Pp
154ea76be72SSteve PriceUnlike older versions of
155ea76be72SSteve Price.Nm
156ea76be72SSteve Pricethe
157ea76be72SSteve Price.Ev ENV
158eccea571SRuslan Ermilovscript is only sourced on invocation of interactive shells.
159eccea571SRuslan ErmilovThis
160ea76be72SSteve Pricecloses a well-known, and sometimes easily exploitable security
161ea76be72SSteve Pricehole related to poorly thought out
162ea76be72SSteve Price.Ev ENV
163ea76be72SSteve Pricescripts.
164ab72124fSSteve Price.Ss Argument List Processing
165ab72124fSSteve PriceAll of the single letter options to
166ab72124fSSteve Price.Nm
16749e11e3aSSheldon Hearnhave a corresponding long name,
16849e11e3aSSheldon Hearnwith the exception of
16949e11e3aSSheldon Hearn.Fl c
17049e11e3aSSheldon Hearnand
17149e11e3aSSheldon Hearn.Fl /+o .
17249e11e3aSSheldon HearnThese long names are provided next to the single letter options
17349e11e3aSSheldon Hearnin the descriptions below.
17449e11e3aSSheldon HearnThe long name for an option may be specified as an argument to the
17549e11e3aSSheldon Hearn.Fl /+o
17649e11e3aSSheldon Hearnoption of
177facc6767SRuslan Ermilov.Nm .
17849e11e3aSSheldon HearnOnce the shell is running,
17949e11e3aSSheldon Hearnthe long name for an option may be specified as an argument to the
18049e11e3aSSheldon Hearn.Fl /+o
18149e11e3aSSheldon Hearnoption of the
182c644db6aSSheldon Hearn.Ic set
1838eed22b1SJens Schweikhardtbuilt-in command
18449e11e3aSSheldon Hearn(described later in the section called
1858eed22b1SJens Schweikhardt.Sx Built-in Commands ) .
18649e11e3aSSheldon HearnIntroducing an option with a dash
187a762afe5SGiorgos Keramidas.Pq Ql -
18849e11e3aSSheldon Hearnenables the option,
18949e11e3aSSheldon Hearnwhile using a plus
190a762afe5SGiorgos Keramidas.Pq Ql +
19149e11e3aSSheldon Hearndisables the option.
19249e11e3aSSheldon HearnA
19349e11e3aSSheldon Hearn.Dq Li --
194fab26805SJoseph Koshyor plain
195dfe302abSRuslan Ermilov.Ql -
196fab26805SJoseph Koshywill stop option processing and will force the remaining
197fab26805SJoseph Koshywords on the command line to be treated as arguments.
19849e11e3aSSheldon HearnThe
19949e11e3aSSheldon Hearn.Fl /+o
20049e11e3aSSheldon Hearnand
20149e11e3aSSheldon Hearn.Fl c
20249e11e3aSSheldon Hearnoptions do not have long names.
20349e11e3aSSheldon HearnThey take arguments and are described after the single letter options.
20481e9cda2SRuslan Ermilov.Bl -tag -width indent
205ab72124fSSteve Price.It Fl a Li allexport
20649e11e3aSSheldon HearnFlag variables for export when assignments are made to them.
207ab72124fSSteve Price.It Fl b Li notify
208ab72124fSSteve PriceEnable asynchronous notification of background job
209ab72124fSSteve Pricecompletion.
210c4d9468eSRuslan Ermilov(UNIMPLEMENTED)
211ab72124fSSteve Price.It Fl C Li noclobber
212c644db6aSSheldon HearnDo not overwrite existing files with
213dfe302abSRuslan Ermilov.Ql > .
214ab72124fSSteve Price.It Fl E Li emacs
2158eed22b1SJens SchweikhardtEnable the built-in
216825225e5SJens Schweikhardt.Xr emacs 1 Pq Pa ports/editors/emacs
21749e11e3aSSheldon Hearncommand line editor (disables the
218ab72124fSSteve Price.Fl V
219d5f6b530SJilles Tjoelkeroption if it has been set;
220d5f6b530SJilles Tjoelkerset automatically when interactive on terminals).
221ab72124fSSteve Price.It Fl e Li errexit
22249e11e3aSSheldon HearnExit immediately if any untested command fails in non-interactive mode.
2234b88c807SRodney W. GrimesThe exit status of a command is considered to be
22474053659SStefan Farfelederexplicitly tested if the command is part of the list used to control
2258af11131SRuslan Ermilovan
2268af11131SRuslan Ermilov.Ic if , elif , while ,
2278af11131SRuslan Ermilovor
2288af11131SRuslan Ermilov.Ic until ;
2298af11131SRuslan Ermilovif the command is the left
230ab72124fSSteve Pricehand operand of an
23149e11e3aSSheldon Hearn.Dq Li &&
232ab72124fSSteve Priceor
23349e11e3aSSheldon Hearn.Dq Li ||
23474053659SStefan Farfelederoperator; or if the command is a pipeline preceded by the
23574053659SStefan Farfeleder.Ic !\&
236b14cfdf6SJilles Tjoelkerkeyword.
23774053659SStefan FarfelederIf a shell function is executed and its exit status is explicitly
23874053659SStefan Farfeledertested, all commands of the function are considered to be tested as
23974053659SStefan Farfelederwell.
240b14cfdf6SJilles Tjoelker.Pp
241*7bd8da72SMichael OsipovNote that many commands return non-zero values to convey information other
242*7bd8da72SMichael Osipovthan errors, which can cause unexpected program termination with
243*7bd8da72SMichael Osipov.Fl e .
244ab72124fSSteve Price.It Fl f Li noglob
2454b88c807SRodney W. GrimesDisable pathname expansion.
24654b16435SJilles Tjoelker.It Fl h Li trackall
24717e98da8SMateusz PiotrowskiA do-nothing option for POSIX compliance.
248ab72124fSSteve Price.It Fl I Li ignoreeof
24949e11e3aSSheldon HearnIgnore
250dfe302abSRuslan Ermilov.Dv EOF Ap s
25149e11e3aSSheldon Hearnfrom input when in interactive mode.
252ab72124fSSteve Price.It Fl i Li interactive
253ab72124fSSteve PriceForce the shell to behave interactively.
25465f5dd42SBaptiste Daroussin.It Fl l
25565f5dd42SBaptiste DaroussinForce the shell to act as if it has been invoked as a login shell.
256ab72124fSSteve Price.It Fl m Li monitor
257ab72124fSSteve PriceTurn on job control (set automatically when interactive).
258cd60e2c6SJilles TjoelkerA new process group is created for each pipeline (called a job).
259cd60e2c6SJilles TjoelkerIt is possible to suspend jobs or to have them run in the foreground or
260cd60e2c6SJilles Tjoelkerin the background.
261cd60e2c6SJilles TjoelkerIn a non-interactive shell,
262cd60e2c6SJilles Tjoelkerthis option can be set even if no terminal is available
263cd60e2c6SJilles Tjoelkerand is useful to place processes in separate process groups.
264ab72124fSSteve Price.It Fl n Li noexec
2654b88c807SRodney W. GrimesIf not interactive, read commands but do not
266eccea571SRuslan Ermilovexecute them.
267eccea571SRuslan ErmilovThis is useful for checking the
2684b88c807SRodney W. Grimessyntax of shell scripts.
2697e1975c2STim J. Robbins.It Fl P Li physical
2707e1975c2STim J. RobbinsChange the default for the
2717e1975c2STim J. Robbins.Ic cd
2727e1975c2STim J. Robbinsand
2737e1975c2STim J. Robbins.Ic pwd
2747e1975c2STim J. Robbinscommands from
2757e1975c2STim J. Robbins.Fl L
2767e1975c2STim J. Robbins(logical directory layout)
2777e1975c2STim J. Robbinsto
2787e1975c2STim J. Robbins.Fl P
2797e1975c2STim J. Robbins(physical directory layout).
280ab72124fSSteve Price.It Fl p Li privileged
281eccea571SRuslan ErmilovTurn on privileged mode.
282eccea571SRuslan ErmilovThis mode is enabled on startup
283dfe302abSRuslan Ermilovif either the effective user or group ID is not equal to the
284dfe302abSRuslan Ermilovreal user or group ID.
285eccea571SRuslan ErmilovTurning this mode off sets the
286dfe302abSRuslan Ermiloveffective user and group IDs to the real user and group IDs.
28749e11e3aSSheldon HearnWhen this mode is enabled for interactive shells, the file
288ab72124fSSteve Price.Pa /etc/suid_profile
28949e11e3aSSheldon Hearnis sourced instead of
29049e11e3aSSheldon Hearn.Pa ~/.profile
291ab72124fSSteve Priceafter
292ab72124fSSteve Price.Pa /etc/profile
29349e11e3aSSheldon Hearnis sourced, and the contents of the
294ab72124fSSteve Price.Ev ENV
29549e11e3aSSheldon Hearnvariable are ignored.
296ab72124fSSteve Price.It Fl s Li stdin
2974b88c807SRodney W. GrimesRead commands from standard input (set automatically
298eccea571SRuslan Ermilovif no file arguments are present).
299eccea571SRuslan ErmilovThis option has
3004b88c807SRodney W. Grimesno effect when set after the shell has already started
301eccea571SRuslan Ermilovrunning (i.e., when set with the
302c644db6aSSheldon Hearn.Ic set
3036b15476eSSheldon Hearncommand).
304304199ceSTom Rhodes.It Fl T Li trapsasync
305e6d3cf26SSheldon HearnWhen waiting for a child, execute traps immediately.
306e6d3cf26SSheldon HearnIf this option is not set,
307e6d3cf26SSheldon Hearntraps are executed after the child exits,
308e6d3cf26SSheldon Hearnas specified in
309af5eb6fcSMaxim Konovalov.St -p1003.2 .
3106b15476eSSheldon HearnThis nonstandard option is useful for putting guarding shells around
311eccea571SRuslan Ermilovchildren that block signals.
312eccea571SRuslan ErmilovThe surrounding shell may kill the child
3136b15476eSSheldon Hearnor it may just return control to the tty and leave the child alone,
3146b15476eSSheldon Hearnlike this:
315c1c72a3cSMartin Cracauer.Bd -literal -offset indent
316c1c72a3cSMartin Cracauersh -T -c "trap 'exit 1' 2 ; some-blocking-program"
317c1c72a3cSMartin Cracauer.Ed
318ab72124fSSteve Price.It Fl u Li nounset
319ab72124fSSteve PriceWrite a message to standard error when attempting
32064254a66SJilles Tjoelkerto expand a variable, a positional parameter or
32164254a66SJilles Tjoelkerthe special parameter
32264254a66SJilles Tjoelker.Va \&!
32364254a66SJilles Tjoelkerthat is not set, and if the
324ab72124fSSteve Priceshell is not interactive, exit immediately.
325ab72124fSSteve Price.It Fl V Li vi
3268eed22b1SJens SchweikhardtEnable the built-in
327ab72124fSSteve Price.Xr vi 1
328ab72124fSSteve Pricecommand line editor (disables
329ab72124fSSteve Price.Fl E
330ab72124fSSteve Priceif it has been set).
331ab72124fSSteve Price.It Fl v Li verbose
332ab72124fSSteve PriceThe shell writes its input to standard error
333eccea571SRuslan Ermilovas it is read.
334eccea571SRuslan ErmilovUseful for debugging.
335ab72124fSSteve Price.It Fl x Li xtrace
33649e11e3aSSheldon HearnWrite each command
337120c8e6cSStefan Farfeleder(preceded by the value of the
338dfe302abSRuslan Ermilov.Va PS4
339292e6676SJilles Tjoelkervariable subjected to parameter expansion and arithmetic expansion)
34049e11e3aSSheldon Hearnto standard error before it is executed.
34149e11e3aSSheldon HearnUseful for debugging.
3426986f58fSJilles Tjoelker.It Li nolog
34317e98da8SMateusz PiotrowskiAnother do-nothing option for POSIX compliance.
34462c37116SJilles TjoelkerIt only has a long name.
345484160a9SJilles Tjoelker.It Li pipefail
346484160a9SJilles TjoelkerChange the exit status of a pipeline to the last non-zero exit status of
347484160a9SJilles Tjoelkerany command in the pipeline, if any.
348484160a9SJilles TjoelkerSince an exit due to
349484160a9SJilles Tjoelker.Dv SIGPIPE
350484160a9SJilles Tjoelkercounts as a non-zero exit status,
351484160a9SJilles Tjoelkerthis option may cause non-zero exit status for successful pipelines
352484160a9SJilles Tjoelkerif a command such as
353484160a9SJilles Tjoelker.Xr head 1
354484160a9SJilles Tjoelkerin the pipeline terminates with status 0 without reading its
355484160a9SJilles Tjoelkerinput completely.
356484160a9SJilles TjoelkerThis option only has a long name.
357d2c23317SStephane Rochoy.It Li verify
358d2c23317SStephane RochoySet
359d2c23317SStephane Rochoy.Dv O_VERIFY
360b131efe3SGordon Berglingwhen sourcing files or loading profiles.
361825225e5SJens Schweikhardt.\" See also
362825225e5SJens Schweikhardt.\" .Xr mac_veriexec 4 . TODO Does not exist; write it.
36349e11e3aSSheldon Hearn.El
36449e11e3aSSheldon Hearn.Pp
36549e11e3aSSheldon HearnThe
366fd50df30SRuslan Ermilov.Fl c
367fd50df30SRuslan Ermilovoption causes the commands to be read from the
368fd50df30SRuslan Ermilov.Ar string
369fd50df30SRuslan Ermilovoperand instead of from the standard input.
370fd50df30SRuslan ErmilovKeep in mind that this option only accepts a single string as its
371fd50df30SRuslan Ermilovargument, hence multi-word strings must be quoted.
372fd50df30SRuslan Ermilov.Pp
373fd50df30SRuslan ErmilovThe
37449e11e3aSSheldon Hearn.Fl /+o
37549e11e3aSSheldon Hearnoption takes as its only argument the long name of an option
37649e11e3aSSheldon Hearnto be enabled or disabled.
37749e11e3aSSheldon HearnFor example, the following two invocations of
37849e11e3aSSheldon Hearn.Nm
3798eed22b1SJens Schweikhardtboth enable the built-in
380825225e5SJens Schweikhardt.Xr emacs 1 Pq Pa ports/editors/emacs
38149e11e3aSSheldon Hearncommand line editor:
38249e11e3aSSheldon Hearn.Bd -literal -offset indent
38349e11e3aSSheldon Hearnset -E
38449e11e3aSSheldon Hearnset -o emacs
38549e11e3aSSheldon Hearn.Ed
386d513af6aSTim J. Robbins.Pp
387d513af6aSTim J. RobbinsIf used without an argument, the
388d513af6aSTim J. Robbins.Fl o
389d513af6aSTim J. Robbinsoption displays the current option settings in a human-readable format.
390d513af6aSTim J. RobbinsIf
391d513af6aSTim J. Robbins.Cm +o
392d513af6aSTim J. Robbinsis used without an argument, the current option settings are output
393d513af6aSTim J. Robbinsin a format suitable for re-input into the shell.
394ab72124fSSteve Price.Ss Lexical Structure
3954b88c807SRodney W. GrimesThe shell reads input in terms of lines from a file and breaks
3964b88c807SRodney W. Grimesit up into words at whitespace (blanks and tabs), and at
3974b88c807SRodney W. Grimescertain sequences of
3986b15476eSSheldon Hearncharacters called
3996b15476eSSheldon Hearn.Dq operators ,
4006b15476eSSheldon Hearnwhich are special to the shell.
4014b88c807SRodney W. GrimesThere are two types of operators: control operators and
4024b88c807SRodney W. Grimesredirection operators (their meaning is discussed later).
403ab72124fSSteve PriceThe following is a list of valid operators:
40481e9cda2SRuslan Ermilov.Bl -tag -width indent
40549e11e3aSSheldon Hearn.It Control operators:
40649e11e3aSSheldon Hearn.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
40735471bf8SJoel Dahl.It Li & Ta Li && Ta Li \&( Ta Li \&) Ta Li \en
40835471bf8SJoel Dahl.It Li ;; Ta Li ;& Ta Li \&; Ta Li \&| Ta Li ||
40949e11e3aSSheldon Hearn.El
41049e11e3aSSheldon Hearn.It Redirection operators:
4114682f420SBrian Somers.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
41281e9cda2SRuslan Ermilov.It Li < Ta Li > Ta Li << Ta Li >> Ta Li <>
413748611c9SJoel Dahl.It Li <& Ta Li >& Ta Li <<- Ta Li >| Ta \&
41449e11e3aSSheldon Hearn.El
415ab72124fSSteve Price.El
4167d1a55fcSStefan Farfeleder.Pp
4177d1a55fcSStefan FarfelederThe character
4187d1a55fcSStefan Farfeleder.Ql #
4197d1a55fcSStefan Farfelederintroduces a comment if used at the beginning of a word.
4207d1a55fcSStefan FarfelederThe word starting with
4217d1a55fcSStefan Farfeleder.Ql #
4227d1a55fcSStefan Farfelederand the rest of the line are ignored.
4239764aa41SJilles Tjoelker.Pp
42417e98da8SMateusz PiotrowskiASCII
4259764aa41SJilles Tjoelker.Dv NUL
4269764aa41SJilles Tjoelkercharacters (character code 0) are not allowed in shell input.
427ab72124fSSteve Price.Ss Quoting
4284b88c807SRodney W. GrimesQuoting is used to remove the special meaning of certain characters
429a886288eSJesus R. Camouor words to the shell, such as operators, whitespace, keywords,
430a886288eSJesus R. Camouor alias names.
431a886288eSJesus R. Camou.Pp
432a62ab027SJilles TjoelkerThere are four types of quoting: matched single quotes,
433a62ab027SJilles Tjoelkerdollar-single quotes,
4344b88c807SRodney W. Grimesmatched double quotes, and backslash.
43581e9cda2SRuslan Ermilov.Bl -tag -width indent
436ab72124fSSteve Price.It Single Quotes
4374b88c807SRodney W. GrimesEnclosing characters in single quotes preserves the literal
438ab0a2172SSteve Pricemeaning of all the characters (except single quotes, making
439ab0a2172SSteve Priceit impossible to put single-quotes in a single-quoted string).
440a62ab027SJilles Tjoelker.It Dollar-Single Quotes
441a62ab027SJilles TjoelkerEnclosing characters between
442a62ab027SJilles Tjoelker.Li $'
443a62ab027SJilles Tjoelkerand
444a62ab027SJilles Tjoelker.Li '
445a62ab027SJilles Tjoelkerpreserves the literal meaning of all characters
446a62ab027SJilles Tjoelkerexcept backslashes and single quotes.
447a62ab027SJilles TjoelkerA backslash introduces a C-style escape sequence:
448a62ab027SJilles Tjoelker.Bl -tag -width xUnnnnnnnn
449a62ab027SJilles Tjoelker.It \ea
450a62ab027SJilles TjoelkerAlert (ring the terminal bell)
451a62ab027SJilles Tjoelker.It \eb
452a62ab027SJilles TjoelkerBackspace
453a62ab027SJilles Tjoelker.It \ec Ns Ar c
454a62ab027SJilles TjoelkerThe control character denoted by
455a62ab027SJilles Tjoelker.Li ^ Ns Ar c
456a62ab027SJilles Tjoelkerin
457a62ab027SJilles Tjoelker.Xr stty 1 .
458a62ab027SJilles TjoelkerIf
459a62ab027SJilles Tjoelker.Ar c
460a62ab027SJilles Tjoelkeris a backslash, it must be doubled.
461a62ab027SJilles Tjoelker.It \ee
46217e98da8SMateusz PiotrowskiThe ESC character (ASCII 0x1b)
463a62ab027SJilles Tjoelker.It \ef
464a62ab027SJilles TjoelkerFormfeed
465a62ab027SJilles Tjoelker.It \en
466a62ab027SJilles TjoelkerNewline
467a62ab027SJilles Tjoelker.It \er
468a62ab027SJilles TjoelkerCarriage return
469a62ab027SJilles Tjoelker.It \et
470a62ab027SJilles TjoelkerHorizontal tab
471a62ab027SJilles Tjoelker.It \ev
472a62ab027SJilles TjoelkerVertical tab
473a62ab027SJilles Tjoelker.It \e\e
474a62ab027SJilles TjoelkerLiteral backslash
475a62ab027SJilles Tjoelker.It \e\&'
476a62ab027SJilles TjoelkerLiteral single-quote
477a62ab027SJilles Tjoelker.It \e\&"
478a62ab027SJilles TjoelkerLiteral double-quote
479a62ab027SJilles Tjoelker.It \e Ns Ar nnn
480a62ab027SJilles TjoelkerThe byte whose octal value is
481a62ab027SJilles Tjoelker.Ar nnn
482a62ab027SJilles Tjoelker(one to three digits)
483a62ab027SJilles Tjoelker.It \ex Ns Ar nn
484a62ab027SJilles TjoelkerThe byte whose hexadecimal value is
485a62ab027SJilles Tjoelker.Ar nn
486a62ab027SJilles Tjoelker(one or more digits only the last two of which are used)
487a62ab027SJilles Tjoelker.It \eu Ns Ar nnnn
488a62ab027SJilles TjoelkerThe Unicode code point
489a62ab027SJilles Tjoelker.Ar nnnn
490a62ab027SJilles Tjoelker(four hexadecimal digits)
491a62ab027SJilles Tjoelker.It \eU Ns Ar nnnnnnnn
492a62ab027SJilles TjoelkerThe Unicode code point
493a62ab027SJilles Tjoelker.Ar nnnnnnnn
494a62ab027SJilles Tjoelker(eight hexadecimal digits)
495a62ab027SJilles Tjoelker.El
496a62ab027SJilles Tjoelker.Pp
49707eb7033SJilles TjoelkerThe sequences for Unicode code points are currently only useful with
49807eb7033SJilles TjoelkerUTF-8 locales.
499a62ab027SJilles TjoelkerThey reject code point 0 and UTF-16 surrogates.
500a62ab027SJilles Tjoelker.Pp
501a62ab027SJilles TjoelkerIf an escape sequence would produce a byte with value 0,
502a62ab027SJilles Tjoelkerthat byte and the rest of the string until the matching single-quote
503a62ab027SJilles Tjoelkerare ignored.
504a62ab027SJilles Tjoelker.Pp
505a62ab027SJilles TjoelkerAny other string starting with a backslash is an error.
506ab72124fSSteve Price.It Double Quotes
5074b88c807SRodney W. GrimesEnclosing characters within double quotes preserves the literal
50849e11e3aSSheldon Hearnmeaning of all characters except dollar sign
509a762afe5SGiorgos Keramidas.Pq Ql $ ,
51049e11e3aSSheldon Hearnbackquote
511a762afe5SGiorgos Keramidas.Pq Ql ` ,
51249e11e3aSSheldon Hearnand backslash
513a762afe5SGiorgos Keramidas.Pq Ql \e .
51449e11e3aSSheldon HearnThe backslash inside double quotes is historically weird.
51549e11e3aSSheldon HearnIt remains literal unless it precedes the following characters,
51649e11e3aSSheldon Hearnwhich it serves to quote:
517afc5a69aSJoel Dahl.Pp
51849e11e3aSSheldon Hearn.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
519afc5a69aSJoel Dahl.It Li $ Ta Li ` Ta Li \&" Ta Li \e Ta Li \en
52049e11e3aSSheldon Hearn.El
521ab72124fSSteve Price.It Backslash
522ab72124fSSteve PriceA backslash preserves the literal meaning of the following
52349e11e3aSSheldon Hearncharacter, with the exception of the newline character
524a762afe5SGiorgos Keramidas.Pq Ql \en .
52549e11e3aSSheldon HearnA backslash preceding a newline is treated as a line continuation.
526ab72124fSSteve Price.El
527d806a030SJilles Tjoelker.Ss Keywords
528d806a030SJilles TjoelkerKeywords or reserved words are words that have special meaning to the
5294b88c807SRodney W. Grimesshell and are recognized at the beginning of a line and
530eccea571SRuslan Ermilovafter a control operator.
531d806a030SJilles TjoelkerThe following are keywords:
53249e11e3aSSheldon Hearn.Bl -column "doneXX" "elifXX" "elseXX" "untilXX" "whileX" -offset center
53381e9cda2SRuslan Ermilov.It Li \&! Ta { Ta } Ta Ic case Ta Ic do
53481e9cda2SRuslan Ermilov.It Ic done Ta Ic elif Ta Ic else Ta Ic esac Ta Ic fi
53581e9cda2SRuslan Ermilov.It Ic for Ta Ic if Ta Ic then Ta Ic until Ta Ic while
53649e11e3aSSheldon Hearn.El
537ab72124fSSteve Price.Ss Aliases
538ab72124fSSteve PriceAn alias is a name and corresponding value set using the
539c644db6aSSheldon Hearn.Ic alias
540eccea571SRuslan Ermilovbuilt-in command.
54105a447d0SJilles TjoelkerWherever the command word of a simple command may occur,
54205a447d0SJilles Tjoelkerand after checking for keywords if a keyword may occur, the shell
543e6d3cf26SSheldon Hearnchecks the word to see if it matches an alias.
544e6d3cf26SSheldon HearnIf it does, it replaces it in the input stream with its value.
545e6d3cf26SSheldon HearnFor example, if there is an alias called
54649e11e3aSSheldon Hearn.Dq Li lf
5476b15476eSSheldon Hearnwith the value
548dfe302abSRuslan Ermilov.Dq Li "ls -F" ,
5494b88c807SRodney W. Grimesthen the input
550dfe302abSRuslan Ermilov.Pp
551dfe302abSRuslan Ermilov.Dl "lf foobar"
552ab72124fSSteve Price.Pp
5534b88c807SRodney W. Grimeswould become
554dfe302abSRuslan Ermilov.Pp
555dfe302abSRuslan Ermilov.Dl "ls -F foobar"
556ab72124fSSteve Price.Pp
55748f49aacSJilles TjoelkerAliases are also recognized after an alias
55848f49aacSJilles Tjoelkerwhose value ends with a space or tab.
55948f49aacSJilles TjoelkerFor example, if there is also an alias called
56048f49aacSJilles Tjoelker.Dq Li nohup
56148f49aacSJilles Tjoelkerwith the value
56248f49aacSJilles Tjoelker.Dq Li "nohup " ,
56348f49aacSJilles Tjoelkerthen the input
56448f49aacSJilles Tjoelker.Pp
56548f49aacSJilles Tjoelker.Dl "nohup lf foobar"
56648f49aacSJilles Tjoelker.Pp
56748f49aacSJilles Tjoelkerwould become
56848f49aacSJilles Tjoelker.Pp
56948f49aacSJilles Tjoelker.Dl "nohup ls -F foobar"
57048f49aacSJilles Tjoelker.Pp
5714b88c807SRodney W. GrimesAliases provide a convenient way for naive users to
5724b88c807SRodney W. Grimescreate shorthands for commands without having to learn how
573eccea571SRuslan Ermilovto create functions with arguments.
57412dfb7a5SJilles TjoelkerUsing aliases in scripts is discouraged
57512dfb7a5SJilles Tjoelkerbecause the command that defines them must be executed
57612dfb7a5SJilles Tjoelkerbefore the code that uses them is parsed.
57712dfb7a5SJilles TjoelkerThis is fragile and not portable.
578a886288eSJesus R. Camou.Pp
579a886288eSJesus R. CamouAn alias name may be escaped in a command line, so that it is not
580a886288eSJesus R. Camoureplaced by its alias value, by using quoting characters within or
58119162ab9SJesus R. Camouadjacent to the alias name.
58219162ab9SJesus R. CamouThis is most often done by prefixing
583a886288eSJesus R. Camouan alias name with a backslash to execute a function, built-in, or
584a886288eSJesus R. Camounormal program with the same name.
585a886288eSJesus R. CamouSee the
586a886288eSJesus R. Camou.Sx Quoting
587a886288eSJesus R. Camousubsection.
588ab72124fSSteve Price.Ss Commands
5894b88c807SRodney W. GrimesThe shell interprets the words it reads according to a
5904b88c807SRodney W. Grimeslanguage, the specification of which is outside the scope
591ab72124fSSteve Priceof this man page (refer to the BNF in the
592ab72124fSSteve Price.St -p1003.2
593eccea571SRuslan Ermilovdocument).
594eccea571SRuslan ErmilovEssentially though, a line is read and if
5954b88c807SRodney W. Grimesthe first word of the line (or after a control operator)
596d806a030SJilles Tjoelkeris not a keyword, then the shell has recognized a
597eccea571SRuslan Ermilovsimple command.
598eccea571SRuslan ErmilovOtherwise, a complex command or some
5994b88c807SRodney W. Grimesother special construct may have been recognized.
600ab72124fSSteve Price.Ss Simple Commands
6014b88c807SRodney W. GrimesIf a simple command has been recognized, the shell performs
6024b88c807SRodney W. Grimesthe following actions:
603ab72124fSSteve Price.Bl -enum
604ab72124fSSteve Price.It
6056b15476eSSheldon HearnLeading words of the form
60649e11e3aSSheldon Hearn.Dq Li name=value
6076b15476eSSheldon Hearnare stripped off and assigned to the environment of
608000dda7bSJilles Tjoelkerthe simple command
609000dda7bSJilles Tjoelker(they do not affect expansions).
610eccea571SRuslan ErmilovRedirection operators and
6114b88c807SRodney W. Grimestheir arguments (as described below) are stripped
6124b88c807SRodney W. Grimesoff and saved for processing.
613ab72124fSSteve Price.It
614ab72124fSSteve PriceThe remaining words are expanded as described in
6156b15476eSSheldon Hearnthe section called
6166b15476eSSheldon Hearn.Sx Word Expansions ,
6176b15476eSSheldon Hearnand the first remaining word is considered the command
618eccea571SRuslan Ermilovname and the command is located.
619eccea571SRuslan ErmilovThe remaining
6204b88c807SRodney W. Grimeswords are considered the arguments of the command.
6216b15476eSSheldon HearnIf no command name resulted, then the
62249e11e3aSSheldon Hearn.Dq Li name=value
6234b88c807SRodney W. Grimesvariable assignments recognized in 1) affect the
6244b88c807SRodney W. Grimescurrent shell.
625ab72124fSSteve Price.It
626ab72124fSSteve PriceRedirections are performed as described in
6274b88c807SRodney W. Grimesthe next section.
628ab72124fSSteve Price.El
629ab72124fSSteve Price.Ss Redirections
6304b88c807SRodney W. GrimesRedirections are used to change where a command reads its input
631eccea571SRuslan Ermilovor sends its output.
632eccea571SRuslan ErmilovIn general, redirections open, close, or
633eccea571SRuslan Ermilovduplicate an existing reference to a file.
634eccea571SRuslan ErmilovThe overall format
6354b88c807SRodney W. Grimesused for redirection is:
63649e11e3aSSheldon Hearn.Pp
637dfe302abSRuslan Ermilov.D1 Oo Ar n Oc Ar redir-op file
63849e11e3aSSheldon Hearn.Pp
63949e11e3aSSheldon HearnThe
640dfe302abSRuslan Ermilov.Ar redir-op
64149e11e3aSSheldon Hearnis one of the redirection operators mentioned
642eccea571SRuslan Ermilovpreviously.
643eccea571SRuslan ErmilovThe following gives some examples of how these
64449e11e3aSSheldon Hearnoperators can be used.
64549e11e3aSSheldon HearnNote that stdin and stdout are commonly used abbreviations
64649e11e3aSSheldon Hearnfor standard input and standard output respectively.
64749e11e3aSSheldon Hearn.Bl -tag -width "1234567890XX" -offset indent
648dfe302abSRuslan Ermilov.It Oo Ar n Oc Ns Li > Ar file
649dfe302abSRuslan Ermilovredirect stdout (or file descriptor
650dfe302abSRuslan Ermilov.Ar n )
651dfe302abSRuslan Ermilovto
652dfe302abSRuslan Ermilov.Ar file
653dfe302abSRuslan Ermilov.It Oo Ar n Oc Ns Li >| Ar file
65449e11e3aSSheldon Hearnsame as above, but override the
65549e11e3aSSheldon Hearn.Fl C
65649e11e3aSSheldon Hearnoption
657dfe302abSRuslan Ermilov.It Oo Ar n Oc Ns Li >> Ar file
658dfe302abSRuslan Ermilovappend stdout (or file descriptor
659dfe302abSRuslan Ermilov.Ar n )
660dfe302abSRuslan Ermilovto
661dfe302abSRuslan Ermilov.Ar file
662dfe302abSRuslan Ermilov.It Oo Ar n Oc Ns Li < Ar file
663dfe302abSRuslan Ermilovredirect stdin (or file descriptor
664dfe302abSRuslan Ermilov.Ar n )
665dfe302abSRuslan Ermilovfrom
666dfe302abSRuslan Ermilov.Ar file
667dfe302abSRuslan Ermilov.It Oo Ar n Oc Ns Li <> Ar file
668dfe302abSRuslan Ermilovredirect stdin (or file descriptor
669dfe302abSRuslan Ermilov.Ar n )
670dfe302abSRuslan Ermilovto and from
671dfe302abSRuslan Ermilov.Ar file
672dfe302abSRuslan Ermilov.It Oo Ar n1 Oc Ns Li <& Ns Ar n2
673dfe302abSRuslan Ermilovduplicate stdin (or file descriptor
674dfe302abSRuslan Ermilov.Ar n1 )
675dfe302abSRuslan Ermilovfrom file descriptor
676dfe302abSRuslan Ermilov.Ar n2
677dfe302abSRuslan Ermilov.It Oo Ar n Oc Ns Li <&-
678dfe302abSRuslan Ermilovclose stdin (or file descriptor
679dfe302abSRuslan Ermilov.Ar n )
680dfe302abSRuslan Ermilov.It Oo Ar n1 Oc Ns Li >& Ns Ar n2
681dfe302abSRuslan Ermilovduplicate stdout (or file descriptor
682dfe302abSRuslan Ermilov.Ar n1 )
683dfe302abSRuslan Ermilovto file descriptor
684dfe302abSRuslan Ermilov.Ar n2
685dfe302abSRuslan Ermilov.It Oo Ar n Oc Ns Li >&-
686dfe302abSRuslan Ermilovclose stdout (or file descriptor
687dfe302abSRuslan Ermilov.Ar n )
688ab72124fSSteve Price.El
689ab72124fSSteve Price.Pp
6906b15476eSSheldon HearnThe following redirection is often called a
6916b15476eSSheldon Hearn.Dq here-document .
692dfe302abSRuslan Ermilov.Bd -unfilled -offset indent
693dfe302abSRuslan Ermilov.Oo Ar n Oc Ns Li << Ar delimiter
694a2442c2fSJoel Dahl.Ar here-doc-text
695a2442c2fSJoel Dahl.Ar ...
696dfe302abSRuslan Ermilov.Ar delimiter
697ab72124fSSteve Price.Ed
698ab72124fSSteve Price.Pp
6994b88c807SRodney W. GrimesAll the text on successive lines up to the delimiter is
7004b88c807SRodney W. Grimessaved away and made available to the command on standard
701dfe302abSRuslan Ermilovinput, or file descriptor
702dfe302abSRuslan Ermilov.Ar n
703dfe302abSRuslan Ermilovif it is specified.
704dfe302abSRuslan ErmilovIf the
705dfe302abSRuslan Ermilov.Ar delimiter
706dfe302abSRuslan Ermilovas specified on the initial line is quoted, then the
707dfe302abSRuslan Ermilov.Ar here-doc-text
7084b88c807SRodney W. Grimesis treated literally, otherwise the text is subjected to
7094b88c807SRodney W. Grimesparameter expansion, command substitution, and arithmetic
7106b15476eSSheldon Hearnexpansion (as described in the section on
7116b15476eSSheldon Hearn.Sx Word Expansions ) .
7126b15476eSSheldon HearnIf the operator is
71349e11e3aSSheldon Hearn.Dq Li <<-
7146b15476eSSheldon Hearninstead of
71549e11e3aSSheldon Hearn.Dq Li << ,
7166b15476eSSheldon Hearnthen leading tabs
717dfe302abSRuslan Ermilovin the
718dfe302abSRuslan Ermilov.Ar here-doc-text
719dfe302abSRuslan Ermilovare stripped.
720ab72124fSSteve Price.Ss Search and Execution
721ab0a2172SSteve PriceThere are three types of commands: shell functions,
7228eed22b1SJens Schweikhardtbuilt-in commands, and normal programs.
72349e11e3aSSheldon HearnThe command is searched for (by name) in that order.
72449e11e3aSSheldon HearnThe three types of commands are all executed in a different way.
725ab72124fSSteve Price.Pp
726ab0a2172SSteve PriceWhen a shell function is executed, all of the shell positional
727dfe302abSRuslan Ermilovparameters (except
728dfe302abSRuslan Ermilov.Li $0 ,
729dfe302abSRuslan Ermilovwhich remains unchanged) are
7304b88c807SRodney W. Grimesset to the arguments of the shell function.
7314b88c807SRodney W. GrimesThe variables which are explicitly placed in the environment of
7324b88c807SRodney W. Grimesthe command (by placing assignments to them before the
7334b88c807SRodney W. Grimesfunction name) are made local to the function and are set
734e6d3cf26SSheldon Hearnto the values given.
735e6d3cf26SSheldon HearnThen the command given in the function definition is executed.
736e6d3cf26SSheldon HearnThe positional parameters are restored to their original values
737e6d3cf26SSheldon Hearnwhen the command completes.
738ab0a2172SSteve PriceThis all occurs within the current shell.
739ab72124fSSteve Price.Pp
7408eed22b1SJens SchweikhardtShell built-in commands are executed internally to the shell, without
741ab0a2172SSteve Pricespawning a new process.
74230268dfaSJilles TjoelkerThere are two kinds of built-in commands: regular and special.
74330268dfaSJilles TjoelkerAssignments before special builtins persist after they finish
74430268dfaSJilles Tjoelkerexecuting and assignment errors, redirection errors and certain
74530268dfaSJilles Tjoelkeroperand errors cause a script to be aborted.
7464b985a89SJilles TjoelkerSpecial builtins cannot be overridden with a function.
74730268dfaSJilles TjoelkerBoth regular and special builtins can affect the shell in ways
74830268dfaSJilles Tjoelkernormal programs cannot.
749ab72124fSSteve Price.Pp
750c644db6aSSheldon HearnOtherwise, if the command name does not match a function
7518eed22b1SJens Schweikhardtor built-in command, the command is searched for as a normal
7524b88c807SRodney W. Grimesprogram in the file system (as described in the next section).
7534b88c807SRodney W. GrimesWhen a normal program is executed, the shell runs the program,
754e6d3cf26SSheldon Hearnpassing the arguments and the environment to the program.
755e6d3cf26SSheldon HearnIf the program is not a normal executable file
756eccea571SRuslan Ermilov(i.e., if it does not begin with the
757dfe302abSRuslan Ermilov.Dq "magic number"
75817e98da8SMateusz Piotrowskiwhose ASCII representation is
759dfe302abSRuslan Ermilov.Dq Li #! ,
76049e11e3aSSheldon Hearnresulting in an
761b4f0f4aaSRuslan Ermilov.Er ENOEXEC
76249e11e3aSSheldon Hearnreturn value from
76349e11e3aSSheldon Hearn.Xr execve 2 )
764604e8224SJilles Tjoelkerbut appears to be a text file,
7653835f47cSJilles Tjoelkerthe shell will run a new instance of
7663835f47cSJilles Tjoelker.Nm
7673835f47cSJilles Tjoelkerto interpret it.
768ab72124fSSteve Price.Pp
769ab0a2172SSteve PriceNote that previous versions of this document
770ab0a2172SSteve Priceand the source code itself misleadingly and sporadically
771ab0a2172SSteve Pricerefer to a shell script without a magic number
7726b15476eSSheldon Hearnas a
773dfe302abSRuslan Ermilov.Dq "shell procedure" .
774ab72124fSSteve Price.Ss Path Search
7754b88c807SRodney W. GrimesWhen locating a command, the shell first looks to see if
776eccea571SRuslan Ermilovit has a shell function by that name.
777eccea571SRuslan ErmilovThen it looks for a
778eccea571SRuslan Ermilovbuilt-in command by that name.
779eccea571SRuslan ErmilovIf a built-in command is not found,
780ab0a2172SSteve Priceone of two things happen:
781ab72124fSSteve Price.Bl -enum
782ab72124fSSteve Price.It
783ab72124fSSteve PriceCommand names containing a slash are simply executed without
784ab0a2172SSteve Priceperforming any searches.
785ab72124fSSteve Price.It
786dfe302abSRuslan ErmilovThe shell searches each entry in the
7871788b7ffSJilles Tjoelker.Va PATH
7881788b7ffSJilles Tjoelkervariable
789eccea571SRuslan Ermilovin turn for the command.
790eccea571SRuslan ErmilovThe value of the
7911788b7ffSJilles Tjoelker.Va PATH
792ab72124fSSteve Pricevariable should be a series of
793eccea571SRuslan Ermiloventries separated by colons.
794eccea571SRuslan ErmilovEach entry consists of a
7954b88c807SRodney W. Grimesdirectory name.
7964b88c807SRodney W. GrimesThe current directory
797ab0a2172SSteve Pricemay be indicated implicitly by an empty directory name,
798ab0a2172SSteve Priceor explicitly by a single period.
799ab72124fSSteve Price.El
800ab72124fSSteve Price.Ss Command Exit Status
8014b88c807SRodney W. GrimesEach command has an exit status that can influence the behavior
802eccea571SRuslan Ermilovof other shell commands.
803eccea571SRuslan ErmilovThe paradigm is that a command exits
8044b88c807SRodney W. Grimeswith zero for normal or success, and non-zero for failure,
805eccea571SRuslan Ermiloverror, or a false indication.
806eccea571SRuslan ErmilovThe man page for each command
8074b88c807SRodney W. Grimesshould indicate the various exit codes and what they mean.
8088eed22b1SJens SchweikhardtAdditionally, the built-in commands return exit codes, as does
809ab0a2172SSteve Pricean executed shell function.
810c351e089SJohn Polstra.Pp
811b1f60592SJilles TjoelkerIf a command is terminated by a signal, its exit status is greater than 128.
812b1f60592SJilles TjoelkerThe signal name can be found by passing the exit status to
813b1f60592SJilles Tjoelker.Li kill -l .
814b1f60592SJilles Tjoelker.Pp
815b1f60592SJilles TjoelkerIf there is no command word,
816b1f60592SJilles Tjoelkerthe exit status is the exit status of the last command substitution executed,
817b1f60592SJilles Tjoelkeror zero if the command does not contain any command substitutions.
818ab72124fSSteve Price.Ss Complex Commands
8194b88c807SRodney W. GrimesComplex commands are combinations of simple commands
820d806a030SJilles Tjoelkerwith control operators or keywords, together creating a larger complex
821eccea571SRuslan Ermilovcommand.
822eccea571SRuslan ErmilovMore generally, a command is one of the following:
823ab72124fSSteve Price.Bl -item -offset indent
824ab72124fSSteve Price.It
825ab72124fSSteve Pricesimple command
826ab72124fSSteve Price.It
827ab72124fSSteve Pricepipeline
828ab72124fSSteve Price.It
829ab72124fSSteve Pricelist or compound-list
830ab72124fSSteve Price.It
831ab72124fSSteve Pricecompound command
832ab72124fSSteve Price.It
833ab72124fSSteve Pricefunction definition
834ab72124fSSteve Price.El
835ab72124fSSteve Price.Pp
8364b88c807SRodney W. GrimesUnless otherwise stated, the exit status of a command is
837b1f60592SJilles Tjoelkerthat of the last simple command executed by the command,
838b1f60592SJilles Tjoelkeror zero if no simple command was executed.
839ab72124fSSteve Price.Ss Pipelines
8404b88c807SRodney W. GrimesA pipeline is a sequence of one or more commands separated
841dfe302abSRuslan Ermilovby the control operator
842dfe302abSRuslan Ermilov.Ql \&| .
843eccea571SRuslan ErmilovThe standard output of all but
8444b88c807SRodney W. Grimesthe last command is connected to the standard input
845eccea571SRuslan Ermilovof the next command.
846eccea571SRuslan ErmilovThe standard output of the last
847ab0a2172SSteve Pricecommand is inherited from the shell, as usual.
848ab72124fSSteve Price.Pp
8494b88c807SRodney W. GrimesThe format for a pipeline is:
85049e11e3aSSheldon Hearn.Pp
851dfe302abSRuslan Ermilov.D1 Oo Li \&! Oc Ar command1 Op Li \&| Ar command2 ...
852ab72124fSSteve Price.Pp
853dfe302abSRuslan ErmilovThe standard output of
854dfe302abSRuslan Ermilov.Ar command1
855dfe302abSRuslan Ermilovis connected to the standard input of
856dfe302abSRuslan Ermilov.Ar command2 .
857eccea571SRuslan ErmilovThe standard input, standard output, or
8584b88c807SRodney W. Grimesboth of a command is considered to be assigned by the
8594b88c807SRodney W. Grimespipeline before any redirection specified by redirection
8604b88c807SRodney W. Grimesoperators that are part of the command.
861ab72124fSSteve Price.Pp
862f1ec0581SJilles TjoelkerNote that unlike some other shells,
863f1ec0581SJilles Tjoelker.Nm
864f1ec0581SJilles Tjoelkerexecutes each process in a pipeline with more than one command
865f1ec0581SJilles Tjoelkerin a subshell environment and as a child of the
866f1ec0581SJilles Tjoelker.Nm
867f1ec0581SJilles Tjoelkerprocess.
868f1ec0581SJilles Tjoelker.Pp
8694b88c807SRodney W. GrimesIf the pipeline is not in the background (discussed later),
8704b88c807SRodney W. Grimesthe shell waits for all commands to complete.
871ab72124fSSteve Price.Pp
872d806a030SJilles TjoelkerIf the keyword
873eccea571SRuslan Ermilov.Ic !\&
874eccea571SRuslan Ermilovdoes not precede the pipeline, the
8754b88c807SRodney W. Grimesexit status is the exit status of the last command specified
876484160a9SJilles Tjoelkerin the pipeline if the
877484160a9SJilles Tjoelker.Cm pipefail
878484160a9SJilles Tjoelkeroption is not set or all commands returned zero,
879484160a9SJilles Tjoelkeror the last non-zero exit status of any command in the pipeline otherwise.
880eccea571SRuslan ErmilovOtherwise, the exit status is the logical
881484160a9SJilles TjoelkerNOT of that exit status.
882eccea571SRuslan ErmilovThat is, if
883484160a9SJilles Tjoelkerthat status is zero, the exit status is 1; if
884484160a9SJilles Tjoelkerthat status is greater than zero, the exit status
8854b88c807SRodney W. Grimesis zero.
886ab72124fSSteve Price.Pp
8874b88c807SRodney W. GrimesBecause pipeline assignment of standard input or standard
8884b88c807SRodney W. Grimesoutput or both takes place before redirection, it can be
889eccea571SRuslan Ermilovmodified by redirection.
890eccea571SRuslan ErmilovFor example:
891ab72124fSSteve Price.Pp
892dfe302abSRuslan Ermilov.Dl "command1 2>&1 | command2"
893ab72124fSSteve Price.Pp
89449e11e3aSSheldon Hearnsends both the standard output and standard error of
895dfe302abSRuslan Ermilov.Ar command1
89649e11e3aSSheldon Hearnto the standard input of
897dfe302abSRuslan Ermilov.Ar command2 .
89849e11e3aSSheldon Hearn.Pp
89949e11e3aSSheldon HearnA
900dfe302abSRuslan Ermilov.Ql \&;
90149e11e3aSSheldon Hearnor newline terminator causes the preceding
902e918fc8fSSheldon HearnAND-OR-list
903e918fc8fSSheldon Hearn(described below in the section called
904e918fc8fSSheldon Hearn.Sx Short-Circuit List Operators )
905e918fc8fSSheldon Hearnto be executed sequentially;
906e918fc8fSSheldon Hearnan
907dfe302abSRuslan Ermilov.Ql &
908e918fc8fSSheldon Hearncauses asynchronous execution of the preceding AND-OR-list.
90949e11e3aSSheldon Hearn.Ss Background Commands (&)
9104b88c807SRodney W. GrimesIf a command is terminated by the control operator ampersand
911a762afe5SGiorgos Keramidas.Pq Ql & ,
91251a514adSJilles Tjoelkerthe shell executes the command in a subshell environment (see
91351a514adSJilles Tjoelker.Sx Grouping Commands Together
91451a514adSJilles Tjoelkerbelow) and asynchronously;
91549e11e3aSSheldon Hearnthe shell does not wait for the command to finish
91649e11e3aSSheldon Hearnbefore executing the next command.
917ab72124fSSteve Price.Pp
9184b88c807SRodney W. GrimesThe format for running a command in background is:
919dfe302abSRuslan Ermilov.Pp
920dfe302abSRuslan Ermilov.D1 Ar command1 Li & Op Ar command2 Li & Ar ...
921ab72124fSSteve Price.Pp
9224b88c807SRodney W. GrimesIf the shell is not interactive, the standard input of an
923dfe302abSRuslan Ermilovasynchronous command is set to
924dfe302abSRuslan Ermilov.Pa /dev/null .
925b1f60592SJilles Tjoelker.Pp
926b1f60592SJilles TjoelkerThe exit status is zero.
92749e11e3aSSheldon Hearn.Ss Lists (Generally Speaking)
9284b88c807SRodney W. GrimesA list is a sequence of zero or more commands separated by
9294b88c807SRodney W. Grimesnewlines, semicolons, or ampersands,
9304b88c807SRodney W. Grimesand optionally terminated by one of these three characters.
9314b88c807SRodney W. GrimesThe commands in a
9324b88c807SRodney W. Grimeslist are executed in the order they are written.
9334b88c807SRodney W. GrimesIf command is followed by an ampersand, the shell starts the
9348eed22b1SJens Schweikhardtcommand and immediately proceeds onto the next command;
9354b88c807SRodney W. Grimesotherwise it waits for the command to terminate before
9364b88c807SRodney W. Grimesproceeding to the next one.
937ab72124fSSteve Price.Ss Short-Circuit List Operators
93849e11e3aSSheldon Hearn.Dq Li &&
9396b15476eSSheldon Hearnand
94049e11e3aSSheldon Hearn.Dq Li ||
9416b15476eSSheldon Hearnare AND-OR list operators.
94249e11e3aSSheldon Hearn.Dq Li &&
9436b15476eSSheldon Hearnexecutes the first command, and then executes the second command
9446b15476eSSheldon Hearnif the exit status of the first command is zero.
94549e11e3aSSheldon Hearn.Dq Li ||
946120ac26dSMasafumi Max NAKANEis similar, but executes the second command if the exit
9476b15476eSSheldon Hearnstatus of the first command is nonzero.
94849e11e3aSSheldon Hearn.Dq Li &&
9496b15476eSSheldon Hearnand
95049e11e3aSSheldon Hearn.Dq Li ||
9514b88c807SRodney W. Grimesboth have the same priority.
95249e11e3aSSheldon Hearn.Ss Flow-Control Constructs (if, while, for, case)
95349e11e3aSSheldon HearnThe syntax of the
95449e11e3aSSheldon Hearn.Ic if
95549e11e3aSSheldon Hearncommand is:
956dfe302abSRuslan Ermilov.Bd -unfilled -offset indent -compact
957dfe302abSRuslan Ermilov.Ic if Ar list
958dfe302abSRuslan Ermilov.Ic then Ar list
959dfe302abSRuslan Ermilov.Oo Ic elif Ar list
960dfe302abSRuslan Ermilov.Ic then Ar list Oc Ar ...
961dfe302abSRuslan Ermilov.Op Ic else Ar list
962dfe302abSRuslan Ermilov.Ic fi
963dfe302abSRuslan Ermilov.Ed
964ab72124fSSteve Price.Pp
965b1f60592SJilles TjoelkerThe exit status is that of selected
966b1f60592SJilles Tjoelker.Ic then
967b1f60592SJilles Tjoelkeror
968b1f60592SJilles Tjoelker.Ic else
969b1f60592SJilles Tjoelkerlist,
970b1f60592SJilles Tjoelkeror zero if no list was selected.
971b1f60592SJilles Tjoelker.Pp
97249e11e3aSSheldon HearnThe syntax of the
97349e11e3aSSheldon Hearn.Ic while
97449e11e3aSSheldon Hearncommand is:
975dfe302abSRuslan Ermilov.Bd -unfilled -offset indent -compact
976dfe302abSRuslan Ermilov.Ic while Ar list
977dfe302abSRuslan Ermilov.Ic do Ar list
978dfe302abSRuslan Ermilov.Ic done
979dfe302abSRuslan Ermilov.Ed
980ab72124fSSteve Price.Pp
981ab0a2172SSteve PriceThe two lists are executed repeatedly while the exit status of the
98249e11e3aSSheldon Hearnfirst list is zero.
98349e11e3aSSheldon HearnThe
98449e11e3aSSheldon Hearn.Ic until
98549e11e3aSSheldon Hearncommand is similar, but has the word
98649e11e3aSSheldon Hearn.Ic until
98749e11e3aSSheldon Hearnin place of
98849e11e3aSSheldon Hearn.Ic while ,
98949e11e3aSSheldon Hearnwhich causes it to
9904b88c807SRodney W. Grimesrepeat until the exit status of the first list is zero.
991ab72124fSSteve Price.Pp
992b1f60592SJilles TjoelkerThe exit status is that of the last execution of the second list,
993b1f60592SJilles Tjoelkeror zero if it was never executed.
994b1f60592SJilles Tjoelker.Pp
99549e11e3aSSheldon HearnThe syntax of the
99649e11e3aSSheldon Hearn.Ic for
99749e11e3aSSheldon Hearncommand is:
998dfe302abSRuslan Ermilov.Bd -unfilled -offset indent -compact
999dfe302abSRuslan Ermilov.Ic for Ar variable Op Ic in Ar word ...
1000dfe302abSRuslan Ermilov.Ic do Ar list
1001dfe302abSRuslan Ermilov.Ic done
1002dfe302abSRuslan Ermilov.Ed
1003ab72124fSSteve Price.Pp
1004a01459dfSStefan FarfelederIf
1005a01459dfSStefan Farfeleder.Ic in
10067b14d37fSRalf S. Engelschalland the following words are omitted,
1007776e6b33SJilles Tjoelker.Ic in Li \&"$@\&"
1008a01459dfSStefan Farfelederis used instead.
10094b88c807SRodney W. GrimesThe words are expanded, and then the list is executed
101049e11e3aSSheldon Hearnrepeatedly with the variable set to each word in turn.
101149e11e3aSSheldon HearnThe
101249e11e3aSSheldon Hearn.Ic do
10136b15476eSSheldon Hearnand
101449e11e3aSSheldon Hearn.Ic done
101549e11e3aSSheldon Hearncommands may be replaced with
1016dfe302abSRuslan Ermilov.Ql {
101749e11e3aSSheldon Hearnand
1018dfe302abSRuslan Ermilov.Ql } .
1019ab72124fSSteve Price.Pp
102049e11e3aSSheldon HearnThe syntax of the
102149e11e3aSSheldon Hearn.Ic break
102249e11e3aSSheldon Hearnand
102349e11e3aSSheldon Hearn.Ic continue
102449e11e3aSSheldon Hearncommands is:
1025dfe302abSRuslan Ermilov.D1 Ic break Op Ar num
1026dfe302abSRuslan Ermilov.D1 Ic continue Op Ar num
1027ab72124fSSteve Price.Pp
102849e11e3aSSheldon HearnThe
102949e11e3aSSheldon Hearn.Ic break
103049e11e3aSSheldon Hearncommand terminates the
103149e11e3aSSheldon Hearn.Ar num
103249e11e3aSSheldon Hearninnermost
103349e11e3aSSheldon Hearn.Ic for
103449e11e3aSSheldon Hearnor
103549e11e3aSSheldon Hearn.Ic while
103649e11e3aSSheldon Hearnloops.
103749e11e3aSSheldon HearnThe
103849e11e3aSSheldon Hearn.Ic continue
103949e11e3aSSheldon Hearncommand continues with the next iteration of the innermost loop.
104030268dfaSJilles TjoelkerThese are implemented as special built-in commands.
1041ab72124fSSteve Price.Pp
104249e11e3aSSheldon HearnThe syntax of the
104349e11e3aSSheldon Hearn.Ic case
1044dfe302abSRuslan Ermilovcommand is:
1045dfe302abSRuslan Ermilov.Bd -unfilled -offset indent -compact
1046dfe302abSRuslan Ermilov.Ic case Ar word Ic in
1047f3aff7c9SBenjamin Kaduk.Ar pattern ) Ar list Li ;;
1048dfe302abSRuslan Ermilov.Ar ...
1049dfe302abSRuslan Ermilov.Ic esac
1050dfe302abSRuslan Ermilov.Ed
1051ab72124fSSteve Price.Pp
105249e11e3aSSheldon HearnThe pattern can actually be one or more patterns
105349e11e3aSSheldon Hearn(see
105449e11e3aSSheldon Hearn.Sx Shell Patterns
105549e11e3aSSheldon Hearndescribed later),
105649e11e3aSSheldon Hearnseparated by
1057dfe302abSRuslan Ermilov.Ql \&|
10586b15476eSSheldon Hearncharacters.
1059b97989d6SJilles TjoelkerTilde expansion, parameter expansion, command substitution,
1060b97989d6SJilles Tjoelkerarithmetic expansion and quote removal are applied to the word.
1061b97989d6SJilles TjoelkerThen, each pattern is expanded in turn using tilde expansion,
1062b97989d6SJilles Tjoelkerparameter expansion, command substitution and arithmetic expansion and
1063b97989d6SJilles Tjoelkerthe expanded form of the word is checked against it.
1064b97989d6SJilles TjoelkerIf a match is found, the corresponding list is executed.
1065c9afaa63SJilles TjoelkerIf the selected list is terminated by the control operator
1066c9afaa63SJilles Tjoelker.Ql ;&
1067c9afaa63SJilles Tjoelkerinstead of
1068c9afaa63SJilles Tjoelker.Ql ;; ,
1069b97989d6SJilles Tjoelkerexecution continues with the next list,
1070b97989d6SJilles Tjoelkercontinuing until a list terminated with
1071b97989d6SJilles Tjoelker.Ql ;;
1072b97989d6SJilles Tjoelkeror the end of the
1073b97989d6SJilles Tjoelker.Ic case
1074b97989d6SJilles Tjoelkercommand.
1075ab72124fSSteve Price.Ss Grouping Commands Together
10764b88c807SRodney W. GrimesCommands may be grouped by writing either
107717e98da8SMateusz Piotrowski.Sm off
1078c013ca2cSGraham Percival.Bd -literal -offset indent
107917e98da8SMateusz Piotrowski.Po Ar list Pc
108017e98da8SMateusz Piotrowski.Ed
108117e98da8SMateusz Piotrowski.Sm on
1082ab72124fSSteve Price.Pp
10834b88c807SRodney W. Grimesor
1084c013ca2cSGraham Percival.Bd -literal -offset indent
108517e98da8SMateusz Piotrowski.No { Ar list ; }
108617e98da8SMateusz Piotrowski.Ed
1087ab72124fSSteve Price.Pp
1088ef89d04fSJilles TjoelkerThe first form executes the commands in a subshell environment.
108951a514adSJilles TjoelkerA subshell environment has its own copy of:
109051a514adSJilles Tjoelker.Bl -enum
109151a514adSJilles Tjoelker.It
109251a514adSJilles TjoelkerThe current working directory as set by
109351a514adSJilles Tjoelker.Ic cd .
109451a514adSJilles Tjoelker.It
109551a514adSJilles TjoelkerThe file creation mask as set by
109651a514adSJilles Tjoelker.Ic umask .
109751a514adSJilles Tjoelker.It
10980c35cec6SJilles TjoelkerResource limits as set by
10990c35cec6SJilles Tjoelker.Ic ulimit .
11000c35cec6SJilles Tjoelker.It
110151a514adSJilles TjoelkerReferences to open files.
110251a514adSJilles Tjoelker.It
110351a514adSJilles TjoelkerTraps as set by
110451a514adSJilles Tjoelker.Ic trap .
110551a514adSJilles Tjoelker.It
110651a514adSJilles TjoelkerKnown jobs.
110751a514adSJilles Tjoelker.It
110851a514adSJilles TjoelkerPositional parameters and variables.
110951a514adSJilles Tjoelker.It
111051a514adSJilles TjoelkerShell options.
111151a514adSJilles Tjoelker.It
111251a514adSJilles TjoelkerShell functions.
111351a514adSJilles Tjoelker.It
111451a514adSJilles TjoelkerShell aliases.
111551a514adSJilles Tjoelker.El
111651a514adSJilles Tjoelker.Pp
111751a514adSJilles TjoelkerThese are copied from the parent shell environment,
111851a514adSJilles Tjoelkerexcept that trapped (but not ignored) signals are reset to the default action
111951a514adSJilles Tjoelkerand known jobs are cleared.
112051a514adSJilles TjoelkerAny changes do not affect the parent shell environment.
112151a514adSJilles Tjoelker.Pp
112251a514adSJilles TjoelkerA subshell environment may be implemented as a child process or differently.
112351a514adSJilles TjoelkerIf job control is enabled in an interactive shell,
112451a514adSJilles Tjoelkercommands grouped in parentheses can be suspended and continued as a unit.
112551a514adSJilles Tjoelker.Pp
112654ccc8b5SJilles TjoelkerFor compatibility with other shells,
112754ccc8b5SJilles Tjoelkertwo open parentheses in sequence should be separated by whitespace.
112854ccc8b5SJilles Tjoelker.Pp
1129ef89d04fSJilles TjoelkerThe second form never forks another shell,
113049e11e3aSSheldon Hearnso it is slightly more efficient.
113149e11e3aSSheldon HearnGrouping commands together this way allows the user to
1132ab0a2172SSteve Priceredirect their output as though they were one program:
1133ab72124fSSteve Price.Bd -literal -offset indent
1134ee7dc416SSteve Price{ echo -n "hello"; echo " world"; } > greeting
1135ab72124fSSteve Price.Ed
1136ab72124fSSteve Price.Ss Functions
11374b88c807SRodney W. GrimesThe syntax of a function definition is
1138dfe302abSRuslan Ermilov.Pp
1139dfe302abSRuslan Ermilov.D1 Ar name Li \&( \&) Ar command
1140ab72124fSSteve Price.Pp
11414b88c807SRodney W. GrimesA function definition is an executable statement; when
1142dfe302abSRuslan Ermilovexecuted it installs a function named
1143dfe302abSRuslan Ermilov.Ar name
1144dfe302abSRuslan Ermilovand returns an
1145eccea571SRuslan Ermilovexit status of zero.
1146dfe302abSRuslan ErmilovThe
1147dfe302abSRuslan Ermilov.Ar command
1148dfe302abSRuslan Ermilovis normally a list
11496b15476eSSheldon Hearnenclosed between
1150dfe302abSRuslan Ermilov.Ql {
11516b15476eSSheldon Hearnand
1152dfe302abSRuslan Ermilov.Ql } .
1153ab72124fSSteve Price.Pp
11544b88c807SRodney W. GrimesVariables may be declared to be local to a function by
115549e11e3aSSheldon Hearnusing the
115649e11e3aSSheldon Hearn.Ic local
115749e11e3aSSheldon Hearncommand.
115849e11e3aSSheldon HearnThis should appear as the first statement of a function,
115949e11e3aSSheldon Hearnand the syntax is:
1160dfe302abSRuslan Ermilov.Pp
1161dfe302abSRuslan Ermilov.D1 Ic local Oo Ar variable ... Oc Op Fl
1162ab72124fSSteve Price.Pp
116349e11e3aSSheldon HearnThe
116449e11e3aSSheldon Hearn.Ic local
11658eed22b1SJens Schweikhardtcommand is implemented as a built-in command.
1166b1f60592SJilles TjoelkerThe exit status is zero
1167b1f60592SJilles Tjoelkerunless the command is not in a function or a variable name is invalid.
1168ab72124fSSteve Price.Pp
11694b88c807SRodney W. GrimesWhen a variable is made local, it inherits the initial
11704b88c807SRodney W. Grimesvalue and exported and readonly flags from the variable
11714b88c807SRodney W. Grimeswith the same name in the surrounding scope, if there is
1172eccea571SRuslan Ermilovone.
1173eccea571SRuslan ErmilovOtherwise, the variable is initially unset.
1174eccea571SRuslan ErmilovThe shell
1175e918fc8fSSheldon Hearnuses dynamic scoping, so that if the variable
1176dfe302abSRuslan Ermilov.Va x
1177e918fc8fSSheldon Hearnis made local to function
1178e918fc8fSSheldon Hearn.Em f ,
1179e918fc8fSSheldon Hearnwhich then calls function
1180e918fc8fSSheldon Hearn.Em g ,
1181e918fc8fSSheldon Hearnreferences to the variable
1182dfe302abSRuslan Ermilov.Va x
1183e918fc8fSSheldon Hearnmade inside
1184e918fc8fSSheldon Hearn.Em g
1185e918fc8fSSheldon Hearnwill refer to the variable
1186dfe302abSRuslan Ermilov.Va x
1187e918fc8fSSheldon Hearndeclared inside
1188e918fc8fSSheldon Hearn.Em f ,
1189e918fc8fSSheldon Hearnnot to the global variable named
1190dfe302abSRuslan Ermilov.Va x .
1191ab72124fSSteve Price.Pp
11926b4bbd8aSCeri DaviesThe only special parameter that can be made local is
1193dfe302abSRuslan Ermilov.Ql - .
11946b15476eSSheldon HearnMaking
1195dfe302abSRuslan Ermilov.Ql -
119662c37116SJilles Tjoelkerlocal causes any shell options
119762c37116SJilles Tjoelker(including those that only have long names)
119862c37116SJilles Tjoelkerthat are
1199dfe302abSRuslan Ermilovchanged via the
1200dfe302abSRuslan Ermilov.Ic set
1201dfe302abSRuslan Ermilovcommand inside the function to be
12024b88c807SRodney W. Grimesrestored to their original values when the function
12034b88c807SRodney W. Grimesreturns.
1204ab72124fSSteve Price.Pp
120549e11e3aSSheldon HearnThe syntax of the
120649e11e3aSSheldon Hearn.Ic return
120749e11e3aSSheldon Hearncommand is
1208dfe302abSRuslan Ermilov.Pp
1209dfe302abSRuslan Ermilov.D1 Ic return Op Ar exitstatus
1210ab72124fSSteve Price.Pp
12112935c4ccSJilles TjoelkerIt terminates the current executional scope, returning from the closest
12122935c4ccSJilles Tjoelkernested function or sourced script;
12132935c4ccSJilles Tjoelkerif no function or sourced script is being executed,
12142935c4ccSJilles Tjoelkerit exits the shell instance.
121549e11e3aSSheldon HearnThe
121649e11e3aSSheldon Hearn.Ic return
121730268dfaSJilles Tjoelkercommand is implemented as a special built-in command.
1218ab72124fSSteve Price.Ss Variables and Parameters
1219eccea571SRuslan ErmilovThe shell maintains a set of parameters.
1220eccea571SRuslan ErmilovA parameter
12214445af21SJilles Tjoelkerdenoted by a name
12224445af21SJilles Tjoelker(consisting solely
12234445af21SJilles Tjoelkerof alphabetics, numerics, and underscores,
12244445af21SJilles Tjoelkerand starting with an alphabetic or an underscore)
12254445af21SJilles Tjoelkeris called a variable.
1226eccea571SRuslan ErmilovWhen starting up,
12274445af21SJilles Tjoelkerthe shell turns all environment variables with valid names into shell
1228eccea571SRuslan Ermilovvariables.
1229eccea571SRuslan ErmilovNew variables can be set using the form
1230dfe302abSRuslan Ermilov.Pp
1231dfe302abSRuslan Ermilov.D1 Ar name Ns = Ns Ar value
1232ab72124fSSteve Price.Pp
123349e11e3aSSheldon HearnA parameter can also be denoted by a number
12344b88c807SRodney W. Grimesor a special character as explained below.
1235ad56ebdcSJilles Tjoelker.Pp
1236ad56ebdcSJilles TjoelkerAssignments are expanded differently from other words:
1237ad56ebdcSJilles Tjoelkertilde expansion is also performed after the equals sign and after any colon
1238ad56ebdcSJilles Tjoelkerand usernames are also terminated by colons,
1239ad56ebdcSJilles Tjoelkerand field splitting and pathname expansion are not performed.
124084fbdd8cSJilles Tjoelker.Pp
124184fbdd8cSJilles TjoelkerThis special expansion applies not only to assignments that form a simple
124284fbdd8cSJilles Tjoelkercommand by themselves or precede a command word,
124384fbdd8cSJilles Tjoelkerbut also to words passed to the
124484fbdd8cSJilles Tjoelker.Ic export ,
124584fbdd8cSJilles Tjoelker.Ic local
124684fbdd8cSJilles Tjoelkeror
124784fbdd8cSJilles Tjoelker.Ic readonly
124884fbdd8cSJilles Tjoelkerbuilt-in commands that have this form.
124984fbdd8cSJilles TjoelkerFor this, the builtin's name must be literal
125084fbdd8cSJilles Tjoelker(not the result of an expansion)
125184fbdd8cSJilles Tjoelkerand may optionally be preceded by one or more literal instances of
125284fbdd8cSJilles Tjoelker.Ic command
125384fbdd8cSJilles Tjoelkerwithout options.
1254ab72124fSSteve Price.Ss Positional Parameters
125549e11e3aSSheldon HearnA positional parameter is a parameter denoted by a number greater than zero.
1256ab72124fSSteve PriceThe shell sets these initially to the values of its command line
1257eccea571SRuslan Ermilovarguments that follow the name of the shell script.
1258eccea571SRuslan ErmilovThe
1259c644db6aSSheldon Hearn.Ic set
12608eed22b1SJens Schweikhardtbuilt-in command can also be used to set or reset them.
1261ab72124fSSteve Price.Ss Special Parameters
1262626a7b1dSJilles TjoelkerSpecial parameters are parameters denoted by a single special character
1263626a7b1dSJilles Tjoelkeror the digit zero.
1264626a7b1dSJilles TjoelkerThey are shown in the following list, exactly as they would appear in input
126543d6677bSAlfred Perlsteintyped by the user or in the source of a shell script.
1266ab72124fSSteve Price.Bl -hang
126743d6677bSAlfred Perlstein.It Li $*
1268eccea571SRuslan ErmilovExpands to the positional parameters, starting from one.
1269eccea571SRuslan ErmilovWhen
12704b88c807SRodney W. Grimesthe expansion occurs within a double-quoted string
12714b88c807SRodney W. Grimesit expands to a single field with the value of each parameter
127249e11e3aSSheldon Hearnseparated by the first character of the
1273dfe302abSRuslan Ermilov.Va IFS
127449e11e3aSSheldon Hearnvariable,
1275dfe302abSRuslan Ermilovor by a space if
1276dfe302abSRuslan Ermilov.Va IFS
127749e11e3aSSheldon Hearnis unset.
127843d6677bSAlfred Perlstein.It Li $@
1279eccea571SRuslan ErmilovExpands to the positional parameters, starting from one.
1280eccea571SRuslan ErmilovWhen
12814b88c807SRodney W. Grimesthe expansion occurs within double-quotes, each positional
12824b88c807SRodney W. Grimesparameter expands as a separate argument.
12834b88c807SRodney W. GrimesIf there are no positional parameters, the
1284e918fc8fSSheldon Hearnexpansion of
1285e918fc8fSSheldon Hearn.Li @
1286e918fc8fSSheldon Hearngenerates zero arguments, even when
1287e918fc8fSSheldon Hearn.Li @
1288eccea571SRuslan Ermilovis double-quoted.
1289eccea571SRuslan ErmilovWhat this basically means, for example, is
1290dfe302abSRuslan Ermilovif
1291dfe302abSRuslan Ermilov.Li $1
1292dfe302abSRuslan Ermilovis
1293dfe302abSRuslan Ermilov.Dq Li abc
1294dfe302abSRuslan Ermilovand
1295dfe302abSRuslan Ermilov.Li $2
1296dfe302abSRuslan Ermilovis
1297dfe302abSRuslan Ermilov.Dq Li "def ghi" ,
12986b15476eSSheldon Hearnthen
1299776e6b33SJilles Tjoelker.Li \&"$@\&"
13006b15476eSSheldon Hearnexpands to
13014b88c807SRodney W. Grimesthe two arguments:
1302ab72124fSSteve Price.Bd -literal -offset indent
13034b88c807SRodney W. Grimes"abc"   "def ghi"
1304ab72124fSSteve Price.Ed
130543d6677bSAlfred Perlstein.It Li $#
13064b88c807SRodney W. GrimesExpands to the number of positional parameters.
1307dfe302abSRuslan Ermilov.It Li $?
13084b88c807SRodney W. GrimesExpands to the exit status of the most recent pipeline.
130943d6677bSAlfred Perlstein.It Li $-
1310ab72124fSSteve Price(hyphen) Expands to the current option flags (the single-letter
13114b88c807SRodney W. Grimesoption names concatenated into a string) as specified on
1312dfe302abSRuslan Ermilovinvocation, by the
1313dfe302abSRuslan Ermilov.Ic set
1314dfe302abSRuslan Ermilovbuilt-in command, or implicitly
13154b88c807SRodney W. Grimesby the shell.
131643d6677bSAlfred Perlstein.It Li $$
1317eccea571SRuslan ErmilovExpands to the process ID of the invoked shell.
1318eccea571SRuslan ErmilovA subshell
1319dfe302abSRuslan Ermilovretains the same value of
1320dfe302abSRuslan Ermilov.Va $
1321dfe302abSRuslan Ermilovas its parent.
1322dfe302abSRuslan Ermilov.It Li $!
13234b88c807SRodney W. GrimesExpands to the process ID of the most recent background
1324eccea571SRuslan Ermilovcommand executed from the current shell.
1325eccea571SRuslan ErmilovFor a
13264b88c807SRodney W. Grimespipeline, the process ID is that of the last command in the
13274b88c807SRodney W. Grimespipeline.
1328ed4c3b5fSJilles TjoelkerIf this parameter is referenced, the shell will remember
1329ed4c3b5fSJilles Tjoelkerthe process ID and its exit status until the
1330ed4c3b5fSJilles Tjoelker.Ic wait
1331ed4c3b5fSJilles Tjoelkerbuilt-in command reports completion of the process.
133243d6677bSAlfred Perlstein.It Li $0
133357a40f7dSJilles Tjoelker(zero) Expands to the name of the shell script if passed on the command line,
133457a40f7dSJilles Tjoelkerthe
133557a40f7dSJilles Tjoelker.Ar name
133657a40f7dSJilles Tjoelkeroperand if given (with
133757a40f7dSJilles Tjoelker.Fl c )
133857a40f7dSJilles Tjoelkeror otherwise argument 0 passed to the shell.
1339ab72124fSSteve Price.El
13401788b7ffSJilles Tjoelker.Ss Special Variables
13411788b7ffSJilles TjoelkerThe following variables are set by the shell or
13421788b7ffSJilles Tjoelkerhave special meaning to it:
13431788b7ffSJilles Tjoelker.Bl -tag -width ".Va HISTSIZE"
13441788b7ffSJilles Tjoelker.It Va CDPATH
13451788b7ffSJilles TjoelkerThe search path used with the
13461788b7ffSJilles Tjoelker.Ic cd
13471788b7ffSJilles Tjoelkerbuilt-in.
13481788b7ffSJilles Tjoelker.It Va EDITOR
13491788b7ffSJilles TjoelkerThe fallback editor used with the
13501788b7ffSJilles Tjoelker.Ic fc
13511788b7ffSJilles Tjoelkerbuilt-in.
13521788b7ffSJilles TjoelkerIf not set, the default editor is
13531788b7ffSJilles Tjoelker.Xr ed 1 .
13541788b7ffSJilles Tjoelker.It Va FCEDIT
13551788b7ffSJilles TjoelkerThe default editor used with the
13561788b7ffSJilles Tjoelker.Ic fc
13571788b7ffSJilles Tjoelkerbuilt-in.
1358988b1bb0SBaptiste Daroussin.It Va HISTFILE
1359988b1bb0SBaptiste DaroussinFile used for persistent history storage.
1360988b1bb0SBaptiste DaroussinIf unset
1361988b1bb0SBaptiste Daroussin.Pa ~/.sh_history
1362988b1bb0SBaptiste Daroussinwill be used.
1363988b1bb0SBaptiste DaroussinIf set but empty or
1364988b1bb0SBaptiste Daroussin.Va HISTSIZE
1365988b1bb0SBaptiste Daroussinis set to 0
1366988b1bb0SBaptiste Daroussinthe shell will not load and save the history.
13671788b7ffSJilles Tjoelker.It Va HISTSIZE
13681788b7ffSJilles TjoelkerThe number of previous commands that are accessible.
13691788b7ffSJilles Tjoelker.It Va HOME
13701788b7ffSJilles TjoelkerThe user's home directory,
13711788b7ffSJilles Tjoelkerused in tilde expansion and as a default directory for the
13721788b7ffSJilles Tjoelker.Ic cd
13731788b7ffSJilles Tjoelkerbuilt-in.
13741788b7ffSJilles Tjoelker.It Va IFS
13751788b7ffSJilles TjoelkerInput Field Separators.
13767cca93e6SJilles TjoelkerThis is initialized at startup to
13771788b7ffSJilles Tjoelker.Aq space ,
13781788b7ffSJilles Tjoelker.Aq tab ,
13791788b7ffSJilles Tjoelkerand
1380ad56ebdcSJilles Tjoelker.Aq newline
1381ad56ebdcSJilles Tjoelkerin that order.
13827cca93e6SJilles TjoelkerThis value also applies if
1383ad56ebdcSJilles Tjoelker.Va IFS
1384ad56ebdcSJilles Tjoelkeris unset, but not if it is set to the empty string.
13851788b7ffSJilles TjoelkerSee the
13861788b7ffSJilles Tjoelker.Sx White Space Splitting
13871788b7ffSJilles Tjoelkersection for more details.
13881788b7ffSJilles Tjoelker.It Va LINENO
13891788b7ffSJilles TjoelkerThe current line number in the script or function.
13901788b7ffSJilles Tjoelker.It Va MAIL
13911788b7ffSJilles TjoelkerThe name of a mail file, that will be checked for the arrival of new
13921788b7ffSJilles Tjoelkermail.
13931788b7ffSJilles TjoelkerOverridden by
13941788b7ffSJilles Tjoelker.Va MAILPATH .
13951788b7ffSJilles Tjoelker.It Va MAILPATH
13961788b7ffSJilles TjoelkerA colon
13971788b7ffSJilles Tjoelker.Pq Ql \&:
13981788b7ffSJilles Tjoelkerseparated list of file names, for the shell to check for incoming
13991788b7ffSJilles Tjoelkermail.
14001788b7ffSJilles TjoelkerThis variable overrides the
14011788b7ffSJilles Tjoelker.Va MAIL
14021788b7ffSJilles Tjoelkersetting.
14031788b7ffSJilles TjoelkerThere is a maximum of 10 mailboxes that can be monitored at once.
14044445af21SJilles Tjoelker.It Va OPTIND
14054445af21SJilles TjoelkerThe index of the next argument to be processed by
14064445af21SJilles Tjoelker.Ic getopts .
14074445af21SJilles TjoelkerThis is initialized to 1 at startup.
14081788b7ffSJilles Tjoelker.It Va PATH
14091788b7ffSJilles TjoelkerThe default search path for executables.
14101788b7ffSJilles TjoelkerSee the
14111788b7ffSJilles Tjoelker.Sx Path Search
14121788b7ffSJilles Tjoelkersection for details.
14131788b7ffSJilles Tjoelker.It Va PPID
14141788b7ffSJilles TjoelkerThe parent process ID of the invoked shell.
14151788b7ffSJilles TjoelkerThis is set at startup
14161788b7ffSJilles Tjoelkerunless this variable is in the environment.
14171788b7ffSJilles TjoelkerA later change of parent process ID is not reflected.
14181788b7ffSJilles TjoelkerA subshell retains the same value of
14191788b7ffSJilles Tjoelker.Va PPID .
14201788b7ffSJilles Tjoelker.It Va PS1
14211788b7ffSJilles TjoelkerThe primary prompt string, which defaults to
14221788b7ffSJilles Tjoelker.Dq Li "$ " ,
14231788b7ffSJilles Tjoelkerunless you are the superuser, in which case it defaults to
14241788b7ffSJilles Tjoelker.Dq Li "# " .
14251ca3beb9SJoel Dahl.Va PS1
14261ca3beb9SJoel Dahlmay include any of the following formatting sequences,
14271ca3beb9SJoel Dahlwhich are replaced by the given information:
14281ca3beb9SJoel Dahl.Bl -tag -width indent
1429a675eaecSPiotr Pawel Stefaniak.It Li \eD{format}
1430a675eaecSPiotr Pawel StefaniakThe current time in
1431a675eaecSPiotr Pawel Stefaniak.Xr strftime 3
1432a675eaecSPiotr Pawel Stefaniak.Ar format .
1433a675eaecSPiotr Pawel StefaniakThe braces are required.
1434a675eaecSPiotr Pawel StefaniakEmpty
1435a675eaecSPiotr Pawel Stefaniak.Ar format
1436a675eaecSPiotr Pawel Stefaniakis equivalent to
1437a675eaecSPiotr Pawel Stefaniak\&%X,
1438a675eaecSPiotr Pawel Stefaniaknational representation of the time.
14391ca3beb9SJoel Dahl.It Li \eH
14402770ce60SWarren BlockThis system's fully-qualified hostname (FQDN).
1441ae100660SDon Lewis.It Li \eh
14422770ce60SWarren BlockThis system's hostname.
1443d81ca439SEdward Tomasz Napierala.It Li \eu
1444d81ca439SEdward Tomasz NapieralaUser name.
14451ca3beb9SJoel Dahl.It Li \eW
14461ca3beb9SJoel DahlThe final component of the current working directory.
14471ca3beb9SJoel Dahl.It Li \ew
14481ca3beb9SJoel DahlThe entire path of the current working directory.
14491ca3beb9SJoel Dahl.It Li \e$
14501ca3beb9SJoel DahlSuperuser status.
14511ca3beb9SJoel Dahl.Dq Li "$"
14521ca3beb9SJoel Dahlfor normal users and
14531ca3beb9SJoel Dahl.Dq Li "#"
14541ca3beb9SJoel Dahlfor superusers.
14551ca3beb9SJoel Dahl.It Li \e\e
14561ca3beb9SJoel DahlA literal backslash.
14573cf65f8aSJuraj Lutter.It Li \e[
14583cf65f8aSJuraj LutterStart of a sequence of non-printing characters (used, for example,
14593cf65f8aSJuraj Lutterto embed ANSI CSI sequences into the prompt).
14603cf65f8aSJuraj Lutter.It Li \e]
14613cf65f8aSJuraj LutterEnd of a sequence of non-printing characters.
14623cf65f8aSJuraj Lutter.El
14633cf65f8aSJuraj Lutter.Pp
14643cf65f8aSJuraj LutterThe following special and non-printing characters are supported
14653cf65f8aSJuraj Lutterwithin the sequence of non-printing characters:
14663cf65f8aSJuraj Lutter.Bl -tag -width indent
14673cf65f8aSJuraj Lutter.It Li \ea
14683cf65f8aSJuraj LutterEmits ASCII BEL (0x07, 007) character.
14693cf65f8aSJuraj Lutter.It Li \ee
14703cf65f8aSJuraj LutterEmits ASCII ESC (0x1b, 033) character.
14713cf65f8aSJuraj Lutter.It Li \er
14723cf65f8aSJuraj LutterEmits ASCII CR (0x0d, 015) character.
14733cf65f8aSJuraj Lutter.It Li \en
14743cf65f8aSJuraj LutterEmits CRLF sequence.
14751ca3beb9SJoel Dahl.El
14761788b7ffSJilles Tjoelker.It Va PS2
14771788b7ffSJilles TjoelkerThe secondary prompt string, which defaults to
14781788b7ffSJilles Tjoelker.Dq Li "> " .
14791ca3beb9SJoel Dahl.Va PS2
14801ca3beb9SJoel Dahlmay include any of the formatting sequences from
14811ca3beb9SJoel Dahl.Va PS1 .
14821788b7ffSJilles Tjoelker.It Va PS4
14831788b7ffSJilles TjoelkerThe prefix for the trace output (if
14841788b7ffSJilles Tjoelker.Fl x
14851788b7ffSJilles Tjoelkeris active).
14861788b7ffSJilles TjoelkerThe default is
14871788b7ffSJilles Tjoelker.Dq Li "+ " .
14881788b7ffSJilles Tjoelker.El
1489ab72124fSSteve Price.Ss Word Expansions
14904b88c807SRodney W. GrimesThis clause describes the various expansions that are
1491eccea571SRuslan Ermilovperformed on words.
1492eccea571SRuslan ErmilovNot all expansions are performed on
14934b88c807SRodney W. Grimesevery word, as explained later.
1494ab72124fSSteve Price.Pp
14954b88c807SRodney W. GrimesTilde expansions, parameter expansions, command substitutions,
14964b88c807SRodney W. Grimesarithmetic expansions, and quote removals that occur within
1497eccea571SRuslan Ermilova single word expand to a single field.
1498eccea571SRuslan ErmilovIt is only field
14994b88c807SRodney W. Grimessplitting or pathname expansion that can create multiple
1500e6d3cf26SSheldon Hearnfields from a single word.
1501e6d3cf26SSheldon HearnThe single exception to this rule is
1502e918fc8fSSheldon Hearnthe expansion of the special parameter
1503dfe302abSRuslan Ermilov.Va @
1504e918fc8fSSheldon Hearnwithin double-quotes,
1505e6d3cf26SSheldon Hearnas was described above.
1506ab72124fSSteve Price.Pp
15074b88c807SRodney W. GrimesThe order of word expansion is:
1508ab72124fSSteve Price.Bl -enum
1509ab72124fSSteve Price.It
1510ab72124fSSteve PriceTilde Expansion, Parameter Expansion, Command Substitution,
15114b88c807SRodney W. GrimesArithmetic Expansion (these all occur at the same time).
1512ab72124fSSteve Price.It
151349e11e3aSSheldon HearnField Splitting is performed on fields generated by step (1)
151449e11e3aSSheldon Hearnunless the
1515dfe302abSRuslan Ermilov.Va IFS
151649e11e3aSSheldon Hearnvariable is null.
1517ab72124fSSteve Price.It
151849e11e3aSSheldon HearnPathname Expansion (unless the
151949e11e3aSSheldon Hearn.Fl f
152049e11e3aSSheldon Hearnoption is in effect).
1521ab72124fSSteve Price.It
1522ab72124fSSteve PriceQuote Removal.
1523ab72124fSSteve Price.El
1524ab72124fSSteve Price.Pp
152549e11e3aSSheldon HearnThe
1526dfe302abSRuslan Ermilov.Ql $
152749e11e3aSSheldon Hearncharacter is used to introduce parameter expansion, command
152836cf3efeSJilles Tjoelkersubstitution, or arithmetic expansion.
1529ab72124fSSteve Price.Ss Tilde Expansion (substituting a user's home directory)
153049e11e3aSSheldon HearnA word beginning with an unquoted tilde character
1531a762afe5SGiorgos Keramidas.Pq Ql ~
153249e11e3aSSheldon Hearnis
153349e11e3aSSheldon Hearnsubjected to tilde expansion.
153449e11e3aSSheldon HearnAll the characters up to a slash
1535a762afe5SGiorgos Keramidas.Pq Ql /
153649e11e3aSSheldon Hearnor the end of the word are treated as a username
1537eccea571SRuslan Ermilovand are replaced with the user's home directory.
1538eccea571SRuslan ErmilovIf the
1539dfe302abSRuslan Ermilovusername is missing (as in
1540dfe302abSRuslan Ermilov.Pa ~/foobar ) ,
1541dfe302abSRuslan Ermilovthe tilde is replaced with the value of the
15421788b7ffSJilles Tjoelker.Va HOME
1543dfe302abSRuslan Ermilovvariable (the current user's home directory).
1544ab72124fSSteve Price.Ss Parameter Expansion
15454b88c807SRodney W. GrimesThe format for parameter expansion is as follows:
1546ab72124fSSteve Price.Pp
1547dfe302abSRuslan Ermilov.D1 Li ${ Ns Ar expression Ns Li }
1548dfe302abSRuslan Ermilov.Pp
1549dfe302abSRuslan Ermilovwhere
1550dfe302abSRuslan Ermilov.Ar expression
1551dfe302abSRuslan Ermilovconsists of all characters until the matching
1552dfe302abSRuslan Ermilov.Ql } .
155349e11e3aSSheldon HearnAny
1554dfe302abSRuslan Ermilov.Ql }
1555f35d74beSJilles Tjoelkerescaped by a backslash or within a single-quoted or double-quoted
1556f35d74beSJilles Tjoelkerstring, and characters in
15574b88c807SRodney W. Grimesembedded arithmetic expansions, command substitutions, and variable
155849e11e3aSSheldon Hearnexpansions, are not examined in determining the matching
1559dfe302abSRuslan Ermilov.Ql } .
1560f35d74beSJilles TjoelkerIf the variants with
1561227c8e2aSJilles Tjoelker.Ql + ,
1562227c8e2aSJilles Tjoelker.Ql - ,
1563227c8e2aSJilles Tjoelker.Ql =
1564227c8e2aSJilles Tjoelkeror
1565f35d74beSJilles Tjoelker.Ql ?\&
1566f35d74beSJilles Tjoelkeroccur within a double-quoted string,
1567f35d74beSJilles Tjoelkeras an extension there may be unquoted parts
1568f35d74beSJilles Tjoelker(via double-quotes inside the expansion);
1569227c8e2aSJilles Tjoelker.Ql }
1570f35d74beSJilles Tjoelkerwithin such parts are also not examined in determining the matching
1571227c8e2aSJilles Tjoelker.Ql } .
1572ab72124fSSteve Price.Pp
15734b88c807SRodney W. GrimesThe simplest form for parameter expansion is:
1574ab72124fSSteve Price.Pp
1575dfe302abSRuslan Ermilov.D1 Li ${ Ns Ar parameter Ns Li }
1576dfe302abSRuslan Ermilov.Pp
1577dfe302abSRuslan ErmilovThe value, if any, of
1578dfe302abSRuslan Ermilov.Ar parameter
1579dfe302abSRuslan Ermilovis substituted.
1580ab72124fSSteve Price.Pp
15814b88c807SRodney W. GrimesThe parameter name or symbol can be enclosed in braces, which are
15824b88c807SRodney W. Grimesoptional except for positional parameters with more than one digit or
15834b88c807SRodney W. Grimeswhen parameter is followed by a character that could be interpreted as
15844b88c807SRodney W. Grimespart of the name.
1585ab72124fSSteve PriceIf a parameter expansion occurs inside double-quotes:
1586ab72124fSSteve Price.Bl -enum
1587ab72124fSSteve Price.It
1588ab72124fSSteve PriceField splitting is not performed on the results of the
1589e918fc8fSSheldon Hearnexpansion, with the exception of the special parameter
1590dfe302abSRuslan Ermilov.Va @ .
1591ad56ebdcSJilles Tjoelker.It
1592ad56ebdcSJilles TjoelkerPathname expansion is not performed on the results of the
1593ad56ebdcSJilles Tjoelkerexpansion.
1594ab72124fSSteve Price.El
1595ab72124fSSteve Price.Pp
15964b88c807SRodney W. GrimesIn addition, a parameter expansion can be modified by using one of the
15974b88c807SRodney W. Grimesfollowing formats.
159881e9cda2SRuslan Ermilov.Bl -tag -width indent
1599dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li :- Ns Ar word Ns Li }
1600eccea571SRuslan ErmilovUse Default Values.
1601dfe302abSRuslan ErmilovIf
1602dfe302abSRuslan Ermilov.Ar parameter
1603dfe302abSRuslan Ermilovis unset or null, the expansion of
1604dfe302abSRuslan Ermilov.Ar word
1605dfe302abSRuslan Ermilovis substituted; otherwise, the value of
1606dfe302abSRuslan Ermilov.Ar parameter
1607dfe302abSRuslan Ermilovis substituted.
1608dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li := Ns Ar word Ns Li }
1609eccea571SRuslan ErmilovAssign Default Values.
1610dfe302abSRuslan ErmilovIf
1611dfe302abSRuslan Ermilov.Ar parameter
1612dfe302abSRuslan Ermilovis unset or null, the expansion of
1613dfe302abSRuslan Ermilov.Ar word
1614dfe302abSRuslan Ermilovis assigned to
1615dfe302abSRuslan Ermilov.Ar parameter .
1616eccea571SRuslan ErmilovIn all cases, the
1617dfe302abSRuslan Ermilovfinal value of
1618dfe302abSRuslan Ermilov.Ar parameter
1619dfe302abSRuslan Ermilovis substituted.
1620b139165cSJilles TjoelkerQuoting inside
1621b139165cSJilles Tjoelker.Ar word
1622b139165cSJilles Tjoelkerdoes not prevent field splitting or pathname expansion.
1623eccea571SRuslan ErmilovOnly variables, not positional
16244b88c807SRodney W. Grimesparameters or special parameters, can be
16254b88c807SRodney W. Grimesassigned in this way.
1626dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li :? Ns Oo Ar word Oc Ns Li }
1627eccea571SRuslan ErmilovIndicate Error if Null or Unset.
1628eccea571SRuslan ErmilovIf
1629dfe302abSRuslan Ermilov.Ar parameter
1630dfe302abSRuslan Ermilovis unset or null, the expansion of
1631dfe302abSRuslan Ermilov.Ar word
1632dfe302abSRuslan Ermilov(or a message indicating it is unset if
1633dfe302abSRuslan Ermilov.Ar word
1634dfe302abSRuslan Ermilovis omitted) is written to standard
16354b88c807SRodney W. Grimeserror and the shell exits with a nonzero
1636e6d3cf26SSheldon Hearnexit status.
1637e6d3cf26SSheldon HearnOtherwise, the value of
1638dfe302abSRuslan Ermilov.Ar parameter
1639dfe302abSRuslan Ermilovis substituted.
1640eccea571SRuslan ErmilovAn
16414b88c807SRodney W. Grimesinteractive shell need not exit.
1642dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li :+ Ns Ar word Ns Li }
1643eccea571SRuslan ErmilovUse Alternate Value.
1644dfe302abSRuslan ErmilovIf
1645dfe302abSRuslan Ermilov.Ar parameter
1646dfe302abSRuslan Ermilovis unset or null, null is substituted;
1647dfe302abSRuslan Ermilovotherwise, the expansion of
1648dfe302abSRuslan Ermilov.Ar word
1649dfe302abSRuslan Ermilovis substituted.
16501413f7deSTony Finch.El
1651ab72124fSSteve Price.Pp
16524b88c807SRodney W. GrimesIn the parameter expansions shown previously, use of the colon in the
16534b88c807SRodney W. Grimesformat results in a test for a parameter that is unset or null; omission
16544b88c807SRodney W. Grimesof the colon results in a test for a parameter that is only unset.
1655227c8e2aSJilles Tjoelker.Pp
1656227c8e2aSJilles TjoelkerThe
1657227c8e2aSJilles Tjoelker.Ar word
1658227c8e2aSJilles Tjoelkerinherits the type of quoting
1659227c8e2aSJilles Tjoelker(unquoted, double-quoted or here-document)
1660227c8e2aSJilles Tjoelkerfrom the surroundings,
1661227c8e2aSJilles Tjoelkerwith the exception that a backslash that quotes a closing brace is removed
1662227c8e2aSJilles Tjoelkerduring quote removal.
16631413f7deSTony Finch.Bl -tag -width indent
1664dfe302abSRuslan Ermilov.It Li ${# Ns Ar parameter Ns Li }
1665eccea571SRuslan ErmilovString Length.
1666eccea571SRuslan ErmilovThe length in characters of
1667dfe302abSRuslan Ermilovthe value of
1668dfe302abSRuslan Ermilov.Ar parameter .
166971337c33SSheldon Hearn.El
1670ab72124fSSteve Price.Pp
16714b88c807SRodney W. GrimesThe following four varieties of parameter expansion provide for substring
167249e11e3aSSheldon Hearnprocessing.
167349e11e3aSSheldon HearnIn each case, pattern matching notation
167449e11e3aSSheldon Hearn(see
167549e11e3aSSheldon Hearn.Sx Shell Patterns ) ,
167649e11e3aSSheldon Hearnrather than regular expression notation,
167749e11e3aSSheldon Hearnis used to evaluate the patterns.
1678e918fc8fSSheldon HearnIf parameter is one of the special parameters
1679dfe302abSRuslan Ermilov.Va *
168049e11e3aSSheldon Hearnor
1681dfe302abSRuslan Ermilov.Va @ ,
168249e11e3aSSheldon Hearnthe result of the expansion is unspecified.
16834b88c807SRodney W. GrimesEnclosing the full parameter expansion string in double-quotes does not
16844b88c807SRodney W. Grimescause the following four varieties of pattern characters to be quoted,
16854b88c807SRodney W. Grimeswhereas quoting characters within the braces has this effect.
168681e9cda2SRuslan Ermilov.Bl -tag -width indent
1687dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li % Ns Ar word Ns Li }
1688eccea571SRuslan ErmilovRemove Smallest Suffix Pattern.
1689dfe302abSRuslan ErmilovThe
1690dfe302abSRuslan Ermilov.Ar word
1691eccea571SRuslan Ermilovis expanded to produce a pattern.
1692eccea571SRuslan ErmilovThe
16934b88c807SRodney W. Grimesparameter expansion then results in
1694dfe302abSRuslan Ermilov.Ar parameter ,
1695dfe302abSRuslan Ermilovwith the smallest portion of the
16964b88c807SRodney W. Grimessuffix matched by the pattern deleted.
1697dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li %% Ns Ar word Ns Li }
1698eccea571SRuslan ErmilovRemove Largest Suffix Pattern.
1699dfe302abSRuslan ErmilovThe
1700dfe302abSRuslan Ermilov.Ar word
1701eccea571SRuslan Ermilovis expanded to produce a pattern.
1702eccea571SRuslan ErmilovThe
17034b88c807SRodney W. Grimesparameter expansion then results in
1704dfe302abSRuslan Ermilov.Ar parameter ,
1705dfe302abSRuslan Ermilovwith the largest portion of the
17064b88c807SRodney W. Grimessuffix matched by the pattern deleted.
1707dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li # Ns Ar word Ns Li }
1708eccea571SRuslan ErmilovRemove Smallest Prefix Pattern.
1709dfe302abSRuslan ErmilovThe
1710dfe302abSRuslan Ermilov.Ar word
1711eccea571SRuslan Ermilovis expanded to produce a pattern.
1712eccea571SRuslan ErmilovThe
17134b88c807SRodney W. Grimesparameter expansion then results in
1714dfe302abSRuslan Ermilov.Ar parameter ,
1715dfe302abSRuslan Ermilovwith the smallest portion of the
17164b88c807SRodney W. Grimesprefix matched by the pattern deleted.
1717dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li ## Ns Ar word Ns Li }
1718eccea571SRuslan ErmilovRemove Largest Prefix Pattern.
1719dfe302abSRuslan ErmilovThe
1720dfe302abSRuslan Ermilov.Ar word
1721eccea571SRuslan Ermilovis expanded to produce a pattern.
1722eccea571SRuslan ErmilovThe
17234b88c807SRodney W. Grimesparameter expansion then results in
1724dfe302abSRuslan Ermilov.Ar parameter ,
1725dfe302abSRuslan Ermilovwith the largest portion of the
17264b88c807SRodney W. Grimesprefix matched by the pattern deleted.
1727ab72124fSSteve Price.El
1728ab72124fSSteve Price.Ss Command Substitution
17294b88c807SRodney W. GrimesCommand substitution allows the output of a command to be substituted in
1730eccea571SRuslan Ermilovplace of the command name itself.
1731eccea571SRuslan ErmilovCommand substitution occurs when
17324b88c807SRodney W. Grimesthe command is enclosed as follows:
1733dfe302abSRuslan Ermilov.Pp
1734dfe302abSRuslan Ermilov.D1 Li $( Ns Ar command Ns Li )\&
1735ab72124fSSteve Price.Pp
173649e11e3aSSheldon Hearnor the backquoted version:
1737dfe302abSRuslan Ermilov.Pp
1738dfe302abSRuslan Ermilov.D1 Li ` Ns Ar command Ns Li `
1739ab72124fSSteve Price.Pp
1740ef89d04fSJilles TjoelkerThe shell expands the command substitution by executing command
1741ef89d04fSJilles Tjoelkerand replacing the command substitution
174249e11e3aSSheldon Hearnwith the standard output of the command,
174349e11e3aSSheldon Hearnremoving sequences of one or more newlines at the end of the substitution.
174449e11e3aSSheldon HearnEmbedded newlines before the end of the output are not removed;
174549e11e3aSSheldon Hearnhowever, during field splitting, they may be translated into spaces
174649e11e3aSSheldon Hearndepending on the value of
1747dfe302abSRuslan Ermilov.Va IFS
174849e11e3aSSheldon Hearnand the quoting that is in effect.
1749ef89d04fSJilles TjoelkerThe command is executed in a subshell environment,
1750ef89d04fSJilles Tjoelkerexcept that the built-in commands
1751ef89d04fSJilles Tjoelker.Ic jobid ,
1752ef89d04fSJilles Tjoelker.Ic jobs ,
1753ef89d04fSJilles Tjoelkerand
1754ef89d04fSJilles Tjoelker.Ic trap
175551a514adSJilles Tjoelkerreturn information about the parent shell environment
175651a514adSJilles Tjoelkerand
175751a514adSJilles Tjoelker.Ic times
175851a514adSJilles Tjoelkerreturns information about the same process
1759c543e1aeSJilles Tjoelkerif they are the only command in a command substitution.
176054ccc8b5SJilles Tjoelker.Pp
176154ccc8b5SJilles TjoelkerIf a command substitution of the
176254ccc8b5SJilles Tjoelker.Li $(
176354ccc8b5SJilles Tjoelkerform begins with a subshell,
176454ccc8b5SJilles Tjoelkerthe
176554ccc8b5SJilles Tjoelker.Li $(
176654ccc8b5SJilles Tjoelkerand
17675127efa3SJoel Dahl.Li (\&
176854ccc8b5SJilles Tjoelkermust be separated by whitespace
176954ccc8b5SJilles Tjoelkerto avoid ambiguity with arithmetic expansion.
1770ab72124fSSteve Price.Ss Arithmetic Expansion
17714b88c807SRodney W. GrimesArithmetic expansion provides a mechanism for evaluating an arithmetic
1772e6d3cf26SSheldon Hearnexpression and substituting its value.
1773e6d3cf26SSheldon HearnThe format for arithmetic expansion is as follows:
1774ab72124fSSteve Price.Pp
1775dfe302abSRuslan Ermilov.D1 Li $(( Ns Ar expression Ns Li ))
1776dfe302abSRuslan Ermilov.Pp
1777dfe302abSRuslan ErmilovThe
1778dfe302abSRuslan Ermilov.Ar expression
1779dfe302abSRuslan Ermilovis treated as if it were in double-quotes, except
1780eccea571SRuslan Ermilovthat a double-quote inside the expression is not treated specially.
1781eccea571SRuslan ErmilovThe
1782dfe302abSRuslan Ermilovshell expands all tokens in the
1783dfe302abSRuslan Ermilov.Ar expression
1784dfe302abSRuslan Ermilovfor parameter expansion,
178536cf3efeSJilles Tjoelkercommand substitution,
178636cf3efeSJilles Tjoelkerarithmetic expansion
178736cf3efeSJilles Tjoelkerand quote removal.
1788ab72124fSSteve Price.Pp
178936cf3efeSJilles TjoelkerThe allowed expressions are a subset of C expressions,
179036cf3efeSJilles Tjoelkersummarized below.
179136cf3efeSJilles Tjoelker.Bl -tag -width "Variables" -offset indent
179236cf3efeSJilles Tjoelker.It Values
179336cf3efeSJilles TjoelkerAll values are of type
179436cf3efeSJilles Tjoelker.Ft intmax_t .
179536cf3efeSJilles Tjoelker.It Constants
179636cf3efeSJilles TjoelkerDecimal, octal (starting with
179736cf3efeSJilles Tjoelker.Li 0 )
179836cf3efeSJilles Tjoelkerand hexadecimal (starting with
179936cf3efeSJilles Tjoelker.Li 0x )
180036cf3efeSJilles Tjoelkerinteger constants.
180136cf3efeSJilles Tjoelker.It Variables
180236cf3efeSJilles TjoelkerShell variables can be read and written
180336cf3efeSJilles Tjoelkerand contain integer constants.
180436cf3efeSJilles Tjoelker.It Unary operators
180536cf3efeSJilles Tjoelker.Li "! ~ + -"
180636cf3efeSJilles Tjoelker.It Binary operators
180717e98da8SMateusz Piotrowski.Li "* / % + - << >> < <= > >= == != & ^ | && ||"\&
180836cf3efeSJilles Tjoelker.It Assignment operators
180936cf3efeSJilles Tjoelker.Li "= += -= *= /= %= <<= >>= &= ^= |="
1810075b72efSJilles Tjoelker.It Conditional operator
181117e98da8SMateusz Piotrowski.Li "? :"\&
181236cf3efeSJilles Tjoelker.El
181336cf3efeSJilles Tjoelker.Pp
181436cf3efeSJilles TjoelkerThe result of the expression is substituted in decimal.
1815ab72124fSSteve Price.Ss White Space Splitting (Field Splitting)
1816ad56ebdcSJilles TjoelkerIn certain contexts,
1817ad56ebdcSJilles Tjoelkerafter parameter expansion, command substitution, and
18184b88c807SRodney W. Grimesarithmetic expansion the shell scans the results of
18194b88c807SRodney W. Grimesexpansions and substitutions that did not occur in double-quotes for
18204b88c807SRodney W. Grimesfield splitting and multiple fields can result.
1821ab72124fSSteve Price.Pp
1822ad56ebdcSJilles TjoelkerCharacters in
1823dfe302abSRuslan Ermilov.Va IFS
1824ad56ebdcSJilles Tjoelkerthat are whitespace
1825ad56ebdcSJilles Tjoelker.Po
1826ad56ebdcSJilles Tjoelker.Aq space ,
1827ad56ebdcSJilles Tjoelker.Aq tab ,
1828ad56ebdcSJilles Tjoelkerand
1829ad56ebdcSJilles Tjoelker.Aq newline
1830ad56ebdcSJilles Tjoelker.Pc
1831ad56ebdcSJilles Tjoelkerare treated differently from other characters in
1832ad56ebdcSJilles Tjoelker.Va IFS .
1833ad56ebdcSJilles Tjoelker.Pp
1834ad56ebdcSJilles TjoelkerWhitespace in
1835ad56ebdcSJilles Tjoelker.Va IFS
1836ad56ebdcSJilles Tjoelkerat the beginning or end of a word is discarded.
1837ad56ebdcSJilles Tjoelker.Pp
1838ad56ebdcSJilles TjoelkerSubsequently, a field is delimited by either
1839ad56ebdcSJilles Tjoelker.Bl -enum
1840ad56ebdcSJilles Tjoelker.It
1841ad56ebdcSJilles Tjoelkera non-whitespace character in
1842ad56ebdcSJilles Tjoelker.Va IFS
1843ad56ebdcSJilles Tjoelkerwith any whitespace in
1844ad56ebdcSJilles Tjoelker.Va IFS
1845ad56ebdcSJilles Tjoelkersurrounding it, or
1846ad56ebdcSJilles Tjoelker.It
1847ad56ebdcSJilles Tjoelkerone or more whitespace characters in
1848ad56ebdcSJilles Tjoelker.Va IFS .
1849ad56ebdcSJilles Tjoelker.El
1850ad56ebdcSJilles Tjoelker.Pp
1851ad56ebdcSJilles TjoelkerIf a word ends with a non-whitespace character in
1852ad56ebdcSJilles Tjoelker.Va IFS ,
1853ad56ebdcSJilles Tjoelkerthere is no empty field after this character.
1854ad56ebdcSJilles Tjoelker.Pp
1855ad56ebdcSJilles TjoelkerIf no field is delimited, the word is discarded.
1856ad56ebdcSJilles TjoelkerIn particular, if a word consists solely of an unquoted substitution
1857ad56ebdcSJilles Tjoelkerand the result of the substitution is null,
1858ad56ebdcSJilles Tjoelkerit is removed by field splitting even if
1859ad56ebdcSJilles Tjoelker.Va IFS
1860ad56ebdcSJilles Tjoelkeris null.
1861ab72124fSSteve Price.Ss Pathname Expansion (File Name Generation)
186249e11e3aSSheldon HearnUnless the
186349e11e3aSSheldon Hearn.Fl f
186449e11e3aSSheldon Hearnoption is set,
186549e11e3aSSheldon Hearnfile name generation is performed
1866eccea571SRuslan Ermilovafter word splitting is complete.
1867eccea571SRuslan ErmilovEach word is
1868eccea571SRuslan Ermilovviewed as a series of patterns, separated by slashes.
1869eccea571SRuslan ErmilovThe
18704b88c807SRodney W. Grimesprocess of expansion replaces the word with the names of
18714b88c807SRodney W. Grimesall existing files whose names can be formed by replacing
18724b88c807SRodney W. Grimeseach pattern with a string that matches the specified pattern.
1873ab0a2172SSteve PriceThere are two restrictions on this: first, a pattern cannot match
1874ab0a2172SSteve Pricea string containing a slash, and second,
18754b88c807SRodney W. Grimesa pattern cannot match a string starting with a period
18764b88c807SRodney W. Grimesunless the first character of the pattern is a period.
1877519c4ef3SJilles TjoelkerThe next section describes the patterns used for
1878519c4ef3SJilles TjoelkerPathname Expansion,
1879519c4ef3SJilles Tjoelkerthe four varieties of parameter expansion for substring processing and the
1880c644db6aSSheldon Hearn.Ic case
1881ab72124fSSteve Pricecommand.
1882ab72124fSSteve Price.Ss Shell Patterns
1883f434d1dfSAdam DavidA pattern consists of normal characters, which match themselves,
188449e11e3aSSheldon Hearnand meta-characters.
188549e11e3aSSheldon HearnThe meta-characters are
1886dfe302abSRuslan Ermilov.Ql * ,
1887dfe302abSRuslan Ermilov.Ql \&? ,
18886b15476eSSheldon Hearnand
1889dfe302abSRuslan Ermilov.Ql \&[ .
18906b15476eSSheldon HearnThese characters lose their special meanings if they are quoted.
18916b15476eSSheldon HearnWhen command or variable substitution is performed and the dollar sign
18926b15476eSSheldon Hearnor back quotes are not double-quoted, the value of the
18934b88c807SRodney W. Grimesvariable or the output of the command is scanned for these
18944b88c807SRodney W. Grimescharacters and they are turned into meta-characters.
1895ab72124fSSteve Price.Pp
18966b15476eSSheldon HearnAn asterisk
1897a762afe5SGiorgos Keramidas.Pq Ql *
1898e918fc8fSSheldon Hearnmatches any string of characters.
1899e918fc8fSSheldon HearnA question mark
1900a762afe5SGiorgos Keramidas.Pq Ql \&?
1901e918fc8fSSheldon Hearnmatches any single character.
1902e6d3cf26SSheldon HearnA left bracket
1903dfe302abSRuslan Ermilov.Pq Ql \&[
1904e6d3cf26SSheldon Hearnintroduces a character class.
1905e6d3cf26SSheldon HearnThe end of the character class is indicated by a
1906dfe302abSRuslan Ermilov.Ql \&] ;
19076b15476eSSheldon Hearnif the
1908dfe302abSRuslan Ermilov.Ql \&]
19096b15476eSSheldon Hearnis missing then the
1910dfe302abSRuslan Ermilov.Ql \&[
19116b15476eSSheldon Hearnmatches a
1912dfe302abSRuslan Ermilov.Ql \&[
1913e6d3cf26SSheldon Hearnrather than introducing a character class.
1914e6d3cf26SSheldon HearnA character class matches any of the characters between the square brackets.
1915519c4ef3SJilles TjoelkerA locale-dependent range of characters may be specified using a minus sign.
1916ff4dc672SJilles TjoelkerA named class of characters (see
1917ff4dc672SJilles Tjoelker.Xr wctype 3 )
1918ff4dc672SJilles Tjoelkermay be specified by surrounding the name with
191917e98da8SMateusz Piotrowski.Ql \&[:\&
1920ff4dc672SJilles Tjoelkerand
1921ff4dc672SJilles Tjoelker.Ql :\&] .
1922ff4dc672SJilles TjoelkerFor example,
1923ff4dc672SJilles Tjoelker.Ql \&[\&[:alpha:\&]\&]
1924ff4dc672SJilles Tjoelkeris a shell pattern that matches a single letter.
1925e918fc8fSSheldon HearnThe character class may be complemented by making an exclamation point
1926a762afe5SGiorgos Keramidas.Pq Ql !\&
1927e918fc8fSSheldon Hearnthe first character of the character class.
1928519c4ef3SJilles TjoelkerA caret
1929519c4ef3SJilles Tjoelker.Pq Ql ^
1930519c4ef3SJilles Tjoelkerhas the same effect but is non-standard.
1931ab72124fSSteve Price.Pp
19326b15476eSSheldon HearnTo include a
1933dfe302abSRuslan Ermilov.Ql \&]
1934e6d3cf26SSheldon Hearnin a character class, make it the first character listed
1935e6d3cf26SSheldon Hearn(after the
1936519c4ef3SJilles Tjoelker.Ql \&!
1937519c4ef3SJilles Tjoelkeror
1938519c4ef3SJilles Tjoelker.Ql ^ ,
1939e6d3cf26SSheldon Hearnif any).
1940e918fc8fSSheldon HearnTo include a
1941dfe302abSRuslan Ermilov.Ql - ,
1942e918fc8fSSheldon Hearnmake it the first or last character listed.
19438eed22b1SJens Schweikhardt.Ss Built-in Commands
194425f6b31fSJilles TjoelkerThis section lists the built-in commands.
194581e9cda2SRuslan Ermilov.Bl -tag -width indent
1946f4727ecaSAlexander Ziaee.It Ic \&: Op Ar arg Ar ...
1947aa9caaf6SPeter WemmA null command that returns a 0 (true) exit value.
1948f4727ecaSAlexander ZiaeeAny arguments are discarded.
1949d0353b83SRuslan Ermilov.It Ic \&. Ar file
1950aa9caaf6SPeter WemmThe commands in the specified file are read and executed by the shell.
19514ad8968aSBrian FeldmanThe
19524ad8968aSBrian Feldman.Ic return
19534ad8968aSBrian Feldmancommand may be used to return to the
19544ad8968aSBrian Feldman.Ic \&.
19554ad8968aSBrian Feldmancommand's caller.
19566466945aSJohn PolstraIf
19576466945aSJohn Polstra.Ar file
19586466945aSJohn Polstracontains any
1959dfe302abSRuslan Ermilov.Ql /
1960eccea571SRuslan Ermilovcharacters, it is used as is.
1961eccea571SRuslan ErmilovOtherwise, the shell searches the
19621788b7ffSJilles Tjoelker.Va PATH
1963eccea571SRuslan Ermilovfor the file.
1964eccea571SRuslan ErmilovIf it is not found in the
19651788b7ffSJilles Tjoelker.Va PATH ,
19666466945aSJohn Polstrait is sought in the current working directory.
196710845922SYaroslav Tykhiy.It Ic \&[
196810845922SYaroslav TykhiyA built-in equivalent of
196910845922SYaroslav Tykhiy.Xr test 1 .
1970f4727ecaSAlexander Ziaee.It Ic alias Oo Ar name Ns Oo = Ns Ar string Oc Ar ... Oc
197149e11e3aSSheldon HearnIf
1972d0353b83SRuslan Ermilov.Ar name Ns = Ns Ar string
197349e11e3aSSheldon Hearnis specified, the shell defines the alias
197449e11e3aSSheldon Hearn.Ar name
19756b15476eSSheldon Hearnwith value
197649e11e3aSSheldon Hearn.Ar string .
19776b15476eSSheldon HearnIf just
197849e11e3aSSheldon Hearn.Ar name
19796b15476eSSheldon Hearnis specified, the value of the alias
198049e11e3aSSheldon Hearn.Ar name
19816b15476eSSheldon Hearnis printed.
198249e11e3aSSheldon HearnWith no arguments, the
198349e11e3aSSheldon Hearn.Ic alias
19848eed22b1SJens Schweikhardtbuilt-in command prints the names and values of all defined aliases
198549e11e3aSSheldon Hearn(see
198649e11e3aSSheldon Hearn.Ic unalias ) .
1987e5341cbbSTim J. RobbinsAlias values are written with appropriate quoting so that they are
19889ba31ca0SRuslan Ermilovsuitable for re-input to the shell.
1989a886288eSJesus R. CamouAlso see the
1990a886288eSJesus R. Camou.Sx Aliases
1991a886288eSJesus R. Camousubsection.
199249e11e3aSSheldon Hearn.It Ic bg Op Ar job ...
199349e11e3aSSheldon HearnContinue the specified jobs
199449e11e3aSSheldon Hearn(or the current job if no jobs are given)
199549e11e3aSSheldon Hearnin the background.
199625f6b31fSJilles Tjoelker.It Ic bind Oo Fl aeklrsv Oc Oo Ar key Oo Ar command Oc Oc
199725f6b31fSJilles TjoelkerList or alter key bindings for the line editor.
199825f6b31fSJilles TjoelkerThis command is documented in
199925f6b31fSJilles Tjoelker.Xr editrc 5 .
200025f6b31fSJilles Tjoelker.It Ic break Op Ar num
200125f6b31fSJilles TjoelkerSee the
200225f6b31fSJilles Tjoelker.Sx Flow-Control Constructs
200325f6b31fSJilles Tjoelkersubsection.
2004df9e587bSTim J. Robbins.It Ic builtin Ar cmd Op Ar arg ...
20058eed22b1SJens SchweikhardtExecute the specified built-in command,
200649e11e3aSSheldon Hearn.Ar cmd .
200749e11e3aSSheldon HearnThis is useful when the user wishes to override a shell function
20088eed22b1SJens Schweikhardtwith the same name as a built-in command.
2009d6ee26adSJilles Tjoelker.It Ic cd Oo Fl L | P Oc Oo Fl e Oc Op Ar directory
20107b6779b6SJilles Tjoelker.It Ic cd Fl
201149e11e3aSSheldon HearnSwitch to the specified
201249e11e3aSSheldon Hearn.Ar directory ,
20137b6779b6SJilles Tjoelkerto the directory specified in the
20141788b7ffSJilles Tjoelker.Va HOME
201549e11e3aSSheldon Hearnenvironment variable if no
201649e11e3aSSheldon Hearn.Ar directory
20177b6779b6SJilles Tjoelkeris specified or
20187b6779b6SJilles Tjoelkerto the directory specified in the
20197b6779b6SJilles Tjoelker.Va OLDPWD
20207b6779b6SJilles Tjoelkerenvironment variable if
20217b6779b6SJilles Tjoelker.Ar directory
20227b6779b6SJilles Tjoelkeris
20237b6779b6SJilles Tjoelker.Fl .
202449e11e3aSSheldon HearnIf
202549e11e3aSSheldon Hearn.Ar directory
2026029c8ee4STim J. Robbinsdoes not begin with
20279ba31ca0SRuslan Ermilov.Pa / , \&. ,
202849e11e3aSSheldon Hearnor
2029029c8ee4STim J. Robbins.Pa .. ,
203049e11e3aSSheldon Hearnthen the directories listed in the
20311788b7ffSJilles Tjoelker.Va CDPATH
203249e11e3aSSheldon Hearnvariable will be
203349e11e3aSSheldon Hearnsearched for the specified
203449e11e3aSSheldon Hearn.Ar directory .
2035029c8ee4STim J. RobbinsIf
20361788b7ffSJilles Tjoelker.Va CDPATH
2037029c8ee4STim J. Robbinsis unset, the current directory is searched.
203849e11e3aSSheldon HearnThe format of
20391788b7ffSJilles Tjoelker.Va CDPATH
204049e11e3aSSheldon Hearnis the same as that of
20411788b7ffSJilles Tjoelker.Va PATH .
2042e6d3cf26SSheldon HearnIn an interactive shell,
204349e11e3aSSheldon Hearnthe
204449e11e3aSSheldon Hearn.Ic cd
204549e11e3aSSheldon Hearncommand will print out the name of the directory
2046e6d3cf26SSheldon Hearnthat it actually switched to
20477b6779b6SJilles Tjoelkerif the
20481788b7ffSJilles Tjoelker.Va CDPATH
20497b6779b6SJilles Tjoelkermechanism was used or if
20507b6779b6SJilles Tjoelker.Ar directory
20517b6779b6SJilles Tjoelkerwas
20527b6779b6SJilles Tjoelker.Fl .
2053178897f1STim J. Robbins.Pp
2054178897f1STim J. RobbinsIf the
2055178897f1STim J. Robbins.Fl P
2056178897f1STim J. Robbinsoption is specified,
205781e9cda2SRuslan Ermilov.Pa ..
2058178897f1STim J. Robbinsis handled physically and symbolic links are resolved before
205981e9cda2SRuslan Ermilov.Pa ..
2060178897f1STim J. Robbinscomponents are processed.
2061178897f1STim J. RobbinsIf the
2062178897f1STim J. Robbins.Fl L
2063178897f1STim J. Robbinsoption is specified,
206481e9cda2SRuslan Ermilov.Pa ..
2065178897f1STim J. Robbinsis handled logically.
2066178897f1STim J. RobbinsThis is the default.
2067d6ee26adSJilles Tjoelker.Pp
2068d6ee26adSJilles TjoelkerThe
2069d6ee26adSJilles Tjoelker.Fl e
2070d6ee26adSJilles Tjoelkeroption causes
2071d6ee26adSJilles Tjoelker.Ic cd
2072d6ee26adSJilles Tjoelkerto return exit status 1 if the full pathname of the new directory
2073d6ee26adSJilles Tjoelkercannot be determined reliably or at all.
2074d6ee26adSJilles TjoelkerNormally this is not considered an error,
2075d6ee26adSJilles Tjoelkeralthough a warning is printed.
20766f49cd26SJilles Tjoelker.Pp
20776f49cd26SJilles TjoelkerIf changing the directory fails, the exit status is greater than 1.
20786f49cd26SJilles TjoelkerIf the directory is changed, the exit status is 0, or also 1 if
20796f49cd26SJilles Tjoelker.Fl e
20806f49cd26SJilles Tjoelkerwas given.
20819434a1c7SDima Dorfman.It Ic chdir
20829434a1c7SDima DorfmanA synonym for the
20839434a1c7SDima Dorfman.Ic cd
20848eed22b1SJens Schweikhardtbuilt-in command.
20859ba31ca0SRuslan Ermilov.It Ic command Oo Fl p Oc Op Ar utility Op Ar argument ...
2086b97d13b3SJilles Tjoelker.It Ic command Oo Fl p Oc Fl v Ar utility
2087b97d13b3SJilles Tjoelker.It Ic command Oo Fl p Oc Fl V Ar utility
208833b222b9SStefan FarfelederThe first form of invocation executes the specified
2089c848bc18SJilles Tjoelker.Ar utility ,
2090c848bc18SJilles Tjoelkerignoring shell functions in the search.
2091c848bc18SJilles TjoelkerIf
20922babaf74STim J. Robbins.Ar utility
2093c848bc18SJilles Tjoelkeris a special builtin,
2094c848bc18SJilles Tjoelkerit is executed as if it were a regular builtin.
20952babaf74STim J. Robbins.Pp
20962babaf74STim J. RobbinsIf the
20972babaf74STim J. Robbins.Fl p
20982babaf74STim J. Robbinsoption is specified, the command search is performed using a
20992babaf74STim J. Robbinsdefault value of
21001788b7ffSJilles Tjoelker.Va PATH
21012babaf74STim J. Robbinsthat is guaranteed to find all of the standard utilities.
210233b222b9SStefan Farfeleder.Pp
210333b222b9SStefan FarfelederIf the
210433b222b9SStefan Farfeleder.Fl v
210533b222b9SStefan Farfelederoption is specified,
210633b222b9SStefan Farfeleder.Ar utility
210733b222b9SStefan Farfelederis not executed but a description of its interpretation by the shell is
210833b222b9SStefan Farfelederprinted.
210933b222b9SStefan FarfelederFor ordinary commands the output is the path name; for shell built-in
211033b222b9SStefan Farfeledercommands, shell functions and keywords only the name is written.
211133b222b9SStefan FarfelederAliases are printed as
211233b222b9SStefan Farfeleder.Dq Ic alias Ar name Ns = Ns Ar value .
211333b222b9SStefan Farfeleder.Pp
211433b222b9SStefan FarfelederThe
211533b222b9SStefan Farfeleder.Fl V
211633b222b9SStefan Farfelederoption is identical to
211733b222b9SStefan Farfeleder.Fl v
211833b222b9SStefan Farfelederexcept for the output.
211933b222b9SStefan FarfelederIt prints
212033b222b9SStefan Farfeleder.Dq Ar utility Ic is Ar description
212133b222b9SStefan Farfelederwhere
212233b222b9SStefan Farfeleder.Ar description
212333b222b9SStefan Farfelederis either
212433b222b9SStefan Farfelederthe path name to
212533b222b9SStefan Farfeleder.Ar utility ,
212630268dfaSJilles Tjoelkera special shell builtin,
21278af11131SRuslan Ermilova shell builtin,
21288af11131SRuslan Ermilova shell function,
21298af11131SRuslan Ermilova shell keyword
213033b222b9SStefan Farfelederor
21318af11131SRuslan Ermilovan alias for
21328af11131SRuslan Ermilov.Ar value .
213325f6b31fSJilles Tjoelker.It Ic continue Op Ar num
213425f6b31fSJilles TjoelkerSee the
213525f6b31fSJilles Tjoelker.Sx Flow-Control Constructs
213625f6b31fSJilles Tjoelkersubsection.
213773313421SStefan Farfeleder.It Ic echo Oo Fl e | n Oc Op Ar string ...
213873313421SStefan FarfelederPrint a space-separated list of the arguments to the standard output
213973313421SStefan Farfelederand append a newline character.
214081e9cda2SRuslan Ermilov.Bl -tag -width indent
2141501e74b7SMark Ovens.It Fl n
2142501e74b7SMark OvensSuppress the output of the trailing newline.
2143501e74b7SMark Ovens.It Fl e
2144501e74b7SMark OvensProcess C-style backslash escape sequences.
2145dfe302abSRuslan ErmilovThe
2146501e74b7SMark Ovens.Ic echo
2147dfe302abSRuslan Ermilovcommand understands the following character escapes:
214881e9cda2SRuslan Ermilov.Bl -tag -width indent
2149501e74b7SMark Ovens.It \ea
2150501e74b7SMark OvensAlert (ring the terminal bell)
2151501e74b7SMark Ovens.It \eb
2152501e74b7SMark OvensBackspace
2153501e74b7SMark Ovens.It \ec
2154501e74b7SMark OvensSuppress the trailing newline (this has the side-effect of truncating the
2155501e74b7SMark Ovensline if it is not the last character)
2156501e74b7SMark Ovens.It \ee
215717e98da8SMateusz PiotrowskiThe ESC character (ASCII 0x1b)
2158501e74b7SMark Ovens.It \ef
2159501e74b7SMark OvensFormfeed
2160501e74b7SMark Ovens.It \en
2161501e74b7SMark OvensNewline
2162501e74b7SMark Ovens.It \er
2163501e74b7SMark OvensCarriage return
2164501e74b7SMark Ovens.It \et
2165501e74b7SMark OvensHorizontal tab
2166501e74b7SMark Ovens.It \ev
2167501e74b7SMark OvensVertical tab
2168501e74b7SMark Ovens.It \e\e
2169501e74b7SMark OvensLiteral backslash
2170501e74b7SMark Ovens.It \e0nnn
2171dfe302abSRuslan Ermilov(Zero) The character whose octal value is
2172dfe302abSRuslan Ermilov.Ar nnn
2173501e74b7SMark Ovens.El
2174501e74b7SMark Ovens.Pp
2175501e74b7SMark OvensIf
2176501e74b7SMark Ovens.Ar string
2177501e74b7SMark Ovensis not enclosed in quotes then the backslash itself must be escaped
2178eccea571SRuslan Ermilovwith a backslash to protect it from the shell.
2179eccea571SRuslan ErmilovFor example
2180501e74b7SMark Ovens.Bd -literal -offset indent
2181501e74b7SMark Ovens$ echo -e "a\evb"
2182501e74b7SMark Ovensa
2183501e74b7SMark Ovens b
2184501e74b7SMark Ovens$ echo -e a\e\evb
2185501e74b7SMark Ovensa
2186501e74b7SMark Ovens b
2187501e74b7SMark Ovens$ echo -e "a\e\eb"
2188501e74b7SMark Ovensa\eb
2189501e74b7SMark Ovens$ echo -e a\e\e\e\eb
2190501e74b7SMark Ovensa\eb
2191501e74b7SMark Ovens.Ed
2192501e74b7SMark Ovens.El
2193558175caSTim J. Robbins.Pp
2194558175caSTim J. RobbinsOnly one of the
2195558175caSTim J. Robbins.Fl e
2196558175caSTim J. Robbinsand
2197558175caSTim J. Robbins.Fl n
2198558175caSTim J. Robbinsoptions may be specified.
219949e11e3aSSheldon Hearn.It Ic eval Ar string ...
2200e6d3cf26SSheldon HearnConcatenate all the arguments with spaces.
2201e6d3cf26SSheldon HearnThen re-parse and execute the command.
220249e11e3aSSheldon Hearn.It Ic exec Op Ar command Op arg ...
220349e11e3aSSheldon HearnUnless
220449e11e3aSSheldon Hearn.Ar command
220549e11e3aSSheldon Hearnis omitted,
220649e11e3aSSheldon Hearnthe shell process is replaced with the specified program
22078eed22b1SJens Schweikhardt(which must be a real program, not a shell built-in command or function).
220849e11e3aSSheldon HearnAny redirections on the
220949e11e3aSSheldon Hearn.Ic exec
221049e11e3aSSheldon Hearncommand are marked as permanent,
221149e11e3aSSheldon Hearnso that they are not undone when the
221249e11e3aSSheldon Hearn.Ic exec
221349e11e3aSSheldon Hearncommand finishes.
221449e11e3aSSheldon Hearn.It Ic exit Op Ar exitstatus
221549e11e3aSSheldon HearnTerminate the shell process.
221649e11e3aSSheldon HearnIf
221749e11e3aSSheldon Hearn.Ar exitstatus
221849e11e3aSSheldon Hearnis given
2219ea381e69SJilles Tjoelkerit is used as the exit status of the shell.
2220ea381e69SJilles TjoelkerOtherwise, if the shell is executing an
2221421fb021SJilles Tjoelker.Cm EXIT
2222421fb021SJilles Tjoelkertrap, the exit status of the last command before the trap is used;
2223421fb021SJilles Tjoelkerif the shell is executing a trap for a signal,
2224ea381e69SJilles Tjoelkerthe shell exits by resending the signal to itself.
2225ea381e69SJilles TjoelkerOtherwise, the exit status of the preceding command is used.
2226a68fbc44SJilles TjoelkerThe exit status should be an integer between 0 and 255.
2227ef9791a3SStefan Farfeleder.It Ic export Ar name ...
2228ef9791a3SStefan Farfeleder.It Ic export Op Fl p
22294b88c807SRodney W. GrimesThe specified names are exported so that they will
22304b88c807SRodney W. Grimesappear in the environment of subsequent commands.
223149e11e3aSSheldon HearnThe only way to un-export a variable is to
223249e11e3aSSheldon Hearn.Ic unset
223349e11e3aSSheldon Hearnit.
223449e11e3aSSheldon HearnThe shell allows the value of a variable to be set
223549e11e3aSSheldon Hearnat the same time as it is exported by writing
2236ab72124fSSteve Price.Pp
2237dfe302abSRuslan Ermilov.D1 Ic export Ar name Ns = Ns Ar value
2238dfe302abSRuslan Ermilov.Pp
2239dfe302abSRuslan ErmilovWith no arguments the
2240dfe302abSRuslan Ermilov.Ic export
2241dfe302abSRuslan Ermilovcommand lists the names
22424b88c807SRodney W. Grimesof all exported variables.
224345086f8cSTim J. RobbinsIf the
224445086f8cSTim J. Robbins.Fl p
224545086f8cSTim J. Robbinsoption is specified, the exported variables are printed as
224645086f8cSTim J. Robbins.Dq Ic export Ar name Ns = Ns Ar value
224745086f8cSTim J. Robbinslines, suitable for re-input to the shell.
2248a6557dcbSYaroslav Tykhiy.It Ic false
2249a6557dcbSYaroslav TykhiyA null command that returns a non-zero (false) exit value.
225081e9cda2SRuslan Ermilov.It Ic fc Oo Fl e Ar editor Oc Op Ar first Op Ar last
225181e9cda2SRuslan Ermilov.It Ic fc Fl l Oo Fl nr Oc Op Ar first Op Ar last
225281e9cda2SRuslan Ermilov.It Ic fc Fl s Oo Ar old Ns = Ns Ar new Oc Op Ar first
225349e11e3aSSheldon HearnThe
225449e11e3aSSheldon Hearn.Ic fc
22558eed22b1SJens Schweikhardtbuilt-in command lists, or edits and re-executes,
225649e11e3aSSheldon Hearncommands previously entered to an interactive shell.
225781e9cda2SRuslan Ermilov.Bl -tag -width indent
225849e11e3aSSheldon Hearn.It Fl e Ar editor
225949e11e3aSSheldon HearnUse the editor named by
226049e11e3aSSheldon Hearn.Ar editor
226149e11e3aSSheldon Hearnto edit the commands.
2262dfe302abSRuslan ErmilovThe
2263dfe302abSRuslan Ermilov.Ar editor
2264dfe302abSRuslan Ermilovstring is a command name,
226549e11e3aSSheldon Hearnsubject to search via the
22661788b7ffSJilles Tjoelker.Va PATH
226749e11e3aSSheldon Hearnvariable.
226849e11e3aSSheldon HearnThe value in the
22691788b7ffSJilles Tjoelker.Va FCEDIT
227049e11e3aSSheldon Hearnvariable is used as a default when
227149e11e3aSSheldon Hearn.Fl e
227249e11e3aSSheldon Hearnis not specified.
227349e11e3aSSheldon HearnIf
22741788b7ffSJilles Tjoelker.Va FCEDIT
227549e11e3aSSheldon Hearnis null or unset, the value of the
22761788b7ffSJilles Tjoelker.Va EDITOR
227749e11e3aSSheldon Hearnvariable is used.
227849e11e3aSSheldon HearnIf
22791788b7ffSJilles Tjoelker.Va EDITOR
228049e11e3aSSheldon Hearnis null or unset,
2281ab72124fSSteve Price.Xr ed 1
2282ab72124fSSteve Priceis used as the editor.
228349e11e3aSSheldon Hearn.It Fl l No (ell)
22844b88c807SRodney W. GrimesList the commands rather than invoking
2285eccea571SRuslan Ermilovan editor on them.
2286eccea571SRuslan ErmilovThe commands are written in the
2287dfe302abSRuslan Ermilovsequence indicated by the
2288dfe302abSRuslan Ermilov.Ar first
2289dfe302abSRuslan Ermilovand
2290dfe302abSRuslan Ermilov.Ar last
2291dfe302abSRuslan Ermilovoperands, as affected by
229249e11e3aSSheldon Hearn.Fl r ,
229349e11e3aSSheldon Hearnwith each command preceded by the command number.
229449e11e3aSSheldon Hearn.It Fl n
229549e11e3aSSheldon HearnSuppress command numbers when listing with
229649e11e3aSSheldon Hearn.Fl l .
229749e11e3aSSheldon Hearn.It Fl r
229849e11e3aSSheldon HearnReverse the order of the commands listed
229949e11e3aSSheldon Hearn(with
230049e11e3aSSheldon Hearn.Fl l )
230149e11e3aSSheldon Hearnor edited
230249e11e3aSSheldon Hearn(with neither
230349e11e3aSSheldon Hearn.Fl l
230449e11e3aSSheldon Hearnnor
230549e11e3aSSheldon Hearn.Fl s ) .
230649e11e3aSSheldon Hearn.It Fl s
23074b88c807SRodney W. GrimesRe-execute the command without invoking an editor.
230849e11e3aSSheldon Hearn.It Ar first
230949e11e3aSSheldon Hearn.It Ar last
231049e11e3aSSheldon HearnSelect the commands to list or edit.
231149e11e3aSSheldon HearnThe number of previous commands that can be accessed
231249e11e3aSSheldon Hearnare determined by the value of the
23131788b7ffSJilles Tjoelker.Va HISTSIZE
2314d0353b83SRuslan Ermilovvariable.
231549e11e3aSSheldon HearnThe value of
231649e11e3aSSheldon Hearn.Ar first
231749e11e3aSSheldon Hearnor
231849e11e3aSSheldon Hearn.Ar last
231949e11e3aSSheldon Hearnor both are one of the following:
232081e9cda2SRuslan Ermilov.Bl -tag -width indent
2321dfe302abSRuslan Ermilov.It Oo Cm + Oc Ns Ar num
232249e11e3aSSheldon HearnA positive number representing a command number;
232349e11e3aSSheldon Hearncommand numbers can be displayed with the
232449e11e3aSSheldon Hearn.Fl l
232549e11e3aSSheldon Hearnoption.
2326dfe302abSRuslan Ermilov.It Fl Ar num
232749e11e3aSSheldon HearnA negative decimal number representing the
232849e11e3aSSheldon Hearncommand that was executed
232949e11e3aSSheldon Hearn.Ar num
233049e11e3aSSheldon Hearnof
233149e11e3aSSheldon Hearncommands previously.
2332dfe302abSRuslan ErmilovFor example, \-1 is the immediately previous command.
233349e11e3aSSheldon Hearn.It Ar string
233449e11e3aSSheldon HearnA string indicating the most recently entered command
233549e11e3aSSheldon Hearnthat begins with that string.
233649e11e3aSSheldon HearnIf the
2337dfe302abSRuslan Ermilov.Ar old Ns = Ns Ar new
233849e11e3aSSheldon Hearnoperand is not also specified with
233949e11e3aSSheldon Hearn.Fl s ,
234049e11e3aSSheldon Hearnthe string form of the first operand cannot contain an embedded equal sign.
234149e11e3aSSheldon Hearn.El
234249e11e3aSSheldon Hearn.El
234349e11e3aSSheldon Hearn.Pp
23441788b7ffSJilles TjoelkerThe following variables affect the execution of
234549e11e3aSSheldon Hearn.Ic fc :
23461788b7ffSJilles Tjoelker.Bl -tag -width ".Va HISTSIZE"
23471788b7ffSJilles Tjoelker.It Va FCEDIT
234838afdcbcSGiorgos KeramidasName of the editor to use for history editing.
23491788b7ffSJilles Tjoelker.It Va HISTSIZE
23503d7b5b93SPhilippe CharnierThe number of previous commands that are accessible.
2351ab72124fSSteve Price.El
235249e11e3aSSheldon Hearn.It Ic fg Op Ar job
235349e11e3aSSheldon HearnMove the specified
235449e11e3aSSheldon Hearn.Ar job
235549e11e3aSSheldon Hearnor the current job to the foreground.
2356dfe302abSRuslan Ermilov.It Ic getopts Ar optstring var
23572980318bSPiotr Pawel StefaniakParse command-line options and arguments.
23582980318bSPiotr Pawel StefaniakThe first argument
23592980318bSPiotr Pawel Stefaniak.Va optstring
23602980318bSPiotr Pawel Stefaniakshould be a series of letters, each possibly
2361ab0a2172SSteve Pricefollowed by a colon which indicates that the option takes an argument.
23622980318bSPiotr Pawel StefaniakThe specified variable
23632980318bSPiotr Pawel Stefaniak.Va var
23642980318bSPiotr Pawel Stefaniakis set to the parsed option.
2365eccea571SRuslan ErmilovThe index of
236649e11e3aSSheldon Hearnthe next argument is placed into the shell variable
2367dfe302abSRuslan Ermilov.Va OPTIND .
2368ab0a2172SSteve PriceIf an option takes an argument, it is placed into the shell variable
2369dfe302abSRuslan Ermilov.Va OPTARG .
23702980318bSPiotr Pawel Stefaniak.Pp
23712980318bSPiotr Pawel StefaniakIf the found character
23722980318bSPiotr Pawel Stefaniakis not specified by
23732980318bSPiotr Pawel Stefaniak.Va optstring
23742980318bSPiotr Pawel Stefaniakor if it is missing a required argument,
23752980318bSPiotr Pawel Stefaniakthe option is considered invalid and:
23762980318bSPiotr Pawel Stefaniak.Bl -offset indent
23772980318bSPiotr Pawel Stefaniak.It
23782980318bSPiotr Pawel StefaniakIf the first character of
23792980318bSPiotr Pawel Stefaniak.Va optstring
23802980318bSPiotr Pawel Stefaniakis not a colon then
23812980318bSPiotr Pawel Stefaniak.Va OPTARG
23822980318bSPiotr Pawel Stefaniakis unset,
2383dfe302abSRuslan Ermilov.Ar var
238449e11e3aSSheldon Hearnis set to
23852980318bSPiotr Pawel Stefaniak.Ql \&?
23862980318bSPiotr Pawel Stefaniakand a diagnostic message is written to stderr.
23872980318bSPiotr Pawel Stefaniak.It
23882980318bSPiotr Pawel StefaniakIf the first character of
23892980318bSPiotr Pawel Stefaniak.Va optstring
23902980318bSPiotr Pawel Stefaniakis a colon then
23912980318bSPiotr Pawel Stefaniak.Va OPTARG
23922980318bSPiotr Pawel Stefaniakis set to the the option character found,
23932980318bSPiotr Pawel Stefaniak.Ar var
23942980318bSPiotr Pawel Stefaniakis set to
23952980318bSPiotr Pawel Stefaniak.Ql \&:
23962980318bSPiotr Pawel Stefaniakwhen a required argument is missing or to
23972980318bSPiotr Pawel Stefaniak.Ql \&?
23982980318bSPiotr Pawel Stefaniakwhen the option was not specified by
23992980318bSPiotr Pawel Stefaniak.Va optstring ,
24002980318bSPiotr Pawel Stefaniakand no diagnostic message is written to stderr.
24012980318bSPiotr Pawel Stefaniak.El
24022980318bSPiotr Pawel Stefaniak.Pp
24032980318bSPiotr Pawel Stefaniak.Ic getopts
24042980318bSPiotr Pawel Stefaniakreturns a false value (1) when it encounters the end of the options.
24054445af21SJilles TjoelkerA new set of arguments may be parsed by assigning
24064445af21SJilles Tjoelker.Li OPTIND=1 .
24072980318bSPiotr Pawel StefaniakThe POSIX
24082980318bSPiotr Pawel Stefaniak.Ic getopts
24092980318bSPiotr Pawel Stefaniakcommand deprecates the older
24102980318bSPiotr Pawel Stefaniak.Xr getopt 1
24112980318bSPiotr Pawel Stefaniakcommand.
241281e9cda2SRuslan Ermilov.It Ic hash Oo Fl rv Oc Op Ar command ...
241349e11e3aSSheldon HearnThe shell maintains a hash table which remembers the locations of commands.
241449e11e3aSSheldon HearnWith no arguments whatsoever, the
241549e11e3aSSheldon Hearn.Ic hash
241649e11e3aSSheldon Hearncommand prints out the contents of this table.
2417ab72124fSSteve Price.Pp
241849e11e3aSSheldon HearnWith arguments, the
241949e11e3aSSheldon Hearn.Ic hash
242049e11e3aSSheldon Hearncommand removes each specified
242149e11e3aSSheldon Hearn.Ar command
242249e11e3aSSheldon Hearnfrom the hash table (unless they are functions) and then locates it.
242349e11e3aSSheldon HearnWith the
242449e11e3aSSheldon Hearn.Fl v
242549e11e3aSSheldon Hearnoption,
242649e11e3aSSheldon Hearn.Ic hash
242749e11e3aSSheldon Hearnprints the locations of the commands as it finds them.
242849e11e3aSSheldon HearnThe
242949e11e3aSSheldon Hearn.Fl r
243049e11e3aSSheldon Hearnoption causes the
243149e11e3aSSheldon Hearn.Ic hash
243249e11e3aSSheldon Hearncommand to delete all the entries in the hash table except for functions.
243349e11e3aSSheldon Hearn.It Ic jobid Op Ar job
2434dfe302abSRuslan ErmilovPrint the process IDs of the processes in the specified
243549e11e3aSSheldon Hearn.Ar job .
243649e11e3aSSheldon HearnIf the
243749e11e3aSSheldon Hearn.Ar job
243849e11e3aSSheldon Hearnargument is omitted, use the current job.
2439de37e41cSStefan Farfeleder.It Ic jobs Oo Fl lps Oc Op Ar job ...
2440ad8a0759STim J. RobbinsPrint information about the specified jobs, or all jobs if no
2441ad8a0759STim J. Robbins.Ar job
2442ad8a0759STim J. Robbinsargument is given.
2443ad8a0759STim J. RobbinsThe information printed includes job ID, status and command name.
2444ad8a0759STim J. Robbins.Pp
2445ad8a0759STim J. RobbinsIf the
2446ad8a0759STim J. Robbins.Fl l
2447ad8a0759STim J. Robbinsoption is specified, the PID of each job is also printed.
2448ad8a0759STim J. RobbinsIf the
2449de37e41cSStefan Farfeleder.Fl p
2450de37e41cSStefan Farfelederoption is specified, only the process IDs for the process group leaders
2451de37e41cSStefan Farfelederare printed, one per line.
2452de37e41cSStefan FarfelederIf the
2453ad8a0759STim J. Robbins.Fl s
2454de37e41cSStefan Farfelederoption is specified, only the PIDs of the job commands are printed, one per
2455de37e41cSStefan Farfelederline.
24560a62a9caSJilles Tjoelker.It Ic kill
24570a62a9caSJilles TjoelkerA built-in equivalent of
24580a62a9caSJilles Tjoelker.Xr kill 1
24590a62a9caSJilles Tjoelkerthat additionally supports sending signals to jobs.
24608af11131SRuslan Ermilov.It Ic local Oo Ar variable ... Oc Op Fl
2461d2f90294SJesus R. CamouSee the
2462d2f90294SJesus R. Camou.Sx Functions
2463d2f90294SJesus R. Camousubsection.
24649897c45fSJilles Tjoelker.It Ic printf
24659897c45fSJilles TjoelkerA built-in equivalent of
24669897c45fSJilles Tjoelker.Xr printf 1 .
2467d74e011fSRuslan Ermilov.It Ic pwd Op Fl L | P
2468eccea571SRuslan ErmilovPrint the path of the current directory.
2469eccea571SRuslan ErmilovThe built-in command may
24704b88c807SRodney W. Grimesdiffer from the program of the same name because the
24718eed22b1SJens Schweikhardtbuilt-in command remembers what the current directory
2472eccea571SRuslan Ermilovis rather than recomputing it each time.
2473eccea571SRuslan ErmilovThis makes
2474eccea571SRuslan Ermilovit faster.
2475eccea571SRuslan ErmilovHowever, if the current directory is
247649e11e3aSSheldon Hearnrenamed,
24778eed22b1SJens Schweikhardtthe built-in version of
247849e11e3aSSheldon Hearn.Xr pwd 1
247949e11e3aSSheldon Hearnwill continue to print the old name for the directory.
2480178897f1STim J. Robbins.Pp
2481178897f1STim J. RobbinsIf the
2482178897f1STim J. Robbins.Fl P
2483178897f1STim J. Robbinsoption is specified, symbolic links are resolved.
2484178897f1STim J. RobbinsIf the
2485178897f1STim J. Robbins.Fl L
2486178897f1STim J. Robbinsoption is specified, the shell's notion of the current directory
2487178897f1STim J. Robbinsis printed (symbolic links are not resolved).
2488178897f1STim J. RobbinsThis is the default.
2489dfe302abSRuslan Ermilov.It Ic read Oo Fl p Ar prompt Oc Oo
2490dfe302abSRuslan Ermilov.Fl t Ar timeout Oc Oo Fl er Oc Ar variable ...
249149e11e3aSSheldon HearnThe
249249e11e3aSSheldon Hearn.Ar prompt
249349e11e3aSSheldon Hearnis printed if the
249449e11e3aSSheldon Hearn.Fl p
249549e11e3aSSheldon Hearnoption is specified
2496eccea571SRuslan Ermilovand the standard input is a terminal.
2497eccea571SRuslan ErmilovThen a line is
2498eccea571SRuslan Ermilovread from the standard input.
2499eccea571SRuslan ErmilovThe trailing newline
25004b88c807SRodney W. Grimesis deleted from the line and the line is split as
250149e11e3aSSheldon Hearndescribed in the section on
250217e98da8SMateusz Piotrowski.Sx White Space Splitting (Field Splitting)\&
250349e11e3aSSheldon Hearnabove, and
25044b88c807SRodney W. Grimesthe pieces are assigned to the variables in order.
2505f434d1dfSAdam DavidIf there are more pieces than variables, the remaining
250649e11e3aSSheldon Hearnpieces (along with the characters in
2507dfe302abSRuslan Ermilov.Va IFS
250849e11e3aSSheldon Hearnthat separated them)
250949e11e3aSSheldon Hearnare assigned to the last variable.
2510f434d1dfSAdam DavidIf there are more variables than pieces, the remaining
2511f434d1dfSAdam Davidvariables are assigned the null string.
2512ab72124fSSteve Price.Pp
251349e11e3aSSheldon HearnBackslashes are treated specially, unless the
251449e11e3aSSheldon Hearn.Fl r
251549e11e3aSSheldon Hearnoption is
2516eccea571SRuslan Ermilovspecified.
2517eccea571SRuslan ErmilovIf a backslash is followed by
25188f0561ccSThomas Gellekuma newline, the backslash and the newline will be
2519eccea571SRuslan Ermilovdeleted.
2520eccea571SRuslan ErmilovIf a backslash is followed by any other
25218f0561ccSThomas Gellekumcharacter, the backslash will be deleted and the following
252249e11e3aSSheldon Hearncharacter will be treated as though it were not in
2523dfe302abSRuslan Ermilov.Va IFS ,
252449e11e3aSSheldon Hearneven if it is.
25258f0561ccSThomas Gellekum.Pp
252649e11e3aSSheldon HearnIf the
252749e11e3aSSheldon Hearn.Fl t
252849e11e3aSSheldon Hearnoption is specified and the
252949e11e3aSSheldon Hearn.Ar timeout
2530fe40d6d3SJilles Tjoelkerelapses before a complete line of input is supplied,
253149e11e3aSSheldon Hearnthe
253249e11e3aSSheldon Hearn.Ic read
2533c4539460SJilles Tjoelkercommand will return an exit status as if terminated by
2534c4539460SJilles Tjoelker.Dv SIGALRM
2535c4539460SJilles Tjoelkerwithout assigning any values.
253649e11e3aSSheldon HearnThe
253749e11e3aSSheldon Hearn.Ar timeout
253849e11e3aSSheldon Hearnvalue may optionally be followed by one of
2539dfe302abSRuslan Ermilov.Ql s ,
2540dfe302abSRuslan Ermilov.Ql m
254149e11e3aSSheldon Hearnor
2542dfe302abSRuslan Ermilov.Ql h
2543a910f192SDima Dorfmanto explicitly specify seconds, minutes or hours.
254449e11e3aSSheldon HearnIf none is supplied,
2545dfe302abSRuslan Ermilov.Ql s
254649e11e3aSSheldon Hearnis assumed.
2547afa53c8dSMike Smith.Pp
254849e11e3aSSheldon HearnThe
254949e11e3aSSheldon Hearn.Fl e
255049e11e3aSSheldon Hearnoption exists only for backward compatibility with older scripts.
2551c4539460SJilles Tjoelker.Pp
2552c4539460SJilles TjoelkerThe exit status is 0 on success, 1 on end of file,
2553c4539460SJilles Tjoelkerbetween 2 and 128 if an error occurs
2554c4539460SJilles Tjoelkerand greater than 128 if a trapped signal interrupts
2555c4539460SJilles Tjoelker.Ic read .
25569ba31ca0SRuslan Ermilov.It Ic readonly Oo Fl p Oc Op Ar name ...
255749e11e3aSSheldon HearnEach specified
255849e11e3aSSheldon Hearn.Ar name
255949e11e3aSSheldon Hearnis marked as read only,
256049e11e3aSSheldon Hearnso that it cannot be subsequently modified or unset.
256149e11e3aSSheldon HearnThe shell allows the value of a variable to be set
256249e11e3aSSheldon Hearnat the same time as it is marked read only
256349e11e3aSSheldon Hearnby using the following form:
2564dfe302abSRuslan Ermilov.Pp
2565dfe302abSRuslan Ermilov.D1 Ic readonly Ar name Ns = Ns Ar value
2566ab72124fSSteve Price.Pp
256749e11e3aSSheldon HearnWith no arguments the
256849e11e3aSSheldon Hearn.Ic readonly
256949e11e3aSSheldon Hearncommand lists the names of all read only variables.
257045086f8cSTim J. RobbinsIf the
257145086f8cSTim J. Robbins.Fl p
257245086f8cSTim J. Robbinsoption is specified, the read-only variables are printed as
257345086f8cSTim J. Robbins.Dq Ic readonly Ar name Ns = Ns Ar value
257445086f8cSTim J. Robbinslines, suitable for re-input to the shell.
2575d2f90294SJesus R. Camou.It Ic return Op Ar exitstatus
2576d2f90294SJesus R. CamouSee the
2577d2f90294SJesus R. Camou.Sx Functions
2578d2f90294SJesus R. Camousubsection.
25791becbc64SJilles Tjoelker.It Ic set Oo Fl /+abCEefIimnpTuVvx Oc Oo Fl /+o Ar longname
25801becbc64SJilles Tjoelker.Oc Op Fl - Ar arg ...
2581e918fc8fSSheldon HearnThe
2582e918fc8fSSheldon Hearn.Ic set
2583e918fc8fSSheldon Hearncommand performs three different functions:
2584ab72124fSSteve Price.Bl -item
2585ab72124fSSteve Price.It
258649e11e3aSSheldon HearnWith no arguments, it lists the values of all shell variables.
2587ab72124fSSteve Price.It
258849e11e3aSSheldon HearnIf options are given,
258949e11e3aSSheldon Hearneither in short form or using the long
259049e11e3aSSheldon Hearn.Dq Fl /+o Ar longname
259149e11e3aSSheldon Hearnform,
259249e11e3aSSheldon Hearnit sets or clears the specified options as described in the section called
25936b15476eSSheldon Hearn.Sx Argument List Processing .
2594ab72124fSSteve Price.It
259549e11e3aSSheldon HearnIf the
259649e11e3aSSheldon Hearn.Dq Fl -
259749e11e3aSSheldon Hearnoption is specified,
259849e11e3aSSheldon Hearn.Ic set
259949e11e3aSSheldon Hearnwill replace the shell's positional parameters with the subsequent
260049e11e3aSSheldon Hearnarguments.
260149e11e3aSSheldon HearnIf no arguments follow the
260249e11e3aSSheldon Hearn.Dq Fl -
260349e11e3aSSheldon Hearnoption,
260449e11e3aSSheldon Hearnall the positional parameters will be cleared,
260549e11e3aSSheldon Hearnwhich is equivalent to executing the command
2606dfe302abSRuslan Ermilov.Dq Li "shift $#" .
260749e11e3aSSheldon HearnThe
260849e11e3aSSheldon Hearn.Dq Fl -
26098eed22b1SJens Schweikhardtflag may be omitted when specifying arguments to be used
261049e11e3aSSheldon Hearnas positional replacement parameters.
261149e11e3aSSheldon HearnThis is not recommended,
261249e11e3aSSheldon Hearnbecause the first argument may begin with a dash
2613a762afe5SGiorgos Keramidas.Pq Ql -
261449e11e3aSSheldon Hearnor a plus
2615a762afe5SGiorgos Keramidas.Pq Ql + ,
261649e11e3aSSheldon Hearnwhich the
261749e11e3aSSheldon Hearn.Ic set
261849e11e3aSSheldon Hearncommand will interpret as a request to enable or disable options.
2619ab72124fSSteve Price.El
2620dfe302abSRuslan Ermilov.It Ic setvar Ar variable value
262149e11e3aSSheldon HearnAssigns the specified
262249e11e3aSSheldon Hearn.Ar value
262349e11e3aSSheldon Hearnto the specified
262449e11e3aSSheldon Hearn.Ar variable .
2625dfe302abSRuslan ErmilovThe
2626dfe302abSRuslan Ermilov.Ic setvar
2627dfe302abSRuslan Ermilovcommand is intended to be used in functions that
2628e6d3cf26SSheldon Hearnassign values to variables whose names are passed as parameters.
262949e11e3aSSheldon HearnIn general it is better to write
2630dfe302abSRuslan Ermilov.Dq Ar variable Ns = Ns Ar value
263149e11e3aSSheldon Hearnrather than using
263249e11e3aSSheldon Hearn.Ic setvar .
263349e11e3aSSheldon Hearn.It Ic shift Op Ar n
263449e11e3aSSheldon HearnShift the positional parameters
263549e11e3aSSheldon Hearn.Ar n
2636e918fc8fSSheldon Hearntimes, or once if
263749e11e3aSSheldon Hearn.Ar n
263849e11e3aSSheldon Hearnis not specified.
2639dfe302abSRuslan ErmilovA shift sets the value of
2640dfe302abSRuslan Ermilov.Li $1
2641dfe302abSRuslan Ermilovto the value of
2642dfe302abSRuslan Ermilov.Li $2 ,
2643dfe302abSRuslan Ermilovthe value of
2644dfe302abSRuslan Ermilov.Li $2
2645dfe302abSRuslan Ermilovto the value of
2646dfe302abSRuslan Ermilov.Li $3 ,
2647dfe302abSRuslan Ermilovand so on,
2648dfe302abSRuslan Ermilovdecreasing the value of
2649dfe302abSRuslan Ermilov.Li $#
2650dfe302abSRuslan Ermilovby one.
2651945e1562SJilles TjoelkerFor portability, shifting if there are zero positional parameters
2652945e1562SJilles Tjoelkershould be avoided, since the shell may abort.
265310845922SYaroslav Tykhiy.It Ic test
265410845922SYaroslav TykhiyA built-in equivalent of
265510845922SYaroslav Tykhiy.Xr test 1 .
26561974986aSStefan Farfeleder.It Ic times
265751a514adSJilles TjoelkerPrint the amount of time spent executing the shell process and its children.
265851a514adSJilles TjoelkerThe first output line shows the user and system times for the shell process
26591974986aSStefan Farfelederitself, the second one contains the user and system times for the
26601974986aSStefan Farfelederchildren.
266181e9cda2SRuslan Ermilov.It Ic trap Oo Ar action Oc Ar signal ...
26620673e800SStefan Farfeleder.It Ic trap Fl l
266349e11e3aSSheldon HearnCause the shell to parse and execute
266449e11e3aSSheldon Hearn.Ar action
266549e11e3aSSheldon Hearnwhen any specified
266649e11e3aSSheldon Hearn.Ar signal
266749e11e3aSSheldon Hearnis received.
2668162ae3aeSTony FinchThe signals are specified by name or number.
2669162ae3aeSTony FinchIn addition, the pseudo-signal
2670f5d1157fSRuslan Ermilov.Cm EXIT
2671dfe302abSRuslan Ermilovmay be used to specify an
2672dfe302abSRuslan Ermilov.Ar action
2673dfe302abSRuslan Ermilovthat is performed when the shell terminates.
267449e11e3aSSheldon HearnThe
267549e11e3aSSheldon Hearn.Ar action
26760673e800SStefan Farfeledermay be an empty string or a dash
26779badf57fSRuslan Ermilov.Pq Ql - ;
267849e11e3aSSheldon Hearnthe former causes the specified signal to be ignored
267949e11e3aSSheldon Hearnand the latter causes the default action to be taken.
26800673e800SStefan FarfelederOmitting the
26810673e800SStefan Farfeleder.Ar action
2682a59f8174SBryan Dreweryand using only signal numbers is another way to request the default action.
268351a514adSJilles TjoelkerIn a subshell or utility environment,
2684ef89d04fSJilles Tjoelkerthe shell resets trapped (but not ignored) signals to the default action.
268549e11e3aSSheldon HearnThe
268649e11e3aSSheldon Hearn.Ic trap
268749e11e3aSSheldon Hearncommand has no effect on signals that were ignored on entry to the shell.
26880673e800SStefan Farfeleder.Pp
26890673e800SStefan FarfelederOption
26900673e800SStefan Farfeleder.Fl l
26910673e800SStefan Farfeledercauses the
26920673e800SStefan Farfeleder.Ic trap
26930673e800SStefan Farfeledercommand to display a list of valid signal names.
2694a6557dcbSYaroslav Tykhiy.It Ic true
2695a6557dcbSYaroslav TykhiyA null command that returns a 0 (true) exit value.
269649e11e3aSSheldon Hearn.It Ic type Op Ar name ...
269749e11e3aSSheldon HearnInterpret each
269849e11e3aSSheldon Hearn.Ar name
269949e11e3aSSheldon Hearnas a command and print the resolution of the command search.
2700e6d3cf26SSheldon HearnPossible resolutions are:
270130268dfaSJilles Tjoelkershell keyword, alias, special shell builtin, shell builtin, command,
270230268dfaSJilles Tjoelkertracked alias
270349e11e3aSSheldon Hearnand not found.
270449e11e3aSSheldon HearnFor aliases the alias expansion is printed;
270549e11e3aSSheldon Hearnfor commands and tracked aliases
270649e11e3aSSheldon Hearnthe complete pathname of the command is printed.
2707250d9fd8SKonstantin Belousov.It Ic ulimit Oo Fl HSabcdfklmnopstuvw Oc Op Ar limit
2708ab72124fSSteve PriceSet or display resource limits (see
2709ab72124fSSteve Price.Xr getrlimit 2 ) .
27106b15476eSSheldon HearnIf
271149e11e3aSSheldon Hearn.Ar limit
27126b15476eSSheldon Hearnis specified, the named resource will be set;
27137a2afe64SJoerg Wunschotherwise the current resource value will be displayed.
2714ab72124fSSteve Price.Pp
27156b15476eSSheldon HearnIf
271649e11e3aSSheldon Hearn.Fl H
271749e11e3aSSheldon Hearnis specified, the hard limits will be set or displayed.
271849e11e3aSSheldon HearnWhile everybody is allowed to reduce a hard limit,
271949e11e3aSSheldon Hearnonly the superuser can increase it.
272049e11e3aSSheldon HearnThe
272149e11e3aSSheldon Hearn.Fl S
272249e11e3aSSheldon Hearnoption
2723eccea571SRuslan Ermilovspecifies the soft limits instead.
2724eccea571SRuslan ErmilovWhen displaying limits,
27256b15476eSSheldon Hearnonly one of
272649e11e3aSSheldon Hearn.Fl S
27276b15476eSSheldon Hearnor
272849e11e3aSSheldon Hearn.Fl H
272949e11e3aSSheldon Hearncan be given.
273049e11e3aSSheldon HearnThe default is to display the soft limits,
273149e11e3aSSheldon Hearnand to set both the hard and the soft limits.
2732ab72124fSSteve Price.Pp
27336b15476eSSheldon HearnOption
273449e11e3aSSheldon Hearn.Fl a
273549e11e3aSSheldon Hearncauses the
273649e11e3aSSheldon Hearn.Ic ulimit
273749e11e3aSSheldon Hearncommand to display all resources.
273849e11e3aSSheldon HearnThe parameter
273949e11e3aSSheldon Hearn.Ar limit
27406b15476eSSheldon Hearnis not acceptable in this mode.
2741ab72124fSSteve Price.Pp
27427a2afe64SJoerg WunschThe remaining options specify which resource value is to be
274349e11e3aSSheldon Hearndisplayed or modified.
274449e11e3aSSheldon HearnThey are mutually exclusive.
274581e9cda2SRuslan Ermilov.Bl -tag -width indent
27463652a236SMartin Cracauer.It Fl b Ar sbsize
27473652a236SMartin CracauerThe maximum size of socket buffer usage, in bytes.
274849e11e3aSSheldon Hearn.It Fl c Ar coredumpsize
27497a2afe64SJoerg WunschThe maximal size of core dump files, in 512-byte blocks.
275017e98da8SMateusz PiotrowskiSetting
275117e98da8SMateusz Piotrowski.Ar coredumpsize
275217e98da8SMateusz Piotrowskito 0 prevents core dump files from being created.
275349e11e3aSSheldon Hearn.It Fl d Ar datasize
27547a2afe64SJoerg WunschThe maximal size of the data segment of a process, in kilobytes.
275549e11e3aSSheldon Hearn.It Fl f Ar filesize
275649e11e3aSSheldon HearnThe maximal size of a file, in 512-byte blocks.
2757d2c068eaSJilles Tjoelker.It Fl k Ar kqueues
2758d2c068eaSJilles TjoelkerThe maximal number of kqueues
2759d2c068eaSJilles Tjoelker(see
2760d2c068eaSJilles Tjoelker.Xr kqueue 2 )
2761d2c068eaSJilles Tjoelkerfor this user ID.
276249e11e3aSSheldon Hearn.It Fl l Ar lockedmem
27637a2afe64SJoerg WunschThe maximal size of memory that can be locked by a process, in
27647a2afe64SJoerg Wunschkilobytes.
276549e11e3aSSheldon Hearn.It Fl m Ar memoryuse
27667a2afe64SJoerg WunschThe maximal resident set size of a process, in kilobytes.
276749e11e3aSSheldon Hearn.It Fl n Ar nofiles
27687a2afe64SJoerg WunschThe maximal number of descriptors that could be opened by a process.
2769250d9fd8SKonstantin Belousov.It Fl o Ar umtxp
2770250d9fd8SKonstantin BelousovThe maximal number of process-shared locks
2771250d9fd8SKonstantin Belousov(see
2772250d9fd8SKonstantin Belousov.Xr pthread 3 )
2773250d9fd8SKonstantin Belousovfor this user ID.
2774331773cdSEd Schouten.It Fl p Ar pseudoterminals
2775331773cdSEd SchoutenThe maximal number of pseudo-terminals for this user ID.
277649e11e3aSSheldon Hearn.It Fl s Ar stacksize
27777a2afe64SJoerg WunschThe maximal size of the stack segment, in kilobytes.
277849e11e3aSSheldon Hearn.It Fl t Ar time
27797a2afe64SJoerg WunschThe maximal amount of CPU time to be used by each process, in seconds.
278049e11e3aSSheldon Hearn.It Fl u Ar userproc
2781ab72124fSSteve PriceThe maximal number of simultaneous processes for this user ID.
278262df9b62SSheldon Hearn.It Fl v Ar virtualmem
27832d41ef4bSMatthew DillonThe maximal virtual size of a process, in kilobytes.
2784c8054a61SJilles Tjoelker.It Fl w Ar swapuse
2785c8054a61SJilles TjoelkerThe maximum amount of swap space reserved or used for this user ID,
2786c8054a61SJilles Tjoelkerin kilobytes.
2787ab72124fSSteve Price.El
2788faa1ed35SStefan Farfeleder.It Ic umask Oo Fl S Oc Op Ar mask
278949e11e3aSSheldon HearnSet the file creation mask (see
2790ab72124fSSteve Price.Xr umask 2 )
2791faa1ed35SStefan Farfelederto the octal or symbolic (see
2792faa1ed35SStefan Farfeleder.Xr chmod 1 )
2793faa1ed35SStefan Farfeledervalue specified by
27945d3b843bSSheldon Hearn.Ar mask .
279549e11e3aSSheldon HearnIf the argument is omitted, the current mask value is printed.
2796faa1ed35SStefan FarfelederIf the
2797faa1ed35SStefan Farfeleder.Fl S
2798faa1ed35SStefan Farfelederoption is specified, the output is symbolic, otherwise the output is octal.
2799de4ad382SStefan Farfeleder.It Ic unalias Oo Fl a Oc Op Ar name ...
2800de4ad382SStefan FarfelederThe specified alias names are removed.
28016b15476eSSheldon HearnIf
28021b9735e6SSheldon Hearn.Fl a
28036b15476eSSheldon Hearnis specified, all aliases are removed.
28049ba31ca0SRuslan Ermilov.It Ic unset Oo Fl fv Oc Ar name ...
28058b34ad92STim J. RobbinsThe specified variables or functions are unset and unexported.
28068b34ad92STim J. RobbinsIf the
28078b34ad92STim J. Robbins.Fl v
28088b34ad92STim J. Robbinsoption is specified or no options are given, the
280949e11e3aSSheldon Hearn.Ar name
28108b34ad92STim J. Robbinsarguments are treated as variable names.
28118b34ad92STim J. RobbinsIf the
28128b34ad92STim J. Robbins.Fl f
28138b34ad92STim J. Robbinsoption is specified, the
28148b34ad92STim J. Robbins.Ar name
28158b34ad92STim J. Robbinsarguments are treated as function names.
2816bd76c6b8SJilles Tjoelker.It Ic wait Op Ar job ...
2817bd76c6b8SJilles TjoelkerWait for each specified
281849e11e3aSSheldon Hearn.Ar job
281949e11e3aSSheldon Hearnto complete and return the exit status of the last process in the
2820bd76c6b8SJilles Tjoelkerlast specified
282149e11e3aSSheldon Hearn.Ar job .
2822bd76c6b8SJilles TjoelkerIf any
2823bd76c6b8SJilles Tjoelker.Ar job
2824bd76c6b8SJilles Tjoelkerspecified is unknown to the shell, it is treated as if it
2825bd76c6b8SJilles Tjoelkerwere a known job that exited with exit status 127.
2826bd76c6b8SJilles TjoelkerIf no operands are given, wait for all jobs to complete
2827e6d3cf26SSheldon Hearnand return an exit status of zero.
2828ab72124fSSteve Price.El
28296986f58fSJilles Tjoelker.Ss Command Line Editing
2830ab72124fSSteve PriceWhen
2831ab72124fSSteve Price.Nm
2832ab72124fSSteve Priceis being used interactively from a terminal, the current command
283349e11e3aSSheldon Hearnand the command history
283449e11e3aSSheldon Hearn(see
283549e11e3aSSheldon Hearn.Ic fc
283649e11e3aSSheldon Hearnin
28378eed22b1SJens Schweikhardt.Sx Built-in Commands )
2838dfe302abSRuslan Ermilovcan be edited using
2839dfe302abSRuslan Ermilov.Nm vi Ns -mode
2840dfe302abSRuslan Ermilovcommand line editing.
284149e11e3aSSheldon HearnThis mode uses commands similar
2842dfe302abSRuslan Ermilovto a subset of those described in the
2843dfe302abSRuslan Ermilov.Xr vi 1
2844dfe302abSRuslan Ermilovman page.
284549e11e3aSSheldon HearnThe command
2846dfe302abSRuslan Ermilov.Dq Li "set -o vi"
284749e11e3aSSheldon Hearn(or
2848dfe302abSRuslan Ermilov.Dq Li "set -V" )
2849dfe302abSRuslan Ermilovenables
2850dfe302abSRuslan Ermilov.Nm vi Ns -mode
2851dfe302abSRuslan Ermilovediting and places
2852ab72124fSSteve Price.Nm
2853dfe302abSRuslan Ermilovinto
2854dfe302abSRuslan Ermilov.Nm vi
2855dfe302abSRuslan Ermilovinsert mode.
2856dfe302abSRuslan ErmilovWith
2857dfe302abSRuslan Ermilov.Nm vi Ns -mode
2858dfe302abSRuslan Ermilovenabled,
2859ab72124fSSteve Price.Nm
286049e11e3aSSheldon Hearncan be switched between insert mode and command mode by typing
286149e11e3aSSheldon Hearn.Aq ESC .
286249e11e3aSSheldon HearnHitting
286349e11e3aSSheldon Hearn.Aq return
286449e11e3aSSheldon Hearnwhile in command mode will pass the line to the shell.
2865ab72124fSSteve Price.Pp
286649e11e3aSSheldon HearnSimilarly, the
2867dfe302abSRuslan Ermilov.Dq Li "set -o emacs"
286849e11e3aSSheldon Hearn(or
2869dfe302abSRuslan Ermilov.Dq Li "set -E" )
287049e11e3aSSheldon Hearncommand can be used to enable a subset of
2871dfe302abSRuslan Ermilov.Nm emacs Ns -style
2872dfe302abSRuslan Ermilovcommand line editing features.
287338afdcbcSGiorgos Keramidas.Sh ENVIRONMENT
287438afdcbcSGiorgos KeramidasThe following environment variables affect the execution of
287538afdcbcSGiorgos Keramidas.Nm :
28761788b7ffSJilles Tjoelker.Bl -tag -width ".Ev LANGXXXXXX"
28771788b7ffSJilles Tjoelker.It Ev ENV
28781788b7ffSJilles TjoelkerInitialization file for interactive shells.
28791788b7ffSJilles Tjoelker.It Ev LANG , Ev LC_*
28801788b7ffSJilles TjoelkerLocale settings.
28811788b7ffSJilles TjoelkerThese are inherited by children of the shell,
28821788b7ffSJilles Tjoelkerand is used in a limited manner by the shell itself.
28837b6779b6SJilles Tjoelker.It Ev OLDPWD
28847b6779b6SJilles TjoelkerThe previous current directory.
28857b6779b6SJilles TjoelkerThis is used and updated by
28867b6779b6SJilles Tjoelker.Ic cd .
28871788b7ffSJilles Tjoelker.It Ev PWD
28881788b7ffSJilles TjoelkerAn absolute pathname for the current directory,
28891788b7ffSJilles Tjoelkerpossibly containing symbolic links.
28901788b7ffSJilles TjoelkerThis is used and updated by the shell.
289138afdcbcSGiorgos Keramidas.It Ev TERM
289238afdcbcSGiorgos KeramidasThe default terminal setting for the shell.
289338afdcbcSGiorgos KeramidasThis is inherited by children of the shell, and is used in the history
289438afdcbcSGiorgos Keramidasediting modes.
289538afdcbcSGiorgos Keramidas.El
28961788b7ffSJilles Tjoelker.Pp
28974445af21SJilles TjoelkerAdditionally, environment variables are turned into shell variables
28981788b7ffSJilles Tjoelkerat startup,
28991788b7ffSJilles Tjoelkerwhich may affect the shell as described under
29001788b7ffSJilles Tjoelker.Sx Special Variables .
2901cb5a6a58SJoel Dahl.Sh FILES
2902cb5a6a58SJoel Dahl.Bl -tag -width "/etc/suid_profileXX" -compact
2903cb5a6a58SJoel Dahl.It Pa ~/.profile
2904cb5a6a58SJoel DahlUser's login profile.
2905cb5a6a58SJoel Dahl.It Pa /etc/profile
2906cb5a6a58SJoel DahlSystem login profile.
2907cb5a6a58SJoel Dahl.It Pa /etc/shells
2908cb5a6a58SJoel DahlShell database.
2909cb5a6a58SJoel Dahl.It Pa /etc/suid_profile
2910cb5a6a58SJoel DahlPrivileged shell profile.
2911cb5a6a58SJoel Dahl.El
2912fb039b55SRuslan Ermilov.Sh EXIT STATUS
291377da4a95SJilles TjoelkerIf the
291477da4a95SJilles Tjoelker.Ar script
291577da4a95SJilles Tjoelkercannot be found, the exit status will be 127;
291677da4a95SJilles Tjoelkerif it cannot be opened for another reason, the exit status will be 126.
291777da4a95SJilles TjoelkerOther errors that are detected by the shell, such as a syntax error, will
2918fb039b55SRuslan Ermilovcause the shell to exit with a non-zero exit status.
2919fb039b55SRuslan ErmilovIf the shell is not an interactive shell, the execution of the shell
2920fb039b55SRuslan Ermilovfile will be aborted.
2921fb039b55SRuslan ErmilovOtherwise the shell will return the exit status of the last command
2922dfe302abSRuslan Ermilovexecuted, or if the
2923dfe302abSRuslan Ermilov.Ic exit
2924dfe302abSRuslan Ermilovbuiltin is used with a numeric argument, it
2925fb039b55SRuslan Ermilovwill return the argument.
2926c83e7c50SJoseph Koshy.Sh SEE ALSO
2927c644db6aSSheldon Hearn.Xr builtin 1 ,
292838afdcbcSGiorgos Keramidas.Xr chsh 1 ,
2929c644db6aSSheldon Hearn.Xr echo 1 ,
293038afdcbcSGiorgos Keramidas.Xr ed 1 ,
2931825225e5SJens Schweikhardt.Xr emacs 1 Pq Pa ports/editors/emacs ,
29320a62a9caSJilles Tjoelker.Xr kill 1 ,
29339897c45fSJilles Tjoelker.Xr printf 1 ,
29345521ff5aSRuslan Ermilov.Xr pwd 1 ,
293538afdcbcSGiorgos Keramidas.Xr test 1 ,
293638afdcbcSGiorgos Keramidas.Xr vi 1 ,
293738afdcbcSGiorgos Keramidas.Xr execve 2 ,
293838afdcbcSGiorgos Keramidas.Xr getrlimit 2 ,
2939fd50df30SRuslan Ermilov.Xr umask 2 ,
2940ff4dc672SJilles Tjoelker.Xr wctype 3 ,
2941cb5a6a58SJoel Dahl.Xr editrc 5 ,
2942cb5a6a58SJoel Dahl.Xr shells 5
2943ab72124fSSteve Price.Sh HISTORY
2944b8923d4cSWolfram SchneiderA
2945ab72124fSSteve Price.Nm
29469cdd1e3fSYaroslav Tykhiycommand, the Thompson shell, appeared in
2947fc482908STim J. Robbins.At v1 .
29489cdd1e3fSYaroslav TykhiyIt was superseded in
29499cdd1e3fSYaroslav Tykhiy.At v7
29509cdd1e3fSYaroslav Tykhiyby the Bourne shell, which inherited the name
29519cdd1e3fSYaroslav Tykhiy.Nm .
29529cdd1e3fSYaroslav Tykhiy.Pp
29539cdd1e3fSYaroslav TykhiyThis version of
29549cdd1e3fSYaroslav Tykhiy.Nm
29559cdd1e3fSYaroslav Tykhiywas rewritten in 1989 under the
29569cdd1e3fSYaroslav Tykhiy.Bx
29579cdd1e3fSYaroslav Tykhiylicense after the Bourne shell from
2958e2a72442SAlexander Ziaee.At V.4
2959e2a72442SAlexander Ziaeeand first appeared in
2960e2a72442SAlexander Ziaee.Bx 4.3 Net/2 .
29619cdd1e3fSYaroslav Tykhiy.Sh AUTHORS
29629cdd1e3fSYaroslav TykhiyThis version of
29639cdd1e3fSYaroslav Tykhiy.Nm
29649badf57fSRuslan Ermilovwas originally written by
29659badf57fSRuslan Ermilov.An Kenneth Almquist .
2966128dc4a2STim J. Robbins.Sh BUGS
2967128dc4a2STim J. RobbinsThe
2968128dc4a2STim J. Robbins.Nm
296950df3424SJilles Tjoelkerutility does not recognize multibyte characters other than UTF-8.
297050df3424SJilles TjoelkerSplitting using
297150df3424SJilles Tjoelker.Va IFS
29728956c4ecSJilles Tjoelkerdoes not recognize multibyte characters.
2973