xref: /freebsd/bin/sh/sh.1 (revision 000dda7b47cba82af7fa1144d2c8c5bcd1625ec9)
19ddb49cbSWarner Losh.\"-
24b88c807SRodney W. Grimes.\" Copyright (c) 1991, 1993
34b88c807SRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes.\"
54b88c807SRodney W. Grimes.\" This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes.\" Kenneth Almquist.
74b88c807SRodney W. Grimes.\"
84b88c807SRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes.\" modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes.\" are met:
114b88c807SRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes.\" 4. Neither the name of the University nor the names of its contributors
174b88c807SRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
184b88c807SRodney W. Grimes.\"    without specific prior written permission.
194b88c807SRodney W. Grimes.\"
204b88c807SRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214b88c807SRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224b88c807SRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234b88c807SRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244b88c807SRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254b88c807SRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264b88c807SRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274b88c807SRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284b88c807SRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294b88c807SRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304b88c807SRodney W. Grimes.\" SUCH DAMAGE.
314b88c807SRodney W. Grimes.\"
32ab72124fSSteve Price.\"	from: @(#)sh.1	8.6 (Berkeley) 5/4/95
332a456239SPeter Wemm.\" $FreeBSD$
344b88c807SRodney W. Grimes.\"
35*000dda7bSJilles Tjoelker.Dd September 21, 2014
36ab72124fSSteve Price.Dt SH 1
37caa2db3bSRuslan Ermilov.Os
38ab72124fSSteve Price.Sh NAME
39ab72124fSSteve Price.Nm sh
40ab72124fSSteve Price.Nd command interpreter (shell)
41ab72124fSSteve Price.Sh SYNOPSIS
42ab72124fSSteve Price.Nm
43455b6546SDaichi GOTO.Op Fl /+abCEefhIimnPpTuVvx
44ab72124fSSteve Price.Op Fl /+o Ar longname
4557a40f7dSJilles Tjoelker.Oo
4657a40f7dSJilles Tjoelker.Ar script
4757a40f7dSJilles Tjoelker.Op Ar arg ...
4857a40f7dSJilles Tjoelker.Oc
4957a40f7dSJilles Tjoelker.Nm
50455b6546SDaichi GOTO.Op Fl /+abCEefhIimnPpTuVvx
5157a40f7dSJilles Tjoelker.Op Fl /+o Ar longname
5257a40f7dSJilles Tjoelker.Fl c Ar string
5357a40f7dSJilles Tjoelker.Oo
5457a40f7dSJilles Tjoelker.Ar name
5557a40f7dSJilles Tjoelker.Op Ar arg ...
5657a40f7dSJilles Tjoelker.Oc
5757a40f7dSJilles Tjoelker.Nm
58455b6546SDaichi GOTO.Op Fl /+abCEefhIimnPpTuVvx
5957a40f7dSJilles Tjoelker.Op Fl /+o Ar longname
6057a40f7dSJilles Tjoelker.Fl s
6181e9cda2SRuslan Ermilov.Op Ar arg ...
62ab72124fSSteve Price.Sh DESCRIPTION
63e918fc8fSSheldon HearnThe
64e918fc8fSSheldon Hearn.Nm
65e918fc8fSSheldon Hearnutility is the standard command interpreter for the system.
66ab72124fSSteve PriceThe current version of
67ab72124fSSteve Price.Nm
683fdfd0a4SJilles Tjoelkeris close to the
693fdfd0a4SJilles Tjoelker.St -p1003.1
70eccea571SRuslan Ermilovspecification for the shell.
713fdfd0a4SJilles TjoelkerIt only supports features
72dfe302abSRuslan Ermilovdesignated by
73dfe302abSRuslan Ermilov.Tn POSIX ,
743fdfd0a4SJilles Tjoelkerplus a few Berkeley extensions.
7549e11e3aSSheldon HearnThis man page is not intended to be a tutorial nor a complete
764b88c807SRodney W. Grimesspecification of the shell.
77ab72124fSSteve Price.Ss Overview
784b88c807SRodney W. GrimesThe shell is a command that reads lines from
794b88c807SRodney W. Grimeseither a file or the terminal, interprets them, and
80e6d3cf26SSheldon Hearngenerally executes other commands.
8149e11e3aSSheldon HearnIt is the program that is started when a user logs into the system,
8249e11e3aSSheldon Hearnalthough a user can select a different shell with the
83c644db6aSSheldon Hearn.Xr chsh 1
8449e11e3aSSheldon Hearncommand.
854b88c807SRodney W. GrimesThe shell
864b88c807SRodney W. Grimesimplements a language that has flow control constructs,
874b88c807SRodney W. Grimesa macro facility that provides a variety of features in
888eed22b1SJens Schweikhardtaddition to data storage, along with built-in history and line
89eccea571SRuslan Ermilovediting capabilities.
90eccea571SRuslan ErmilovIt incorporates many features to
914b88c807SRodney W. Grimesaid interactive use and has the advantage that the interpretative
924b88c807SRodney W. Grimeslanguage is common to both interactive and non-interactive
93eccea571SRuslan Ermilovuse (shell scripts).
94eccea571SRuslan ErmilovThat is, commands can be typed directly
9549e11e3aSSheldon Hearnto the running shell or can be put into a file,
9649e11e3aSSheldon Hearnwhich can be executed directly by the shell.
97ab72124fSSteve Price.Ss Invocation
9849e11e3aSSheldon Hearn.\"
9949e11e3aSSheldon Hearn.\" XXX This next sentence is incredibly confusing.
10049e11e3aSSheldon Hearn.\"
10149e11e3aSSheldon HearnIf no arguments are present and if the standard input of the shell
10249e11e3aSSheldon Hearnis connected to a terminal
10349e11e3aSSheldon Hearn(or if the
10449e11e3aSSheldon Hearn.Fl i
10549e11e3aSSheldon Hearnoption is set),
106eccea571SRuslan Ermilovthe shell is considered an interactive shell.
107eccea571SRuslan ErmilovAn interactive shell
1084b88c807SRodney W. Grimesgenerally prompts before each command and handles programming
1094b88c807SRodney W. Grimesand command errors differently (as described below).
1104b88c807SRodney W. GrimesWhen first starting, the shell inspects argument 0, and
11149e11e3aSSheldon Hearnif it begins with a dash
112a762afe5SGiorgos Keramidas.Pq Ql - ,
11349e11e3aSSheldon Hearnthe shell is also considered a login shell.
11449e11e3aSSheldon HearnThis is normally done automatically by the system
115eccea571SRuslan Ermilovwhen the user first logs in.
116eccea571SRuslan ErmilovA login shell first reads commands
117ab72124fSSteve Pricefrom the files
118ab72124fSSteve Price.Pa /etc/profile
11949e11e3aSSheldon Hearnand then
120ab72124fSSteve Price.Pa .profile
121dfe302abSRuslan Ermilovin a user's home directory,
122eccea571SRuslan Ermilovif they exist.
123eccea571SRuslan ErmilovIf the environment variable
124ab72124fSSteve Price.Ev ENV
125ab72124fSSteve Priceis set on entry to a shell, or is set in the
126ab72124fSSteve Price.Pa .profile
1271a62d884SJilles Tjoelkerof a login shell, the shell then subjects its value to parameter expansion
1281a62d884SJilles Tjoelkerand arithmetic expansion and reads commands from the named file.
129ab72124fSSteve PriceTherefore, a user should place commands that are to be executed only
130ab72124fSSteve Priceat login time in the
131ab72124fSSteve Price.Pa .profile
132ab72124fSSteve Pricefile, and commands that are executed for every shell inside the
133ab72124fSSteve Price.Ev ENV
134e6d3cf26SSheldon Hearnfile.
13549e11e3aSSheldon HearnThe user can set the
136ab72124fSSteve Price.Ev ENV
13749e11e3aSSheldon Hearnvariable to some file by placing the following line in the file
138ab72124fSSteve Price.Pa .profile
13949e11e3aSSheldon Hearnin the home directory,
140ab72124fSSteve Pricesubstituting for
1415dd78c9fSJoel Dahl.Pa .shrc
14249e11e3aSSheldon Hearnthe filename desired:
14349e11e3aSSheldon Hearn.Pp
1445dd78c9fSJoel Dahl.Dl "ENV=$HOME/.shrc; export ENV"
14549e11e3aSSheldon Hearn.Pp
14649e11e3aSSheldon HearnThe first non-option argument specified on the command line
14749e11e3aSSheldon Hearnwill be treated as the
1484b88c807SRodney W. Grimesname of a file from which to read commands (a shell script), and
1494b88c807SRodney W. Grimesthe remaining arguments are set as the positional parameters
150dfe302abSRuslan Ermilovof the shell
151dfe302abSRuslan Ermilov.Li ( $1 , $2 ,
152dfe302abSRuslan Ermilovetc.).
153eccea571SRuslan ErmilovOtherwise, the shell reads commands
1544b88c807SRodney W. Grimesfrom its standard input.
155ea76be72SSteve Price.Pp
156ea76be72SSteve PriceUnlike older versions of
157ea76be72SSteve Price.Nm
158ea76be72SSteve Pricethe
159ea76be72SSteve Price.Ev ENV
160eccea571SRuslan Ermilovscript is only sourced on invocation of interactive shells.
161eccea571SRuslan ErmilovThis
162ea76be72SSteve Pricecloses a well-known, and sometimes easily exploitable security
163ea76be72SSteve Pricehole related to poorly thought out
164ea76be72SSteve Price.Ev ENV
165ea76be72SSteve Pricescripts.
166ab72124fSSteve Price.Ss Argument List Processing
167ab72124fSSteve PriceAll of the single letter options to
168ab72124fSSteve Price.Nm
16949e11e3aSSheldon Hearnhave a corresponding long name,
17049e11e3aSSheldon Hearnwith the exception of
17149e11e3aSSheldon Hearn.Fl c
17249e11e3aSSheldon Hearnand
17349e11e3aSSheldon Hearn.Fl /+o .
17449e11e3aSSheldon HearnThese long names are provided next to the single letter options
17549e11e3aSSheldon Hearnin the descriptions below.
17649e11e3aSSheldon HearnThe long name for an option may be specified as an argument to the
17749e11e3aSSheldon Hearn.Fl /+o
17849e11e3aSSheldon Hearnoption of
179facc6767SRuslan Ermilov.Nm .
18049e11e3aSSheldon HearnOnce the shell is running,
18149e11e3aSSheldon Hearnthe long name for an option may be specified as an argument to the
18249e11e3aSSheldon Hearn.Fl /+o
18349e11e3aSSheldon Hearnoption of the
184c644db6aSSheldon Hearn.Ic set
1858eed22b1SJens Schweikhardtbuilt-in command
18649e11e3aSSheldon Hearn(described later in the section called
1878eed22b1SJens Schweikhardt.Sx Built-in Commands ) .
18849e11e3aSSheldon HearnIntroducing an option with a dash
189a762afe5SGiorgos Keramidas.Pq Ql -
19049e11e3aSSheldon Hearnenables the option,
19149e11e3aSSheldon Hearnwhile using a plus
192a762afe5SGiorgos Keramidas.Pq Ql +
19349e11e3aSSheldon Hearndisables the option.
19449e11e3aSSheldon HearnA
19549e11e3aSSheldon Hearn.Dq Li --
196fab26805SJoseph Koshyor plain
197dfe302abSRuslan Ermilov.Ql -
198fab26805SJoseph Koshywill stop option processing and will force the remaining
199fab26805SJoseph Koshywords on the command line to be treated as arguments.
20049e11e3aSSheldon HearnThe
20149e11e3aSSheldon Hearn.Fl /+o
20249e11e3aSSheldon Hearnand
20349e11e3aSSheldon Hearn.Fl c
20449e11e3aSSheldon Hearnoptions do not have long names.
20549e11e3aSSheldon HearnThey take arguments and are described after the single letter options.
20681e9cda2SRuslan Ermilov.Bl -tag -width indent
207ab72124fSSteve Price.It Fl a Li allexport
20849e11e3aSSheldon HearnFlag variables for export when assignments are made to them.
209ab72124fSSteve Price.It Fl b Li notify
210ab72124fSSteve PriceEnable asynchronous notification of background job
211ab72124fSSteve Pricecompletion.
212c4d9468eSRuslan Ermilov(UNIMPLEMENTED)
213ab72124fSSteve Price.It Fl C Li noclobber
214c644db6aSSheldon HearnDo not overwrite existing files with
215dfe302abSRuslan Ermilov.Ql > .
216ab72124fSSteve Price.It Fl E Li emacs
2178eed22b1SJens SchweikhardtEnable the built-in
218ab72124fSSteve Price.Xr emacs 1
21949e11e3aSSheldon Hearncommand line editor (disables the
220ab72124fSSteve Price.Fl V
221d5f6b530SJilles Tjoelkeroption if it has been set;
222d5f6b530SJilles Tjoelkerset automatically when interactive on terminals).
223ab72124fSSteve Price.It Fl e Li errexit
22449e11e3aSSheldon HearnExit immediately if any untested command fails in non-interactive mode.
2254b88c807SRodney W. GrimesThe exit status of a command is considered to be
22674053659SStefan Farfelederexplicitly tested if the command is part of the list used to control
2278af11131SRuslan Ermilovan
2288af11131SRuslan Ermilov.Ic if , elif , while ,
2298af11131SRuslan Ermilovor
2308af11131SRuslan Ermilov.Ic until ;
2318af11131SRuslan Ermilovif the command is the left
232ab72124fSSteve Pricehand operand of an
23349e11e3aSSheldon Hearn.Dq Li &&
234ab72124fSSteve Priceor
23549e11e3aSSheldon Hearn.Dq Li ||
23674053659SStefan Farfelederoperator; or if the command is a pipeline preceded by the
23774053659SStefan Farfeleder.Ic !\&
238b14cfdf6SJilles Tjoelkerkeyword.
23974053659SStefan FarfelederIf a shell function is executed and its exit status is explicitly
24074053659SStefan Farfeledertested, all commands of the function are considered to be tested as
24174053659SStefan Farfelederwell.
242b14cfdf6SJilles Tjoelker.Pp
243b14cfdf6SJilles TjoelkerIt is recommended to check for failures explicitly
244b14cfdf6SJilles Tjoelkerinstead of relying on
245b14cfdf6SJilles Tjoelker.Fl e
246b14cfdf6SJilles Tjoelkerbecause it tends to behave in unexpected ways,
247b14cfdf6SJilles Tjoelkerparticularly in larger scripts.
248ab72124fSSteve Price.It Fl f Li noglob
2494b88c807SRodney W. GrimesDisable pathname expansion.
25054b16435SJilles Tjoelker.It Fl h Li trackall
25154b16435SJilles TjoelkerA do-nothing option for
25254b16435SJilles Tjoelker.Tn POSIX
25354b16435SJilles Tjoelkercompliance.
254ab72124fSSteve Price.It Fl I Li ignoreeof
25549e11e3aSSheldon HearnIgnore
256dfe302abSRuslan Ermilov.Dv EOF Ap s
25749e11e3aSSheldon Hearnfrom input when in interactive mode.
258ab72124fSSteve Price.It Fl i Li interactive
259ab72124fSSteve PriceForce the shell to behave interactively.
260ab72124fSSteve Price.It Fl m Li monitor
261ab72124fSSteve PriceTurn on job control (set automatically when interactive).
262cd60e2c6SJilles TjoelkerA new process group is created for each pipeline (called a job).
263cd60e2c6SJilles TjoelkerIt is possible to suspend jobs or to have them run in the foreground or
264cd60e2c6SJilles Tjoelkerin the background.
265cd60e2c6SJilles TjoelkerIn a non-interactive shell,
266cd60e2c6SJilles Tjoelkerthis option can be set even if no terminal is available
267cd60e2c6SJilles Tjoelkerand is useful to place processes in separate process groups.
268ab72124fSSteve Price.It Fl n Li noexec
2694b88c807SRodney W. GrimesIf not interactive, read commands but do not
270eccea571SRuslan Ermilovexecute them.
271eccea571SRuslan ErmilovThis is useful for checking the
2724b88c807SRodney W. Grimessyntax of shell scripts.
2737e1975c2STim J. Robbins.It Fl P Li physical
2747e1975c2STim J. RobbinsChange the default for the
2757e1975c2STim J. Robbins.Ic cd
2767e1975c2STim J. Robbinsand
2777e1975c2STim J. Robbins.Ic pwd
2787e1975c2STim J. Robbinscommands from
2797e1975c2STim J. Robbins.Fl L
2807e1975c2STim J. Robbins(logical directory layout)
2817e1975c2STim J. Robbinsto
2827e1975c2STim J. Robbins.Fl P
2837e1975c2STim J. Robbins(physical directory layout).
284ab72124fSSteve Price.It Fl p Li privileged
285eccea571SRuslan ErmilovTurn on privileged mode.
286eccea571SRuslan ErmilovThis mode is enabled on startup
287dfe302abSRuslan Ermilovif either the effective user or group ID is not equal to the
288dfe302abSRuslan Ermilovreal user or group ID.
289eccea571SRuslan ErmilovTurning this mode off sets the
290dfe302abSRuslan Ermiloveffective user and group IDs to the real user and group IDs.
29149e11e3aSSheldon HearnWhen this mode is enabled for interactive shells, the file
292ab72124fSSteve Price.Pa /etc/suid_profile
29349e11e3aSSheldon Hearnis sourced instead of
29449e11e3aSSheldon Hearn.Pa ~/.profile
295ab72124fSSteve Priceafter
296ab72124fSSteve Price.Pa /etc/profile
29749e11e3aSSheldon Hearnis sourced, and the contents of the
298ab72124fSSteve Price.Ev ENV
29949e11e3aSSheldon Hearnvariable are ignored.
300ab72124fSSteve Price.It Fl s Li stdin
3014b88c807SRodney W. GrimesRead commands from standard input (set automatically
302eccea571SRuslan Ermilovif no file arguments are present).
303eccea571SRuslan ErmilovThis option has
3044b88c807SRodney W. Grimesno effect when set after the shell has already started
305eccea571SRuslan Ermilovrunning (i.e., when set with the
306c644db6aSSheldon Hearn.Ic set
3076b15476eSSheldon Hearncommand).
308304199ceSTom Rhodes.It Fl T Li trapsasync
309e6d3cf26SSheldon HearnWhen waiting for a child, execute traps immediately.
310e6d3cf26SSheldon HearnIf this option is not set,
311e6d3cf26SSheldon Hearntraps are executed after the child exits,
312e6d3cf26SSheldon Hearnas specified in
313af5eb6fcSMaxim Konovalov.St -p1003.2 .
3146b15476eSSheldon HearnThis nonstandard option is useful for putting guarding shells around
315eccea571SRuslan Ermilovchildren that block signals.
316eccea571SRuslan ErmilovThe surrounding shell may kill the child
3176b15476eSSheldon Hearnor it may just return control to the tty and leave the child alone,
3186b15476eSSheldon Hearnlike this:
319c1c72a3cSMartin Cracauer.Bd -literal -offset indent
320c1c72a3cSMartin Cracauersh -T -c "trap 'exit 1' 2 ; some-blocking-program"
321c1c72a3cSMartin Cracauer.Ed
322ab72124fSSteve Price.It Fl u Li nounset
323ab72124fSSteve PriceWrite a message to standard error when attempting
32464254a66SJilles Tjoelkerto expand a variable, a positional parameter or
32564254a66SJilles Tjoelkerthe special parameter
32664254a66SJilles Tjoelker.Va \&!
32764254a66SJilles Tjoelkerthat is not set, and if the
328ab72124fSSteve Priceshell is not interactive, exit immediately.
329ab72124fSSteve Price.It Fl V Li vi
3308eed22b1SJens SchweikhardtEnable the built-in
331ab72124fSSteve Price.Xr vi 1
332ab72124fSSteve Pricecommand line editor (disables
333ab72124fSSteve Price.Fl E
334ab72124fSSteve Priceif it has been set).
335ab72124fSSteve Price.It Fl v Li verbose
336ab72124fSSteve PriceThe shell writes its input to standard error
337eccea571SRuslan Ermilovas it is read.
338eccea571SRuslan ErmilovUseful for debugging.
339ab72124fSSteve Price.It Fl x Li xtrace
34049e11e3aSSheldon HearnWrite each command
341120c8e6cSStefan Farfeleder(preceded by the value of the
342dfe302abSRuslan Ermilov.Va PS4
343292e6676SJilles Tjoelkervariable subjected to parameter expansion and arithmetic expansion)
34449e11e3aSSheldon Hearnto standard error before it is executed.
34549e11e3aSSheldon HearnUseful for debugging.
34649e11e3aSSheldon Hearn.El
34749e11e3aSSheldon Hearn.Pp
34849e11e3aSSheldon HearnThe
349fd50df30SRuslan Ermilov.Fl c
350fd50df30SRuslan Ermilovoption causes the commands to be read from the
351fd50df30SRuslan Ermilov.Ar string
352fd50df30SRuslan Ermilovoperand instead of from the standard input.
353fd50df30SRuslan ErmilovKeep in mind that this option only accepts a single string as its
354fd50df30SRuslan Ermilovargument, hence multi-word strings must be quoted.
355fd50df30SRuslan Ermilov.Pp
356fd50df30SRuslan ErmilovThe
35749e11e3aSSheldon Hearn.Fl /+o
35849e11e3aSSheldon Hearnoption takes as its only argument the long name of an option
35949e11e3aSSheldon Hearnto be enabled or disabled.
36049e11e3aSSheldon HearnFor example, the following two invocations of
36149e11e3aSSheldon Hearn.Nm
3628eed22b1SJens Schweikhardtboth enable the built-in
36349e11e3aSSheldon Hearn.Xr emacs 1
36449e11e3aSSheldon Hearncommand line editor:
36549e11e3aSSheldon Hearn.Bd -literal -offset indent
36649e11e3aSSheldon Hearnset -E
36749e11e3aSSheldon Hearnset -o emacs
36849e11e3aSSheldon Hearn.Ed
369d513af6aSTim J. Robbins.Pp
370d513af6aSTim J. RobbinsIf used without an argument, the
371d513af6aSTim J. Robbins.Fl o
372d513af6aSTim J. Robbinsoption displays the current option settings in a human-readable format.
373d513af6aSTim J. RobbinsIf
374d513af6aSTim J. Robbins.Cm +o
375d513af6aSTim J. Robbinsis used without an argument, the current option settings are output
376d513af6aSTim J. Robbinsin a format suitable for re-input into the shell.
377ab72124fSSteve Price.Ss Lexical Structure
3784b88c807SRodney W. GrimesThe shell reads input in terms of lines from a file and breaks
3794b88c807SRodney W. Grimesit up into words at whitespace (blanks and tabs), and at
3804b88c807SRodney W. Grimescertain sequences of
3816b15476eSSheldon Hearncharacters called
3826b15476eSSheldon Hearn.Dq operators ,
3836b15476eSSheldon Hearnwhich are special to the shell.
3844b88c807SRodney W. GrimesThere are two types of operators: control operators and
3854b88c807SRodney W. Grimesredirection operators (their meaning is discussed later).
386ab72124fSSteve PriceThe following is a list of valid operators:
38781e9cda2SRuslan Ermilov.Bl -tag -width indent
38849e11e3aSSheldon Hearn.It Control operators:
38949e11e3aSSheldon Hearn.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
39035471bf8SJoel Dahl.It Li & Ta Li && Ta Li \&( Ta Li \&) Ta Li \en
39135471bf8SJoel Dahl.It Li ;; Ta Li ;& Ta Li \&; Ta Li \&| Ta Li ||
39249e11e3aSSheldon Hearn.El
39349e11e3aSSheldon Hearn.It Redirection operators:
3944682f420SBrian Somers.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
39581e9cda2SRuslan Ermilov.It Li < Ta Li > Ta Li << Ta Li >> Ta Li <>
396748611c9SJoel Dahl.It Li <& Ta Li >& Ta Li <<- Ta Li >| Ta \&
39749e11e3aSSheldon Hearn.El
398ab72124fSSteve Price.El
3997d1a55fcSStefan Farfeleder.Pp
4007d1a55fcSStefan FarfelederThe character
4017d1a55fcSStefan Farfeleder.Ql #
4027d1a55fcSStefan Farfelederintroduces a comment if used at the beginning of a word.
4037d1a55fcSStefan FarfelederThe word starting with
4047d1a55fcSStefan Farfeleder.Ql #
4057d1a55fcSStefan Farfelederand the rest of the line are ignored.
4069764aa41SJilles Tjoelker.Pp
4079764aa41SJilles Tjoelker.Tn ASCII
4089764aa41SJilles Tjoelker.Dv NUL
4099764aa41SJilles Tjoelkercharacters (character code 0) are not allowed in shell input.
410ab72124fSSteve Price.Ss Quoting
4114b88c807SRodney W. GrimesQuoting is used to remove the special meaning of certain characters
412a886288eSJesus R. Camouor words to the shell, such as operators, whitespace, keywords,
413a886288eSJesus R. Camouor alias names.
414a886288eSJesus R. Camou.Pp
415a62ab027SJilles TjoelkerThere are four types of quoting: matched single quotes,
416a62ab027SJilles Tjoelkerdollar-single quotes,
4174b88c807SRodney W. Grimesmatched double quotes, and backslash.
41881e9cda2SRuslan Ermilov.Bl -tag -width indent
419ab72124fSSteve Price.It Single Quotes
4204b88c807SRodney W. GrimesEnclosing characters in single quotes preserves the literal
421ab0a2172SSteve Pricemeaning of all the characters (except single quotes, making
422ab0a2172SSteve Priceit impossible to put single-quotes in a single-quoted string).
423a62ab027SJilles Tjoelker.It Dollar-Single Quotes
424a62ab027SJilles TjoelkerEnclosing characters between
425a62ab027SJilles Tjoelker.Li $'
426a62ab027SJilles Tjoelkerand
427a62ab027SJilles Tjoelker.Li '
428a62ab027SJilles Tjoelkerpreserves the literal meaning of all characters
429a62ab027SJilles Tjoelkerexcept backslashes and single quotes.
430a62ab027SJilles TjoelkerA backslash introduces a C-style escape sequence:
431a62ab027SJilles Tjoelker.Bl -tag -width xUnnnnnnnn
432a62ab027SJilles Tjoelker.It \ea
433a62ab027SJilles TjoelkerAlert (ring the terminal bell)
434a62ab027SJilles Tjoelker.It \eb
435a62ab027SJilles TjoelkerBackspace
436a62ab027SJilles Tjoelker.It \ec Ns Ar c
437a62ab027SJilles TjoelkerThe control character denoted by
438a62ab027SJilles Tjoelker.Li ^ Ns Ar c
439a62ab027SJilles Tjoelkerin
440a62ab027SJilles Tjoelker.Xr stty 1 .
441a62ab027SJilles TjoelkerIf
442a62ab027SJilles Tjoelker.Ar c
443a62ab027SJilles Tjoelkeris a backslash, it must be doubled.
444a62ab027SJilles Tjoelker.It \ee
445a62ab027SJilles TjoelkerThe ESC character
446a62ab027SJilles Tjoelker.Tn ( ASCII
447a62ab027SJilles Tjoelker0x1b)
448a62ab027SJilles Tjoelker.It \ef
449a62ab027SJilles TjoelkerFormfeed
450a62ab027SJilles Tjoelker.It \en
451a62ab027SJilles TjoelkerNewline
452a62ab027SJilles Tjoelker.It \er
453a62ab027SJilles TjoelkerCarriage return
454a62ab027SJilles Tjoelker.It \et
455a62ab027SJilles TjoelkerHorizontal tab
456a62ab027SJilles Tjoelker.It \ev
457a62ab027SJilles TjoelkerVertical tab
458a62ab027SJilles Tjoelker.It \e\e
459a62ab027SJilles TjoelkerLiteral backslash
460a62ab027SJilles Tjoelker.It \e\&'
461a62ab027SJilles TjoelkerLiteral single-quote
462a62ab027SJilles Tjoelker.It \e\&"
463a62ab027SJilles TjoelkerLiteral double-quote
464a62ab027SJilles Tjoelker.It \e Ns Ar nnn
465a62ab027SJilles TjoelkerThe byte whose octal value is
466a62ab027SJilles Tjoelker.Ar nnn
467a62ab027SJilles Tjoelker(one to three digits)
468a62ab027SJilles Tjoelker.It \ex Ns Ar nn
469a62ab027SJilles TjoelkerThe byte whose hexadecimal value is
470a62ab027SJilles Tjoelker.Ar nn
471a62ab027SJilles Tjoelker(one or more digits only the last two of which are used)
472a62ab027SJilles Tjoelker.It \eu Ns Ar nnnn
473a62ab027SJilles TjoelkerThe Unicode code point
474a62ab027SJilles Tjoelker.Ar nnnn
475a62ab027SJilles Tjoelker(four hexadecimal digits)
476a62ab027SJilles Tjoelker.It \eU Ns Ar nnnnnnnn
477a62ab027SJilles TjoelkerThe Unicode code point
478a62ab027SJilles Tjoelker.Ar nnnnnnnn
479a62ab027SJilles Tjoelker(eight hexadecimal digits)
480a62ab027SJilles Tjoelker.El
481a62ab027SJilles Tjoelker.Pp
48207eb7033SJilles TjoelkerThe sequences for Unicode code points are currently only useful with
48307eb7033SJilles TjoelkerUTF-8 locales.
484a62ab027SJilles TjoelkerThey reject code point 0 and UTF-16 surrogates.
485a62ab027SJilles Tjoelker.Pp
486a62ab027SJilles TjoelkerIf an escape sequence would produce a byte with value 0,
487a62ab027SJilles Tjoelkerthat byte and the rest of the string until the matching single-quote
488a62ab027SJilles Tjoelkerare ignored.
489a62ab027SJilles Tjoelker.Pp
490a62ab027SJilles TjoelkerAny other string starting with a backslash is an error.
491ab72124fSSteve Price.It Double Quotes
4924b88c807SRodney W. GrimesEnclosing characters within double quotes preserves the literal
49349e11e3aSSheldon Hearnmeaning of all characters except dollar sign
494a762afe5SGiorgos Keramidas.Pq Ql $ ,
49549e11e3aSSheldon Hearnbackquote
496a762afe5SGiorgos Keramidas.Pq Ql ` ,
49749e11e3aSSheldon Hearnand backslash
498a762afe5SGiorgos Keramidas.Pq Ql \e .
49949e11e3aSSheldon HearnThe backslash inside double quotes is historically weird.
50049e11e3aSSheldon HearnIt remains literal unless it precedes the following characters,
50149e11e3aSSheldon Hearnwhich it serves to quote:
502afc5a69aSJoel Dahl.Pp
50349e11e3aSSheldon Hearn.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
504afc5a69aSJoel Dahl.It Li $ Ta Li ` Ta Li \&" Ta Li \e Ta Li \en
50549e11e3aSSheldon Hearn.El
506ab72124fSSteve Price.It Backslash
507ab72124fSSteve PriceA backslash preserves the literal meaning of the following
50849e11e3aSSheldon Hearncharacter, with the exception of the newline character
509a762afe5SGiorgos Keramidas.Pq Ql \en .
51049e11e3aSSheldon HearnA backslash preceding a newline is treated as a line continuation.
511ab72124fSSteve Price.El
512d806a030SJilles Tjoelker.Ss Keywords
513d806a030SJilles TjoelkerKeywords or reserved words are words that have special meaning to the
5144b88c807SRodney W. Grimesshell and are recognized at the beginning of a line and
515eccea571SRuslan Ermilovafter a control operator.
516d806a030SJilles TjoelkerThe following are keywords:
51749e11e3aSSheldon Hearn.Bl -column "doneXX" "elifXX" "elseXX" "untilXX" "whileX" -offset center
51881e9cda2SRuslan Ermilov.It Li \&! Ta { Ta } Ta Ic case Ta Ic do
51981e9cda2SRuslan Ermilov.It Ic done Ta Ic elif Ta Ic else Ta Ic esac Ta Ic fi
52081e9cda2SRuslan Ermilov.It Ic for Ta Ic if Ta Ic then Ta Ic until Ta Ic while
52149e11e3aSSheldon Hearn.El
522ab72124fSSteve Price.Ss Aliases
523ab72124fSSteve PriceAn alias is a name and corresponding value set using the
524c644db6aSSheldon Hearn.Ic alias
525eccea571SRuslan Ermilovbuilt-in command.
52605a447d0SJilles TjoelkerWherever the command word of a simple command may occur,
52705a447d0SJilles Tjoelkerand after checking for keywords if a keyword may occur, the shell
528e6d3cf26SSheldon Hearnchecks the word to see if it matches an alias.
529e6d3cf26SSheldon HearnIf it does, it replaces it in the input stream with its value.
530e6d3cf26SSheldon HearnFor example, if there is an alias called
53149e11e3aSSheldon Hearn.Dq Li lf
5326b15476eSSheldon Hearnwith the value
533dfe302abSRuslan Ermilov.Dq Li "ls -F" ,
5344b88c807SRodney W. Grimesthen the input
535dfe302abSRuslan Ermilov.Pp
536dfe302abSRuslan Ermilov.Dl "lf foobar"
537ab72124fSSteve Price.Pp
5384b88c807SRodney W. Grimeswould become
539dfe302abSRuslan Ermilov.Pp
540dfe302abSRuslan Ermilov.Dl "ls -F foobar"
541ab72124fSSteve Price.Pp
54248f49aacSJilles TjoelkerAliases are also recognized after an alias
54348f49aacSJilles Tjoelkerwhose value ends with a space or tab.
54448f49aacSJilles TjoelkerFor example, if there is also an alias called
54548f49aacSJilles Tjoelker.Dq Li nohup
54648f49aacSJilles Tjoelkerwith the value
54748f49aacSJilles Tjoelker.Dq Li "nohup " ,
54848f49aacSJilles Tjoelkerthen the input
54948f49aacSJilles Tjoelker.Pp
55048f49aacSJilles Tjoelker.Dl "nohup lf foobar"
55148f49aacSJilles Tjoelker.Pp
55248f49aacSJilles Tjoelkerwould become
55348f49aacSJilles Tjoelker.Pp
55448f49aacSJilles Tjoelker.Dl "nohup ls -F foobar"
55548f49aacSJilles Tjoelker.Pp
5564b88c807SRodney W. GrimesAliases provide a convenient way for naive users to
5574b88c807SRodney W. Grimescreate shorthands for commands without having to learn how
558eccea571SRuslan Ermilovto create functions with arguments.
55912dfb7a5SJilles TjoelkerUsing aliases in scripts is discouraged
56012dfb7a5SJilles Tjoelkerbecause the command that defines them must be executed
56112dfb7a5SJilles Tjoelkerbefore the code that uses them is parsed.
56212dfb7a5SJilles TjoelkerThis is fragile and not portable.
563a886288eSJesus R. Camou.Pp
564a886288eSJesus R. CamouAn alias name may be escaped in a command line, so that it is not
565a886288eSJesus R. Camoureplaced by its alias value, by using quoting characters within or
56619162ab9SJesus R. Camouadjacent to the alias name.
56719162ab9SJesus R. CamouThis is most often done by prefixing
568a886288eSJesus R. Camouan alias name with a backslash to execute a function, built-in, or
569a886288eSJesus R. Camounormal program with the same name.
570a886288eSJesus R. CamouSee the
571a886288eSJesus R. Camou.Sx Quoting
572a886288eSJesus R. Camousubsection.
573ab72124fSSteve Price.Ss Commands
5744b88c807SRodney W. GrimesThe shell interprets the words it reads according to a
5754b88c807SRodney W. Grimeslanguage, the specification of which is outside the scope
576ab72124fSSteve Priceof this man page (refer to the BNF in the
577ab72124fSSteve Price.St -p1003.2
578eccea571SRuslan Ermilovdocument).
579eccea571SRuslan ErmilovEssentially though, a line is read and if
5804b88c807SRodney W. Grimesthe first word of the line (or after a control operator)
581d806a030SJilles Tjoelkeris not a keyword, then the shell has recognized a
582eccea571SRuslan Ermilovsimple command.
583eccea571SRuslan ErmilovOtherwise, a complex command or some
5844b88c807SRodney W. Grimesother special construct may have been recognized.
585ab72124fSSteve Price.Ss Simple Commands
5864b88c807SRodney W. GrimesIf a simple command has been recognized, the shell performs
5874b88c807SRodney W. Grimesthe following actions:
588ab72124fSSteve Price.Bl -enum
589ab72124fSSteve Price.It
5906b15476eSSheldon HearnLeading words of the form
59149e11e3aSSheldon Hearn.Dq Li name=value
5926b15476eSSheldon Hearnare stripped off and assigned to the environment of
593*000dda7bSJilles Tjoelkerthe simple command
594*000dda7bSJilles Tjoelker(they do not affect expansions).
595eccea571SRuslan ErmilovRedirection operators and
5964b88c807SRodney W. Grimestheir arguments (as described below) are stripped
5974b88c807SRodney W. Grimesoff and saved for processing.
598ab72124fSSteve Price.It
599ab72124fSSteve PriceThe remaining words are expanded as described in
6006b15476eSSheldon Hearnthe section called
6016b15476eSSheldon Hearn.Sx Word Expansions ,
6026b15476eSSheldon Hearnand the first remaining word is considered the command
603eccea571SRuslan Ermilovname and the command is located.
604eccea571SRuslan ErmilovThe remaining
6054b88c807SRodney W. Grimeswords are considered the arguments of the command.
6066b15476eSSheldon HearnIf no command name resulted, then the
60749e11e3aSSheldon Hearn.Dq Li name=value
6084b88c807SRodney W. Grimesvariable assignments recognized in 1) affect the
6094b88c807SRodney W. Grimescurrent shell.
610ab72124fSSteve Price.It
611ab72124fSSteve PriceRedirections are performed as described in
6124b88c807SRodney W. Grimesthe next section.
613ab72124fSSteve Price.El
614ab72124fSSteve Price.Ss Redirections
6154b88c807SRodney W. GrimesRedirections are used to change where a command reads its input
616eccea571SRuslan Ermilovor sends its output.
617eccea571SRuslan ErmilovIn general, redirections open, close, or
618eccea571SRuslan Ermilovduplicate an existing reference to a file.
619eccea571SRuslan ErmilovThe overall format
6204b88c807SRodney W. Grimesused for redirection is:
62149e11e3aSSheldon Hearn.Pp
622dfe302abSRuslan Ermilov.D1 Oo Ar n Oc Ar redir-op file
62349e11e3aSSheldon Hearn.Pp
62449e11e3aSSheldon HearnThe
625dfe302abSRuslan Ermilov.Ar redir-op
62649e11e3aSSheldon Hearnis one of the redirection operators mentioned
627eccea571SRuslan Ermilovpreviously.
628eccea571SRuslan ErmilovThe following gives some examples of how these
62949e11e3aSSheldon Hearnoperators can be used.
63049e11e3aSSheldon HearnNote that stdin and stdout are commonly used abbreviations
63149e11e3aSSheldon Hearnfor standard input and standard output respectively.
63249e11e3aSSheldon Hearn.Bl -tag -width "1234567890XX" -offset indent
633dfe302abSRuslan Ermilov.It Oo Ar n Oc Ns Li > Ar file
634dfe302abSRuslan Ermilovredirect stdout (or file descriptor
635dfe302abSRuslan Ermilov.Ar n )
636dfe302abSRuslan Ermilovto
637dfe302abSRuslan Ermilov.Ar file
638dfe302abSRuslan Ermilov.It Oo Ar n Oc Ns Li >| Ar file
63949e11e3aSSheldon Hearnsame as above, but override the
64049e11e3aSSheldon Hearn.Fl C
64149e11e3aSSheldon Hearnoption
642dfe302abSRuslan Ermilov.It Oo Ar n Oc Ns Li >> Ar file
643dfe302abSRuslan Ermilovappend stdout (or file descriptor
644dfe302abSRuslan Ermilov.Ar n )
645dfe302abSRuslan Ermilovto
646dfe302abSRuslan Ermilov.Ar file
647dfe302abSRuslan Ermilov.It Oo Ar n Oc Ns Li < Ar file
648dfe302abSRuslan Ermilovredirect stdin (or file descriptor
649dfe302abSRuslan Ermilov.Ar n )
650dfe302abSRuslan Ermilovfrom
651dfe302abSRuslan Ermilov.Ar file
652dfe302abSRuslan Ermilov.It Oo Ar n Oc Ns Li <> Ar file
653dfe302abSRuslan Ermilovredirect stdin (or file descriptor
654dfe302abSRuslan Ermilov.Ar n )
655dfe302abSRuslan Ermilovto and from
656dfe302abSRuslan Ermilov.Ar file
657dfe302abSRuslan Ermilov.It Oo Ar n1 Oc Ns Li <& Ns Ar n2
658dfe302abSRuslan Ermilovduplicate stdin (or file descriptor
659dfe302abSRuslan Ermilov.Ar n1 )
660dfe302abSRuslan Ermilovfrom file descriptor
661dfe302abSRuslan Ermilov.Ar n2
662dfe302abSRuslan Ermilov.It Oo Ar n Oc Ns Li <&-
663dfe302abSRuslan Ermilovclose stdin (or file descriptor
664dfe302abSRuslan Ermilov.Ar n )
665dfe302abSRuslan Ermilov.It Oo Ar n1 Oc Ns Li >& Ns Ar n2
666dfe302abSRuslan Ermilovduplicate stdout (or file descriptor
667dfe302abSRuslan Ermilov.Ar n1 )
668dfe302abSRuslan Ermilovto file descriptor
669dfe302abSRuslan Ermilov.Ar n2
670dfe302abSRuslan Ermilov.It Oo Ar n Oc Ns Li >&-
671dfe302abSRuslan Ermilovclose stdout (or file descriptor
672dfe302abSRuslan Ermilov.Ar n )
673ab72124fSSteve Price.El
674ab72124fSSteve Price.Pp
6756b15476eSSheldon HearnThe following redirection is often called a
6766b15476eSSheldon Hearn.Dq here-document .
677dfe302abSRuslan Ermilov.Bd -unfilled -offset indent
678dfe302abSRuslan Ermilov.Oo Ar n Oc Ns Li << Ar delimiter
679a2442c2fSJoel Dahl.Ar here-doc-text
680a2442c2fSJoel Dahl.Ar ...
681dfe302abSRuslan Ermilov.Ar delimiter
682ab72124fSSteve Price.Ed
683ab72124fSSteve Price.Pp
6844b88c807SRodney W. GrimesAll the text on successive lines up to the delimiter is
6854b88c807SRodney W. Grimessaved away and made available to the command on standard
686dfe302abSRuslan Ermilovinput, or file descriptor
687dfe302abSRuslan Ermilov.Ar n
688dfe302abSRuslan Ermilovif it is specified.
689dfe302abSRuslan ErmilovIf the
690dfe302abSRuslan Ermilov.Ar delimiter
691dfe302abSRuslan Ermilovas specified on the initial line is quoted, then the
692dfe302abSRuslan Ermilov.Ar here-doc-text
6934b88c807SRodney W. Grimesis treated literally, otherwise the text is subjected to
6944b88c807SRodney W. Grimesparameter expansion, command substitution, and arithmetic
6956b15476eSSheldon Hearnexpansion (as described in the section on
6966b15476eSSheldon Hearn.Sx Word Expansions ) .
6976b15476eSSheldon HearnIf the operator is
69849e11e3aSSheldon Hearn.Dq Li <<-
6996b15476eSSheldon Hearninstead of
70049e11e3aSSheldon Hearn.Dq Li << ,
7016b15476eSSheldon Hearnthen leading tabs
702dfe302abSRuslan Ermilovin the
703dfe302abSRuslan Ermilov.Ar here-doc-text
704dfe302abSRuslan Ermilovare stripped.
705ab72124fSSteve Price.Ss Search and Execution
706ab0a2172SSteve PriceThere are three types of commands: shell functions,
7078eed22b1SJens Schweikhardtbuilt-in commands, and normal programs.
70849e11e3aSSheldon HearnThe command is searched for (by name) in that order.
70949e11e3aSSheldon HearnThe three types of commands are all executed in a different way.
710ab72124fSSteve Price.Pp
711ab0a2172SSteve PriceWhen a shell function is executed, all of the shell positional
712dfe302abSRuslan Ermilovparameters (except
713dfe302abSRuslan Ermilov.Li $0 ,
714dfe302abSRuslan Ermilovwhich remains unchanged) are
7154b88c807SRodney W. Grimesset to the arguments of the shell function.
7164b88c807SRodney W. GrimesThe variables which are explicitly placed in the environment of
7174b88c807SRodney W. Grimesthe command (by placing assignments to them before the
7184b88c807SRodney W. Grimesfunction name) are made local to the function and are set
719e6d3cf26SSheldon Hearnto the values given.
720e6d3cf26SSheldon HearnThen the command given in the function definition is executed.
721e6d3cf26SSheldon HearnThe positional parameters are restored to their original values
722e6d3cf26SSheldon Hearnwhen the command completes.
723ab0a2172SSteve PriceThis all occurs within the current shell.
724ab72124fSSteve Price.Pp
7258eed22b1SJens SchweikhardtShell built-in commands are executed internally to the shell, without
726ab0a2172SSteve Pricespawning a new process.
72730268dfaSJilles TjoelkerThere are two kinds of built-in commands: regular and special.
72830268dfaSJilles TjoelkerAssignments before special builtins persist after they finish
72930268dfaSJilles Tjoelkerexecuting and assignment errors, redirection errors and certain
73030268dfaSJilles Tjoelkeroperand errors cause a script to be aborted.
7314b985a89SJilles TjoelkerSpecial builtins cannot be overridden with a function.
73230268dfaSJilles TjoelkerBoth regular and special builtins can affect the shell in ways
73330268dfaSJilles Tjoelkernormal programs cannot.
734ab72124fSSteve Price.Pp
735c644db6aSSheldon HearnOtherwise, if the command name does not match a function
7368eed22b1SJens Schweikhardtor built-in command, the command is searched for as a normal
7374b88c807SRodney W. Grimesprogram in the file system (as described in the next section).
7384b88c807SRodney W. GrimesWhen a normal program is executed, the shell runs the program,
739e6d3cf26SSheldon Hearnpassing the arguments and the environment to the program.
740e6d3cf26SSheldon HearnIf the program is not a normal executable file
741eccea571SRuslan Ermilov(i.e., if it does not begin with the
742dfe302abSRuslan Ermilov.Dq "magic number"
7436b15476eSSheldon Hearnwhose
744ab72124fSSteve Price.Tn ASCII
7456b15476eSSheldon Hearnrepresentation is
746dfe302abSRuslan Ermilov.Dq Li #! ,
74749e11e3aSSheldon Hearnresulting in an
748b4f0f4aaSRuslan Ermilov.Er ENOEXEC
74949e11e3aSSheldon Hearnreturn value from
75049e11e3aSSheldon Hearn.Xr execve 2 )
751604e8224SJilles Tjoelkerbut appears to be a text file,
7523835f47cSJilles Tjoelkerthe shell will run a new instance of
7533835f47cSJilles Tjoelker.Nm
7543835f47cSJilles Tjoelkerto interpret it.
755ab72124fSSteve Price.Pp
756ab0a2172SSteve PriceNote that previous versions of this document
757ab0a2172SSteve Priceand the source code itself misleadingly and sporadically
758ab0a2172SSteve Pricerefer to a shell script without a magic number
7596b15476eSSheldon Hearnas a
760dfe302abSRuslan Ermilov.Dq "shell procedure" .
761ab72124fSSteve Price.Ss Path Search
7624b88c807SRodney W. GrimesWhen locating a command, the shell first looks to see if
763eccea571SRuslan Ermilovit has a shell function by that name.
764eccea571SRuslan ErmilovThen it looks for a
765eccea571SRuslan Ermilovbuilt-in command by that name.
766eccea571SRuslan ErmilovIf a built-in command is not found,
767ab0a2172SSteve Priceone of two things happen:
768ab72124fSSteve Price.Bl -enum
769ab72124fSSteve Price.It
770ab72124fSSteve PriceCommand names containing a slash are simply executed without
771ab0a2172SSteve Priceperforming any searches.
772ab72124fSSteve Price.It
773dfe302abSRuslan ErmilovThe shell searches each entry in the
7741788b7ffSJilles Tjoelker.Va PATH
7751788b7ffSJilles Tjoelkervariable
776eccea571SRuslan Ermilovin turn for the command.
777eccea571SRuslan ErmilovThe value of the
7781788b7ffSJilles Tjoelker.Va PATH
779ab72124fSSteve Pricevariable should be a series of
780eccea571SRuslan Ermiloventries separated by colons.
781eccea571SRuslan ErmilovEach entry consists of a
7824b88c807SRodney W. Grimesdirectory name.
7834b88c807SRodney W. GrimesThe current directory
784ab0a2172SSteve Pricemay be indicated implicitly by an empty directory name,
785ab0a2172SSteve Priceor explicitly by a single period.
786ab72124fSSteve Price.El
787ab72124fSSteve Price.Ss Command Exit Status
7884b88c807SRodney W. GrimesEach command has an exit status that can influence the behavior
789eccea571SRuslan Ermilovof other shell commands.
790eccea571SRuslan ErmilovThe paradigm is that a command exits
7914b88c807SRodney W. Grimeswith zero for normal or success, and non-zero for failure,
792eccea571SRuslan Ermiloverror, or a false indication.
793eccea571SRuslan ErmilovThe man page for each command
7944b88c807SRodney W. Grimesshould indicate the various exit codes and what they mean.
7958eed22b1SJens SchweikhardtAdditionally, the built-in commands return exit codes, as does
796ab0a2172SSteve Pricean executed shell function.
797c351e089SJohn Polstra.Pp
798c351e089SJohn PolstraIf a command is terminated by a signal, its exit status is 128 plus
799eccea571SRuslan Ermilovthe signal number.
800eccea571SRuslan ErmilovSignal numbers are defined in the header file
801fe08efe6SRuslan Ermilov.In sys/signal.h .
802ab72124fSSteve Price.Ss Complex Commands
8034b88c807SRodney W. GrimesComplex commands are combinations of simple commands
804d806a030SJilles Tjoelkerwith control operators or keywords, together creating a larger complex
805eccea571SRuslan Ermilovcommand.
806eccea571SRuslan ErmilovMore generally, a command is one of the following:
807ab72124fSSteve Price.Bl -item -offset indent
808ab72124fSSteve Price.It
809ab72124fSSteve Pricesimple command
810ab72124fSSteve Price.It
811ab72124fSSteve Pricepipeline
812ab72124fSSteve Price.It
813ab72124fSSteve Pricelist or compound-list
814ab72124fSSteve Price.It
815ab72124fSSteve Pricecompound command
816ab72124fSSteve Price.It
817ab72124fSSteve Pricefunction definition
818ab72124fSSteve Price.El
819ab72124fSSteve Price.Pp
8204b88c807SRodney W. GrimesUnless otherwise stated, the exit status of a command is
8214b88c807SRodney W. Grimesthat of the last simple command executed by the command.
822ab72124fSSteve Price.Ss Pipelines
8234b88c807SRodney W. GrimesA pipeline is a sequence of one or more commands separated
824dfe302abSRuslan Ermilovby the control operator
825dfe302abSRuslan Ermilov.Ql \&| .
826eccea571SRuslan ErmilovThe standard output of all but
8274b88c807SRodney W. Grimesthe last command is connected to the standard input
828eccea571SRuslan Ermilovof the next command.
829eccea571SRuslan ErmilovThe standard output of the last
830ab0a2172SSteve Pricecommand is inherited from the shell, as usual.
831ab72124fSSteve Price.Pp
8324b88c807SRodney W. GrimesThe format for a pipeline is:
83349e11e3aSSheldon Hearn.Pp
834dfe302abSRuslan Ermilov.D1 Oo Li \&! Oc Ar command1 Op Li \&| Ar command2 ...
835ab72124fSSteve Price.Pp
836dfe302abSRuslan ErmilovThe standard output of
837dfe302abSRuslan Ermilov.Ar command1
838dfe302abSRuslan Ermilovis connected to the standard input of
839dfe302abSRuslan Ermilov.Ar command2 .
840eccea571SRuslan ErmilovThe standard input, standard output, or
8414b88c807SRodney W. Grimesboth of a command is considered to be assigned by the
8424b88c807SRodney W. Grimespipeline before any redirection specified by redirection
8434b88c807SRodney W. Grimesoperators that are part of the command.
844ab72124fSSteve Price.Pp
845f1ec0581SJilles TjoelkerNote that unlike some other shells,
846f1ec0581SJilles Tjoelker.Nm
847f1ec0581SJilles Tjoelkerexecutes each process in a pipeline with more than one command
848f1ec0581SJilles Tjoelkerin a subshell environment and as a child of the
849f1ec0581SJilles Tjoelker.Nm
850f1ec0581SJilles Tjoelkerprocess.
851f1ec0581SJilles Tjoelker.Pp
8524b88c807SRodney W. GrimesIf the pipeline is not in the background (discussed later),
8534b88c807SRodney W. Grimesthe shell waits for all commands to complete.
854ab72124fSSteve Price.Pp
855d806a030SJilles TjoelkerIf the keyword
856eccea571SRuslan Ermilov.Ic !\&
857eccea571SRuslan Ermilovdoes not precede the pipeline, the
8584b88c807SRodney W. Grimesexit status is the exit status of the last command specified
859eccea571SRuslan Ermilovin the pipeline.
860eccea571SRuslan ErmilovOtherwise, the exit status is the logical
861eccea571SRuslan ErmilovNOT of the exit status of the last command.
862eccea571SRuslan ErmilovThat is, if
8634b88c807SRodney W. Grimesthe last command returns zero, the exit status is 1; if
8644b88c807SRodney W. Grimesthe last command returns greater than zero, the exit status
8654b88c807SRodney W. Grimesis zero.
866ab72124fSSteve Price.Pp
8674b88c807SRodney W. GrimesBecause pipeline assignment of standard input or standard
8684b88c807SRodney W. Grimesoutput or both takes place before redirection, it can be
869eccea571SRuslan Ermilovmodified by redirection.
870eccea571SRuslan ErmilovFor example:
871ab72124fSSteve Price.Pp
872dfe302abSRuslan Ermilov.Dl "command1 2>&1 | command2"
873ab72124fSSteve Price.Pp
87449e11e3aSSheldon Hearnsends both the standard output and standard error of
875dfe302abSRuslan Ermilov.Ar command1
87649e11e3aSSheldon Hearnto the standard input of
877dfe302abSRuslan Ermilov.Ar command2 .
87849e11e3aSSheldon Hearn.Pp
87949e11e3aSSheldon HearnA
880dfe302abSRuslan Ermilov.Ql \&;
88149e11e3aSSheldon Hearnor newline terminator causes the preceding
882e918fc8fSSheldon HearnAND-OR-list
883e918fc8fSSheldon Hearn(described below in the section called
884e918fc8fSSheldon Hearn.Sx Short-Circuit List Operators )
885e918fc8fSSheldon Hearnto be executed sequentially;
886e918fc8fSSheldon Hearnan
887dfe302abSRuslan Ermilov.Ql &
888e918fc8fSSheldon Hearncauses asynchronous execution of the preceding AND-OR-list.
88949e11e3aSSheldon Hearn.Ss Background Commands (&)
8904b88c807SRodney W. GrimesIf a command is terminated by the control operator ampersand
891a762afe5SGiorgos Keramidas.Pq Ql & ,
89251a514adSJilles Tjoelkerthe shell executes the command in a subshell environment (see
89351a514adSJilles Tjoelker.Sx Grouping Commands Together
89451a514adSJilles Tjoelkerbelow) and asynchronously;
89549e11e3aSSheldon Hearnthe shell does not wait for the command to finish
89649e11e3aSSheldon Hearnbefore executing the next command.
897ab72124fSSteve Price.Pp
8984b88c807SRodney W. GrimesThe format for running a command in background is:
899dfe302abSRuslan Ermilov.Pp
900dfe302abSRuslan Ermilov.D1 Ar command1 Li & Op Ar command2 Li & Ar ...
901ab72124fSSteve Price.Pp
9024b88c807SRodney W. GrimesIf the shell is not interactive, the standard input of an
903dfe302abSRuslan Ermilovasynchronous command is set to
904dfe302abSRuslan Ermilov.Pa /dev/null .
90549e11e3aSSheldon Hearn.Ss Lists (Generally Speaking)
9064b88c807SRodney W. GrimesA list is a sequence of zero or more commands separated by
9074b88c807SRodney W. Grimesnewlines, semicolons, or ampersands,
9084b88c807SRodney W. Grimesand optionally terminated by one of these three characters.
9094b88c807SRodney W. GrimesThe commands in a
9104b88c807SRodney W. Grimeslist are executed in the order they are written.
9114b88c807SRodney W. GrimesIf command is followed by an ampersand, the shell starts the
9128eed22b1SJens Schweikhardtcommand and immediately proceeds onto the next command;
9134b88c807SRodney W. Grimesotherwise it waits for the command to terminate before
9144b88c807SRodney W. Grimesproceeding to the next one.
915ab72124fSSteve Price.Ss Short-Circuit List Operators
91649e11e3aSSheldon Hearn.Dq Li &&
9176b15476eSSheldon Hearnand
91849e11e3aSSheldon Hearn.Dq Li ||
9196b15476eSSheldon Hearnare AND-OR list operators.
92049e11e3aSSheldon Hearn.Dq Li &&
9216b15476eSSheldon Hearnexecutes the first command, and then executes the second command
9226b15476eSSheldon Hearnif the exit status of the first command is zero.
92349e11e3aSSheldon Hearn.Dq Li ||
924120ac26dSMasafumi Max NAKANEis similar, but executes the second command if the exit
9256b15476eSSheldon Hearnstatus of the first command is nonzero.
92649e11e3aSSheldon Hearn.Dq Li &&
9276b15476eSSheldon Hearnand
92849e11e3aSSheldon Hearn.Dq Li ||
9294b88c807SRodney W. Grimesboth have the same priority.
93049e11e3aSSheldon Hearn.Ss Flow-Control Constructs (if, while, for, case)
93149e11e3aSSheldon HearnThe syntax of the
93249e11e3aSSheldon Hearn.Ic if
93349e11e3aSSheldon Hearncommand is:
934dfe302abSRuslan Ermilov.Bd -unfilled -offset indent -compact
935dfe302abSRuslan Ermilov.Ic if Ar list
936dfe302abSRuslan Ermilov.Ic then Ar list
937dfe302abSRuslan Ermilov.Oo Ic elif Ar list
938dfe302abSRuslan Ermilov.Ic then Ar list Oc Ar ...
939dfe302abSRuslan Ermilov.Op Ic else Ar list
940dfe302abSRuslan Ermilov.Ic fi
941dfe302abSRuslan Ermilov.Ed
942ab72124fSSteve Price.Pp
94349e11e3aSSheldon HearnThe syntax of the
94449e11e3aSSheldon Hearn.Ic while
94549e11e3aSSheldon Hearncommand is:
946dfe302abSRuslan Ermilov.Bd -unfilled -offset indent -compact
947dfe302abSRuslan Ermilov.Ic while Ar list
948dfe302abSRuslan Ermilov.Ic do Ar list
949dfe302abSRuslan Ermilov.Ic done
950dfe302abSRuslan Ermilov.Ed
951ab72124fSSteve Price.Pp
952ab0a2172SSteve PriceThe two lists are executed repeatedly while the exit status of the
95349e11e3aSSheldon Hearnfirst list is zero.
95449e11e3aSSheldon HearnThe
95549e11e3aSSheldon Hearn.Ic until
95649e11e3aSSheldon Hearncommand is similar, but has the word
95749e11e3aSSheldon Hearn.Ic until
95849e11e3aSSheldon Hearnin place of
95949e11e3aSSheldon Hearn.Ic while ,
96049e11e3aSSheldon Hearnwhich causes it to
9614b88c807SRodney W. Grimesrepeat until the exit status of the first list is zero.
962ab72124fSSteve Price.Pp
96349e11e3aSSheldon HearnThe syntax of the
96449e11e3aSSheldon Hearn.Ic for
96549e11e3aSSheldon Hearncommand is:
966dfe302abSRuslan Ermilov.Bd -unfilled -offset indent -compact
967dfe302abSRuslan Ermilov.Ic for Ar variable Op Ic in Ar word ...
968dfe302abSRuslan Ermilov.Ic do Ar list
969dfe302abSRuslan Ermilov.Ic done
970dfe302abSRuslan Ermilov.Ed
971ab72124fSSteve Price.Pp
972a01459dfSStefan FarfelederIf
973a01459dfSStefan Farfeleder.Ic in
9747b14d37fSRalf S. Engelschalland the following words are omitted,
975776e6b33SJilles Tjoelker.Ic in Li \&"$@\&"
976a01459dfSStefan Farfelederis used instead.
9774b88c807SRodney W. GrimesThe words are expanded, and then the list is executed
97849e11e3aSSheldon Hearnrepeatedly with the variable set to each word in turn.
97949e11e3aSSheldon HearnThe
98049e11e3aSSheldon Hearn.Ic do
9816b15476eSSheldon Hearnand
98249e11e3aSSheldon Hearn.Ic done
98349e11e3aSSheldon Hearncommands may be replaced with
984dfe302abSRuslan Ermilov.Ql {
98549e11e3aSSheldon Hearnand
986dfe302abSRuslan Ermilov.Ql } .
987ab72124fSSteve Price.Pp
98849e11e3aSSheldon HearnThe syntax of the
98949e11e3aSSheldon Hearn.Ic break
99049e11e3aSSheldon Hearnand
99149e11e3aSSheldon Hearn.Ic continue
99249e11e3aSSheldon Hearncommands is:
993dfe302abSRuslan Ermilov.D1 Ic break Op Ar num
994dfe302abSRuslan Ermilov.D1 Ic continue Op Ar num
995ab72124fSSteve Price.Pp
99649e11e3aSSheldon HearnThe
99749e11e3aSSheldon Hearn.Ic break
99849e11e3aSSheldon Hearncommand terminates the
99949e11e3aSSheldon Hearn.Ar num
100049e11e3aSSheldon Hearninnermost
100149e11e3aSSheldon Hearn.Ic for
100249e11e3aSSheldon Hearnor
100349e11e3aSSheldon Hearn.Ic while
100449e11e3aSSheldon Hearnloops.
100549e11e3aSSheldon HearnThe
100649e11e3aSSheldon Hearn.Ic continue
100749e11e3aSSheldon Hearncommand continues with the next iteration of the innermost loop.
100830268dfaSJilles TjoelkerThese are implemented as special built-in commands.
1009ab72124fSSteve Price.Pp
101049e11e3aSSheldon HearnThe syntax of the
101149e11e3aSSheldon Hearn.Ic case
1012dfe302abSRuslan Ermilovcommand is:
1013dfe302abSRuslan Ermilov.Bd -unfilled -offset indent -compact
1014dfe302abSRuslan Ermilov.Ic case Ar word Ic in
1015dfe302abSRuslan Ermilov.Ar pattern Ns Li ) Ar list Li ;;
1016dfe302abSRuslan Ermilov.Ar ...
1017dfe302abSRuslan Ermilov.Ic esac
1018dfe302abSRuslan Ermilov.Ed
1019ab72124fSSteve Price.Pp
102049e11e3aSSheldon HearnThe pattern can actually be one or more patterns
102149e11e3aSSheldon Hearn(see
102249e11e3aSSheldon Hearn.Sx Shell Patterns
102349e11e3aSSheldon Hearndescribed later),
102449e11e3aSSheldon Hearnseparated by
1025dfe302abSRuslan Ermilov.Ql \&|
10266b15476eSSheldon Hearncharacters.
1027b97989d6SJilles TjoelkerTilde expansion, parameter expansion, command substitution,
1028b97989d6SJilles Tjoelkerarithmetic expansion and quote removal are applied to the word.
1029b97989d6SJilles TjoelkerThen, each pattern is expanded in turn using tilde expansion,
1030b97989d6SJilles Tjoelkerparameter expansion, command substitution and arithmetic expansion and
1031b97989d6SJilles Tjoelkerthe expanded form of the word is checked against it.
1032b97989d6SJilles TjoelkerIf a match is found, the corresponding list is executed.
1033c9afaa63SJilles TjoelkerIf the selected list is terminated by the control operator
1034c9afaa63SJilles Tjoelker.Ql ;&
1035c9afaa63SJilles Tjoelkerinstead of
1036c9afaa63SJilles Tjoelker.Ql ;; ,
1037b97989d6SJilles Tjoelkerexecution continues with the next list,
1038b97989d6SJilles Tjoelkercontinuing until a list terminated with
1039b97989d6SJilles Tjoelker.Ql ;;
1040b97989d6SJilles Tjoelkeror the end of the
1041b97989d6SJilles Tjoelker.Ic case
1042b97989d6SJilles Tjoelkercommand.
1043aafd6a87SStefan FarfelederThe exit code of the
1044aafd6a87SStefan Farfeleder.Ic case
1045aafd6a87SStefan Farfeledercommand is the exit code of the last command executed in the list or
1046aafd6a87SStefan Farfelederzero if no patterns were matched.
1047ab72124fSSteve Price.Ss Grouping Commands Together
10484b88c807SRodney W. GrimesCommands may be grouped by writing either
1049dfe302abSRuslan Ermilov.Pp
1050dfe302abSRuslan Ermilov.D1 Li \&( Ns Ar list Ns Li \%)
1051ab72124fSSteve Price.Pp
10524b88c807SRodney W. Grimesor
1053dfe302abSRuslan Ermilov.Pp
1054dfe302abSRuslan Ermilov.D1 Li { Ar list Ns Li \&; }
1055ab72124fSSteve Price.Pp
1056ef89d04fSJilles TjoelkerThe first form executes the commands in a subshell environment.
105751a514adSJilles TjoelkerA subshell environment has its own copy of:
105851a514adSJilles Tjoelker.Bl -enum
105951a514adSJilles Tjoelker.It
106051a514adSJilles TjoelkerThe current working directory as set by
106151a514adSJilles Tjoelker.Ic cd .
106251a514adSJilles Tjoelker.It
106351a514adSJilles TjoelkerThe file creation mask as set by
106451a514adSJilles Tjoelker.Ic umask .
106551a514adSJilles Tjoelker.It
10660c35cec6SJilles TjoelkerResource limits as set by
10670c35cec6SJilles Tjoelker.Ic ulimit .
10680c35cec6SJilles Tjoelker.It
106951a514adSJilles TjoelkerReferences to open files.
107051a514adSJilles Tjoelker.It
107151a514adSJilles TjoelkerTraps as set by
107251a514adSJilles Tjoelker.Ic trap .
107351a514adSJilles Tjoelker.It
107451a514adSJilles TjoelkerKnown jobs.
107551a514adSJilles Tjoelker.It
107651a514adSJilles TjoelkerPositional parameters and variables.
107751a514adSJilles Tjoelker.It
107851a514adSJilles TjoelkerShell options.
107951a514adSJilles Tjoelker.It
108051a514adSJilles TjoelkerShell functions.
108151a514adSJilles Tjoelker.It
108251a514adSJilles TjoelkerShell aliases.
108351a514adSJilles Tjoelker.El
108451a514adSJilles Tjoelker.Pp
108551a514adSJilles TjoelkerThese are copied from the parent shell environment,
108651a514adSJilles Tjoelkerexcept that trapped (but not ignored) signals are reset to the default action
108751a514adSJilles Tjoelkerand known jobs are cleared.
108851a514adSJilles TjoelkerAny changes do not affect the parent shell environment.
108951a514adSJilles Tjoelker.Pp
109051a514adSJilles TjoelkerA subshell environment may be implemented as a child process or differently.
109151a514adSJilles TjoelkerIf job control is enabled in an interactive shell,
109251a514adSJilles Tjoelkercommands grouped in parentheses can be suspended and continued as a unit.
109351a514adSJilles Tjoelker.Pp
109454ccc8b5SJilles TjoelkerFor compatibility with other shells,
109554ccc8b5SJilles Tjoelkertwo open parentheses in sequence should be separated by whitespace.
109654ccc8b5SJilles Tjoelker.Pp
1097ef89d04fSJilles TjoelkerThe second form never forks another shell,
109849e11e3aSSheldon Hearnso it is slightly more efficient.
109949e11e3aSSheldon HearnGrouping commands together this way allows the user to
1100ab0a2172SSteve Priceredirect their output as though they were one program:
1101ab72124fSSteve Price.Bd -literal -offset indent
1102ee7dc416SSteve Price{ echo -n "hello"; echo " world"; } > greeting
1103ab72124fSSteve Price.Ed
1104ab72124fSSteve Price.Ss Functions
11054b88c807SRodney W. GrimesThe syntax of a function definition is
1106dfe302abSRuslan Ermilov.Pp
1107dfe302abSRuslan Ermilov.D1 Ar name Li \&( \&) Ar command
1108ab72124fSSteve Price.Pp
11094b88c807SRodney W. GrimesA function definition is an executable statement; when
1110dfe302abSRuslan Ermilovexecuted it installs a function named
1111dfe302abSRuslan Ermilov.Ar name
1112dfe302abSRuslan Ermilovand returns an
1113eccea571SRuslan Ermilovexit status of zero.
1114dfe302abSRuslan ErmilovThe
1115dfe302abSRuslan Ermilov.Ar command
1116dfe302abSRuslan Ermilovis normally a list
11176b15476eSSheldon Hearnenclosed between
1118dfe302abSRuslan Ermilov.Ql {
11196b15476eSSheldon Hearnand
1120dfe302abSRuslan Ermilov.Ql } .
1121ab72124fSSteve Price.Pp
11224b88c807SRodney W. GrimesVariables may be declared to be local to a function by
112349e11e3aSSheldon Hearnusing the
112449e11e3aSSheldon Hearn.Ic local
112549e11e3aSSheldon Hearncommand.
112649e11e3aSSheldon HearnThis should appear as the first statement of a function,
112749e11e3aSSheldon Hearnand the syntax is:
1128dfe302abSRuslan Ermilov.Pp
1129dfe302abSRuslan Ermilov.D1 Ic local Oo Ar variable ... Oc Op Fl
1130ab72124fSSteve Price.Pp
113149e11e3aSSheldon HearnThe
113249e11e3aSSheldon Hearn.Ic local
11338eed22b1SJens Schweikhardtcommand is implemented as a built-in command.
1134ab72124fSSteve Price.Pp
11354b88c807SRodney W. GrimesWhen a variable is made local, it inherits the initial
11364b88c807SRodney W. Grimesvalue and exported and readonly flags from the variable
11374b88c807SRodney W. Grimeswith the same name in the surrounding scope, if there is
1138eccea571SRuslan Ermilovone.
1139eccea571SRuslan ErmilovOtherwise, the variable is initially unset.
1140eccea571SRuslan ErmilovThe shell
1141e918fc8fSSheldon Hearnuses dynamic scoping, so that if the variable
1142dfe302abSRuslan Ermilov.Va x
1143e918fc8fSSheldon Hearnis made local to function
1144e918fc8fSSheldon Hearn.Em f ,
1145e918fc8fSSheldon Hearnwhich then calls function
1146e918fc8fSSheldon Hearn.Em g ,
1147e918fc8fSSheldon Hearnreferences to the variable
1148dfe302abSRuslan Ermilov.Va x
1149e918fc8fSSheldon Hearnmade inside
1150e918fc8fSSheldon Hearn.Em g
1151e918fc8fSSheldon Hearnwill refer to the variable
1152dfe302abSRuslan Ermilov.Va x
1153e918fc8fSSheldon Hearndeclared inside
1154e918fc8fSSheldon Hearn.Em f ,
1155e918fc8fSSheldon Hearnnot to the global variable named
1156dfe302abSRuslan Ermilov.Va x .
1157ab72124fSSteve Price.Pp
11586b4bbd8aSCeri DaviesThe only special parameter that can be made local is
1159dfe302abSRuslan Ermilov.Ql - .
11606b15476eSSheldon HearnMaking
1161dfe302abSRuslan Ermilov.Ql -
11626b15476eSSheldon Hearnlocal causes any shell options that are
1163dfe302abSRuslan Ermilovchanged via the
1164dfe302abSRuslan Ermilov.Ic set
1165dfe302abSRuslan Ermilovcommand inside the function to be
11664b88c807SRodney W. Grimesrestored to their original values when the function
11674b88c807SRodney W. Grimesreturns.
1168ab72124fSSteve Price.Pp
116949e11e3aSSheldon HearnThe syntax of the
117049e11e3aSSheldon Hearn.Ic return
117149e11e3aSSheldon Hearncommand is
1172dfe302abSRuslan Ermilov.Pp
1173dfe302abSRuslan Ermilov.D1 Ic return Op Ar exitstatus
1174ab72124fSSteve Price.Pp
11752935c4ccSJilles TjoelkerIt terminates the current executional scope, returning from the closest
11762935c4ccSJilles Tjoelkernested function or sourced script;
11772935c4ccSJilles Tjoelkerif no function or sourced script is being executed,
11782935c4ccSJilles Tjoelkerit exits the shell instance.
117949e11e3aSSheldon HearnThe
118049e11e3aSSheldon Hearn.Ic return
118130268dfaSJilles Tjoelkercommand is implemented as a special built-in command.
1182ab72124fSSteve Price.Ss Variables and Parameters
1183eccea571SRuslan ErmilovThe shell maintains a set of parameters.
1184eccea571SRuslan ErmilovA parameter
1185eccea571SRuslan Ermilovdenoted by a name is called a variable.
1186eccea571SRuslan ErmilovWhen starting up,
11874b88c807SRodney W. Grimesthe shell turns all the environment variables into shell
1188eccea571SRuslan Ermilovvariables.
1189eccea571SRuslan ErmilovNew variables can be set using the form
1190dfe302abSRuslan Ermilov.Pp
1191dfe302abSRuslan Ermilov.D1 Ar name Ns = Ns Ar value
1192ab72124fSSteve Price.Pp
11934b88c807SRodney W. GrimesVariables set by the user must have a name consisting solely
119449e11e3aSSheldon Hearnof alphabetics, numerics, and underscores.
119549e11e3aSSheldon HearnThe first letter of a variable name must not be numeric.
119649e11e3aSSheldon HearnA parameter can also be denoted by a number
11974b88c807SRodney W. Grimesor a special character as explained below.
1198ad56ebdcSJilles Tjoelker.Pp
1199ad56ebdcSJilles TjoelkerAssignments are expanded differently from other words:
1200ad56ebdcSJilles Tjoelkertilde expansion is also performed after the equals sign and after any colon
1201ad56ebdcSJilles Tjoelkerand usernames are also terminated by colons,
1202ad56ebdcSJilles Tjoelkerand field splitting and pathname expansion are not performed.
120384fbdd8cSJilles Tjoelker.Pp
120484fbdd8cSJilles TjoelkerThis special expansion applies not only to assignments that form a simple
120584fbdd8cSJilles Tjoelkercommand by themselves or precede a command word,
120684fbdd8cSJilles Tjoelkerbut also to words passed to the
120784fbdd8cSJilles Tjoelker.Ic export ,
120884fbdd8cSJilles Tjoelker.Ic local
120984fbdd8cSJilles Tjoelkeror
121084fbdd8cSJilles Tjoelker.Ic readonly
121184fbdd8cSJilles Tjoelkerbuilt-in commands that have this form.
121284fbdd8cSJilles TjoelkerFor this, the builtin's name must be literal
121384fbdd8cSJilles Tjoelker(not the result of an expansion)
121484fbdd8cSJilles Tjoelkerand may optionally be preceded by one or more literal instances of
121584fbdd8cSJilles Tjoelker.Ic command
121684fbdd8cSJilles Tjoelkerwithout options.
1217ab72124fSSteve Price.Ss Positional Parameters
121849e11e3aSSheldon HearnA positional parameter is a parameter denoted by a number greater than zero.
1219ab72124fSSteve PriceThe shell sets these initially to the values of its command line
1220eccea571SRuslan Ermilovarguments that follow the name of the shell script.
1221eccea571SRuslan ErmilovThe
1222c644db6aSSheldon Hearn.Ic set
12238eed22b1SJens Schweikhardtbuilt-in command can also be used to set or reset them.
1224ab72124fSSteve Price.Ss Special Parameters
1225626a7b1dSJilles TjoelkerSpecial parameters are parameters denoted by a single special character
1226626a7b1dSJilles Tjoelkeror the digit zero.
1227626a7b1dSJilles TjoelkerThey are shown in the following list, exactly as they would appear in input
122843d6677bSAlfred Perlsteintyped by the user or in the source of a shell script.
1229ab72124fSSteve Price.Bl -hang
123043d6677bSAlfred Perlstein.It Li $*
1231eccea571SRuslan ErmilovExpands to the positional parameters, starting from one.
1232eccea571SRuslan ErmilovWhen
12334b88c807SRodney W. Grimesthe expansion occurs within a double-quoted string
12344b88c807SRodney W. Grimesit expands to a single field with the value of each parameter
123549e11e3aSSheldon Hearnseparated by the first character of the
1236dfe302abSRuslan Ermilov.Va IFS
123749e11e3aSSheldon Hearnvariable,
1238dfe302abSRuslan Ermilovor by a space if
1239dfe302abSRuslan Ermilov.Va IFS
124049e11e3aSSheldon Hearnis unset.
124143d6677bSAlfred Perlstein.It Li $@
1242eccea571SRuslan ErmilovExpands to the positional parameters, starting from one.
1243eccea571SRuslan ErmilovWhen
12444b88c807SRodney W. Grimesthe expansion occurs within double-quotes, each positional
12454b88c807SRodney W. Grimesparameter expands as a separate argument.
12464b88c807SRodney W. GrimesIf there are no positional parameters, the
1247e918fc8fSSheldon Hearnexpansion of
1248e918fc8fSSheldon Hearn.Li @
1249e918fc8fSSheldon Hearngenerates zero arguments, even when
1250e918fc8fSSheldon Hearn.Li @
1251eccea571SRuslan Ermilovis double-quoted.
1252eccea571SRuslan ErmilovWhat this basically means, for example, is
1253dfe302abSRuslan Ermilovif
1254dfe302abSRuslan Ermilov.Li $1
1255dfe302abSRuslan Ermilovis
1256dfe302abSRuslan Ermilov.Dq Li abc
1257dfe302abSRuslan Ermilovand
1258dfe302abSRuslan Ermilov.Li $2
1259dfe302abSRuslan Ermilovis
1260dfe302abSRuslan Ermilov.Dq Li "def ghi" ,
12616b15476eSSheldon Hearnthen
1262776e6b33SJilles Tjoelker.Li \&"$@\&"
12636b15476eSSheldon Hearnexpands to
12644b88c807SRodney W. Grimesthe two arguments:
1265ab72124fSSteve Price.Bd -literal -offset indent
12664b88c807SRodney W. Grimes"abc"   "def ghi"
1267ab72124fSSteve Price.Ed
126843d6677bSAlfred Perlstein.It Li $#
12694b88c807SRodney W. GrimesExpands to the number of positional parameters.
1270dfe302abSRuslan Ermilov.It Li $?
12714b88c807SRodney W. GrimesExpands to the exit status of the most recent pipeline.
127243d6677bSAlfred Perlstein.It Li $-
1273ab72124fSSteve Price(hyphen) Expands to the current option flags (the single-letter
12744b88c807SRodney W. Grimesoption names concatenated into a string) as specified on
1275dfe302abSRuslan Ermilovinvocation, by the
1276dfe302abSRuslan Ermilov.Ic set
1277dfe302abSRuslan Ermilovbuilt-in command, or implicitly
12784b88c807SRodney W. Grimesby the shell.
127943d6677bSAlfred Perlstein.It Li $$
1280eccea571SRuslan ErmilovExpands to the process ID of the invoked shell.
1281eccea571SRuslan ErmilovA subshell
1282dfe302abSRuslan Ermilovretains the same value of
1283dfe302abSRuslan Ermilov.Va $
1284dfe302abSRuslan Ermilovas its parent.
1285dfe302abSRuslan Ermilov.It Li $!
12864b88c807SRodney W. GrimesExpands to the process ID of the most recent background
1287eccea571SRuslan Ermilovcommand executed from the current shell.
1288eccea571SRuslan ErmilovFor a
12894b88c807SRodney W. Grimespipeline, the process ID is that of the last command in the
12904b88c807SRodney W. Grimespipeline.
1291ed4c3b5fSJilles TjoelkerIf this parameter is referenced, the shell will remember
1292ed4c3b5fSJilles Tjoelkerthe process ID and its exit status until the
1293ed4c3b5fSJilles Tjoelker.Ic wait
1294ed4c3b5fSJilles Tjoelkerbuilt-in command reports completion of the process.
129543d6677bSAlfred Perlstein.It Li $0
129657a40f7dSJilles Tjoelker(zero) Expands to the name of the shell script if passed on the command line,
129757a40f7dSJilles Tjoelkerthe
129857a40f7dSJilles Tjoelker.Ar name
129957a40f7dSJilles Tjoelkeroperand if given (with
130057a40f7dSJilles Tjoelker.Fl c )
130157a40f7dSJilles Tjoelkeror otherwise argument 0 passed to the shell.
1302ab72124fSSteve Price.El
13031788b7ffSJilles Tjoelker.Ss Special Variables
13041788b7ffSJilles TjoelkerThe following variables are set by the shell or
13051788b7ffSJilles Tjoelkerhave special meaning to it:
13061788b7ffSJilles Tjoelker.Bl -tag -width ".Va HISTSIZE"
13071788b7ffSJilles Tjoelker.It Va CDPATH
13081788b7ffSJilles TjoelkerThe search path used with the
13091788b7ffSJilles Tjoelker.Ic cd
13101788b7ffSJilles Tjoelkerbuilt-in.
13111788b7ffSJilles Tjoelker.It Va EDITOR
13121788b7ffSJilles TjoelkerThe fallback editor used with the
13131788b7ffSJilles Tjoelker.Ic fc
13141788b7ffSJilles Tjoelkerbuilt-in.
13151788b7ffSJilles TjoelkerIf not set, the default editor is
13161788b7ffSJilles Tjoelker.Xr ed 1 .
13171788b7ffSJilles Tjoelker.It Va FCEDIT
13181788b7ffSJilles TjoelkerThe default editor used with the
13191788b7ffSJilles Tjoelker.Ic fc
13201788b7ffSJilles Tjoelkerbuilt-in.
13211788b7ffSJilles Tjoelker.It Va HISTSIZE
13221788b7ffSJilles TjoelkerThe number of previous commands that are accessible.
13231788b7ffSJilles Tjoelker.It Va HOME
13241788b7ffSJilles TjoelkerThe user's home directory,
13251788b7ffSJilles Tjoelkerused in tilde expansion and as a default directory for the
13261788b7ffSJilles Tjoelker.Ic cd
13271788b7ffSJilles Tjoelkerbuilt-in.
13281788b7ffSJilles Tjoelker.It Va IFS
13291788b7ffSJilles TjoelkerInput Field Separators.
1330ad56ebdcSJilles TjoelkerThe default value is
13311788b7ffSJilles Tjoelker.Aq space ,
13321788b7ffSJilles Tjoelker.Aq tab ,
13331788b7ffSJilles Tjoelkerand
1334ad56ebdcSJilles Tjoelker.Aq newline
1335ad56ebdcSJilles Tjoelkerin that order.
1336ad56ebdcSJilles TjoelkerThis default also applies if
1337ad56ebdcSJilles Tjoelker.Va IFS
1338ad56ebdcSJilles Tjoelkeris unset, but not if it is set to the empty string.
13391788b7ffSJilles TjoelkerSee the
13401788b7ffSJilles Tjoelker.Sx White Space Splitting
13411788b7ffSJilles Tjoelkersection for more details.
13421788b7ffSJilles Tjoelker.It Va LINENO
13431788b7ffSJilles TjoelkerThe current line number in the script or function.
13441788b7ffSJilles Tjoelker.It Va MAIL
13451788b7ffSJilles TjoelkerThe name of a mail file, that will be checked for the arrival of new
13461788b7ffSJilles Tjoelkermail.
13471788b7ffSJilles TjoelkerOverridden by
13481788b7ffSJilles Tjoelker.Va MAILPATH .
13491788b7ffSJilles Tjoelker.It Va MAILPATH
13501788b7ffSJilles TjoelkerA colon
13511788b7ffSJilles Tjoelker.Pq Ql \&:
13521788b7ffSJilles Tjoelkerseparated list of file names, for the shell to check for incoming
13531788b7ffSJilles Tjoelkermail.
13541788b7ffSJilles TjoelkerThis variable overrides the
13551788b7ffSJilles Tjoelker.Va MAIL
13561788b7ffSJilles Tjoelkersetting.
13571788b7ffSJilles TjoelkerThere is a maximum of 10 mailboxes that can be monitored at once.
13581788b7ffSJilles Tjoelker.It Va PATH
13591788b7ffSJilles TjoelkerThe default search path for executables.
13601788b7ffSJilles TjoelkerSee the
13611788b7ffSJilles Tjoelker.Sx Path Search
13621788b7ffSJilles Tjoelkersection for details.
13631788b7ffSJilles Tjoelker.It Va PPID
13641788b7ffSJilles TjoelkerThe parent process ID of the invoked shell.
13651788b7ffSJilles TjoelkerThis is set at startup
13661788b7ffSJilles Tjoelkerunless this variable is in the environment.
13671788b7ffSJilles TjoelkerA later change of parent process ID is not reflected.
13681788b7ffSJilles TjoelkerA subshell retains the same value of
13691788b7ffSJilles Tjoelker.Va PPID .
13701788b7ffSJilles Tjoelker.It Va PS1
13711788b7ffSJilles TjoelkerThe primary prompt string, which defaults to
13721788b7ffSJilles Tjoelker.Dq Li "$ " ,
13731788b7ffSJilles Tjoelkerunless you are the superuser, in which case it defaults to
13741788b7ffSJilles Tjoelker.Dq Li "# " .
13751ca3beb9SJoel Dahl.Va PS1
13761ca3beb9SJoel Dahlmay include any of the following formatting sequences,
13771ca3beb9SJoel Dahlwhich are replaced by the given information:
13781ca3beb9SJoel Dahl.Bl -tag -width indent
13791ca3beb9SJoel Dahl.It Li \eH
13801ca3beb9SJoel DahlThe local hostname.
13811ca3beb9SJoel Dahl.It Li \eh
13821ca3beb9SJoel DahlThe fully-qualified hostname.
13831ca3beb9SJoel Dahl.It Li \eW
13841ca3beb9SJoel DahlThe final component of the current working directory.
13851ca3beb9SJoel Dahl.It Li \ew
13861ca3beb9SJoel DahlThe entire path of the current working directory.
13871ca3beb9SJoel Dahl.It Li \e$
13881ca3beb9SJoel DahlSuperuser status.
13891ca3beb9SJoel Dahl.Dq Li "$ "
13901ca3beb9SJoel Dahlfor normal users and
13911ca3beb9SJoel Dahl.Dq Li "# "
13921ca3beb9SJoel Dahlfor superusers.
13931ca3beb9SJoel Dahl.It Li \e\e
13941ca3beb9SJoel DahlA literal backslash.
13951ca3beb9SJoel Dahl.El
13961788b7ffSJilles Tjoelker.It Va PS2
13971788b7ffSJilles TjoelkerThe secondary prompt string, which defaults to
13981788b7ffSJilles Tjoelker.Dq Li "> " .
13991ca3beb9SJoel Dahl.Va PS2
14001ca3beb9SJoel Dahlmay include any of the formatting sequences from
14011ca3beb9SJoel Dahl.Va PS1 .
14021788b7ffSJilles Tjoelker.It Va PS4
14031788b7ffSJilles TjoelkerThe prefix for the trace output (if
14041788b7ffSJilles Tjoelker.Fl x
14051788b7ffSJilles Tjoelkeris active).
14061788b7ffSJilles TjoelkerThe default is
14071788b7ffSJilles Tjoelker.Dq Li "+ " .
14081788b7ffSJilles Tjoelker.El
1409ab72124fSSteve Price.Ss Word Expansions
14104b88c807SRodney W. GrimesThis clause describes the various expansions that are
1411eccea571SRuslan Ermilovperformed on words.
1412eccea571SRuslan ErmilovNot all expansions are performed on
14134b88c807SRodney W. Grimesevery word, as explained later.
1414ab72124fSSteve Price.Pp
14154b88c807SRodney W. GrimesTilde expansions, parameter expansions, command substitutions,
14164b88c807SRodney W. Grimesarithmetic expansions, and quote removals that occur within
1417eccea571SRuslan Ermilova single word expand to a single field.
1418eccea571SRuslan ErmilovIt is only field
14194b88c807SRodney W. Grimessplitting or pathname expansion that can create multiple
1420e6d3cf26SSheldon Hearnfields from a single word.
1421e6d3cf26SSheldon HearnThe single exception to this rule is
1422e918fc8fSSheldon Hearnthe expansion of the special parameter
1423dfe302abSRuslan Ermilov.Va @
1424e918fc8fSSheldon Hearnwithin double-quotes,
1425e6d3cf26SSheldon Hearnas was described above.
1426ab72124fSSteve Price.Pp
14274b88c807SRodney W. GrimesThe order of word expansion is:
1428ab72124fSSteve Price.Bl -enum
1429ab72124fSSteve Price.It
1430ab72124fSSteve PriceTilde Expansion, Parameter Expansion, Command Substitution,
14314b88c807SRodney W. GrimesArithmetic Expansion (these all occur at the same time).
1432ab72124fSSteve Price.It
143349e11e3aSSheldon HearnField Splitting is performed on fields generated by step (1)
143449e11e3aSSheldon Hearnunless the
1435dfe302abSRuslan Ermilov.Va IFS
143649e11e3aSSheldon Hearnvariable is null.
1437ab72124fSSteve Price.It
143849e11e3aSSheldon HearnPathname Expansion (unless the
143949e11e3aSSheldon Hearn.Fl f
144049e11e3aSSheldon Hearnoption is in effect).
1441ab72124fSSteve Price.It
1442ab72124fSSteve PriceQuote Removal.
1443ab72124fSSteve Price.El
1444ab72124fSSteve Price.Pp
144549e11e3aSSheldon HearnThe
1446dfe302abSRuslan Ermilov.Ql $
144749e11e3aSSheldon Hearncharacter is used to introduce parameter expansion, command
144836cf3efeSJilles Tjoelkersubstitution, or arithmetic expansion.
1449ab72124fSSteve Price.Ss Tilde Expansion (substituting a user's home directory)
145049e11e3aSSheldon HearnA word beginning with an unquoted tilde character
1451a762afe5SGiorgos Keramidas.Pq Ql ~
145249e11e3aSSheldon Hearnis
145349e11e3aSSheldon Hearnsubjected to tilde expansion.
145449e11e3aSSheldon HearnAll the characters up to a slash
1455a762afe5SGiorgos Keramidas.Pq Ql /
145649e11e3aSSheldon Hearnor the end of the word are treated as a username
1457eccea571SRuslan Ermilovand are replaced with the user's home directory.
1458eccea571SRuslan ErmilovIf the
1459dfe302abSRuslan Ermilovusername is missing (as in
1460dfe302abSRuslan Ermilov.Pa ~/foobar ) ,
1461dfe302abSRuslan Ermilovthe tilde is replaced with the value of the
14621788b7ffSJilles Tjoelker.Va HOME
1463dfe302abSRuslan Ermilovvariable (the current user's home directory).
1464ab72124fSSteve Price.Ss Parameter Expansion
14654b88c807SRodney W. GrimesThe format for parameter expansion is as follows:
1466ab72124fSSteve Price.Pp
1467dfe302abSRuslan Ermilov.D1 Li ${ Ns Ar expression Ns Li }
1468dfe302abSRuslan Ermilov.Pp
1469dfe302abSRuslan Ermilovwhere
1470dfe302abSRuslan Ermilov.Ar expression
1471dfe302abSRuslan Ermilovconsists of all characters until the matching
1472dfe302abSRuslan Ermilov.Ql } .
147349e11e3aSSheldon HearnAny
1474dfe302abSRuslan Ermilov.Ql }
1475f35d74beSJilles Tjoelkerescaped by a backslash or within a single-quoted or double-quoted
1476f35d74beSJilles Tjoelkerstring, and characters in
14774b88c807SRodney W. Grimesembedded arithmetic expansions, command substitutions, and variable
147849e11e3aSSheldon Hearnexpansions, are not examined in determining the matching
1479dfe302abSRuslan Ermilov.Ql } .
1480f35d74beSJilles TjoelkerIf the variants with
1481227c8e2aSJilles Tjoelker.Ql + ,
1482227c8e2aSJilles Tjoelker.Ql - ,
1483227c8e2aSJilles Tjoelker.Ql =
1484227c8e2aSJilles Tjoelkeror
1485f35d74beSJilles Tjoelker.Ql ?\&
1486f35d74beSJilles Tjoelkeroccur within a double-quoted string,
1487f35d74beSJilles Tjoelkeras an extension there may be unquoted parts
1488f35d74beSJilles Tjoelker(via double-quotes inside the expansion);
1489227c8e2aSJilles Tjoelker.Ql }
1490f35d74beSJilles Tjoelkerwithin such parts are also not examined in determining the matching
1491227c8e2aSJilles Tjoelker.Ql } .
1492ab72124fSSteve Price.Pp
14934b88c807SRodney W. GrimesThe simplest form for parameter expansion is:
1494ab72124fSSteve Price.Pp
1495dfe302abSRuslan Ermilov.D1 Li ${ Ns Ar parameter Ns Li }
1496dfe302abSRuslan Ermilov.Pp
1497dfe302abSRuslan ErmilovThe value, if any, of
1498dfe302abSRuslan Ermilov.Ar parameter
1499dfe302abSRuslan Ermilovis substituted.
1500ab72124fSSteve Price.Pp
15014b88c807SRodney W. GrimesThe parameter name or symbol can be enclosed in braces, which are
15024b88c807SRodney W. Grimesoptional except for positional parameters with more than one digit or
15034b88c807SRodney W. Grimeswhen parameter is followed by a character that could be interpreted as
15044b88c807SRodney W. Grimespart of the name.
1505ab72124fSSteve PriceIf a parameter expansion occurs inside double-quotes:
1506ab72124fSSteve Price.Bl -enum
1507ab72124fSSteve Price.It
1508ab72124fSSteve PriceField splitting is not performed on the results of the
1509e918fc8fSSheldon Hearnexpansion, with the exception of the special parameter
1510dfe302abSRuslan Ermilov.Va @ .
1511ad56ebdcSJilles Tjoelker.It
1512ad56ebdcSJilles TjoelkerPathname expansion is not performed on the results of the
1513ad56ebdcSJilles Tjoelkerexpansion.
1514ab72124fSSteve Price.El
1515ab72124fSSteve Price.Pp
15164b88c807SRodney W. GrimesIn addition, a parameter expansion can be modified by using one of the
15174b88c807SRodney W. Grimesfollowing formats.
151881e9cda2SRuslan Ermilov.Bl -tag -width indent
1519dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li :- Ns Ar word Ns Li }
1520eccea571SRuslan ErmilovUse Default Values.
1521dfe302abSRuslan ErmilovIf
1522dfe302abSRuslan Ermilov.Ar parameter
1523dfe302abSRuslan Ermilovis unset or null, the expansion of
1524dfe302abSRuslan Ermilov.Ar word
1525dfe302abSRuslan Ermilovis substituted; otherwise, the value of
1526dfe302abSRuslan Ermilov.Ar parameter
1527dfe302abSRuslan Ermilovis substituted.
1528dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li := Ns Ar word Ns Li }
1529eccea571SRuslan ErmilovAssign Default Values.
1530dfe302abSRuslan ErmilovIf
1531dfe302abSRuslan Ermilov.Ar parameter
1532dfe302abSRuslan Ermilovis unset or null, the expansion of
1533dfe302abSRuslan Ermilov.Ar word
1534dfe302abSRuslan Ermilovis assigned to
1535dfe302abSRuslan Ermilov.Ar parameter .
1536eccea571SRuslan ErmilovIn all cases, the
1537dfe302abSRuslan Ermilovfinal value of
1538dfe302abSRuslan Ermilov.Ar parameter
1539dfe302abSRuslan Ermilovis substituted.
1540b139165cSJilles TjoelkerQuoting inside
1541b139165cSJilles Tjoelker.Ar word
1542b139165cSJilles Tjoelkerdoes not prevent field splitting or pathname expansion.
1543eccea571SRuslan ErmilovOnly variables, not positional
15444b88c807SRodney W. Grimesparameters or special parameters, can be
15454b88c807SRodney W. Grimesassigned in this way.
1546dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li :? Ns Oo Ar word Oc Ns Li }
1547eccea571SRuslan ErmilovIndicate Error if Null or Unset.
1548eccea571SRuslan ErmilovIf
1549dfe302abSRuslan Ermilov.Ar parameter
1550dfe302abSRuslan Ermilovis unset or null, the expansion of
1551dfe302abSRuslan Ermilov.Ar word
1552dfe302abSRuslan Ermilov(or a message indicating it is unset if
1553dfe302abSRuslan Ermilov.Ar word
1554dfe302abSRuslan Ermilovis omitted) is written to standard
15554b88c807SRodney W. Grimeserror and the shell exits with a nonzero
1556e6d3cf26SSheldon Hearnexit status.
1557e6d3cf26SSheldon HearnOtherwise, the value of
1558dfe302abSRuslan Ermilov.Ar parameter
1559dfe302abSRuslan Ermilovis substituted.
1560eccea571SRuslan ErmilovAn
15614b88c807SRodney W. Grimesinteractive shell need not exit.
1562dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li :+ Ns Ar word Ns Li }
1563eccea571SRuslan ErmilovUse Alternate Value.
1564dfe302abSRuslan ErmilovIf
1565dfe302abSRuslan Ermilov.Ar parameter
1566dfe302abSRuslan Ermilovis unset or null, null is substituted;
1567dfe302abSRuslan Ermilovotherwise, the expansion of
1568dfe302abSRuslan Ermilov.Ar word
1569dfe302abSRuslan Ermilovis substituted.
15701413f7deSTony Finch.El
1571ab72124fSSteve Price.Pp
15724b88c807SRodney W. GrimesIn the parameter expansions shown previously, use of the colon in the
15734b88c807SRodney W. Grimesformat results in a test for a parameter that is unset or null; omission
15744b88c807SRodney W. Grimesof the colon results in a test for a parameter that is only unset.
1575227c8e2aSJilles Tjoelker.Pp
1576227c8e2aSJilles TjoelkerThe
1577227c8e2aSJilles Tjoelker.Ar word
1578227c8e2aSJilles Tjoelkerinherits the type of quoting
1579227c8e2aSJilles Tjoelker(unquoted, double-quoted or here-document)
1580227c8e2aSJilles Tjoelkerfrom the surroundings,
1581227c8e2aSJilles Tjoelkerwith the exception that a backslash that quotes a closing brace is removed
1582227c8e2aSJilles Tjoelkerduring quote removal.
15831413f7deSTony Finch.Bl -tag -width indent
1584dfe302abSRuslan Ermilov.It Li ${# Ns Ar parameter Ns Li }
1585eccea571SRuslan ErmilovString Length.
1586eccea571SRuslan ErmilovThe length in characters of
1587dfe302abSRuslan Ermilovthe value of
1588dfe302abSRuslan Ermilov.Ar parameter .
158971337c33SSheldon Hearn.El
1590ab72124fSSteve Price.Pp
15914b88c807SRodney W. GrimesThe following four varieties of parameter expansion provide for substring
159249e11e3aSSheldon Hearnprocessing.
159349e11e3aSSheldon HearnIn each case, pattern matching notation
159449e11e3aSSheldon Hearn(see
159549e11e3aSSheldon Hearn.Sx Shell Patterns ) ,
159649e11e3aSSheldon Hearnrather than regular expression notation,
159749e11e3aSSheldon Hearnis used to evaluate the patterns.
1598e918fc8fSSheldon HearnIf parameter is one of the special parameters
1599dfe302abSRuslan Ermilov.Va *
160049e11e3aSSheldon Hearnor
1601dfe302abSRuslan Ermilov.Va @ ,
160249e11e3aSSheldon Hearnthe result of the expansion is unspecified.
16034b88c807SRodney W. GrimesEnclosing the full parameter expansion string in double-quotes does not
16044b88c807SRodney W. Grimescause the following four varieties of pattern characters to be quoted,
16054b88c807SRodney W. Grimeswhereas quoting characters within the braces has this effect.
160681e9cda2SRuslan Ermilov.Bl -tag -width indent
1607dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li % Ns Ar word Ns Li }
1608eccea571SRuslan ErmilovRemove Smallest Suffix Pattern.
1609dfe302abSRuslan ErmilovThe
1610dfe302abSRuslan Ermilov.Ar word
1611eccea571SRuslan Ermilovis expanded to produce a pattern.
1612eccea571SRuslan ErmilovThe
16134b88c807SRodney W. Grimesparameter expansion then results in
1614dfe302abSRuslan Ermilov.Ar parameter ,
1615dfe302abSRuslan Ermilovwith the smallest portion of the
16164b88c807SRodney W. Grimessuffix matched by the pattern deleted.
1617dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li %% Ns Ar word Ns Li }
1618eccea571SRuslan ErmilovRemove Largest Suffix Pattern.
1619dfe302abSRuslan ErmilovThe
1620dfe302abSRuslan Ermilov.Ar word
1621eccea571SRuslan Ermilovis expanded to produce a pattern.
1622eccea571SRuslan ErmilovThe
16234b88c807SRodney W. Grimesparameter expansion then results in
1624dfe302abSRuslan Ermilov.Ar parameter ,
1625dfe302abSRuslan Ermilovwith the largest portion of the
16264b88c807SRodney W. Grimessuffix matched by the pattern deleted.
1627dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li # Ns Ar word Ns Li }
1628eccea571SRuslan ErmilovRemove Smallest Prefix Pattern.
1629dfe302abSRuslan ErmilovThe
1630dfe302abSRuslan Ermilov.Ar word
1631eccea571SRuslan Ermilovis expanded to produce a pattern.
1632eccea571SRuslan ErmilovThe
16334b88c807SRodney W. Grimesparameter expansion then results in
1634dfe302abSRuslan Ermilov.Ar parameter ,
1635dfe302abSRuslan Ermilovwith the smallest portion of the
16364b88c807SRodney W. Grimesprefix matched by the pattern deleted.
1637dfe302abSRuslan Ermilov.It Li ${ Ns Ar parameter Ns Li ## Ns Ar word Ns Li }
1638eccea571SRuslan ErmilovRemove Largest Prefix Pattern.
1639dfe302abSRuslan ErmilovThe
1640dfe302abSRuslan Ermilov.Ar word
1641eccea571SRuslan Ermilovis expanded to produce a pattern.
1642eccea571SRuslan ErmilovThe
16434b88c807SRodney W. Grimesparameter expansion then results in
1644dfe302abSRuslan Ermilov.Ar parameter ,
1645dfe302abSRuslan Ermilovwith the largest portion of the
16464b88c807SRodney W. Grimesprefix matched by the pattern deleted.
1647ab72124fSSteve Price.El
1648ab72124fSSteve Price.Ss Command Substitution
16494b88c807SRodney W. GrimesCommand substitution allows the output of a command to be substituted in
1650eccea571SRuslan Ermilovplace of the command name itself.
1651eccea571SRuslan ErmilovCommand substitution occurs when
16524b88c807SRodney W. Grimesthe command is enclosed as follows:
1653dfe302abSRuslan Ermilov.Pp
1654dfe302abSRuslan Ermilov.D1 Li $( Ns Ar command Ns Li )\&
1655ab72124fSSteve Price.Pp
165649e11e3aSSheldon Hearnor the backquoted version:
1657dfe302abSRuslan Ermilov.Pp
1658dfe302abSRuslan Ermilov.D1 Li ` Ns Ar command Ns Li `
1659ab72124fSSteve Price.Pp
1660ef89d04fSJilles TjoelkerThe shell expands the command substitution by executing command
1661ef89d04fSJilles Tjoelkerand replacing the command substitution
166249e11e3aSSheldon Hearnwith the standard output of the command,
166349e11e3aSSheldon Hearnremoving sequences of one or more newlines at the end of the substitution.
166449e11e3aSSheldon HearnEmbedded newlines before the end of the output are not removed;
166549e11e3aSSheldon Hearnhowever, during field splitting, they may be translated into spaces
166649e11e3aSSheldon Hearndepending on the value of
1667dfe302abSRuslan Ermilov.Va IFS
166849e11e3aSSheldon Hearnand the quoting that is in effect.
1669ef89d04fSJilles TjoelkerThe command is executed in a subshell environment,
1670ef89d04fSJilles Tjoelkerexcept that the built-in commands
1671ef89d04fSJilles Tjoelker.Ic jobid ,
1672ef89d04fSJilles Tjoelker.Ic jobs ,
1673ef89d04fSJilles Tjoelkerand
1674ef89d04fSJilles Tjoelker.Ic trap
167551a514adSJilles Tjoelkerreturn information about the parent shell environment
167651a514adSJilles Tjoelkerand
167751a514adSJilles Tjoelker.Ic times
167851a514adSJilles Tjoelkerreturns information about the same process
1679c543e1aeSJilles Tjoelkerif they are the only command in a command substitution.
168054ccc8b5SJilles Tjoelker.Pp
168154ccc8b5SJilles TjoelkerIf a command substitution of the
168254ccc8b5SJilles Tjoelker.Li $(
168354ccc8b5SJilles Tjoelkerform begins with a subshell,
168454ccc8b5SJilles Tjoelkerthe
168554ccc8b5SJilles Tjoelker.Li $(
168654ccc8b5SJilles Tjoelkerand
16875127efa3SJoel Dahl.Li (\&
168854ccc8b5SJilles Tjoelkermust be separated by whitespace
168954ccc8b5SJilles Tjoelkerto avoid ambiguity with arithmetic expansion.
1690ab72124fSSteve Price.Ss Arithmetic Expansion
16914b88c807SRodney W. GrimesArithmetic expansion provides a mechanism for evaluating an arithmetic
1692e6d3cf26SSheldon Hearnexpression and substituting its value.
1693e6d3cf26SSheldon HearnThe format for arithmetic expansion is as follows:
1694ab72124fSSteve Price.Pp
1695dfe302abSRuslan Ermilov.D1 Li $(( Ns Ar expression Ns Li ))
1696dfe302abSRuslan Ermilov.Pp
1697dfe302abSRuslan ErmilovThe
1698dfe302abSRuslan Ermilov.Ar expression
1699dfe302abSRuslan Ermilovis treated as if it were in double-quotes, except
1700eccea571SRuslan Ermilovthat a double-quote inside the expression is not treated specially.
1701eccea571SRuslan ErmilovThe
1702dfe302abSRuslan Ermilovshell expands all tokens in the
1703dfe302abSRuslan Ermilov.Ar expression
1704dfe302abSRuslan Ermilovfor parameter expansion,
170536cf3efeSJilles Tjoelkercommand substitution,
170636cf3efeSJilles Tjoelkerarithmetic expansion
170736cf3efeSJilles Tjoelkerand quote removal.
1708ab72124fSSteve Price.Pp
170936cf3efeSJilles TjoelkerThe allowed expressions are a subset of C expressions,
171036cf3efeSJilles Tjoelkersummarized below.
171136cf3efeSJilles Tjoelker.Bl -tag -width "Variables" -offset indent
171236cf3efeSJilles Tjoelker.It Values
171336cf3efeSJilles TjoelkerAll values are of type
171436cf3efeSJilles Tjoelker.Ft intmax_t .
171536cf3efeSJilles Tjoelker.It Constants
171636cf3efeSJilles TjoelkerDecimal, octal (starting with
171736cf3efeSJilles Tjoelker.Li 0 )
171836cf3efeSJilles Tjoelkerand hexadecimal (starting with
171936cf3efeSJilles Tjoelker.Li 0x )
172036cf3efeSJilles Tjoelkerinteger constants.
172136cf3efeSJilles Tjoelker.It Variables
172236cf3efeSJilles TjoelkerShell variables can be read and written
172336cf3efeSJilles Tjoelkerand contain integer constants.
172436cf3efeSJilles Tjoelker.It Unary operators
172536cf3efeSJilles Tjoelker.Li "! ~ + -"
172636cf3efeSJilles Tjoelker.It Binary operators
172736cf3efeSJilles Tjoelker.Li "* / % + - << >> < <= > >= == != & ^ | && ||"
172836cf3efeSJilles Tjoelker.It Assignment operators
172936cf3efeSJilles Tjoelker.Li "= += -= *= /= %= <<= >>= &= ^= |="
1730075b72efSJilles Tjoelker.It Conditional operator
1731075b72efSJilles Tjoelker.Li "? :"
173236cf3efeSJilles Tjoelker.El
173336cf3efeSJilles Tjoelker.Pp
173436cf3efeSJilles TjoelkerThe result of the expression is substituted in decimal.
1735ab72124fSSteve Price.Ss White Space Splitting (Field Splitting)
1736ad56ebdcSJilles TjoelkerIn certain contexts,
1737ad56ebdcSJilles Tjoelkerafter parameter expansion, command substitution, and
17384b88c807SRodney W. Grimesarithmetic expansion the shell scans the results of
17394b88c807SRodney W. Grimesexpansions and substitutions that did not occur in double-quotes for
17404b88c807SRodney W. Grimesfield splitting and multiple fields can result.
1741ab72124fSSteve Price.Pp
1742ad56ebdcSJilles TjoelkerCharacters in
1743dfe302abSRuslan Ermilov.Va IFS
1744ad56ebdcSJilles Tjoelkerthat are whitespace
1745ad56ebdcSJilles Tjoelker.Po
1746ad56ebdcSJilles Tjoelker.Aq space ,
1747ad56ebdcSJilles Tjoelker.Aq tab ,
1748ad56ebdcSJilles Tjoelkerand
1749ad56ebdcSJilles Tjoelker.Aq newline
1750ad56ebdcSJilles Tjoelker.Pc
1751ad56ebdcSJilles Tjoelkerare treated differently from other characters in
1752ad56ebdcSJilles Tjoelker.Va IFS .
1753ad56ebdcSJilles Tjoelker.Pp
1754ad56ebdcSJilles TjoelkerWhitespace in
1755ad56ebdcSJilles Tjoelker.Va IFS
1756ad56ebdcSJilles Tjoelkerat the beginning or end of a word is discarded.
1757ad56ebdcSJilles Tjoelker.Pp
1758ad56ebdcSJilles TjoelkerSubsequently, a field is delimited by either
1759ad56ebdcSJilles Tjoelker.Bl -enum
1760ad56ebdcSJilles Tjoelker.It
1761ad56ebdcSJilles Tjoelkera non-whitespace character in
1762ad56ebdcSJilles Tjoelker.Va IFS
1763ad56ebdcSJilles Tjoelkerwith any whitespace in
1764ad56ebdcSJilles Tjoelker.Va IFS
1765ad56ebdcSJilles Tjoelkersurrounding it, or
1766ad56ebdcSJilles Tjoelker.It
1767ad56ebdcSJilles Tjoelkerone or more whitespace characters in
1768ad56ebdcSJilles Tjoelker.Va IFS .
1769ad56ebdcSJilles Tjoelker.El
1770ad56ebdcSJilles Tjoelker.Pp
1771ad56ebdcSJilles TjoelkerIf a word ends with a non-whitespace character in
1772ad56ebdcSJilles Tjoelker.Va IFS ,
1773ad56ebdcSJilles Tjoelkerthere is no empty field after this character.
1774ad56ebdcSJilles Tjoelker.Pp
1775ad56ebdcSJilles TjoelkerIf no field is delimited, the word is discarded.
1776ad56ebdcSJilles TjoelkerIn particular, if a word consists solely of an unquoted substitution
1777ad56ebdcSJilles Tjoelkerand the result of the substitution is null,
1778ad56ebdcSJilles Tjoelkerit is removed by field splitting even if
1779ad56ebdcSJilles Tjoelker.Va IFS
1780ad56ebdcSJilles Tjoelkeris null.
1781ab72124fSSteve Price.Ss Pathname Expansion (File Name Generation)
178249e11e3aSSheldon HearnUnless the
178349e11e3aSSheldon Hearn.Fl f
178449e11e3aSSheldon Hearnoption is set,
178549e11e3aSSheldon Hearnfile name generation is performed
1786eccea571SRuslan Ermilovafter word splitting is complete.
1787eccea571SRuslan ErmilovEach word is
1788eccea571SRuslan Ermilovviewed as a series of patterns, separated by slashes.
1789eccea571SRuslan ErmilovThe
17904b88c807SRodney W. Grimesprocess of expansion replaces the word with the names of
17914b88c807SRodney W. Grimesall existing files whose names can be formed by replacing
17924b88c807SRodney W. Grimeseach pattern with a string that matches the specified pattern.
1793ab0a2172SSteve PriceThere are two restrictions on this: first, a pattern cannot match
1794ab0a2172SSteve Pricea string containing a slash, and second,
17954b88c807SRodney W. Grimesa pattern cannot match a string starting with a period
17964b88c807SRodney W. Grimesunless the first character of the pattern is a period.
1797519c4ef3SJilles TjoelkerThe next section describes the patterns used for
1798519c4ef3SJilles TjoelkerPathname Expansion,
1799519c4ef3SJilles Tjoelkerthe four varieties of parameter expansion for substring processing and the
1800c644db6aSSheldon Hearn.Ic case
1801ab72124fSSteve Pricecommand.
1802ab72124fSSteve Price.Ss Shell Patterns
1803f434d1dfSAdam DavidA pattern consists of normal characters, which match themselves,
180449e11e3aSSheldon Hearnand meta-characters.
180549e11e3aSSheldon HearnThe meta-characters are
1806dfe302abSRuslan Ermilov.Ql * ,
1807dfe302abSRuslan Ermilov.Ql \&? ,
18086b15476eSSheldon Hearnand
1809dfe302abSRuslan Ermilov.Ql \&[ .
18106b15476eSSheldon HearnThese characters lose their special meanings if they are quoted.
18116b15476eSSheldon HearnWhen command or variable substitution is performed and the dollar sign
18126b15476eSSheldon Hearnor back quotes are not double-quoted, the value of the
18134b88c807SRodney W. Grimesvariable or the output of the command is scanned for these
18144b88c807SRodney W. Grimescharacters and they are turned into meta-characters.
1815ab72124fSSteve Price.Pp
18166b15476eSSheldon HearnAn asterisk
1817a762afe5SGiorgos Keramidas.Pq Ql *
1818e918fc8fSSheldon Hearnmatches any string of characters.
1819e918fc8fSSheldon HearnA question mark
1820a762afe5SGiorgos Keramidas.Pq Ql \&?
1821e918fc8fSSheldon Hearnmatches any single character.
1822e6d3cf26SSheldon HearnA left bracket
1823dfe302abSRuslan Ermilov.Pq Ql \&[
1824e6d3cf26SSheldon Hearnintroduces a character class.
1825e6d3cf26SSheldon HearnThe end of the character class is indicated by a
1826dfe302abSRuslan Ermilov.Ql \&] ;
18276b15476eSSheldon Hearnif the
1828dfe302abSRuslan Ermilov.Ql \&]
18296b15476eSSheldon Hearnis missing then the
1830dfe302abSRuslan Ermilov.Ql \&[
18316b15476eSSheldon Hearnmatches a
1832dfe302abSRuslan Ermilov.Ql \&[
1833e6d3cf26SSheldon Hearnrather than introducing a character class.
1834e6d3cf26SSheldon HearnA character class matches any of the characters between the square brackets.
1835519c4ef3SJilles TjoelkerA locale-dependent range of characters may be specified using a minus sign.
1836ff4dc672SJilles TjoelkerA named class of characters (see
1837ff4dc672SJilles Tjoelker.Xr wctype 3 )
1838ff4dc672SJilles Tjoelkermay be specified by surrounding the name with
1839ff4dc672SJilles Tjoelker.Ql \&[:
1840ff4dc672SJilles Tjoelkerand
1841ff4dc672SJilles Tjoelker.Ql :\&] .
1842ff4dc672SJilles TjoelkerFor example,
1843ff4dc672SJilles Tjoelker.Ql \&[\&[:alpha:\&]\&]
1844ff4dc672SJilles Tjoelkeris a shell pattern that matches a single letter.
1845e918fc8fSSheldon HearnThe character class may be complemented by making an exclamation point
1846a762afe5SGiorgos Keramidas.Pq Ql !\&
1847e918fc8fSSheldon Hearnthe first character of the character class.
1848519c4ef3SJilles TjoelkerA caret
1849519c4ef3SJilles Tjoelker.Pq Ql ^
1850519c4ef3SJilles Tjoelkerhas the same effect but is non-standard.
1851ab72124fSSteve Price.Pp
18526b15476eSSheldon HearnTo include a
1853dfe302abSRuslan Ermilov.Ql \&]
1854e6d3cf26SSheldon Hearnin a character class, make it the first character listed
1855e6d3cf26SSheldon Hearn(after the
1856519c4ef3SJilles Tjoelker.Ql \&!
1857519c4ef3SJilles Tjoelkeror
1858519c4ef3SJilles Tjoelker.Ql ^ ,
1859e6d3cf26SSheldon Hearnif any).
1860e918fc8fSSheldon HearnTo include a
1861dfe302abSRuslan Ermilov.Ql - ,
1862e918fc8fSSheldon Hearnmake it the first or last character listed.
18638eed22b1SJens Schweikhardt.Ss Built-in Commands
186425f6b31fSJilles TjoelkerThis section lists the built-in commands.
186581e9cda2SRuslan Ermilov.Bl -tag -width indent
1866d0353b83SRuslan Ermilov.It Ic \&:
1867aa9caaf6SPeter WemmA null command that returns a 0 (true) exit value.
1868d0353b83SRuslan Ermilov.It Ic \&. Ar file
1869aa9caaf6SPeter WemmThe commands in the specified file are read and executed by the shell.
18704ad8968aSBrian FeldmanThe
18714ad8968aSBrian Feldman.Ic return
18724ad8968aSBrian Feldmancommand may be used to return to the
18734ad8968aSBrian Feldman.Ic \&.
18744ad8968aSBrian Feldmancommand's caller.
18756466945aSJohn PolstraIf
18766466945aSJohn Polstra.Ar file
18776466945aSJohn Polstracontains any
1878dfe302abSRuslan Ermilov.Ql /
1879eccea571SRuslan Ermilovcharacters, it is used as is.
1880eccea571SRuslan ErmilovOtherwise, the shell searches the
18811788b7ffSJilles Tjoelker.Va PATH
1882eccea571SRuslan Ermilovfor the file.
1883eccea571SRuslan ErmilovIf it is not found in the
18841788b7ffSJilles Tjoelker.Va PATH ,
18856466945aSJohn Polstrait is sought in the current working directory.
188610845922SYaroslav Tykhiy.It Ic \&[
188710845922SYaroslav TykhiyA built-in equivalent of
188810845922SYaroslav Tykhiy.Xr test 1 .
1889a886288eSJesus R. Camou.It Ic alias Oo Ar name Ns Oo = Ns Ar string Oc ... Oc
189049e11e3aSSheldon HearnIf
1891d0353b83SRuslan Ermilov.Ar name Ns = Ns Ar string
189249e11e3aSSheldon Hearnis specified, the shell defines the alias
189349e11e3aSSheldon Hearn.Ar name
18946b15476eSSheldon Hearnwith value
189549e11e3aSSheldon Hearn.Ar string .
18966b15476eSSheldon HearnIf just
189749e11e3aSSheldon Hearn.Ar name
18986b15476eSSheldon Hearnis specified, the value of the alias
189949e11e3aSSheldon Hearn.Ar name
19006b15476eSSheldon Hearnis printed.
190149e11e3aSSheldon HearnWith no arguments, the
190249e11e3aSSheldon Hearn.Ic alias
19038eed22b1SJens Schweikhardtbuilt-in command prints the names and values of all defined aliases
190449e11e3aSSheldon Hearn(see
190549e11e3aSSheldon Hearn.Ic unalias ) .
1906e5341cbbSTim J. RobbinsAlias values are written with appropriate quoting so that they are
19079ba31ca0SRuslan Ermilovsuitable for re-input to the shell.
1908a886288eSJesus R. CamouAlso see the
1909a886288eSJesus R. Camou.Sx Aliases
1910a886288eSJesus R. Camousubsection.
191149e11e3aSSheldon Hearn.It Ic bg Op Ar job ...
191249e11e3aSSheldon HearnContinue the specified jobs
191349e11e3aSSheldon Hearn(or the current job if no jobs are given)
191449e11e3aSSheldon Hearnin the background.
191525f6b31fSJilles Tjoelker.It Ic bind Oo Fl aeklrsv Oc Oo Ar key Oo Ar command Oc Oc
191625f6b31fSJilles TjoelkerList or alter key bindings for the line editor.
191725f6b31fSJilles TjoelkerThis command is documented in
191825f6b31fSJilles Tjoelker.Xr editrc 5 .
191925f6b31fSJilles Tjoelker.It Ic break Op Ar num
192025f6b31fSJilles TjoelkerSee the
192125f6b31fSJilles Tjoelker.Sx Flow-Control Constructs
192225f6b31fSJilles Tjoelkersubsection.
1923df9e587bSTim J. Robbins.It Ic builtin Ar cmd Op Ar arg ...
19248eed22b1SJens SchweikhardtExecute the specified built-in command,
192549e11e3aSSheldon Hearn.Ar cmd .
192649e11e3aSSheldon HearnThis is useful when the user wishes to override a shell function
19278eed22b1SJens Schweikhardtwith the same name as a built-in command.
1928d6ee26adSJilles Tjoelker.It Ic cd Oo Fl L | P Oc Oo Fl e Oc Op Ar directory
192949e11e3aSSheldon HearnSwitch to the specified
193049e11e3aSSheldon Hearn.Ar directory ,
193149e11e3aSSheldon Hearnor to the directory specified in the
19321788b7ffSJilles Tjoelker.Va HOME
193349e11e3aSSheldon Hearnenvironment variable if no
193449e11e3aSSheldon Hearn.Ar directory
193549e11e3aSSheldon Hearnis specified.
193649e11e3aSSheldon HearnIf
193749e11e3aSSheldon Hearn.Ar directory
1938029c8ee4STim J. Robbinsdoes not begin with
19399ba31ca0SRuslan Ermilov.Pa / , \&. ,
194049e11e3aSSheldon Hearnor
1941029c8ee4STim J. Robbins.Pa .. ,
194249e11e3aSSheldon Hearnthen the directories listed in the
19431788b7ffSJilles Tjoelker.Va CDPATH
194449e11e3aSSheldon Hearnvariable will be
194549e11e3aSSheldon Hearnsearched for the specified
194649e11e3aSSheldon Hearn.Ar directory .
1947029c8ee4STim J. RobbinsIf
19481788b7ffSJilles Tjoelker.Va CDPATH
1949029c8ee4STim J. Robbinsis unset, the current directory is searched.
195049e11e3aSSheldon HearnThe format of
19511788b7ffSJilles Tjoelker.Va CDPATH
195249e11e3aSSheldon Hearnis the same as that of
19531788b7ffSJilles Tjoelker.Va PATH .
1954e6d3cf26SSheldon HearnIn an interactive shell,
195549e11e3aSSheldon Hearnthe
195649e11e3aSSheldon Hearn.Ic cd
195749e11e3aSSheldon Hearncommand will print out the name of the directory
1958e6d3cf26SSheldon Hearnthat it actually switched to
1959e6d3cf26SSheldon Hearnif this is different from the name that the user gave.
196049e11e3aSSheldon HearnThese may be different either because the
19611788b7ffSJilles Tjoelker.Va CDPATH
196249e11e3aSSheldon Hearnmechanism was used or because a symbolic link was crossed.
1963178897f1STim J. Robbins.Pp
1964178897f1STim J. RobbinsIf the
1965178897f1STim J. Robbins.Fl P
1966178897f1STim J. Robbinsoption is specified,
196781e9cda2SRuslan Ermilov.Pa ..
1968178897f1STim J. Robbinsis handled physically and symbolic links are resolved before
196981e9cda2SRuslan Ermilov.Pa ..
1970178897f1STim J. Robbinscomponents are processed.
1971178897f1STim J. RobbinsIf the
1972178897f1STim J. Robbins.Fl L
1973178897f1STim J. Robbinsoption is specified,
197481e9cda2SRuslan Ermilov.Pa ..
1975178897f1STim J. Robbinsis handled logically.
1976178897f1STim J. RobbinsThis is the default.
1977d6ee26adSJilles Tjoelker.Pp
1978d6ee26adSJilles TjoelkerThe
1979d6ee26adSJilles Tjoelker.Fl e
1980d6ee26adSJilles Tjoelkeroption causes
1981d6ee26adSJilles Tjoelker.Ic cd
1982d6ee26adSJilles Tjoelkerto return exit status 1 if the full pathname of the new directory
1983d6ee26adSJilles Tjoelkercannot be determined reliably or at all.
1984d6ee26adSJilles TjoelkerNormally this is not considered an error,
1985d6ee26adSJilles Tjoelkeralthough a warning is printed.
19869434a1c7SDima Dorfman.It Ic chdir
19879434a1c7SDima DorfmanA synonym for the
19889434a1c7SDima Dorfman.Ic cd
19898eed22b1SJens Schweikhardtbuilt-in command.
19909ba31ca0SRuslan Ermilov.It Ic command Oo Fl p Oc Op Ar utility Op Ar argument ...
1991b97d13b3SJilles Tjoelker.It Ic command Oo Fl p Oc Fl v Ar utility
1992b97d13b3SJilles Tjoelker.It Ic command Oo Fl p Oc Fl V Ar utility
199333b222b9SStefan FarfelederThe first form of invocation executes the specified
1994c848bc18SJilles Tjoelker.Ar utility ,
1995c848bc18SJilles Tjoelkerignoring shell functions in the search.
1996c848bc18SJilles TjoelkerIf
19972babaf74STim J. Robbins.Ar utility
1998c848bc18SJilles Tjoelkeris a special builtin,
1999c848bc18SJilles Tjoelkerit is executed as if it were a regular builtin.
20002babaf74STim J. Robbins.Pp
20012babaf74STim J. RobbinsIf the
20022babaf74STim J. Robbins.Fl p
20032babaf74STim J. Robbinsoption is specified, the command search is performed using a
20042babaf74STim J. Robbinsdefault value of
20051788b7ffSJilles Tjoelker.Va PATH
20062babaf74STim J. Robbinsthat is guaranteed to find all of the standard utilities.
200733b222b9SStefan Farfeleder.Pp
200833b222b9SStefan FarfelederIf the
200933b222b9SStefan Farfeleder.Fl v
201033b222b9SStefan Farfelederoption is specified,
201133b222b9SStefan Farfeleder.Ar utility
201233b222b9SStefan Farfelederis not executed but a description of its interpretation by the shell is
201333b222b9SStefan Farfelederprinted.
201433b222b9SStefan FarfelederFor ordinary commands the output is the path name; for shell built-in
201533b222b9SStefan Farfeledercommands, shell functions and keywords only the name is written.
201633b222b9SStefan FarfelederAliases are printed as
201733b222b9SStefan Farfeleder.Dq Ic alias Ar name Ns = Ns Ar value .
201833b222b9SStefan Farfeleder.Pp
201933b222b9SStefan FarfelederThe
202033b222b9SStefan Farfeleder.Fl V
202133b222b9SStefan Farfelederoption is identical to
202233b222b9SStefan Farfeleder.Fl v
202333b222b9SStefan Farfelederexcept for the output.
202433b222b9SStefan FarfelederIt prints
202533b222b9SStefan Farfeleder.Dq Ar utility Ic is Ar description
202633b222b9SStefan Farfelederwhere
202733b222b9SStefan Farfeleder.Ar description
202833b222b9SStefan Farfelederis either
202933b222b9SStefan Farfelederthe path name to
203033b222b9SStefan Farfeleder.Ar utility ,
203130268dfaSJilles Tjoelkera special shell builtin,
20328af11131SRuslan Ermilova shell builtin,
20338af11131SRuslan Ermilova shell function,
20348af11131SRuslan Ermilova shell keyword
203533b222b9SStefan Farfelederor
20368af11131SRuslan Ermilovan alias for
20378af11131SRuslan Ermilov.Ar value .
203825f6b31fSJilles Tjoelker.It Ic continue Op Ar num
203925f6b31fSJilles TjoelkerSee the
204025f6b31fSJilles Tjoelker.Sx Flow-Control Constructs
204125f6b31fSJilles Tjoelkersubsection.
204273313421SStefan Farfeleder.It Ic echo Oo Fl e | n Oc Op Ar string ...
204373313421SStefan FarfelederPrint a space-separated list of the arguments to the standard output
204473313421SStefan Farfelederand append a newline character.
204581e9cda2SRuslan Ermilov.Bl -tag -width indent
2046501e74b7SMark Ovens.It Fl n
2047501e74b7SMark OvensSuppress the output of the trailing newline.
2048501e74b7SMark Ovens.It Fl e
2049501e74b7SMark OvensProcess C-style backslash escape sequences.
2050dfe302abSRuslan ErmilovThe
2051501e74b7SMark Ovens.Ic echo
2052dfe302abSRuslan Ermilovcommand understands the following character escapes:
205381e9cda2SRuslan Ermilov.Bl -tag -width indent
2054501e74b7SMark Ovens.It \ea
2055501e74b7SMark OvensAlert (ring the terminal bell)
2056501e74b7SMark Ovens.It \eb
2057501e74b7SMark OvensBackspace
2058501e74b7SMark Ovens.It \ec
2059501e74b7SMark OvensSuppress the trailing newline (this has the side-effect of truncating the
2060501e74b7SMark Ovensline if it is not the last character)
2061501e74b7SMark Ovens.It \ee
2062dfe302abSRuslan ErmilovThe ESC character
2063dfe302abSRuslan Ermilov.Tn ( ASCII
2064dfe302abSRuslan Ermilov0x1b)
2065501e74b7SMark Ovens.It \ef
2066501e74b7SMark OvensFormfeed
2067501e74b7SMark Ovens.It \en
2068501e74b7SMark OvensNewline
2069501e74b7SMark Ovens.It \er
2070501e74b7SMark OvensCarriage return
2071501e74b7SMark Ovens.It \et
2072501e74b7SMark OvensHorizontal tab
2073501e74b7SMark Ovens.It \ev
2074501e74b7SMark OvensVertical tab
2075501e74b7SMark Ovens.It \e\e
2076501e74b7SMark OvensLiteral backslash
2077501e74b7SMark Ovens.It \e0nnn
2078dfe302abSRuslan Ermilov(Zero) The character whose octal value is
2079dfe302abSRuslan Ermilov.Ar nnn
2080501e74b7SMark Ovens.El
2081501e74b7SMark Ovens.Pp
2082501e74b7SMark OvensIf
2083501e74b7SMark Ovens.Ar string
2084501e74b7SMark Ovensis not enclosed in quotes then the backslash itself must be escaped
2085eccea571SRuslan Ermilovwith a backslash to protect it from the shell.
2086eccea571SRuslan ErmilovFor example
2087501e74b7SMark Ovens.Bd -literal -offset indent
2088501e74b7SMark Ovens$ echo -e "a\evb"
2089501e74b7SMark Ovensa
2090501e74b7SMark Ovens b
2091501e74b7SMark Ovens$ echo -e a\e\evb
2092501e74b7SMark Ovensa
2093501e74b7SMark Ovens b
2094501e74b7SMark Ovens$ echo -e "a\e\eb"
2095501e74b7SMark Ovensa\eb
2096501e74b7SMark Ovens$ echo -e a\e\e\e\eb
2097501e74b7SMark Ovensa\eb
2098501e74b7SMark Ovens.Ed
2099501e74b7SMark Ovens.El
2100558175caSTim J. Robbins.Pp
2101558175caSTim J. RobbinsOnly one of the
2102558175caSTim J. Robbins.Fl e
2103558175caSTim J. Robbinsand
2104558175caSTim J. Robbins.Fl n
2105558175caSTim J. Robbinsoptions may be specified.
210649e11e3aSSheldon Hearn.It Ic eval Ar string ...
2107e6d3cf26SSheldon HearnConcatenate all the arguments with spaces.
2108e6d3cf26SSheldon HearnThen re-parse and execute the command.
210949e11e3aSSheldon Hearn.It Ic exec Op Ar command Op arg ...
211049e11e3aSSheldon HearnUnless
211149e11e3aSSheldon Hearn.Ar command
211249e11e3aSSheldon Hearnis omitted,
211349e11e3aSSheldon Hearnthe shell process is replaced with the specified program
21148eed22b1SJens Schweikhardt(which must be a real program, not a shell built-in command or function).
211549e11e3aSSheldon HearnAny redirections on the
211649e11e3aSSheldon Hearn.Ic exec
211749e11e3aSSheldon Hearncommand are marked as permanent,
211849e11e3aSSheldon Hearnso that they are not undone when the
211949e11e3aSSheldon Hearn.Ic exec
212049e11e3aSSheldon Hearncommand finishes.
212149e11e3aSSheldon Hearn.It Ic exit Op Ar exitstatus
212249e11e3aSSheldon HearnTerminate the shell process.
212349e11e3aSSheldon HearnIf
212449e11e3aSSheldon Hearn.Ar exitstatus
212549e11e3aSSheldon Hearnis given
2126ea381e69SJilles Tjoelkerit is used as the exit status of the shell.
2127ea381e69SJilles TjoelkerOtherwise, if the shell is executing an
2128421fb021SJilles Tjoelker.Cm EXIT
2129421fb021SJilles Tjoelkertrap, the exit status of the last command before the trap is used;
2130421fb021SJilles Tjoelkerif the shell is executing a trap for a signal,
2131ea381e69SJilles Tjoelkerthe shell exits by resending the signal to itself.
2132ea381e69SJilles TjoelkerOtherwise, the exit status of the preceding command is used.
2133a68fbc44SJilles TjoelkerThe exit status should be an integer between 0 and 255.
2134ef9791a3SStefan Farfeleder.It Ic export Ar name ...
2135ef9791a3SStefan Farfeleder.It Ic export Op Fl p
21364b88c807SRodney W. GrimesThe specified names are exported so that they will
21374b88c807SRodney W. Grimesappear in the environment of subsequent commands.
213849e11e3aSSheldon HearnThe only way to un-export a variable is to
213949e11e3aSSheldon Hearn.Ic unset
214049e11e3aSSheldon Hearnit.
214149e11e3aSSheldon HearnThe shell allows the value of a variable to be set
214249e11e3aSSheldon Hearnat the same time as it is exported by writing
2143ab72124fSSteve Price.Pp
2144dfe302abSRuslan Ermilov.D1 Ic export Ar name Ns = Ns Ar value
2145dfe302abSRuslan Ermilov.Pp
2146dfe302abSRuslan ErmilovWith no arguments the
2147dfe302abSRuslan Ermilov.Ic export
2148dfe302abSRuslan Ermilovcommand lists the names
21494b88c807SRodney W. Grimesof all exported variables.
215045086f8cSTim J. RobbinsIf the
215145086f8cSTim J. Robbins.Fl p
215245086f8cSTim J. Robbinsoption is specified, the exported variables are printed as
215345086f8cSTim J. Robbins.Dq Ic export Ar name Ns = Ns Ar value
215445086f8cSTim J. Robbinslines, suitable for re-input to the shell.
2155a6557dcbSYaroslav Tykhiy.It Ic false
2156a6557dcbSYaroslav TykhiyA null command that returns a non-zero (false) exit value.
215781e9cda2SRuslan Ermilov.It Ic fc Oo Fl e Ar editor Oc Op Ar first Op Ar last
215881e9cda2SRuslan Ermilov.It Ic fc Fl l Oo Fl nr Oc Op Ar first Op Ar last
215981e9cda2SRuslan Ermilov.It Ic fc Fl s Oo Ar old Ns = Ns Ar new Oc Op Ar first
216049e11e3aSSheldon HearnThe
216149e11e3aSSheldon Hearn.Ic fc
21628eed22b1SJens Schweikhardtbuilt-in command lists, or edits and re-executes,
216349e11e3aSSheldon Hearncommands previously entered to an interactive shell.
216481e9cda2SRuslan Ermilov.Bl -tag -width indent
216549e11e3aSSheldon Hearn.It Fl e Ar editor
216649e11e3aSSheldon HearnUse the editor named by
216749e11e3aSSheldon Hearn.Ar editor
216849e11e3aSSheldon Hearnto edit the commands.
2169dfe302abSRuslan ErmilovThe
2170dfe302abSRuslan Ermilov.Ar editor
2171dfe302abSRuslan Ermilovstring is a command name,
217249e11e3aSSheldon Hearnsubject to search via the
21731788b7ffSJilles Tjoelker.Va PATH
217449e11e3aSSheldon Hearnvariable.
217549e11e3aSSheldon HearnThe value in the
21761788b7ffSJilles Tjoelker.Va FCEDIT
217749e11e3aSSheldon Hearnvariable is used as a default when
217849e11e3aSSheldon Hearn.Fl e
217949e11e3aSSheldon Hearnis not specified.
218049e11e3aSSheldon HearnIf
21811788b7ffSJilles Tjoelker.Va FCEDIT
218249e11e3aSSheldon Hearnis null or unset, the value of the
21831788b7ffSJilles Tjoelker.Va EDITOR
218449e11e3aSSheldon Hearnvariable is used.
218549e11e3aSSheldon HearnIf
21861788b7ffSJilles Tjoelker.Va EDITOR
218749e11e3aSSheldon Hearnis null or unset,
2188ab72124fSSteve Price.Xr ed 1
2189ab72124fSSteve Priceis used as the editor.
219049e11e3aSSheldon Hearn.It Fl l No (ell)
21914b88c807SRodney W. GrimesList the commands rather than invoking
2192eccea571SRuslan Ermilovan editor on them.
2193eccea571SRuslan ErmilovThe commands are written in the
2194dfe302abSRuslan Ermilovsequence indicated by the
2195dfe302abSRuslan Ermilov.Ar first
2196dfe302abSRuslan Ermilovand
2197dfe302abSRuslan Ermilov.Ar last
2198dfe302abSRuslan Ermilovoperands, as affected by
219949e11e3aSSheldon Hearn.Fl r ,
220049e11e3aSSheldon Hearnwith each command preceded by the command number.
220149e11e3aSSheldon Hearn.It Fl n
220249e11e3aSSheldon HearnSuppress command numbers when listing with
220349e11e3aSSheldon Hearn.Fl l .
220449e11e3aSSheldon Hearn.It Fl r
220549e11e3aSSheldon HearnReverse the order of the commands listed
220649e11e3aSSheldon Hearn(with
220749e11e3aSSheldon Hearn.Fl l )
220849e11e3aSSheldon Hearnor edited
220949e11e3aSSheldon Hearn(with neither
221049e11e3aSSheldon Hearn.Fl l
221149e11e3aSSheldon Hearnnor
221249e11e3aSSheldon Hearn.Fl s ) .
221349e11e3aSSheldon Hearn.It Fl s
22144b88c807SRodney W. GrimesRe-execute the command without invoking an editor.
221549e11e3aSSheldon Hearn.It Ar first
221649e11e3aSSheldon Hearn.It Ar last
221749e11e3aSSheldon HearnSelect the commands to list or edit.
221849e11e3aSSheldon HearnThe number of previous commands that can be accessed
221949e11e3aSSheldon Hearnare determined by the value of the
22201788b7ffSJilles Tjoelker.Va HISTSIZE
2221d0353b83SRuslan Ermilovvariable.
222249e11e3aSSheldon HearnThe value of
222349e11e3aSSheldon Hearn.Ar first
222449e11e3aSSheldon Hearnor
222549e11e3aSSheldon Hearn.Ar last
222649e11e3aSSheldon Hearnor both are one of the following:
222781e9cda2SRuslan Ermilov.Bl -tag -width indent
2228dfe302abSRuslan Ermilov.It Oo Cm + Oc Ns Ar num
222949e11e3aSSheldon HearnA positive number representing a command number;
223049e11e3aSSheldon Hearncommand numbers can be displayed with the
223149e11e3aSSheldon Hearn.Fl l
223249e11e3aSSheldon Hearnoption.
2233dfe302abSRuslan Ermilov.It Fl Ar num
223449e11e3aSSheldon HearnA negative decimal number representing the
223549e11e3aSSheldon Hearncommand that was executed
223649e11e3aSSheldon Hearn.Ar num
223749e11e3aSSheldon Hearnof
223849e11e3aSSheldon Hearncommands previously.
2239dfe302abSRuslan ErmilovFor example, \-1 is the immediately previous command.
224049e11e3aSSheldon Hearn.It Ar string
224149e11e3aSSheldon HearnA string indicating the most recently entered command
224249e11e3aSSheldon Hearnthat begins with that string.
224349e11e3aSSheldon HearnIf the
2244dfe302abSRuslan Ermilov.Ar old Ns = Ns Ar new
224549e11e3aSSheldon Hearnoperand is not also specified with
224649e11e3aSSheldon Hearn.Fl s ,
224749e11e3aSSheldon Hearnthe string form of the first operand cannot contain an embedded equal sign.
224849e11e3aSSheldon Hearn.El
224949e11e3aSSheldon Hearn.El
225049e11e3aSSheldon Hearn.Pp
22511788b7ffSJilles TjoelkerThe following variables affect the execution of
225249e11e3aSSheldon Hearn.Ic fc :
22531788b7ffSJilles Tjoelker.Bl -tag -width ".Va HISTSIZE"
22541788b7ffSJilles Tjoelker.It Va FCEDIT
225538afdcbcSGiorgos KeramidasName of the editor to use for history editing.
22561788b7ffSJilles Tjoelker.It Va HISTSIZE
22573d7b5b93SPhilippe CharnierThe number of previous commands that are accessible.
2258ab72124fSSteve Price.El
225949e11e3aSSheldon Hearn.It Ic fg Op Ar job
226049e11e3aSSheldon HearnMove the specified
226149e11e3aSSheldon Hearn.Ar job
226249e11e3aSSheldon Hearnor the current job to the foreground.
2263dfe302abSRuslan Ermilov.It Ic getopts Ar optstring var
2264dfe302abSRuslan ErmilovThe
2265dfe302abSRuslan Ermilov.Tn POSIX
226649e11e3aSSheldon Hearn.Ic getopts
226749e11e3aSSheldon Hearncommand.
226849e11e3aSSheldon HearnThe
226949e11e3aSSheldon Hearn.Ic getopts
227049e11e3aSSheldon Hearncommand deprecates the older
227149e11e3aSSheldon Hearn.Xr getopt 1
227249e11e3aSSheldon Hearncommand.
2273ab0a2172SSteve PriceThe first argument should be a series of letters, each possibly
2274ab0a2172SSteve Pricefollowed by a colon which indicates that the option takes an argument.
2275eccea571SRuslan ErmilovThe specified variable is set to the parsed option.
2276eccea571SRuslan ErmilovThe index of
227749e11e3aSSheldon Hearnthe next argument is placed into the shell variable
2278dfe302abSRuslan Ermilov.Va OPTIND .
2279ab0a2172SSteve PriceIf an option takes an argument, it is placed into the shell variable
2280dfe302abSRuslan Ermilov.Va OPTARG .
228149e11e3aSSheldon HearnIf an invalid option is encountered,
2282dfe302abSRuslan Ermilov.Ar var
228349e11e3aSSheldon Hearnis set to
2284dfe302abSRuslan Ermilov.Ql \&? .
2285ab0a2172SSteve PriceIt returns a false value (1) when it encounters the end of the options.
228681e9cda2SRuslan Ermilov.It Ic hash Oo Fl rv Oc Op Ar command ...
228749e11e3aSSheldon HearnThe shell maintains a hash table which remembers the locations of commands.
228849e11e3aSSheldon HearnWith no arguments whatsoever, the
228949e11e3aSSheldon Hearn.Ic hash
229049e11e3aSSheldon Hearncommand prints out the contents of this table.
2291ab72124fSSteve Price.Pp
229249e11e3aSSheldon HearnWith arguments, the
229349e11e3aSSheldon Hearn.Ic hash
229449e11e3aSSheldon Hearncommand removes each specified
229549e11e3aSSheldon Hearn.Ar command
229649e11e3aSSheldon Hearnfrom the hash table (unless they are functions) and then locates it.
229749e11e3aSSheldon HearnWith the
229849e11e3aSSheldon Hearn.Fl v
229949e11e3aSSheldon Hearnoption,
230049e11e3aSSheldon Hearn.Ic hash
230149e11e3aSSheldon Hearnprints the locations of the commands as it finds them.
230249e11e3aSSheldon HearnThe
230349e11e3aSSheldon Hearn.Fl r
230449e11e3aSSheldon Hearnoption causes the
230549e11e3aSSheldon Hearn.Ic hash
230649e11e3aSSheldon Hearncommand to delete all the entries in the hash table except for functions.
230749e11e3aSSheldon Hearn.It Ic jobid Op Ar job
2308dfe302abSRuslan ErmilovPrint the process IDs of the processes in the specified
230949e11e3aSSheldon Hearn.Ar job .
231049e11e3aSSheldon HearnIf the
231149e11e3aSSheldon Hearn.Ar job
231249e11e3aSSheldon Hearnargument is omitted, use the current job.
2313de37e41cSStefan Farfeleder.It Ic jobs Oo Fl lps Oc Op Ar job ...
2314ad8a0759STim J. RobbinsPrint information about the specified jobs, or all jobs if no
2315ad8a0759STim J. Robbins.Ar job
2316ad8a0759STim J. Robbinsargument is given.
2317ad8a0759STim J. RobbinsThe information printed includes job ID, status and command name.
2318ad8a0759STim J. Robbins.Pp
2319ad8a0759STim J. RobbinsIf the
2320ad8a0759STim J. Robbins.Fl l
2321ad8a0759STim J. Robbinsoption is specified, the PID of each job is also printed.
2322ad8a0759STim J. RobbinsIf the
2323de37e41cSStefan Farfeleder.Fl p
2324de37e41cSStefan Farfelederoption is specified, only the process IDs for the process group leaders
2325de37e41cSStefan Farfelederare printed, one per line.
2326de37e41cSStefan FarfelederIf the
2327ad8a0759STim J. Robbins.Fl s
2328de37e41cSStefan Farfelederoption is specified, only the PIDs of the job commands are printed, one per
2329de37e41cSStefan Farfelederline.
23300a62a9caSJilles Tjoelker.It Ic kill
23310a62a9caSJilles TjoelkerA built-in equivalent of
23320a62a9caSJilles Tjoelker.Xr kill 1
23330a62a9caSJilles Tjoelkerthat additionally supports sending signals to jobs.
23348af11131SRuslan Ermilov.It Ic local Oo Ar variable ... Oc Op Fl
2335d2f90294SJesus R. CamouSee the
2336d2f90294SJesus R. Camou.Sx Functions
2337d2f90294SJesus R. Camousubsection.
23389897c45fSJilles Tjoelker.It Ic printf
23399897c45fSJilles TjoelkerA built-in equivalent of
23409897c45fSJilles Tjoelker.Xr printf 1 .
2341d74e011fSRuslan Ermilov.It Ic pwd Op Fl L | P
2342eccea571SRuslan ErmilovPrint the path of the current directory.
2343eccea571SRuslan ErmilovThe built-in command may
23444b88c807SRodney W. Grimesdiffer from the program of the same name because the
23458eed22b1SJens Schweikhardtbuilt-in command remembers what the current directory
2346eccea571SRuslan Ermilovis rather than recomputing it each time.
2347eccea571SRuslan ErmilovThis makes
2348eccea571SRuslan Ermilovit faster.
2349eccea571SRuslan ErmilovHowever, if the current directory is
235049e11e3aSSheldon Hearnrenamed,
23518eed22b1SJens Schweikhardtthe built-in version of
235249e11e3aSSheldon Hearn.Xr pwd 1
235349e11e3aSSheldon Hearnwill continue to print the old name for the directory.
2354178897f1STim J. Robbins.Pp
2355178897f1STim J. RobbinsIf the
2356178897f1STim J. Robbins.Fl P
2357178897f1STim J. Robbinsoption is specified, symbolic links are resolved.
2358178897f1STim J. RobbinsIf the
2359178897f1STim J. Robbins.Fl L
2360178897f1STim J. Robbinsoption is specified, the shell's notion of the current directory
2361178897f1STim J. Robbinsis printed (symbolic links are not resolved).
2362178897f1STim J. RobbinsThis is the default.
2363dfe302abSRuslan Ermilov.It Ic read Oo Fl p Ar prompt Oc Oo
2364dfe302abSRuslan Ermilov.Fl t Ar timeout Oc Oo Fl er Oc Ar variable ...
236549e11e3aSSheldon HearnThe
236649e11e3aSSheldon Hearn.Ar prompt
236749e11e3aSSheldon Hearnis printed if the
236849e11e3aSSheldon Hearn.Fl p
236949e11e3aSSheldon Hearnoption is specified
2370eccea571SRuslan Ermilovand the standard input is a terminal.
2371eccea571SRuslan ErmilovThen a line is
2372eccea571SRuslan Ermilovread from the standard input.
2373eccea571SRuslan ErmilovThe trailing newline
23744b88c807SRodney W. Grimesis deleted from the line and the line is split as
237549e11e3aSSheldon Hearndescribed in the section on
237649e11e3aSSheldon Hearn.Sx White Space Splitting (Field Splitting)
237749e11e3aSSheldon Hearnabove, and
23784b88c807SRodney W. Grimesthe pieces are assigned to the variables in order.
2379f434d1dfSAdam DavidIf there are more pieces than variables, the remaining
238049e11e3aSSheldon Hearnpieces (along with the characters in
2381dfe302abSRuslan Ermilov.Va IFS
238249e11e3aSSheldon Hearnthat separated them)
238349e11e3aSSheldon Hearnare assigned to the last variable.
2384f434d1dfSAdam DavidIf there are more variables than pieces, the remaining
2385f434d1dfSAdam Davidvariables are assigned the null string.
2386ab72124fSSteve Price.Pp
238749e11e3aSSheldon HearnBackslashes are treated specially, unless the
238849e11e3aSSheldon Hearn.Fl r
238949e11e3aSSheldon Hearnoption is
2390eccea571SRuslan Ermilovspecified.
2391eccea571SRuslan ErmilovIf a backslash is followed by
23928f0561ccSThomas Gellekuma newline, the backslash and the newline will be
2393eccea571SRuslan Ermilovdeleted.
2394eccea571SRuslan ErmilovIf a backslash is followed by any other
23958f0561ccSThomas Gellekumcharacter, the backslash will be deleted and the following
239649e11e3aSSheldon Hearncharacter will be treated as though it were not in
2397dfe302abSRuslan Ermilov.Va IFS ,
239849e11e3aSSheldon Hearneven if it is.
23998f0561ccSThomas Gellekum.Pp
240049e11e3aSSheldon HearnIf the
240149e11e3aSSheldon Hearn.Fl t
240249e11e3aSSheldon Hearnoption is specified and the
240349e11e3aSSheldon Hearn.Ar timeout
2404fe40d6d3SJilles Tjoelkerelapses before a complete line of input is supplied,
240549e11e3aSSheldon Hearnthe
240649e11e3aSSheldon Hearn.Ic read
2407c4539460SJilles Tjoelkercommand will return an exit status as if terminated by
2408c4539460SJilles Tjoelker.Dv SIGALRM
2409c4539460SJilles Tjoelkerwithout assigning any values.
241049e11e3aSSheldon HearnThe
241149e11e3aSSheldon Hearn.Ar timeout
241249e11e3aSSheldon Hearnvalue may optionally be followed by one of
2413dfe302abSRuslan Ermilov.Ql s ,
2414dfe302abSRuslan Ermilov.Ql m
241549e11e3aSSheldon Hearnor
2416dfe302abSRuslan Ermilov.Ql h
2417a910f192SDima Dorfmanto explicitly specify seconds, minutes or hours.
241849e11e3aSSheldon HearnIf none is supplied,
2419dfe302abSRuslan Ermilov.Ql s
242049e11e3aSSheldon Hearnis assumed.
2421afa53c8dSMike Smith.Pp
242249e11e3aSSheldon HearnThe
242349e11e3aSSheldon Hearn.Fl e
242449e11e3aSSheldon Hearnoption exists only for backward compatibility with older scripts.
2425c4539460SJilles Tjoelker.Pp
2426c4539460SJilles TjoelkerThe exit status is 0 on success, 1 on end of file,
2427c4539460SJilles Tjoelkerbetween 2 and 128 if an error occurs
2428c4539460SJilles Tjoelkerand greater than 128 if a trapped signal interrupts
2429c4539460SJilles Tjoelker.Ic read .
24309ba31ca0SRuslan Ermilov.It Ic readonly Oo Fl p Oc Op Ar name ...
243149e11e3aSSheldon HearnEach specified
243249e11e3aSSheldon Hearn.Ar name
243349e11e3aSSheldon Hearnis marked as read only,
243449e11e3aSSheldon Hearnso that it cannot be subsequently modified or unset.
243549e11e3aSSheldon HearnThe shell allows the value of a variable to be set
243649e11e3aSSheldon Hearnat the same time as it is marked read only
243749e11e3aSSheldon Hearnby using the following form:
2438dfe302abSRuslan Ermilov.Pp
2439dfe302abSRuslan Ermilov.D1 Ic readonly Ar name Ns = Ns Ar value
2440ab72124fSSteve Price.Pp
244149e11e3aSSheldon HearnWith no arguments the
244249e11e3aSSheldon Hearn.Ic readonly
244349e11e3aSSheldon Hearncommand lists the names of all read only variables.
244445086f8cSTim J. RobbinsIf the
244545086f8cSTim J. Robbins.Fl p
244645086f8cSTim J. Robbinsoption is specified, the read-only variables are printed as
244745086f8cSTim J. Robbins.Dq Ic readonly Ar name Ns = Ns Ar value
244845086f8cSTim J. Robbinslines, suitable for re-input to the shell.
2449d2f90294SJesus R. Camou.It Ic return Op Ar exitstatus
2450d2f90294SJesus R. CamouSee the
2451d2f90294SJesus R. Camou.Sx Functions
2452d2f90294SJesus R. Camousubsection.
245381e9cda2SRuslan Ermilov.It Ic set Oo Fl /+abCEefIimnpTuVvx Oc Oo Fl /+o Ar longname Oc Oo
245481e9cda2SRuslan Ermilov.Fl c Ar string Oc Op Fl - Ar arg ...
2455e918fc8fSSheldon HearnThe
2456e918fc8fSSheldon Hearn.Ic set
2457e918fc8fSSheldon Hearncommand performs three different functions:
2458ab72124fSSteve Price.Bl -item
2459ab72124fSSteve Price.It
246049e11e3aSSheldon HearnWith no arguments, it lists the values of all shell variables.
2461ab72124fSSteve Price.It
246249e11e3aSSheldon HearnIf options are given,
246349e11e3aSSheldon Hearneither in short form or using the long
246449e11e3aSSheldon Hearn.Dq Fl /+o Ar longname
246549e11e3aSSheldon Hearnform,
246649e11e3aSSheldon Hearnit sets or clears the specified options as described in the section called
24676b15476eSSheldon Hearn.Sx Argument List Processing .
2468ab72124fSSteve Price.It
246949e11e3aSSheldon HearnIf the
247049e11e3aSSheldon Hearn.Dq Fl -
247149e11e3aSSheldon Hearnoption is specified,
247249e11e3aSSheldon Hearn.Ic set
247349e11e3aSSheldon Hearnwill replace the shell's positional parameters with the subsequent
247449e11e3aSSheldon Hearnarguments.
247549e11e3aSSheldon HearnIf no arguments follow the
247649e11e3aSSheldon Hearn.Dq Fl -
247749e11e3aSSheldon Hearnoption,
247849e11e3aSSheldon Hearnall the positional parameters will be cleared,
247949e11e3aSSheldon Hearnwhich is equivalent to executing the command
2480dfe302abSRuslan Ermilov.Dq Li "shift $#" .
248149e11e3aSSheldon HearnThe
248249e11e3aSSheldon Hearn.Dq Fl -
24838eed22b1SJens Schweikhardtflag may be omitted when specifying arguments to be used
248449e11e3aSSheldon Hearnas positional replacement parameters.
248549e11e3aSSheldon HearnThis is not recommended,
248649e11e3aSSheldon Hearnbecause the first argument may begin with a dash
2487a762afe5SGiorgos Keramidas.Pq Ql -
248849e11e3aSSheldon Hearnor a plus
2489a762afe5SGiorgos Keramidas.Pq Ql + ,
249049e11e3aSSheldon Hearnwhich the
249149e11e3aSSheldon Hearn.Ic set
249249e11e3aSSheldon Hearncommand will interpret as a request to enable or disable options.
2493ab72124fSSteve Price.El
2494dfe302abSRuslan Ermilov.It Ic setvar Ar variable value
249549e11e3aSSheldon HearnAssigns the specified
249649e11e3aSSheldon Hearn.Ar value
249749e11e3aSSheldon Hearnto the specified
249849e11e3aSSheldon Hearn.Ar variable .
2499dfe302abSRuslan ErmilovThe
2500dfe302abSRuslan Ermilov.Ic setvar
2501dfe302abSRuslan Ermilovcommand is intended to be used in functions that
2502e6d3cf26SSheldon Hearnassign values to variables whose names are passed as parameters.
250349e11e3aSSheldon HearnIn general it is better to write
2504dfe302abSRuslan Ermilov.Dq Ar variable Ns = Ns Ar value
250549e11e3aSSheldon Hearnrather than using
250649e11e3aSSheldon Hearn.Ic setvar .
250749e11e3aSSheldon Hearn.It Ic shift Op Ar n
250849e11e3aSSheldon HearnShift the positional parameters
250949e11e3aSSheldon Hearn.Ar n
2510e918fc8fSSheldon Hearntimes, or once if
251149e11e3aSSheldon Hearn.Ar n
251249e11e3aSSheldon Hearnis not specified.
2513dfe302abSRuslan ErmilovA shift sets the value of
2514dfe302abSRuslan Ermilov.Li $1
2515dfe302abSRuslan Ermilovto the value of
2516dfe302abSRuslan Ermilov.Li $2 ,
2517dfe302abSRuslan Ermilovthe value of
2518dfe302abSRuslan Ermilov.Li $2
2519dfe302abSRuslan Ermilovto the value of
2520dfe302abSRuslan Ermilov.Li $3 ,
2521dfe302abSRuslan Ermilovand so on,
2522dfe302abSRuslan Ermilovdecreasing the value of
2523dfe302abSRuslan Ermilov.Li $#
2524dfe302abSRuslan Ermilovby one.
252549e11e3aSSheldon HearnIf there are zero positional parameters, shifting does not do anything.
252610845922SYaroslav Tykhiy.It Ic test
252710845922SYaroslav TykhiyA built-in equivalent of
252810845922SYaroslav Tykhiy.Xr test 1 .
25291974986aSStefan Farfeleder.It Ic times
253051a514adSJilles TjoelkerPrint the amount of time spent executing the shell process and its children.
253151a514adSJilles TjoelkerThe first output line shows the user and system times for the shell process
25321974986aSStefan Farfelederitself, the second one contains the user and system times for the
25331974986aSStefan Farfelederchildren.
253481e9cda2SRuslan Ermilov.It Ic trap Oo Ar action Oc Ar signal ...
25350673e800SStefan Farfeleder.It Ic trap Fl l
253649e11e3aSSheldon HearnCause the shell to parse and execute
253749e11e3aSSheldon Hearn.Ar action
253849e11e3aSSheldon Hearnwhen any specified
253949e11e3aSSheldon Hearn.Ar signal
254049e11e3aSSheldon Hearnis received.
2541162ae3aeSTony FinchThe signals are specified by name or number.
2542162ae3aeSTony FinchIn addition, the pseudo-signal
2543f5d1157fSRuslan Ermilov.Cm EXIT
2544dfe302abSRuslan Ermilovmay be used to specify an
2545dfe302abSRuslan Ermilov.Ar action
2546dfe302abSRuslan Ermilovthat is performed when the shell terminates.
254749e11e3aSSheldon HearnThe
254849e11e3aSSheldon Hearn.Ar action
25490673e800SStefan Farfeledermay be an empty string or a dash
25509badf57fSRuslan Ermilov.Pq Ql - ;
255149e11e3aSSheldon Hearnthe former causes the specified signal to be ignored
255249e11e3aSSheldon Hearnand the latter causes the default action to be taken.
25530673e800SStefan FarfelederOmitting the
25540673e800SStefan Farfeleder.Ar action
25550673e800SStefan Farfelederis another way to request the default action, for compatibility reasons this
25560673e800SStefan Farfelederusage is not recommended though.
255751a514adSJilles TjoelkerIn a subshell or utility environment,
2558ef89d04fSJilles Tjoelkerthe shell resets trapped (but not ignored) signals to the default action.
255949e11e3aSSheldon HearnThe
256049e11e3aSSheldon Hearn.Ic trap
256149e11e3aSSheldon Hearncommand has no effect on signals that were ignored on entry to the shell.
25620673e800SStefan Farfeleder.Pp
25630673e800SStefan FarfelederOption
25640673e800SStefan Farfeleder.Fl l
25650673e800SStefan Farfeledercauses the
25660673e800SStefan Farfeleder.Ic trap
25670673e800SStefan Farfeledercommand to display a list of valid signal names.
2568a6557dcbSYaroslav Tykhiy.It Ic true
2569a6557dcbSYaroslav TykhiyA null command that returns a 0 (true) exit value.
257049e11e3aSSheldon Hearn.It Ic type Op Ar name ...
257149e11e3aSSheldon HearnInterpret each
257249e11e3aSSheldon Hearn.Ar name
257349e11e3aSSheldon Hearnas a command and print the resolution of the command search.
2574e6d3cf26SSheldon HearnPossible resolutions are:
257530268dfaSJilles Tjoelkershell keyword, alias, special shell builtin, shell builtin, command,
257630268dfaSJilles Tjoelkertracked alias
257749e11e3aSSheldon Hearnand not found.
257849e11e3aSSheldon HearnFor aliases the alias expansion is printed;
257949e11e3aSSheldon Hearnfor commands and tracked aliases
258049e11e3aSSheldon Hearnthe complete pathname of the command is printed.
2581d2c068eaSJilles Tjoelker.It Ic ulimit Oo Fl HSabcdfklmnpstuvw Oc Op Ar limit
2582ab72124fSSteve PriceSet or display resource limits (see
2583ab72124fSSteve Price.Xr getrlimit 2 ) .
25846b15476eSSheldon HearnIf
258549e11e3aSSheldon Hearn.Ar limit
25866b15476eSSheldon Hearnis specified, the named resource will be set;
25877a2afe64SJoerg Wunschotherwise the current resource value will be displayed.
2588ab72124fSSteve Price.Pp
25896b15476eSSheldon HearnIf
259049e11e3aSSheldon Hearn.Fl H
259149e11e3aSSheldon Hearnis specified, the hard limits will be set or displayed.
259249e11e3aSSheldon HearnWhile everybody is allowed to reduce a hard limit,
259349e11e3aSSheldon Hearnonly the superuser can increase it.
259449e11e3aSSheldon HearnThe
259549e11e3aSSheldon Hearn.Fl S
259649e11e3aSSheldon Hearnoption
2597eccea571SRuslan Ermilovspecifies the soft limits instead.
2598eccea571SRuslan ErmilovWhen displaying limits,
25996b15476eSSheldon Hearnonly one of
260049e11e3aSSheldon Hearn.Fl S
26016b15476eSSheldon Hearnor
260249e11e3aSSheldon Hearn.Fl H
260349e11e3aSSheldon Hearncan be given.
260449e11e3aSSheldon HearnThe default is to display the soft limits,
260549e11e3aSSheldon Hearnand to set both the hard and the soft limits.
2606ab72124fSSteve Price.Pp
26076b15476eSSheldon HearnOption
260849e11e3aSSheldon Hearn.Fl a
260949e11e3aSSheldon Hearncauses the
261049e11e3aSSheldon Hearn.Ic ulimit
261149e11e3aSSheldon Hearncommand to display all resources.
261249e11e3aSSheldon HearnThe parameter
261349e11e3aSSheldon Hearn.Ar limit
26146b15476eSSheldon Hearnis not acceptable in this mode.
2615ab72124fSSteve Price.Pp
26167a2afe64SJoerg WunschThe remaining options specify which resource value is to be
261749e11e3aSSheldon Hearndisplayed or modified.
261849e11e3aSSheldon HearnThey are mutually exclusive.
261981e9cda2SRuslan Ermilov.Bl -tag -width indent
26203652a236SMartin Cracauer.It Fl b Ar sbsize
26213652a236SMartin CracauerThe maximum size of socket buffer usage, in bytes.
262249e11e3aSSheldon Hearn.It Fl c Ar coredumpsize
26237a2afe64SJoerg WunschThe maximal size of core dump files, in 512-byte blocks.
262449e11e3aSSheldon Hearn.It Fl d Ar datasize
26257a2afe64SJoerg WunschThe maximal size of the data segment of a process, in kilobytes.
262649e11e3aSSheldon Hearn.It Fl f Ar filesize
262749e11e3aSSheldon HearnThe maximal size of a file, in 512-byte blocks.
2628d2c068eaSJilles Tjoelker.It Fl k Ar kqueues
2629d2c068eaSJilles TjoelkerThe maximal number of kqueues
2630d2c068eaSJilles Tjoelker(see
2631d2c068eaSJilles Tjoelker.Xr kqueue 2 )
2632d2c068eaSJilles Tjoelkerfor this user ID.
263349e11e3aSSheldon Hearn.It Fl l Ar lockedmem
26347a2afe64SJoerg WunschThe maximal size of memory that can be locked by a process, in
26357a2afe64SJoerg Wunschkilobytes.
263649e11e3aSSheldon Hearn.It Fl m Ar memoryuse
26377a2afe64SJoerg WunschThe maximal resident set size of a process, in kilobytes.
263849e11e3aSSheldon Hearn.It Fl n Ar nofiles
26397a2afe64SJoerg WunschThe maximal number of descriptors that could be opened by a process.
2640331773cdSEd Schouten.It Fl p Ar pseudoterminals
2641331773cdSEd SchoutenThe maximal number of pseudo-terminals for this user ID.
264249e11e3aSSheldon Hearn.It Fl s Ar stacksize
26437a2afe64SJoerg WunschThe maximal size of the stack segment, in kilobytes.
264449e11e3aSSheldon Hearn.It Fl t Ar time
26457a2afe64SJoerg WunschThe maximal amount of CPU time to be used by each process, in seconds.
264649e11e3aSSheldon Hearn.It Fl u Ar userproc
2647ab72124fSSteve PriceThe maximal number of simultaneous processes for this user ID.
264862df9b62SSheldon Hearn.It Fl v Ar virtualmem
26492d41ef4bSMatthew DillonThe maximal virtual size of a process, in kilobytes.
2650c8054a61SJilles Tjoelker.It Fl w Ar swapuse
2651c8054a61SJilles TjoelkerThe maximum amount of swap space reserved or used for this user ID,
2652c8054a61SJilles Tjoelkerin kilobytes.
2653ab72124fSSteve Price.El
2654faa1ed35SStefan Farfeleder.It Ic umask Oo Fl S Oc Op Ar mask
265549e11e3aSSheldon HearnSet the file creation mask (see
2656ab72124fSSteve Price.Xr umask 2 )
2657faa1ed35SStefan Farfelederto the octal or symbolic (see
2658faa1ed35SStefan Farfeleder.Xr chmod 1 )
2659faa1ed35SStefan Farfeledervalue specified by
26605d3b843bSSheldon Hearn.Ar mask .
266149e11e3aSSheldon HearnIf the argument is omitted, the current mask value is printed.
2662faa1ed35SStefan FarfelederIf the
2663faa1ed35SStefan Farfeleder.Fl S
2664faa1ed35SStefan Farfelederoption is specified, the output is symbolic, otherwise the output is octal.
2665de4ad382SStefan Farfeleder.It Ic unalias Oo Fl a Oc Op Ar name ...
2666de4ad382SStefan FarfelederThe specified alias names are removed.
26676b15476eSSheldon HearnIf
26681b9735e6SSheldon Hearn.Fl a
26696b15476eSSheldon Hearnis specified, all aliases are removed.
26709ba31ca0SRuslan Ermilov.It Ic unset Oo Fl fv Oc Ar name ...
26718b34ad92STim J. RobbinsThe specified variables or functions are unset and unexported.
26728b34ad92STim J. RobbinsIf the
26738b34ad92STim J. Robbins.Fl v
26748b34ad92STim J. Robbinsoption is specified or no options are given, the
267549e11e3aSSheldon Hearn.Ar name
26768b34ad92STim J. Robbinsarguments are treated as variable names.
26778b34ad92STim J. RobbinsIf the
26788b34ad92STim J. Robbins.Fl f
26798b34ad92STim J. Robbinsoption is specified, the
26808b34ad92STim J. Robbins.Ar name
26818b34ad92STim J. Robbinsarguments are treated as function names.
2682bd76c6b8SJilles Tjoelker.It Ic wait Op Ar job ...
2683bd76c6b8SJilles TjoelkerWait for each specified
268449e11e3aSSheldon Hearn.Ar job
268549e11e3aSSheldon Hearnto complete and return the exit status of the last process in the
2686bd76c6b8SJilles Tjoelkerlast specified
268749e11e3aSSheldon Hearn.Ar job .
2688bd76c6b8SJilles TjoelkerIf any
2689bd76c6b8SJilles Tjoelker.Ar job
2690bd76c6b8SJilles Tjoelkerspecified is unknown to the shell, it is treated as if it
2691bd76c6b8SJilles Tjoelkerwere a known job that exited with exit status 127.
2692bd76c6b8SJilles TjoelkerIf no operands are given, wait for all jobs to complete
2693e6d3cf26SSheldon Hearnand return an exit status of zero.
2694ab72124fSSteve Price.El
2695ab72124fSSteve Price.Ss Commandline Editing
2696ab72124fSSteve PriceWhen
2697ab72124fSSteve Price.Nm
2698ab72124fSSteve Priceis being used interactively from a terminal, the current command
269949e11e3aSSheldon Hearnand the command history
270049e11e3aSSheldon Hearn(see
270149e11e3aSSheldon Hearn.Ic fc
270249e11e3aSSheldon Hearnin
27038eed22b1SJens Schweikhardt.Sx Built-in Commands )
2704dfe302abSRuslan Ermilovcan be edited using
2705dfe302abSRuslan Ermilov.Nm vi Ns -mode
2706dfe302abSRuslan Ermilovcommand line editing.
270749e11e3aSSheldon HearnThis mode uses commands similar
2708dfe302abSRuslan Ermilovto a subset of those described in the
2709dfe302abSRuslan Ermilov.Xr vi 1
2710dfe302abSRuslan Ermilovman page.
271149e11e3aSSheldon HearnThe command
2712dfe302abSRuslan Ermilov.Dq Li "set -o vi"
271349e11e3aSSheldon Hearn(or
2714dfe302abSRuslan Ermilov.Dq Li "set -V" )
2715dfe302abSRuslan Ermilovenables
2716dfe302abSRuslan Ermilov.Nm vi Ns -mode
2717dfe302abSRuslan Ermilovediting and places
2718ab72124fSSteve Price.Nm
2719dfe302abSRuslan Ermilovinto
2720dfe302abSRuslan Ermilov.Nm vi
2721dfe302abSRuslan Ermilovinsert mode.
2722dfe302abSRuslan ErmilovWith
2723dfe302abSRuslan Ermilov.Nm vi Ns -mode
2724dfe302abSRuslan Ermilovenabled,
2725ab72124fSSteve Price.Nm
272649e11e3aSSheldon Hearncan be switched between insert mode and command mode by typing
272749e11e3aSSheldon Hearn.Aq ESC .
272849e11e3aSSheldon HearnHitting
272949e11e3aSSheldon Hearn.Aq return
273049e11e3aSSheldon Hearnwhile in command mode will pass the line to the shell.
2731ab72124fSSteve Price.Pp
273249e11e3aSSheldon HearnSimilarly, the
2733dfe302abSRuslan Ermilov.Dq Li "set -o emacs"
273449e11e3aSSheldon Hearn(or
2735dfe302abSRuslan Ermilov.Dq Li "set -E" )
273649e11e3aSSheldon Hearncommand can be used to enable a subset of
2737dfe302abSRuslan Ermilov.Nm emacs Ns -style
2738dfe302abSRuslan Ermilovcommand line editing features.
273938afdcbcSGiorgos Keramidas.Sh ENVIRONMENT
274038afdcbcSGiorgos KeramidasThe following environment variables affect the execution of
274138afdcbcSGiorgos Keramidas.Nm :
27421788b7ffSJilles Tjoelker.Bl -tag -width ".Ev LANGXXXXXX"
27431788b7ffSJilles Tjoelker.It Ev ENV
27441788b7ffSJilles TjoelkerInitialization file for interactive shells.
27451788b7ffSJilles Tjoelker.It Ev LANG , Ev LC_*
27461788b7ffSJilles TjoelkerLocale settings.
27471788b7ffSJilles TjoelkerThese are inherited by children of the shell,
27481788b7ffSJilles Tjoelkerand is used in a limited manner by the shell itself.
27491788b7ffSJilles Tjoelker.It Ev PWD
27501788b7ffSJilles TjoelkerAn absolute pathname for the current directory,
27511788b7ffSJilles Tjoelkerpossibly containing symbolic links.
27521788b7ffSJilles TjoelkerThis is used and updated by the shell.
275338afdcbcSGiorgos Keramidas.It Ev TERM
275438afdcbcSGiorgos KeramidasThe default terminal setting for the shell.
275538afdcbcSGiorgos KeramidasThis is inherited by children of the shell, and is used in the history
275638afdcbcSGiorgos Keramidasediting modes.
275738afdcbcSGiorgos Keramidas.El
27581788b7ffSJilles Tjoelker.Pp
27591788b7ffSJilles TjoelkerAdditionally, all environment variables are turned into shell variables
27601788b7ffSJilles Tjoelkerat startup,
27611788b7ffSJilles Tjoelkerwhich may affect the shell as described under
27621788b7ffSJilles Tjoelker.Sx Special Variables .
2763cb5a6a58SJoel Dahl.Sh FILES
2764cb5a6a58SJoel Dahl.Bl -tag -width "/etc/suid_profileXX" -compact
2765cb5a6a58SJoel Dahl.It Pa ~/.profile
2766cb5a6a58SJoel DahlUser's login profile.
2767cb5a6a58SJoel Dahl.It Pa /etc/profile
2768cb5a6a58SJoel DahlSystem login profile.
2769cb5a6a58SJoel Dahl.It Pa /etc/shells
2770cb5a6a58SJoel DahlShell database.
2771cb5a6a58SJoel Dahl.It Pa /etc/suid_profile
2772cb5a6a58SJoel DahlPrivileged shell profile.
2773cb5a6a58SJoel Dahl.El
2774fb039b55SRuslan Ermilov.Sh EXIT STATUS
2775fb039b55SRuslan ErmilovErrors that are detected by the shell, such as a syntax error, will
2776fb039b55SRuslan Ermilovcause the shell to exit with a non-zero exit status.
2777fb039b55SRuslan ErmilovIf the shell is not an interactive shell, the execution of the shell
2778fb039b55SRuslan Ermilovfile will be aborted.
2779fb039b55SRuslan ErmilovOtherwise the shell will return the exit status of the last command
2780dfe302abSRuslan Ermilovexecuted, or if the
2781dfe302abSRuslan Ermilov.Ic exit
2782dfe302abSRuslan Ermilovbuiltin is used with a numeric argument, it
2783fb039b55SRuslan Ermilovwill return the argument.
2784c83e7c50SJoseph Koshy.Sh SEE ALSO
2785c644db6aSSheldon Hearn.Xr builtin 1 ,
278638afdcbcSGiorgos Keramidas.Xr chsh 1 ,
2787c644db6aSSheldon Hearn.Xr echo 1 ,
278838afdcbcSGiorgos Keramidas.Xr ed 1 ,
278938afdcbcSGiorgos Keramidas.Xr emacs 1 ,
27900a62a9caSJilles Tjoelker.Xr kill 1 ,
27919897c45fSJilles Tjoelker.Xr printf 1 ,
27925521ff5aSRuslan Ermilov.Xr pwd 1 ,
279338afdcbcSGiorgos Keramidas.Xr test 1 ,
279438afdcbcSGiorgos Keramidas.Xr vi 1 ,
279538afdcbcSGiorgos Keramidas.Xr execve 2 ,
279638afdcbcSGiorgos Keramidas.Xr getrlimit 2 ,
2797fd50df30SRuslan Ermilov.Xr umask 2 ,
2798ff4dc672SJilles Tjoelker.Xr wctype 3 ,
2799cb5a6a58SJoel Dahl.Xr editrc 5 ,
2800cb5a6a58SJoel Dahl.Xr shells 5
2801ab72124fSSteve Price.Sh HISTORY
2802b8923d4cSWolfram SchneiderA
2803ab72124fSSteve Price.Nm
28049cdd1e3fSYaroslav Tykhiycommand, the Thompson shell, appeared in
2805fc482908STim J. Robbins.At v1 .
28069cdd1e3fSYaroslav TykhiyIt was superseded in
28079cdd1e3fSYaroslav Tykhiy.At v7
28089cdd1e3fSYaroslav Tykhiyby the Bourne shell, which inherited the name
28099cdd1e3fSYaroslav Tykhiy.Nm .
28109cdd1e3fSYaroslav Tykhiy.Pp
28119cdd1e3fSYaroslav TykhiyThis version of
28129cdd1e3fSYaroslav Tykhiy.Nm
28139cdd1e3fSYaroslav Tykhiywas rewritten in 1989 under the
28149cdd1e3fSYaroslav Tykhiy.Bx
28159cdd1e3fSYaroslav Tykhiylicense after the Bourne shell from
28169cdd1e3fSYaroslav Tykhiy.At V.4 .
28179cdd1e3fSYaroslav Tykhiy.Sh AUTHORS
28189cdd1e3fSYaroslav TykhiyThis version of
28199cdd1e3fSYaroslav Tykhiy.Nm
28209badf57fSRuslan Ermilovwas originally written by
28219badf57fSRuslan Ermilov.An Kenneth Almquist .
2822128dc4a2STim J. Robbins.Sh BUGS
2823128dc4a2STim J. RobbinsThe
2824128dc4a2STim J. Robbins.Nm
282550df3424SJilles Tjoelkerutility does not recognize multibyte characters other than UTF-8.
282650df3424SJilles TjoelkerSplitting using
282750df3424SJilles Tjoelker.Va IFS
282850df3424SJilles Tjoelkerand the line editing library
282950df3424SJilles Tjoelker.Xr editline 3
283050df3424SJilles Tjoelkerdo not recognize multibyte characters.
2831