xref: /netbsd-src/bin/sh/sh.1 (revision fc4f42693f9b1c31f39f9cf50af1bf2010325808)
1.\"	$NetBSD: sh.1,v 1.203 2018/03/17 01:53:06 uwe Exp $
2.\" Copyright (c) 1991, 1993
3.\"	The Regents of the University of California.  All rights reserved.
4.\"
5.\" This code is derived from software contributed to Berkeley by
6.\" Kenneth Almquist.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"	@(#)sh.1	8.6 (Berkeley) 5/4/95
33.\"
34.Dd October 24, 2017
35.Dt SH 1
36.\" everything except c o and s (keep them ordered)
37.ds flags abCEeFfhIiLmnpquVvXx
38.Os
39.Sh NAME
40.Nm sh
41.Nd command interpreter (shell)
42.Sh SYNOPSIS
43.Nm
44.Bk -words
45.Op Fl \*[flags]
46.Op Cm +\*[flags]
47.Ek
48.Bk -words
49.Op Fl o Ar option_name
50.Op Cm +o Ar option_name
51.Ek
52.Bk -words
53.Op Ar command_file Op Ar argument ...
54.Ek
55.Nm
56.Fl c
57.Bk -words
58.Op Fl s
59.Op Fl \*[flags]
60.Op Cm +\*[flags]
61.Ek
62.Bk -words
63.Op Fl o Ar option_name
64.Op Cm +o Ar option_name
65.Ek
66.Bk -words
67.Ar command_string
68.Op Ar command_name Op Ar argument ...
69.Ek
70.Nm
71.Fl s
72.Bk -words
73.Op Fl \*[flags]
74.Op Cm +\*[flags]
75.Ek
76.Bk -words
77.Op Fl o Ar option_name
78.Op Cm +o Ar option_name
79.Ek
80.Bk -words
81.Op Ar argument ...
82.Ek
83.Sh DESCRIPTION
84.Nm
85is the standard command interpreter for the system.
86The current version of
87.Nm
88is in the process of being changed to conform more closely to the
89POSIX 1003.2 and 1003.2a specifications for the shell.
90This version has many
91features which make it appear similar in some respects to the Korn shell,
92but it is not a Korn shell clone (see
93.Xr ksh 1 ) .
94This man page is not intended
95to be a tutorial or a complete specification of the shell.
96.Ss Overview
97The shell is a command that reads lines from either a file or the
98terminal, interprets them, and generally executes other commands.
99A shell is the program that is running when a user logs into the system.
100(Users can select which shell is executed for them at login with the
101.Xr chsh 1
102command).
103The shell implements a language that has flow control
104constructs, a macro facility that provides a variety of features in
105addition to data storage, along with built in history and line editing
106capabilities.
107It incorporates many features to aid interactive use and
108has the advantage that the interpretative language is common to both
109interactive and non-interactive use (shell scripts).
110That is, commands
111can be typed directly to the running shell or can be put into a file and
112the file can be executed directly by the shell.
113.Ss Invocation
114If no arguments are present and if the standard input,
115and standard error output, of the shell
116are connected to a terminal (or terminals, or if the
117.Fl i
118flag is set),
119and the
120.Fl c
121option is not present, the shell is considered an interactive shell.
122An interactive shell generally prompts before each command and handles
123programming and command errors differently (as described below).
124When first starting,
125the shell inspects argument 0, and if it begins with a dash
126.Sq - ,
127the shell is also considered
128a login shell.
129This is normally done automatically by the system
130when the user first logs in.
131A login shell first reads commands
132from the files
133.Pa /etc/profile
134and
135.Pa .profile
136if they exist.
137If the environment variable
138.Ev ENV
139is set on entry to a shell,
140or is set in the
141.Pa .profile
142of a login shell,
143and either the shell is interactive, or the
144.Cm posix
145option is not set,
146the shell next reads
147commands from the file named in
148.Ev ENV .
149Therefore, a user should place commands that are to be executed only at
150login time in the
151.Pa .profile
152file, and commands that are executed for every shell inside the
153.Ev ENV
154file.
155To set the
156.Ev ENV
157variable to some file, place the following line in your
158.Pa .profile
159of your home directory
160.Pp
161.Dl ENV=$HOME/.shinit; export ENV
162.Pp
163substituting for
164.Dq .shinit
165any filename you wish.
166Since the
167.Ev ENV
168file can be read for every invocation of the shell, including shell scripts
169and non-interactive shells, the following paradigm is useful for
170restricting commands in the
171.Ev ENV
172file to interactive invocations.
173Place commands within the
174.Dq Ic case
175and
176.Dq Ic esac
177below (these commands are described later):
178.Pp
179.Bd -literal -offset indent
180case $- in *i*)
181        # commands for interactive use only
182        ...
183esac
184.Ed
185.Pp
186If command line arguments besides the options have been specified, and
187neither
188.Fl c
189nor
190.Fl s
191was given, then the shell treats the first argument
192as the name of a file from which to read commands (a shell script).
193This also becomes
194.Li $0
195and the remaining arguments are set as the
196positional parameters of the shell
197.Li ( $1 , $2 ,
198etc).
199Otherwise, if
200.Fl c
201was given, then the first argument, which must exist,
202is taken to be a string of
203.Nm
204commands to execute.
205Then if any additional arguments follow the command string,
206those arguments become
207.Li $0 , $1 ,
208\&...
209Otherwise, if additional arguments were given
210(which implies that
211.Fl s
212was set)
213those arguments become
214.Li $1 , $2 ,
215\&...
216If
217.Li $0
218has not been set by the preceding processing, it
219will be set to
220.Va argv\^ Ns [ 0 ]
221as passed to the shell, which will
222usually be the name of the shell itself.
223If
224.Fl s
225was given, or if neither
226.Fl c
227nor any additional (non-option) arguments were present,
228the shell reads commands from its standard input.
229.\"
230.\"
231.Ss Argument List Processing
232.\"
233Currently, all of the single letter options that can meaningfully
234be set using the
235.Ic set
236built-in, have a corresponding name
237that can be used as an argument to the
238.Fl o
239option.
240The
241.Ic set Fl o
242name is provided next to the single letter option in
243the description below.
244Some options have only a long name, they are described after
245the flag options, they are used with
246.Fl o
247or
248.Cm +o
249only, either on the command line, or with the
250.Ic set
251built-in command.
252Other options described are for the command line only.
253Specifying a dash
254.Dq Cm \-
255turns the option on, while using a plus
256.Dq Cm +
257disables the option.
258The following options can be set from the command line and,
259unless otherwise stated, with the
260.Ic set
261built-in (described later).
262.\"
263.\" strlen("quietprofile") == strlen("local_lineno"): pick the latter
264.\" to give the indent as the _ in local_lineno, and the fi ligature in
265.\" quietprofile combine to make "local_lineno' slightly wider when printed
266.\" (in italics) in a variable width font.
267.Bl -tag -width ".Fl L Em local_lineno" -offset indent
268.\"
269.It Fl a Em allexport
270Automatically export any variable to which a value is assigned
271while this flag is set.
272.It Fl b Em notify
273Enable asynchronous notification of background job completion.
274(Not implemented.)
275.It Fl C Em noclobber
276Don't overwrite existing files with
277.Dq > .
278.It Fl c
279Read commands from the
280.Ar command_string
281operand instead of, or in addition to, from the standard input.
282Special parameter
283.Dv 0 \" $0	(comments like this for searching sources only)
284will be set from the
285.Ar command_name
286operand if given, and the positional parameters
287.Dv ( 1 , 2 ,
288etc.)
289set from the remaining argument operands, if any.
290.Fl c
291is only available at invocation, it cannot be
292.Ic set ,
293and there is no form using
294.Dq Cm \&+ .
295.It Fl E Em emacs
296Enable the built-in emacs style
297command line editor (disables
298.Fl V
299if it has been set).
300(See the
301.Sx Command Line Editing
302section below.)
303.It Fl e Em errexit
304If not interactive, exit immediately if any untested command fails.
305If interactive, and an untested command fails,
306cease all processing of the current command and return to
307prompt for a new command.
308The exit status of a command is considered to be
309explicitly tested if the command is used to control an
310.Ic if ,
311.Ic elif ,
312.Ic while ,
313or
314.Ic until ,
315or if the command is the left hand operand of an
316.Dq &&
317or
318.Dq ||
319operator,
320or if it is a pipeline (or simple command) preceded by the
321.Dq \&!
322operator.
323With pipelines, only the status of the entire pipeline
324(indicated by the last command it contains)
325is tested when
326.Fl e
327is set to determine if the shell should exit.
328.It Fl F Em fork
329Cause the shell to always use
330.Xr fork 2
331instead of attempting
332.Xr vfork 2
333when it needs to create a new process.
334This should normally have no visible effect,
335but can slow execution.
336The
337.Nm
338can be compiled to always use
339.Xr fork 2
340in which case altering the
341.Fl F
342flag has no effect.
343.It Fl f Em noglob
344Disable pathname expansion.
345.It Fl h Em trackall
346Functions defined while this option is set will have paths bound to
347commands to be executed by the function at the time of the definition.
348When off when a function is defined,
349the file system is searched for commands each time the function is invoked.
350(Not implemented.)
351.It Fl I Em ignoreeof
352Ignore EOFs from input when interactive.
353(After a large number of consecutive EOFs the shell will exit anyway.)
354.It Fl i Em interactive
355Force the shell to behave interactively.
356.It Fl L Em local_lineno
357When set, before a function is defined,
358causes the variable
359.Dv LINENO
360when used within the function,
361to refer to the line number defined such that
362first line of the function is line 1.
363When reset,
364.Dv LINENO
365in a function refers to the line number within the file
366within which the definition of the function occurs.
367This option defaults to
368.Dq on
369in this shell.
370For more details see the section
371.Sx LINENO
372below.
373.It Fl m Em monitor
374Turn on job control (set automatically when interactive).
375.It Fl n Em noexec
376Read and parse commands, but do not execute them.
377This is useful for checking the syntax of shell scripts.
378If
379.Fl n
380becomes set in an interactive shell, it will automatically be
381cleared just before the next time the command line prompt
382.Pq Ev PS1
383is written.
384.It Fl p Em nopriv
385Do not attempt to reset effective UID if it does not match UID.
386This is not set by default to help avoid incorrect usage by setuid
387root programs via
388.Xr system 3
389or
390.Xr popen 3 .
391.It Fl q Em quietprofile
392If the
393.Fl v
394or
395.Fl x
396options have been set, do not apply them when reading
397initialization files, these being
398.Pa /etc/profile ,
399.Pa .profile ,
400and the file specified by the
401.Ev ENV
402environment variable.
403.It Fl s Em stdin
404Read commands from standard input (set automatically if
405neither
406.Fl c
407nor file arguments are present).
408If after processing a command_string with the
409.Fl c
410option, the shell has not exited, and the
411.Fl s
412option is set, it will continue reading more commands from standard input.
413This option has no effect when set or reset after the shell has
414already started reading from the command_file, or from standard input.
415Note that the
416.Fl s
417flag being set does not cause the shell to be interactive.
418.It Fl u Em nounset
419Write a message to standard error when attempting to obtain a
420value from a variable that is not set,
421and if the shell is not interactive, exit immediately.
422For interactive shells, instead return immediately to the command prompt
423and read the next command.
424Note that expansions (described later, see
425.Sx Word Expansions
426below) using the
427.Sq \&+ ,
428.Sq \&\- ,
429.Sq \&= ,
430or
431.Sq \&?
432operators test if the variable is set, before attempting to
433obtain its value, and hence are unaffected by
434.Fl u .
435.It Fl V Em vi
436Enable the built-in
437.Xr vi 1
438command line editor (disables
439.Fl E
440if it has been set).
441(See the
442.Sx Command Line Editing
443section below.)
444.It Fl v Em verbose
445The shell writes its input to standard error as it is read.
446Useful for debugging.
447.It Fl X Em Xtrace
448Cause output from the
449.Ic xtrace
450.Pq Fl x
451option to be sent to standard error as it exists when the
452.Fl X
453option is enabled (regardless of its previous state.)
454For example:
455.Bd -literal -compact
456        set -X 2>/tmp/trace-file
457.Ed
458will arrange for tracing output to be sent to the file named,
459instead of wherever it was previously being sent,
460until the X option is set again, or cleared.
461.Pp
462Each change (set or clear) to
463.Fl X
464is also performed upon
465.Fl x ,
466but not the converse.
467.It Fl x Em xtrace
468Write each command to standard error (preceded by the expanded value of
469.Li $PS4 )
470before it is executed.
471Unless
472.Fl X
473is set,
474.Dq "standard error"
475means that which existed immediately before any redirections to
476be applied to the command are performed.
477Useful for debugging.
478.It "\ \ " Em cdprint
479Make an interactive shell always print the new directory name when
480changed by the
481.Ic cd
482command.
483In a non-interactive shell this option has no effect.
484.It "\ \ " Em nolog
485Prevent the entry of function definitions into the command history (see
486.Ic fc
487in the
488.Sx Built-ins
489section.)
490(Not implemented.)
491.It "\ \ " Em pipefail
492If set, the way the exit status of a pipeline is determined
493is altered.
494See
495.Sx Pipelines
496below for the details.
497.It "\ \ " Em posix
498Enables closer adherence to the POSIX shell standard.
499This option will default set at shell startup if the
500environment variable
501.Ev POSIXLY_CORRECT
502is present.
503That can be overridden (set or reset) by the
504.Fl o
505option on the command line.
506Currently this option controls whether (!posix) or not (posix)
507the file given by the
508.Ev ENV
509variable is read at startup by a non-interactive shell.
510It also controls whether file descriptors greater than 2
511opened using the
512.Ic exec
513built-in command are passed on to utilities executed
514.Dq ( yes
515in posix mode),
516whether a colon (:) terminates the user name in tilde (~) expansions
517other than in assignment statements
518.Dq ( no
519in posix mode),
520and whether the shell treats
521an empty brace-list compound statement as a syntax error
522(expected by POSIX) or permits it.
523Such statements
524.Dq "{ }"
525can be useful when defining dummy functions.
526Lastly, in posix mode, only one
527.Dq \&!
528is permitted before a pipeline.
529.It "\ \ " Em promptcmds
530Allows command substitutions (as well as parameter
531and arithmetic expansions, which are always performed)
532upon the prompt strings
533.Ev PS1 ,
534.Ev PS2 ,
535and
536.Ev PS4
537each time, before they are output.
538This option should not be set until after the prompts
539have been set (or verified) to avoid accidentally importing
540unwanted command substitutions from the environment.
541.It "\ \ " Em tabcomplete
542Enables filename completion in the command line editor.
543Typing a tab character will extend the current input word to match a
544filename.
545If more than one filename matches it is only extended to be the common prefix.
546Typing a second tab character will list all the matching names.
547One of the editing modes, either
548.Fl E
549or
550.Fl V ,
551must be enabled for this to work.
552.El
553.Ss Lexical Structure
554The shell reads input in terms of lines from a file and breaks it up into
555words at whitespace (blanks and tabs), and at certain sequences of
556characters that are special to the shell called
557.Dq operators .
558There are two types of operators: control operators and redirection
559operators (their meaning is discussed later).
560The following is a list of operators:
561.Bl -ohang -offset indent
562.It "Control operators:"
563.Dl &  &&  \&(  \&)  \&;  ;; ;& \&| || <newline>
564.It "Redirection operators:"
565.Dl <  >  >|  <<  >>  <&  >&  <<-  <>
566.El
567.Ss Quoting
568Quoting is used to remove the special meaning of certain characters or
569words to the shell, such as operators, whitespace, or keywords.
570There are four types of quoting:
571matched single quotes,
572matched double quotes,
573backslash,
574and
575dollar preceding matched single quotes (enhanced C style strings.)
576.Ss Backslash
577An unquoted backslash preserves the literal meaning of the following
578character, with the exception of
579.Aq newline .
580An unquoted backslash preceding a
581.Aq newline
582is treated as a line continuation, the two characters are simply removed.
583.Ss Single Quotes
584Enclosing characters in single quotes preserves the literal meaning of all
585the characters (except single quotes, making it impossible to put
586single quotes in a single-quoted string).
587.Ss Double Quotes
588Enclosing characters within double quotes preserves the literal
589meaning of all characters except dollar sign
590.Pq Li \&$ ,
591backquote
592.Pq Li \&` ,
593and backslash
594.Pq Li \e .
595The backslash inside double quotes is historically weird, and serves to
596quote only the following characters (and these not in all contexts):
597.Dl $  `  \*q  \e  <newline> ,
598where a backslash newline is a line continuation as above.
599Otherwise it remains literal.
600.\"
601.\"
602.Ss Dollar Single Quotes ( Li \&$'...' )
603.\"
604.Bd -filled -offset indent
605.Bf Em
606Note: this form of quoting is still somewhat experimental,
607and yet to be included in the POSIX standard.
608This implementation is based upon the current proposals for
609standardization, and is subject to change should the eventual
610adopted text differ.
611.Ef
612.Ed
613.Pp
614Enclosing characters in a matched pair of single quotes, with the
615first immediately preceded by an unquoted dollar sign
616.Pq Li \&$
617provides a quoting mechanism similar to single quotes, except
618that within the sequence of characters, any backslash
619.Pq Li \e ,
620is an escape character, which causes the following character to
621be treated specially.
622Only a subset of the characters that can occur in the string
623are defined after a backslash, others are reserved for future
624definition, and currently generate a syntax error if used.
625The escape sequences are modeled after the similar sequences
626in strings in the C programming language, with some extensions.
627.Pp
628The following characters are treated literally when following
629the escape character (backslash):
630.Dl \e \&' \(dq
631The sequence
632.Dq Li \e\e
633allows the escape character (backslash) to appear in the string literally.
634.Dq Li \e'
635allows a single quote character into the string, such an
636escaped single quote does not terminate the quoted string.
637.Dq Li \e\(dq
638is for compatibility with C strings, the double quote has
639no special meaning in a shell C-style string,
640and does not need to be escaped, but may be.
641.Pp
642A newline following the escape character is treated as a line continuation,
643like the same sequence in a double quoted string,
644or when not quoted \(en
645the two characters, escape and newline, are removed from the input string.
646.Pp
647The following characters, when escaped, are converted in a
648manner similar to the way they would be in a string in the C language:
649.Dl a b e f n r t v
650An escaped
651.Sq a
652generates an alert (or
653.Sq BEL )
654character, that is, control-G, or 0x07.
655In a similar way,
656.Sq b
657is backspace (0x08),
658.Sq e
659(an extension to C) is escape (0x1B),
660.Sq f
661is form feed (0x0C),
662.Sq n
663is newline (or line feed, 0x0A),
664.Sq r
665is return (0x0D),
666.Sq t
667is horizontal tab (0x09),
668and
669.Sq v
670is vertical tab (0x13).
671.Pp
672In addition to those there are 5 forms that need additional
673data, which is obtained from the subsequent characters.
674An escape
675.Pq Li \e
676followed by one, two or three, octal digits
677.Po So 0 Sc Ns \&.. Ns So 7 Sc Ns Pc
678is processed to form an 8 bit character value.
679If only one or two digits are present, the following
680character must be something other than an octal digit.
681It is safest to always use all 3 digits, with leading
682zeros if needed.
683If all three digits are present, the first must be one of
684.So 0 Sc Ns \&.. Ns So 3 Sc .
685.Pp
686An escape followed by
687.Sq x
688(lower case only) can be followed by one or two
689hexadecimal digits
690.Po So 0 Sc Ns \&.. Ns So 9 Sc , So A Sc Ns \&.. Ns So F Sc , or So a Sc Ns \&.. Ns So f Sc . Pc
691As with octal, if only one hex digit is present, the following
692character must be something other than a hex digit,
693so always giving 2 hex digits is best.
694However, unlike octal, it is unspecified in the standard
695how many hex digits can be consumed.
696This
697.Nm
698takes at most two, but other shells will continue consuming
699characters as long as they remain valid hex digits.
700Consequently, users should ensure that the character
701following the hex escape sequence is something other than
702a hex digit.
703One way to achieve this is to end the
704.Li $'...'
705string immediately
706after the final hex digit, and then, immediately start
707another, so
708.Dl \&$'\ex33'$'4...'
709always gives the character with value 0x33
710.Pq Sq 3 ,
711followed by the character
712.Sq 4 ,
713whereas
714.Dl \&$'\ex334'
715in some other shells would be the hex value 0x334 (10, or more, bits).
716.Pp
717There are two escape sequences beginning with
718.Sq Li \eu
719or
720.Sq Li \eU .
721The former is followed by from 1 to 4 hex digits, the latter by
722from 1 to 8 hex digits.
723Leading zeros can be used to pad the sequences to the maximum
724permitted length, to avoid any possible ambiguity problem with
725the following character, and because there are some shells that
726insist on exactly 4 (or 8) hex digits.
727These sequences are evaluated to form the value of a Unicode code
728point, which is then encoded into UTF-8 form, and entered into the
729string.
730(The code point should be converted to the appropriate
731code point value for the corresponding character in the character
732set given by the current locale, or perhaps the locale in use
733when the shell was started, but is not... currently.)
734Not all values that are possible to write are valid, values that
735specify (known) invalid Unicode code points will be rejected, or
736simply produce
737.Sq \&? .
738.Pp
739Lastly, as another addition to what is available in C, the escape
740character (backslash), followed by
741.Sq c
742(lower case only) followed by one additional character, which must
743be an alphabetic character (a letter), or one of the following:
744.Dl \&@ \&[ \&\e \&] \&^ \&_ \&?
745Other than
746.Sq Li \ec?
747the value obtained is the least significant 5 bits of the
748ASCII value of the character following the
749.Sq Li \ec
750escape sequence.
751That is what is commonly known as the
752.Dq control
753character obtained from the given character.
754The escape sequence
755.Sq Li \ec?
756yields the ASCII DEL character (0x7F).
757Note that to obtain the ASCII FS character (0x1C) this way,
758.Pq "that is control-\e"
759the trailing
760.Sq Li \e
761must be escaped itself, and so for this one case, the full
762escape sequence is
763.Dq Li \ec\e\e .
764The sequence
765.Dq Li \ec\e Ns Ar X\^
766where
767.Sq Ar X\^
768is some character other than
769.Sq Li \e
770is reserved for future use, its meaning is unspecified.
771In this
772.Nm
773an error is generated.
774.Pp
775If any of the preceding escape sequences generate the value
776.Sq \e0
777(a NUL character) that character, and all that follow in the same
778.Li $'...'
779string, are omitted from the resulting word.
780.Pp
781After the
782.Li $'...'
783string has had any included escape sequences
784converted, it is treated as if it had been a single quoted string.
785.\"
786.\"
787.Ss Reserved Words
788.\"
789Reserved words are words that have special meaning to the
790shell and are recognized at the beginning of a line and
791after a control operator.
792The following are reserved words:
793.Bl -column while while while while -offset indent
794.It Ic \&! Ta Ic \&{ Ta Ic \&} Ta Ic case
795.It Ic do Ta Ic done Ta Ic elif Ta Ic else
796.It Ic esac Ta Ic fi Ta Ic for Ta Ic if
797.It Ic in Ta Ic then Ta Ic until Ta Ic while
798.El
799.Pp
800Their meanings are discussed later.
801.\"
802.\"
803.Ss Aliases
804.\"
805An alias is a name and corresponding value set using the
806.Ic alias
807built-in command.
808Whenever a reserved word (see above) may occur,
809and after checking for reserved words, the shell
810checks the word to see if it matches an alias.
811If it does, it replaces it in the input stream with its value.
812For example, if there is an alias called
813.Dq lf
814with the value
815.Dq "ls -F" ,
816then the input:
817.Pp
818.Dl lf foobar Aq return
819.Pp
820would become
821.Pp
822.Dl ls -F foobar Aq return
823.Pp
824Aliases provide a convenient way for naive users to create shorthands for
825commands without having to learn how to create functions with arguments.
826They can also be used to create lexically obscure code.
827This use is strongly discouraged.
828.\"
829.Ss Commands
830.\"
831The shell interprets the words it reads according to a language, the
832specification of which is outside the scope of this man page (refer to the
833BNF in the POSIX 1003.2 document).
834Essentially though, a line is read and if the first
835word of the line (or after a control operator) is not a reserved word,
836then the shell has recognized a simple command.
837Otherwise, a complex
838command or some other special construct may have been recognized.
839.\"
840.\"
841.Ss Simple Commands
842.\"
843If a simple command has been recognized, the shell performs
844the following actions:
845.Bl -enum -offset indent
846.It
847Leading words of the form
848.Dq Ar name Ns Li = Ns Ar value
849are stripped off, the value is expanded, as described below,
850and the results are assigned to the environment of the simple command.
851Redirection operators and their arguments (as described below) are
852stripped off and saved for processing in step 3 below.
853.It
854The remaining words are expanded as described in the
855.Sx Word Expansions
856section below.
857The first remaining word is considered the command name and the
858command is located.
859Any remaining words are considered the arguments of the command.
860If no command name resulted, then the
861.Dq Ar name Ns Li = Ns Ar value
862variable assignments recognized in item 1 affect the current shell.
863.It
864Redirections are performed, from first to last, in the order given,
865as described in the next section.
866.El
867.\"
868.\"
869.Ss Redirections
870.\"
871Redirections are used to change where a command reads its input or sends
872its output.
873In general, redirections open, close, or duplicate an
874existing reference to a file.
875The overall format used for redirection is:
876.Pp
877.Dl Oo Ar n Oc Ns Va redir-op Ar file
878.Pp
879where
880.Va redir-op
881is one of the redirection operators mentioned previously.
882The following is a list of the possible redirections.
883The
884.Op Ar n
885is an optional number, as in
886.Sq Li 3
887(not
888.Li [3] ) ,
889that refers to a file descriptor.
890If present it must occur immediately before the redirection
891operator, with no intervening white space, and becomes a
892part of that operator.
893.Bl -tag -width aaabsfiles -offset indent
894.It Oo Ar n Oc Ns Ic > Ar file
895Redirect standard output (or
896.Ar n )
897to
898.Ar file .
899.It Oo Ar n Oc Ns Ic >| Ar file
900The same, but override the
901.Fl C
902option.
903.It Oo Ar n Oc Ns Ic >> Ar file
904Append standard output (or
905.Ar n )
906to
907.Ar file .
908.It Oo Ar n Oc Ns Ic < Ar file
909Redirect standard input (or
910.Ar n )
911from
912.Ar file .
913.It Oo Ar n1 Oc Ns Ic <& Ns Ar n2
914Duplicate standard input (or
915.Ar n1 )
916from file descriptor
917.Ar n2 .
918.Ar n2
919is expanded if not a digit string, the result must be a number.
920.It Oo Ar n Oc Ns Ic <&-
921Close standard input (or
922.Ar n ) .
923.It Oo Ar n1 Oc Ns Ic >& Ns Ar n2
924Duplicate standard output (or
925.Ar n1 )
926to
927.Ar n2 .
928.It Oo Ar n Oc Ns Ic >&-
929Close standard output (or
930.Ar n ) .
931.It Oo Ar n Oc Ns Ic <> Ar file
932Open
933.Ar file
934for reading and writing on standard input (or
935.Ar n ) .
936.El
937.Pp
938The following redirection is often called a
939.Dq here-document .
940.Bd -unfilled -offset indent
941.Oo Ar n Oc Ns Ic << Ar delimiter
942.Li \&... here-doc-text ...
943.Ar delimiter
944.Ed
945.Pp
946The
947.Dq here-doc-text
948starts immediately after the next unquoted newline character following
949the here-document redirection operator.
950If there is more than one here-document redirection on the same
951line, then the text for the first (from left to right) is read
952first, and subsequent here-doc-text for later here-document redirections
953follows immediately after, until all such redirections have been
954processed.
955.Pp
956All the text on successive lines up to the delimiter,
957which must appear on a line by itself, with nothing other
958than an immediately following newline, is
959saved away and made available to the command on standard input, or file
960descriptor n if it is specified.
961If the delimiter as specified on the initial line is
962quoted, then the here-doc-text is treated literally; otherwise, the text is
963treated much like a double quoted string, except that
964.Sq Li \(dq
965characters have no special meaning, and are not escaped by
966.Sq Li \&\e ,
967and is subjected to parameter expansion, command substitution, and arithmetic
968expansion as described in the
969.Sx Word Expansions
970section below.
971If the operator is
972.Ic <<-
973instead of
974.Ic << ,
975then leading tabs in all lines in the here-doc-text, including before the
976end delimiter, are stripped.
977If the delimiter is not quoted, lines in here-doc-text that end with
978an unquoted
979.Li \e
980are joined to the following line, the
981.Li \e
982and following
983newline are simply removed while reading the here-document,
984which thus guarantees
985that neither of those lines can be the end delimiter.
986.Pp
987It is a syntax error for the end of the input file (or string) to be
988reached before the delimiter is encountered.
989.\"
990.\"
991.Ss Search and Execution
992.\"
993There are three types of commands: shell functions, built-in commands, and
994normal programs \(em and the command is searched for (by name) in that order.
995A command that contains a slash
996.Sq \&/
997in its name is always a normal program.
998They each are executed in a different way.
999.Pp
1000When a shell function is executed, all of the shell positional parameters
1001(note: excluding
1002.Li 0 , \" $0
1003which is a special, not positional, parameter, and remains unchanged)
1004are set to the arguments of the shell function.
1005The variables which are explicitly placed in the environment of
1006the command (by placing assignments to them before the function name) are
1007made local to the function and are set to the values given,
1008and exported for the benefit of programs executed with the function.
1009Then the command given in the function definition is executed.
1010The positional parameters, and local variables, are restored to
1011their original values when the command completes.
1012This all occurs within the current shell, and the function
1013can alter variables, or other settings, of the shell, but
1014not the positional parameters nor their related special parameters.
1015.Pp
1016Shell built-ins are executed internally to the shell, without spawning a
1017new process.
1018.Pp
1019Otherwise, if the command name doesn't match a function or built-in, the
1020command is searched for as a normal program in the file system (as
1021described in the next section).
1022When a normal program is executed, the shell runs the program,
1023passing the arguments and the environment to the program.
1024If the program is not a normal executable file, and if it does
1025not begin with the
1026.Dq magic number
1027whose ASCII representation is
1028.Dq Li "#!" ,
1029so
1030.Xr execve 2
1031returns
1032.Er ENOEXEC
1033then) the shell will interpret the program in a sub-shell.
1034The child shell will reinitialize itself in this case,
1035so that the effect will be as if a
1036new shell had been invoked to handle the ad-hoc shell script, except that
1037the location of hashed commands located in the parent shell will be
1038remembered by the child.
1039.Pp
1040Note that previous versions of this document and the source code itself
1041misleadingly and sporadically refer to a shell script without a magic
1042number as a
1043.Dq shell procedure .
1044.\"
1045.\"
1046.Ss Path Search
1047.\"
1048When locating a command, the shell first looks to see if it has a shell
1049function by that name.
1050Then it looks for a built-in command by that name.
1051If a built-in command is not found, one of two things happen:
1052.Bl -enum
1053.It
1054Command names containing a slash are simply executed without performing
1055any searches.
1056.It
1057Otherwise, the shell searches each entry in
1058.Ev PATH
1059in turn for the command.
1060The value of the
1061.Ev PATH
1062variable should be a series of entries separated by colons.
1063Each entry consists of a directory name.
1064The current directory may be indicated
1065implicitly by an empty directory name, or explicitly by a single period.
1066If a directory searched contains an executable file with the same
1067name as the command given,
1068the search terminates, and that program is executed.
1069.El
1070.Ss Command Exit Status
1071Each command has an exit status that can influence the behavior
1072of other shell commands.
1073The paradigm is that a command exits
1074with zero in normal cases, or to indicate success,
1075and non-zero for failure,
1076error, or a false indication.
1077The man page for each command
1078should indicate the various exit codes and what they mean.
1079Additionally, the built-in commands return exit codes, as does
1080an executed shell function.
1081.Pp
1082If a command consists entirely of variable assignments then the
1083exit status of the command is that of the last command substitution
1084if any, otherwise 0.
1085.Pp
1086If redirections are present, and any fail to be correctly performed,
1087any command present is not executed, and an exit status of 2
1088is returned.
1089.Ss Complex Commands
1090Complex commands are combinations of simple commands with control
1091operators or reserved words, together creating a larger complex command.
1092Overall, a shell program is a:
1093.Bl -tag -width XpipelineX
1094.It list
1095Which is a sequence of one or more AND-OR lists.
1096.It "AND-OR list"
1097is a sequence of one or more pipelines.
1098.It pipeline
1099is a sequence of one or more commands.
1100.It command
1101is one of a simple command, a compound command, or a function definition.
1102.It "simple command"
1103has been explained above, and is the basic building block.
1104.It "compound command"
1105provides mechanisms to group lists to achieve different effects.
1106.It "function definition"
1107allows new simple commands to be created as groupings of existing commands.
1108.El
1109.Pp
1110Unless otherwise stated, the exit status of a list
1111is that of the last simple command executed by the list.
1112.\"
1113.\"
1114.Ss Pipelines
1115.\"
1116A pipeline is a sequence of one or more commands separated
1117by the control operator
1118.Sq Ic \(ba ,
1119and optionally preceded by the
1120.Dq Ic \&!
1121reserved word.
1122Note that
1123.Sq Ic \(ba
1124is an operator, and so is recognized anywhere it appears unquoted,
1125it does not require surrounding white space or other syntax elements.
1126On the other hand
1127.Dq Ic \&!
1128being a reserved word, must be separated from adjacent words by
1129white space (or other operators, perhaps redirects) and is only
1130recognized as the reserved word when it appears in a command word
1131position (such as at the beginning of a pipeline.)
1132.Pp
1133The standard output of all but
1134the last command in the sequence is connected to the standard input
1135of the next command.
1136The standard output of the last
1137command is inherited from the shell, as usual,
1138as is the standard input of the first command.
1139.Pp
1140The format for a pipeline is:
1141.Pp
1142.Dl [!] command1 Op Li \&| command2 No ...
1143.Pp
1144The standard output of command1 is connected to the standard input of
1145command2.
1146The standard input, standard output, or both of each command is
1147considered to be assigned by the pipeline before any redirection specified
1148by redirection operators that are part of the command are performed.
1149.Pp
1150If the pipeline is not in the background (discussed later), the shell
1151waits for all commands to complete.
1152.Pp
1153The commands in a pipeline can either be simple commands,
1154or one of the compound commands described below.
1155The simplest case of a pipeline is a single simple command.
1156.Pp
1157If the
1158.Ic pipefail
1159option is set when the pipeline completes and its status is
1160collected, the pipeline status is the status of
1161the last (rightmost) command in the pipeline to exit with non-zero exit
1162status, or zero, if, and only if, all commands in the pipeline
1163exited with a status of zero.
1164If the
1165.Ic pipefail
1166option is not set, which is the default state,
1167the pipeline status is the exit
1168status of the last command in the pipeline,
1169and the exit status of any other commands in the pipeline is ignored.
1170.Pp
1171If the reserved word
1172.Dq Ic \&!
1173precedes the pipeline, the exit status
1174becomes the logical NOT of the pipeline status as determined above.
1175That is, if the pipeline status is zero, the exit status is 1;
1176if the pipeline status is other than zero, the exit status is zero.
1177If there is no
1178.Dq Ic \&!
1179reserved word, the pipeline status becomes the exit status.
1180.Pp
1181Because pipeline assignment of standard input or standard output or both
1182takes place before redirection, it can be modified by redirection.
1183For example:
1184.Pp
1185.Dl $ command1 2>&1 \&| command2
1186.Pp
1187sends both the standard output and standard error of command1
1188to the standard input of command2.
1189.Pp
1190Note that unlike some other shells, each process in the pipeline is a
1191child of the invoking shell (unless it is a shell built-in, in which case
1192it executes in the current shell \(em but any effect it has on the
1193environment is wiped).
1194.Pp
1195A pipeline is a simple case of an AND-OR-list (described below.)
1196A
1197.Li \&;
1198or
1199.Aq newline
1200terminator causes the preceding pipeline, or more generally,
1201the preceding AND-OR-list to be executed sequentially;
1202that is, the shell executes the commands, and waits for them
1203to finish before proceeding to following commands.
1204An
1205.Li \&&
1206terminator causes asynchronous (background) execution
1207of the preceding AND-OR-list (see the next paragraph below).
1208The exit status of an asynchronous AND-OR-list is zero.
1209The actual status of the commands,
1210after they have completed,
1211can be obtained using the
1212.Ic wait
1213built-in command described later.
1214.\"
1215.\"
1216.Ss Background Commands \(em Ic \&&
1217.\"
1218If a command, pipeline, or AND-OR-list
1219is terminated by the control operator ampersand
1220.Pq Li \&& ,
1221the
1222shell executes the command asynchronously \(em that is, the shell does not
1223wait for the command to finish before executing the next command.
1224.Pp
1225The format for running a command in background is:
1226.Pp
1227.Dl command1 & Op Li command2 & No ...
1228.Pp
1229If the shell is not interactive, the standard input of an asynchronous
1230command is set to
1231.Pa /dev/null .
1232The process identifier of the most recent command started in the
1233background can be obtained from the value of the special parameter
1234.Dq Dv \&! \" $!
1235(see
1236.Sx Special Parameters )
1237provided it is accessed before the next asynchronous command is started.
1238.\"
1239.\"
1240.Ss Lists \(em Generally Speaking
1241.\"
1242A list is a sequence of one or more commands separated by newlines,
1243semicolons, or ampersands, and optionally terminated by one of these three
1244characters.
1245A shell program, which includes the commands given to an
1246interactive shell, is a list.
1247Each command in such a list is executed when it is fully parsed.
1248Another use of a list is as a complete-command,
1249which is parsed in its entirety, and then later the commands in
1250the list are executed only if there were no parsing errors.
1251.Pp
1252The commands in a list are executed in the order they are written.
1253If command is followed by an ampersand, the shell starts the
1254command and immediately proceeds to the next command; otherwise it waits
1255for the command to terminate before proceeding to the next one.
1256A newline is equivalent to a
1257.Sq Li \&;
1258when no other operator is present, and the command being input
1259could syntactically correctly be terminated at the point where
1260the newline is encountered, otherwise it is just whitespace.
1261.\"
1262.\"
1263.Ss AND-OR Lists (Short-Circuit List Operators)
1264.\"
1265.Dq Li \&&&
1266and
1267.Dq Li \&||
1268are AND-OR list operators.
1269After executing the commands that precede the
1270.Dq Li \&&&
1271the subsequent command is executed
1272if and only if the exit status of the preceding command(s) is zero.
1273.Dq Li \&||
1274is similar, but executes the subsequent command if and only if the exit status
1275of the preceding command is nonzero.
1276If a command is not executed, the exit status remains unchanged
1277and the following AND-OR list operator (if any) uses that status.
1278.Dq Li \&&&
1279and
1280.Dq Li \&||
1281both have the same priority.
1282Note that these operators are left-associative, so
1283.Dl true || echo bar && echo baz
1284writes
1285.Dq baz
1286and nothing else.
1287This is not the way it works in C.
1288.\"
1289.\"
1290.Ss Flow-Control Constructs \(em Ic if , while , until , for , case
1291.\"
1292These commands are instances of compound commands.
1293The syntax of the
1294.Ic if
1295command is
1296.Bd -literal -offset indent
1297.Ic if Ar list
1298.Ic then Ar list
1299.Ic [ elif Ar list
1300.Ic then  Ar list ] No ...
1301.Ic [ else Ar list ]
1302.Ic fi
1303.Ed
1304.Pp
1305The first list is executed, and if the exit status of that list is zero,
1306the list following the
1307.Ic then
1308is executed.
1309Otherwise the list after an
1310.Ic elif
1311(if any) is executed and the process repeats.
1312When no more
1313.Ic elif
1314reserved words, and accompanying lists, appear,
1315the list after the
1316.Ic else
1317reserved word, if any, is executed.
1318.Pp
1319The syntax of the
1320.Ic while
1321command is
1322.Bd -literal -offset indent
1323.Ic while Ar list
1324.Ic do Ar list
1325.Ic done
1326.Ed
1327.Pp
1328The two lists are executed repeatedly while the exit status of the
1329first list is zero.
1330The
1331.Ic until
1332command is similar, but has the word
1333.Ic until
1334in place of
1335.Ic while ,
1336which causes it to repeat until the exit status of the first list is zero.
1337.Pp
1338The syntax of the
1339.Ic for
1340command is
1341.Bd -literal -offset indent
1342.Ic for Ar variable Op Ic in Ar word No ...
1343.Ic do Ar list
1344.Ic done
1345.Ed
1346.Pp
1347The words are expanded, or
1348.Li \*q$@\*q
1349if
1350.Ic in
1351(and the following words) is not present,
1352and then the list is executed repeatedly with the
1353variable set to each word in turn.
1354If
1355.Ic in
1356appears after the variable, but no words are
1357present, the list is not executed, and the exit status is zero.
1358.Ic do
1359and
1360.Ic done
1361may be replaced with
1362.Sq Ic \&{
1363and
1364.Sq Ic \&} ,
1365but doing so is non-standard and not recommended.
1366.Pp
1367The syntax of the
1368.Ic break
1369and
1370.Ic continue
1371commands is
1372.Bd -literal -offset indent
1373.Ic break Op Ar num
1374.Ic continue Op Ar num
1375.Ed
1376.Pp
1377.Ic break
1378terminates the
1379.Ar num
1380innermost
1381.Ic for , while ,
1382or
1383.Ic until
1384loops.
1385.Ic continue
1386breaks execution of the
1387.Ar num\^ Ns -1
1388innermost
1389.Ic for , while ,
1390or
1391.Ic until
1392loops, and then continues with the next iteration of the enclosing loop.
1393These are implemented as special built-in commands.
1394The parameter
1395.Ar num ,
1396if given, must be an unsigned positive integer (greater than zero).
1397If not given, 1 is used.
1398.Pp
1399The syntax of the
1400.Ic case
1401command is
1402.Bd -literal -offset indent
1403.Ic case Ar word Ic in
1404.Oo Ic \&( Oc  Ar pattern Ns Ic \&) Oo Ar list Oc Ic \&;&
1405.Oo Ic \&( Oc  Ar pattern Ns Ic \&) Oo Ar list Oc Ic \&;;
1406.No \&...
1407.Ic esac
1408.Ed
1409.Pp
1410The pattern can actually be one or more patterns (see
1411.Sx Shell Patterns
1412described later), separated by
1413.Dq \(or
1414characters.
1415.Pp
1416.Ar word
1417is expanded and matched against each
1418.Ar pattern
1419in turn,
1420from first to last,
1421with each pattern being expanded just before the match is attempted.
1422When a match is found, pattern comparisons cease, and the associated
1423.Ar list ,
1424if given,
1425is evaluated.
1426If the list is terminated with
1427.Dq Ic \&;&
1428execution then falls through to the following list, if any,
1429without evaluating its pattern, or attempting a match.
1430When a list terminated with
1431.Dq Ic \&;;
1432has been executed, or when
1433.Ic esac
1434is reached, execution of the
1435.Ic case
1436statement is complete.
1437The exit status is that of the last command executed
1438from the last list evaluated, if any, or zero otherwise.
1439.\"
1440.\"
1441.Ss Grouping Commands Together
1442.\"
1443Commands may be grouped by writing either
1444.Dl Ic \&( Ns Ar list Ns Ic \&)
1445or
1446.Dl Ic \&{ Ar list Ns Ic \&; \&}
1447These also form compound commands.
1448.Pp
1449Note that while parentheses are operators, and do not require
1450any extra syntax, braces are reserved words, so the opening brace
1451must be followed by white space (or some other operator), and the
1452closing brace must occur in a position where a new command word might
1453otherwise appear.
1454.Pp
1455The first of these executes the commands in a sub-shell.
1456Built-in commands grouped into a
1457.Li \&( Ns Ar list Ns \&)
1458will not affect the current shell.
1459The second form does not fork another shell so is slightly more efficient,
1460and allows for commands which do affect the current shell.
1461Grouping commands together this way allows you to redirect
1462their output as though they were one program:
1463.Bd -literal -offset indent
1464{ echo -n \*qhello \*q ; echo \*qworld\*q ; } > greeting
1465.Ed
1466.Pp
1467Note that
1468.Dq Ic }
1469must follow a control operator (here,
1470.Dq Ic \&; )
1471so that it is recognized as a reserved word and not as another command argument.
1472.\"
1473.\"
1474.Ss Functions
1475.\"
1476The syntax of a function definition is
1477.Pp
1478.Dl Ar name Ns Ic \&() Ar command Op Ar redirect No ...
1479.Pp
1480A function definition is an executable statement; when executed it
1481installs a function named name and returns an exit status of zero.
1482The command is normally a list enclosed between
1483.Dq {
1484and
1485.Dq } .
1486The standard syntax also allows the command to be any of the other
1487compound commands, including a sub-shell, all of which are supported.
1488As an extension, this shell also allows a simple command
1489(or even another function definition) to be
1490used, though users should be aware this is non-standard syntax.
1491This means that
1492.Dl l() ls \*q$@\*q
1493works to make
1494.Dq l
1495an alternative name for the
1496.Ic ls
1497command.
1498.Pp
1499If the optional redirect, (see
1500.Sx Redirections ) ,
1501which may be of any of the normal forms,
1502is given, it is applied each time the
1503function is called.
1504This means that a simple
1505.Dq Hello World
1506function might be written (in the extended syntax) as:
1507.Bd -literal -offset indent
1508hello() cat <<EOF
1509Hello World!
1510EOF
1511.Ed
1512.Pp
1513To be correctly standards conforming this should be re-written as:
1514.Bd -literal -offset indent
1515hello() { cat; } <<EOF
1516Hello World!
1517EOF
1518.Ed
1519.Pp
1520Note the distinction between those forms, and
1521.Bd -literal -offset indent
1522hello() { cat <<EOF
1523Hello World!
1524EOF
1525\&}
1526.Ed
1527.Pp
1528which reads and processes the here-document
1529each time the shell executes the function, and which applies
1530that input only to the cat command, not to any other commands
1531that might appear in the function.
1532.Pp
1533Variables may be declared to be local to a function by using the
1534.Ic local
1535command.
1536This should usually appear as the first statement of a function,
1537though
1538.Ic local
1539is an executable command which can be used anywhere in a function.
1540See
1541.Sx Built-ins
1542below for its definition.
1543.Pp
1544The function completes after having executed
1545.Ar command
1546with exit status set to the status returned by
1547.Ar command .
1548If
1549.Ar command
1550is a compound-command
1551it can use the
1552.Ic return
1553command (see
1554.Sx Built-ins
1555below)
1556to finish before completing all of
1557.Ar command .
1558.Ss Variables and Parameters
1559The shell maintains a set of parameters.
1560A parameter denoted by a name is called a variable.
1561When starting up, the shell turns all the environment
1562variables into shell variables, and exports them.
1563New variables can be set using the form
1564.Pp
1565.Dl Ar name Ns Li = Ns Ar value
1566.Pp
1567Variables set by the user must have a name consisting solely of
1568alphabetics, numerics, and underscores \(em the first of which must not be
1569numeric.
1570A parameter can also be denoted by a number or a special
1571character as explained below.
1572.Ss Positional Parameters
1573A positional parameter is a parameter denoted by a number (n > 0).
1574The shell sets these initially to the values of its command line arguments
1575that follow the name of the shell script.
1576The
1577.Ic set
1578built-in can also be used to set or reset them, and
1579.Ic shift
1580can be used to manipulate the list.
1581.Pp
1582To refer to the 10th (and later) positional parameters,
1583the form
1584.Li \&${ Ns Ar n Ns Li \&}
1585must be used.
1586Without the braces, a digit following
1587.Dq $
1588can only refer to one of the first 9 positional parameters,
1589or the special parameter
1590.Dv 0 . \" $0
1591The word
1592.Dq Li $10
1593is treated identically to
1594.Dq Li ${1}0 .
1595.\"
1596.\"
1597.Ss Special Parameters
1598.\"
1599A special parameter is a parameter denoted by one of the following special
1600characters.
1601The value of the parameter is listed next to its character.
1602.Bl -tag -width thinhyphena
1603.It Dv *
1604Expands to the positional parameters, starting from one.
1605When the
1606expansion occurs within a double-quoted string it expands to a single
1607field with the value of each parameter separated by the first character of
1608the
1609.Ev IFS
1610variable, or by a
1611.Aq space
1612if
1613.Ev IFS
1614is unset.
1615.It Dv @ \" $@
1616Expands to the positional parameters, starting from one.
1617When the expansion occurs within double quotes, each positional
1618parameter expands as a separate argument.
1619If there are no positional parameters, the
1620expansion of @ generates zero arguments, even when
1621.Dv $@
1622is double-quoted.
1623What this basically means, for example, is
1624if
1625.Li $1
1626is
1627.Dq abc
1628and
1629.Li $2
1630is
1631.Dq def\ ghi ,
1632then
1633.Li \*q$@\*q
1634expands to
1635the two arguments:
1636.Pp
1637.Sm off
1638.Dl \*q abc \*q \  \*q def\ ghi \*q
1639.Sm on
1640.It Dv #
1641Expands to the number of positional parameters.
1642.It Dv \&?
1643Expands to the exit status of the most recent pipeline.
1644.It Dv \- No (hyphen, or minus)
1645Expands to the current option flags (the single-letter
1646option names concatenated into a string) as specified on
1647invocation, by the set built-in command, or implicitly
1648by the shell.
1649.It Dv $
1650Expands to the process ID of the invoked shell.
1651A sub-shell retains the same value of
1652.Dv $
1653as its parent.
1654.It Dv \&!
1655Expands to the process ID of the most recent background
1656command executed from the current shell.
1657For a pipeline, the process ID is that of the last command in the pipeline.
1658If no background commands have yet been started by the shell, then
1659.Dq Dv \&!
1660will be unset.
1661Once set, the value of
1662.Dq Dv \&!
1663will be retained until another background command is started.
1664.It Dv 0 No (zero) \" $0
1665Expands to the name of the shell or shell script.
1666.El
1667.\"
1668.\"
1669.Ss Word Expansions
1670.\"
1671This section describes the various expansions that are performed on words.
1672Not all expansions are performed on every word, as explained later.
1673.Pp
1674Tilde expansions, parameter expansions, command substitutions, arithmetic
1675expansions, and quote removals that occur within a single word expand to a
1676single field.
1677It is only field splitting or pathname expansion that can
1678create multiple fields from a single word.
1679The single exception to this
1680rule is the expansion of the special parameter
1681.Dv @ \" $@
1682within double quotes, as was described above.
1683.Pp
1684The order of word expansion is:
1685.Bl -enum
1686.It
1687Tilde Expansion, Parameter Expansion, Command Substitution,
1688Arithmetic Expansion (these all occur at the same time).
1689.It
1690Field Splitting is performed on fields
1691generated by step (1) unless the
1692.Ev IFS
1693variable is null.
1694.It
1695Pathname Expansion (unless set
1696.Fl f
1697is in effect).
1698.It
1699Quote Removal.
1700.El
1701.Pp
1702The $ character is used to introduce parameter expansion, command
1703substitution, or arithmetic evaluation.
1704.Ss Tilde Expansion (substituting a user's home directory)
1705A word beginning with an unquoted tilde character (~) is
1706subjected to tilde expansion.
1707Provided all of the subsequent characters in the word are unquoted
1708up to an unquoted slash (/)
1709or when in an assignment or not in posix mode, an unquoted colon (:),
1710or if neither of those appear, the end of the word,
1711they are treated as a user name
1712and are replaced with the pathname of the named user's home directory.
1713If the user name is missing (as in
1714.Pa ~/foobar ) ,
1715the tilde is replaced with the value of the
1716.Dv HOME
1717variable (the current user's home directory).
1718.Pp
1719In variable assignments,
1720an unquoted tilde immediately after the assignment operator (=), and
1721each unquoted tilde immediately after an unquoted colon in the value
1722to be assigned is also subject to tilde expansion as just stated.
1723.\"
1724.\"
1725.Ss Parameter Expansion
1726.\"
1727The format for parameter expansion is as follows:
1728.Pp
1729.Dl ${ Ns Ar expression Ns Li }
1730.Pp
1731where
1732.Ar expression
1733consists of all characters until the matching
1734.Sq Li } .
1735Any
1736.Sq Li }
1737escaped by a backslash or within a quoted string, and characters in
1738embedded arithmetic expansions, command substitutions, and variable
1739expansions, are not examined in determining the matching
1740.Sq Li } .
1741.Pp
1742The simplest form for parameter expansion is:
1743.Pp
1744.Dl ${ Ns Ar parameter Ns Li }
1745.Pp
1746The value, if any, of
1747.Ar parameter
1748is substituted.
1749.Pp
1750The parameter name or symbol can be enclosed in braces,
1751which are optional in this simple case,
1752except for positional parameters with more than one digit or
1753when parameter is followed by a character that could be interpreted as
1754part of the name.
1755If a parameter expansion occurs inside double quotes:
1756.Bl -enum
1757.It
1758pathname expansion is not performed on the results of the expansion;
1759.It
1760field splitting is not performed on the results of the
1761expansion, with the exception of the special rules for
1762.Dv @ . \" $@
1763.El
1764.Pp
1765In addition, a parameter expansion where braces are used,
1766can be modified by using one of the following formats.
1767If the
1768.Sq Ic \&:
1769is omitted in the following modifiers, then the test in the expansion
1770applies only to unset parameters, not null ones.
1771.Bl -tag -width aaparameterwordaaaaa
1772.It Li ${ Ns Ar parameter Ns Ic :- Ns Ar word Ns Li }
1773.Sy Use Default Values.
1774If
1775.Ar parameter
1776is unset or null, the expansion of
1777.Ar word
1778is substituted; otherwise, the value of
1779.Ar parameter
1780is substituted.
1781.It Li ${ Ns Ar parameter Ns Ic := Ns Ar word Ns Li }
1782.Sy Assign Default Values.
1783If
1784.Ar parameter
1785is unset or null, the expansion of
1786.Ar word
1787is assigned to
1788.Ar parameter .
1789In all cases, the final value of
1790.Ar parameter
1791is substituted.
1792Only variables, not positional parameters or special
1793parameters, can be assigned in this way.
1794.It Li ${ Ns Ar parameter Ns Ic :? Ns Oo Ar word\^ Oc Ns Li }
1795.Sy Indicate Error if Null or Unset.
1796If
1797.Ar parameter
1798is unset or null, the expansion of
1799.Ar word
1800(or a message indicating it is unset if
1801.Ar word
1802is omitted)
1803is written to standard error and a non-interactive shell exits with
1804a nonzero exit status.
1805An interactive shell will not exit, but any associated command(s) will
1806not be executed.
1807If the
1808.Ar parameter
1809is set, its value is substituted.
1810.It Li ${ Ns Ar parameter Ns Ic :+ Ns Ar word Ns Li }
1811.Sy Use Alternative Value.
1812If
1813.Ar parameter
1814is unset or null, null is substituted;
1815otherwise, the expansion of
1816.Ar word
1817is substituted.
1818The value of
1819.Ar parameter
1820.Em is not used
1821in this expansion.
1822.It Li ${ Ns Ic # Ns Ar parameter Ns Li }
1823.Sy String Length.
1824The length in characters of the value of
1825.Ar parameter .
1826.El
1827.Pp
1828The following four varieties of parameter expansion provide for substring
1829processing.
1830In each case, pattern matching notation (see
1831.Sx Shell Patterns ) ,
1832rather than regular expression notation, is used to evaluate the patterns.
1833If parameter is
1834.Dv *
1835or
1836.Dv @ , \" $@
1837the result of the expansion is unspecified.
1838Enclosing the full parameter expansion string in double quotes does not
1839cause the following four varieties of pattern characters to be quoted,
1840whereas quoting characters within the braces has this effect.
1841.Bl -tag -width aaparameterwordaaaaa
1842.It Li ${ Ns Ar parameter Ns Ic % Ns Ar word Ns Li }
1843.Sy Remove Smallest Suffix Pattern.
1844The
1845.Ar word
1846is expanded to produce a pattern.
1847The parameter expansion then results in
1848.Ar parameter ,
1849with the
1850smallest portion of the suffix matched by the pattern deleted.
1851If the
1852.Ar word
1853is to start with a
1854.Sq Li \&%
1855character, it must be quoted.
1856.It Li ${ Ns Ar parameter Ns Ic %% Ns Ar word Ns Li }
1857.Sy Remove Largest Suffix Pattern.
1858The
1859.Ar word
1860is expanded to produce a pattern.
1861The parameter expansion then results in
1862.Ar parameter ,
1863with the largest
1864portion of the suffix matched by the pattern deleted.
1865The
1866.Dq Ic %%
1867pattern operator only produces different results from the
1868.Dq Ic \&%
1869operator when the pattern contains at least one unquoted
1870.Sq Li \&* .
1871.It Li ${ Ns Ar parameter Ns Ic \&# Ns Ar word Ns Li }
1872.Sy Remove Smallest Prefix Pattern.
1873The
1874.Ar word
1875is expanded to produce a pattern.
1876The parameter expansion then results in
1877.Ar parameter ,
1878with the
1879smallest portion of the prefix matched by the pattern deleted.
1880If the
1881.Ar word
1882is to start with a
1883.Sq Li \&#
1884character, it must be quoted.
1885.It Li ${ Ns Ar parameter Ns Ic \&## Ns Ar word Ns Li }
1886.Sy Remove Largest Prefix Pattern.
1887The
1888.Ar word
1889is expanded to produce a pattern.
1890The parameter expansion then results in
1891.Ar parameter ,
1892with the largest
1893portion of the prefix matched by the pattern deleted.
1894This has the same relationship with the
1895.Dq Ic \&#
1896pattern operator as
1897.Dq Ic %%
1898has with
1899.Dq Ic \&% .
1900.El
1901.\"
1902.\"
1903.Ss Command Substitution
1904.\"
1905Command substitution allows the output of a command to be substituted in
1906place of the command (and surrounding syntax).
1907Command substitution occurs when the command is enclosed as follows:
1908.Pp
1909.Dl Ic $( Ns Ar command Ns Ic \&)
1910.Pp
1911or
1912.Po
1913.Dq backquoted
1914version
1915.Pc :
1916.Pp
1917.Dl Ic \&` Ns Ar command Ns Ic \&`
1918.Pp
1919The shell expands the command substitution by executing command in a
1920sub-shell environment and replacing the command substitution with the
1921standard output of the command, removing sequences of one or more
1922.Ao newline Ac Ns s
1923at the end of the substitution.
1924(Embedded
1925.Ao newline Ac Ns s
1926before
1927the end of the output are not removed; however, during field splitting,
1928they may be translated into
1929.Ao space Ac Ns s ,
1930depending on the value of
1931.Ev IFS
1932and any quoting that is in effect.)
1933.\"
1934.\"
1935.Ss Arithmetic Expansion
1936.\"
1937Arithmetic expansion provides a mechanism for evaluating an arithmetic
1938expression and substituting its value.
1939The format for arithmetic expansion is as follows:
1940.Pp
1941.Dl Ic $(( Ns Ar expression Ns Ic \&))
1942.Pp
1943The expression in an arithmetic expansion is treated as if it were in
1944double quotes, except that a double quote character inside the expression
1945is just a normal character (it quotes nothing.)
1946The shell expands all tokens in the expression for parameter expansion,
1947command substitution, and quote removal (the only quoting character is
1948the backslash
1949.Sq \&\e ,
1950and only when followed by another
1951.Sq \&\e ,
1952a dollar sign
1953.Sq \&$ ,
1954a backquote
1955.Sq \&`
1956or a newline.)
1957.Pp
1958Next, the shell evaluates the expanded result as an arithmetic expression
1959and substitutes the calculated value of that expression.
1960.Pp
1961Arithmetic expressions use a syntax similar to that
1962of the C language, and are evaluated using the
1963.Ql intmax_t
1964data type (this is an extension to POSIX, which requires only
1965.Ql long
1966arithmetic.)
1967Shell variables may be referenced by name inside an arithmetic
1968expression, without needing a
1969.Dq \&$
1970sign.
1971Variables that are not set, or which have an empty (null string) value,
1972used this way evaluate as zero (that is,
1973.Dq x
1974in arithmetic, as an R-Value, is evaluated as
1975.Dq ${x:-0} )
1976unless the
1977.Nm
1978.Fl u
1979flag is set, in which case a reference to an unset variable is an error.
1980Note that unset variables used in the ${var} form expand to a null
1981string, which might result in syntax errors.
1982Referencing the value of a variable which is not numeric is an error.
1983.Pp
1984All of the C expression operators applicable to integers are supported,
1985and operate as they would in a C expression.
1986Use white space, or parentheses, to disambiguate confusing syntax,
1987otherwise, as in C, the longest sequence of consecutive characters
1988which make a valid token (operator, variable name, or number) is taken
1989to be that token, even if the token designated cannot be used
1990and a different interpretation could produce a successful parse.
1991This means, as an example, that
1992.Dq a+++++b
1993is parsed as the gibberish sequence
1994.Dq "a ++ ++ + b" ,
1995rather than as the valid alternative
1996.Dq "a ++ + ++ b" .
1997Similarly, separate the
1998.Sq \&,
1999operator from numbers with white space to avoid the possibility
2000of confusion with the decimal indicator in some locales (though
2001fractional, or floating-point, numbers are not supported in this
2002implementation.)
2003.Pp
2004It should not be necessary to state that the C operators which
2005operate on, or produce, pointer types, are not supported.
2006Those include unary
2007.Dq \&*
2008and
2009.Dq \&&
2010and the struct and array referencing binary operators:
2011.Dq \&. ,
2012.Dq \&->
2013and
2014.Dq \&[ .
2015.\"
2016.\"
2017.Ss White Space Splitting (Field Splitting)
2018.\"
2019After parameter expansion, command substitution, and
2020arithmetic expansion the shell scans the results of
2021expansions and substitutions that did not occur in double quotes,
2022and
2023.Dq Li $@
2024even if it did,
2025for field splitting and multiple fields can result.
2026.Pp
2027The shell treats each character of the
2028.Ev IFS
2029as a delimiter and uses the delimiters to split the results of parameter
2030expansion and command substitution into fields.
2031.Pp
2032Non-whitespace characters in
2033.Ev IFS
2034are treated strictly as parameter separators.
2035So adjacent non-whitespace
2036.Ev IFS
2037characters will produce empty parameters.
2038On the other hand, any sequence of whitespace
2039characters that occur in
2040.Ev IFS
2041(known as
2042.Ev IFS
2043whitespace)
2044can occur, leading and trailing
2045.Ev IFS
2046whitespace, and any
2047.Ev IFS
2048whitespace surrounding a non whitespace
2049.Ev IFS
2050delimiter, is removed.
2051Any sequence of
2052.Ev IFS
2053whitespace characters without a non-whitespace
2054.Ev IFS
2055delimiter acts as a single field separator.
2056.Pp
2057If
2058.Ev IFS
2059is unset it is assumed to contain space, tab, and newline,
2060all of which are
2061.Ev IFS
2062whitespace characters.
2063If
2064.Ev IFS
2065is set to a null string, there are no delimiters,
2066and no field splitting occurs.
2067.Ss Pathname Expansion (File Name Generation)
2068Unless the
2069.Fl f
2070flag is set, file name generation is performed after word splitting is
2071complete.
2072Each word is viewed as a series of patterns, separated by slashes.
2073The process of expansion replaces the word with the names of all
2074existing files whose names can be formed by replacing each pattern with a
2075string that matches the specified pattern.
2076There are two restrictions on
2077this: first, a pattern cannot match a string containing a slash, and
2078second, a pattern cannot match a string starting with a period unless the
2079first character of the pattern is a period.
2080The next section describes the
2081patterns used for both Pathname Expansion and the
2082.Ic case
2083command.
2084.Ss Shell Patterns
2085A pattern consists of normal characters, which match themselves,
2086and meta-characters.
2087The meta-characters are
2088.Dq \&! ,
2089.Dq * ,
2090.Dq \&? ,
2091and
2092.Dq \&[ .
2093These characters lose their special meanings if they are quoted.
2094When command or variable substitution is performed
2095and the dollar sign or backquotes are not double-quoted,
2096the value of the variable or the output of
2097the command is scanned for these characters and they are turned into
2098meta-characters.
2099.Pp
2100An asterisk
2101.Pq Dq *
2102matches any string of characters.
2103A question mark
2104.Pq Dq \&?
2105matches any single character.
2106A left bracket
2107.Pq Dq \&[
2108introduces a character class.
2109The end of the character class is indicated by a right bracket
2110.Pq Dq \&] ;
2111if this
2112.Dq \&]
2113is missing then the
2114.Dq \&[
2115matches a
2116.Dq \&[
2117rather than introducing a character class.
2118A character class matches any of the characters between the square brackets.
2119A named class of characters (see
2120.Xr wctype 3 )
2121may be specified by surrounding the name with
2122.Pq Dq [:
2123and
2124.Pq Dq :] .
2125For example,
2126.Pq Dq [[:alpha:]]
2127is a shell pattern that matches a single letter.
2128A range of characters may be specified using a minus sign
2129.Pq Dq \(mi .
2130The character class may be complemented
2131by making an exclamation mark
2132.Pq Dq \&!
2133the first character of the character class.
2134.Pp
2135To include a
2136.Dq \&]
2137in a character class, make it the first character listed (after the
2138.Dq \&! ,
2139if any).
2140To include a
2141.Dq \(mi ,
2142make it the first (after !) or last character listed.
2143If both
2144.Dq \&]
2145and
2146.Dq \(mi
2147are to be included, the
2148.Dq \&]
2149must be first (after !)
2150and the
2151.Dq \(mi
2152last, in the character class.
2153.\"
2154.\"
2155.Ss Built-ins
2156.\"
2157This section lists the built-in commands which are built-in because they
2158need to perform some operation that can't be performed by a separate
2159process.
2160Or just because they traditionally are.
2161In addition to these, there are several other commands that may
2162be built in for efficiency (e.g.
2163.Xr printf 1 ,
2164.Xr echo 1 ,
2165.Xr test 1 ,
2166etc).
2167.Bl -tag -width 5n
2168.It Ic \&: Op Ar arg ...
2169A null command that returns a 0 (true) exit value.
2170Any arguments or redirects are evaluated, then ignored.
2171.It Ic \&. Ar file
2172The dot command reads and executes the commands from the specified
2173.Ar file
2174in the current shell environment.
2175The file does not need to be executable and is looked up from the directories
2176listed in the
2177.Ev PATH
2178variable if its name does not contain a directory separator
2179.Pq Sq / .
2180The
2181.Ic return
2182command (see below)
2183can be used for a premature return from the sourced file.
2184.Pp
2185The POSIX standard has been unclear on how loop control keywords (break
2186and continue) behave across a dot command boundary.
2187This implementation allows them to control loops surrounding the dot command,
2188but obviously such behavior should not be relied on.
2189It is now permitted by the standard, but not required.
2190.It Ic alias Op Ar name Ns Op Li = Ns Ar string ...
2191If
2192.Ar name Ns Li = Ns Ar string
2193is specified, the shell defines the alias
2194.Ar name
2195with value
2196.Ar string .
2197If just
2198.Ar name
2199is specified, the value of the alias
2200.Ar name
2201is printed.
2202With no arguments, the
2203.Ic alias
2204built-in prints the
2205names and values of all defined aliases (see
2206.Ic unalias ) .
2207.It Ic bg Op Ar job ...
2208Continue the specified jobs (or the current job if no
2209jobs are given) in the background.
2210.It Ic command Oo Fl pVv Oc Ar command Op Ar arg ...
2211Execute the specified command but ignore shell functions when searching
2212for it.
2213(This is useful when you
2214have a shell function with the same name as a command.)
2215.Bl -tag -width 5n
2216.It Fl p
2217search for command using a
2218.Ev PATH
2219that guarantees to find all the standard utilities.
2220.It Fl V
2221Do not execute the command but
2222search for the command and print the resolution of the
2223command search.
2224This is the same as the
2225.Ic type
2226built-in.
2227.It Fl v
2228Do not execute the command but
2229search for the command and print the absolute pathname
2230of utilities, the name for built-ins or the expansion of aliases.
2231.El
2232.It Ic cd Oo Fl P Oc Op Ar directory Op Ar replace
2233Switch to the specified directory (default
2234.Ev $HOME ) .
2235If
2236.Ar replace
2237is specified, then the new directory name is generated by replacing
2238the first occurrence of the string
2239.Ar directory
2240in the current directory name with
2241.Ar replace .
2242Otherwise if
2243.Ar directory
2244is
2245.Sq Li - ,
2246then the current working directory is changed to the previous current
2247working directory as set in
2248.Ev OLDPWD .
2249Otherwise if an entry for
2250.Ev CDPATH
2251appears in the environment of the
2252.Ic cd
2253command or the shell variable
2254.Ev CDPATH
2255is set and the directory name does not begin with a slash,
2256and its first (or only) component isn't dot or dot dot,
2257then the directories listed in
2258.Ev CDPATH
2259will be searched for the specified directory.
2260The format of
2261.Ev CDPATH
2262is the same as that of
2263.Ev PATH .
2264.Pp
2265The
2266.Fl P
2267option instructs the shell to update
2268.Ev PWD
2269with the specified physical directory path and change to that directory.
2270This is the default.
2271.Pp
2272When the directory changes, the variable
2273.Ev OLDPWD
2274is set to the working directory before the change.
2275.Pp
2276Some shells also support a
2277.Fl L
2278option, which instructs the shell to update
2279.Ev PWD
2280with the logical path and to change the current directory
2281accordingly.
2282This is not supported.
2283.Pp
2284In an interactive shell, the
2285.Ic cd
2286command will print out the name of the
2287directory that it actually switched to if this is different from the name
2288that the user gave,
2289or always if the
2290.Cm cdprint
2291option is set.
2292The destination may be different either because the
2293.Ev CDPATH
2294mechanism was used
2295.\" or because a symbolic link was crossed.
2296or if the
2297.Ar replace
2298argument was used.
2299.It Ic eval Ar string ...
2300Concatenate all the arguments with spaces.
2301Then re-parse and execute the command.
2302.It Ic exec Op Ar command Op Ar arg ...
2303Unless
2304.Ar command
2305is omitted, the shell process is replaced with the
2306specified program (which must be a real program, not a shell built-in or
2307function).
2308Any redirections on the
2309.Ic exec
2310command are marked as permanent, so that they are not undone when the
2311.Ic exec
2312command finishes.
2313When the
2314.Cm posix
2315option is not set,
2316file descriptors created via such redirections are marked close-on-exec
2317(see
2318.Xr open 2
2319.Dv O_CLOEXEC
2320or
2321.Xr fcntl 2
2322.Dv F_SETFD /
2323.Dv FD_CLOEXEC ) ,
2324unless the descriptors they point to refer to the standard input,
2325output, or error (file descriptors 0, 1, 2).
2326Traditionally Bourne-like shells
2327(except
2328.Xr ksh 1 ) ,
2329made those file descriptors available to exec'ed processes.
2330This behavior is required by the POSIX standard, so when the
2331.Cm posix
2332option is set, this shell also acts that way.
2333To be assured the close-on-exec setting is off,
2334redirect the descriptor to (or from) itself,
2335either when invoking a command for which the descriptor is wanted open,
2336or by using
2337.Ic exec
2338(perhaps the same
2339.Ic exec
2340as opened it, after the open)
2341to leave the descriptor open in the shell
2342and pass it to all commands invoked subsequently.
2343Alternatively, see the
2344.Ic fdflags
2345command below, which can set, or clear, this, and other,
2346file descriptor flags.
2347.It Ic exit Op Ar exitstatus
2348Terminate the shell process.
2349If
2350.Ar exitstatus
2351is given it is used as the exit status of the shell; otherwise the
2352exit status of the preceding command (the current value of $?) is used.
2353.It Ic export Oo Fl npx Oc Ar name ...
2354.It Ic export Fl p Op Fl x
2355With no options,
2356but one or more names,
2357the specified names are exported so that they will appear in the
2358environment of subsequent commands.
2359With
2360.Fl n
2361the specified names are un-exported.
2362Variables can also be un-exported using the
2363.Ic unset
2364built in command.
2365With
2366.Fl x
2367(exclude) the specified names are marked not to be exported,
2368and any that had been exported, will be un-exported.
2369Later attempts to export the variable will be refused.
2370Note this does not prevent explicitly exporting a variable
2371to a single command, script or function by preceding that
2372command invocation by a variable assignment to that variable,
2373provided the variable is not also read-only.
2374That is
2375.Bd -literal -offset indent
2376export -x FOO # FOO will now not be exported by default
2377FOO=some_value my_command
2378.Ed
2379.Pp
2380still passes the value
2381.Pq Li FOO=some_value
2382to
2383.Li my_command
2384through the environment.
2385.Pp
2386The shell allows the value of a variable to be set at the
2387same time it is exported by writing
2388.Pp
2389.Dl export name=value
2390.Pp
2391With no arguments the export command lists the names of all exported variables,
2392or if
2393.Fl x
2394was given, all variables marked not for export.
2395With the
2396.Fl p
2397option specified the output will be formatted suitably for non-interactive use.
2398.Pp
2399The
2400.Ic export
2401built-in exits with status 0,
2402unless an invalid option, or option combination, is given,
2403or unless an attempt is made to export a variable which has
2404been marked as unavailable for export,
2405in which cases it exits with status 1.
2406.Pp
2407Note that there is no restriction upon exporting,
2408or un-exporting, read-only variables.
2409The no-export flag can be reset by unsetting the variable
2410and creating it again \(en provided it is not also read-only.
2411.It Ic fc Oo Fl e Ar editor Oc Op Ar first Op Ar last
2412.It Ic fc Fl l Oo Fl nr Oc Op Ar first Op Ar last
2413.It Ic fc Fl s Oo Ar old=new Oc Op Ar first
2414The
2415.Ic fc
2416built-in lists, or edits and re-executes, commands previously entered
2417to an interactive shell.
2418.Bl -tag -width 5n
2419.It Fl e Ar editor
2420Use the editor named by
2421.Ar editor
2422to edit the commands.
2423The
2424.Ar editor
2425string is a command name, subject to search via the
2426.Ev PATH
2427variable.
2428The value in the
2429.Ev FCEDIT
2430variable is used as a default when
2431.Fl e
2432is not specified.
2433If
2434.Ev FCEDIT
2435is null or unset, the value of the
2436.Ev EDITOR
2437variable is used.
2438If
2439.Ev EDITOR
2440is null or unset,
2441.Xr ed 1
2442is used as the editor.
2443.It Fl l No (ell)
2444List the commands rather than invoking an editor on them.
2445The commands are written in the sequence indicated by
2446the first and last operands, as affected by
2447.Fl r ,
2448with each command preceded by the command number.
2449.It Fl n
2450Suppress command numbers when listing with
2451.Fl l .
2452.It Fl r
2453Reverse the order of the commands listed (with
2454.Fl l )
2455or edited (with neither
2456.Fl l
2457nor
2458.Fl s ) .
2459.It Fl s
2460Re-execute the command without invoking an editor.
2461.It Ar first
2462.It Ar last
2463Select the commands to list or edit.
2464The number of previous commands that
2465can be accessed are determined by the value of the
2466.Ev HISTSIZE
2467variable.
2468The value of
2469.Ar first
2470or
2471.Ar last
2472or both are one of the following:
2473.Bl -tag -width 5n
2474.It Oo Cm + Oc Ns Ar number
2475A positive number representing a command number; command numbers can be
2476displayed with the
2477.Fl l
2478option.
2479.It Cm \- Ns Ar number
2480A negative decimal number representing the command that was executed
2481number of commands previously.
2482For example, \-1 is the immediately previous command.
2483.El
2484.It Ar string
2485A string indicating the most recently entered command that begins with
2486that string.
2487If the
2488.Ar old Ns Li = Ns Ar new
2489operand is not also specified with
2490.Fl s ,
2491the string form of the first operand cannot contain an embedded equal sign.
2492.El
2493.Pp
2494The following environment variables affect the execution of
2495.Ic fc :
2496.Bl -tag -width HISTSIZE
2497.It Ev FCEDIT
2498Name of the editor to use.
2499.It Ev HISTSIZE
2500The number of previous commands that are accessible.
2501.El
2502.It Ic fg Op Ar job
2503Move the specified job or the current job to the foreground.
2504A foreground job can interact with the user via standard input,
2505and receive signals from the terminal.
2506.It Ic fdflags Oo Fl v Oc Op Ar fd ...
2507.It Ic fdflags Oo Fl v Oc Fl s Ar flags fd Op ...
2508Get or set file descriptor flags.
2509The
2510.Fl v
2511argument enables verbose printing, printing flags that are also off, and
2512the flags of the file descriptor being set after setting.
2513The
2514.Fl s
2515flag interprets the
2516.Ar flags
2517argument as a comma separated list of file descriptor flags, each preceded
2518with a
2519.Dq \(pl
2520or a
2521.Dq \(mi
2522indicating to set or clear the respective flag.
2523Valid flags are:
2524.Cm append ,
2525.Cm async ,
2526.Cm sync ,
2527.Cm nonblock ,
2528.Cm fsync ,
2529.Cm dsync ,
2530.Cm rsync ,
2531.Cm direct ,
2532.Cm nosigpipe ,
2533and
2534.Cm cloexec .
2535Unique abbreviations of these names, of at least 2 characters,
2536may be used on input.
2537See
2538.Xr fcntl 2
2539and
2540.Xr open 2
2541for more information.
2542.It Ic getopts Ar optstring var
2543The POSIX
2544.Ic getopts
2545command, not to be confused with the
2546Bell Labs\[en]derived
2547.Xr getopt 1 .
2548.Pp
2549The first argument should be a series of letters, each of which may be
2550optionally followed by a colon to indicate that the option requires an
2551argument.
2552The variable specified is set to the parsed option.
2553.Pp
2554The
2555.Ic getopts
2556command deprecates the older
2557.Xr getopt 1
2558utility due to its handling of arguments containing whitespace.
2559.Pp
2560The
2561.Ic getopts
2562built-in may be used to obtain options and their arguments
2563from a list of parameters.
2564When invoked,
2565.Ic getopts
2566places the value of the next option from the option string in the list in
2567the shell variable specified by
2568.Ar var
2569and its index in the shell variable
2570.Ev OPTIND .
2571When the shell is invoked,
2572.Ev OPTIND
2573is initialized to 1.
2574For each option that requires an argument, the
2575.Ic getopts
2576built-in will place it in the shell variable
2577.Ev OPTARG .
2578If an option is not allowed for in the
2579.Ar optstring ,
2580then
2581.Ev OPTARG
2582will be unset.
2583.Pp
2584.Ar optstring
2585is a string of recognized option letters (see
2586.Xr getopt 3 ) .
2587If a letter is followed by a colon, the option is expected to have an
2588argument which may or may not be separated from it by whitespace.
2589If an option character is not found where expected,
2590.Ic getopts
2591will set the variable
2592.Ar var
2593to a
2594.Sq Li \&? ;
2595.Ic getopts
2596will then unset
2597.Ev OPTARG
2598and write output to standard error.
2599By specifying a colon as the first character of
2600.Ar optstring
2601all errors will be ignored.
2602.Pp
2603A nonzero value is returned when the last option is reached.
2604If there are no remaining arguments,
2605.Ic getopts
2606will set
2607.Ar var
2608to the special option,
2609.Dq Li \-\- ,
2610otherwise, it will set
2611.Ar var
2612to
2613.Sq Li \&? .
2614.Pp
2615The following code fragment shows how one might process the arguments
2616for a command that can take the options
2617.Fl a
2618and
2619.Fl b ,
2620and the option
2621.Fl c ,
2622which requires an argument.
2623.Bd -literal -offset indent
2624while getopts abc: f
2625do
2626	case $f in
2627	a | b)	flag=$f;;
2628	c)	carg=$OPTARG;;
2629	\e?)	echo $USAGE; exit 1;;
2630	esac
2631done
2632shift $((OPTIND - 1))
2633.Ed
2634.Pp
2635This code will accept any of the following as equivalent:
2636.Bd -literal -offset indent
2637cmd \-acarg file file
2638cmd \-a \-c arg file file
2639cmd \-carg -a file file
2640cmd \-a \-carg \-\- file file
2641.Ed
2642.It Ic hash Oo Fl rv Oc Op Ar command ...
2643The shell maintains a hash table which remembers the
2644locations of commands.
2645With no arguments whatsoever,
2646the
2647.Ic hash
2648command prints out the contents of this table.
2649Entries which have not been looked at since the last
2650.Ic cd
2651command are marked with an asterisk; it is possible for these entries
2652to be invalid.
2653.Pp
2654With arguments, the
2655.Ic hash
2656command removes the specified commands from the hash table (unless
2657they are functions) and then locates them.
2658With the
2659.Fl v
2660option, hash prints the locations of the commands as it finds them.
2661The
2662.Fl r
2663option causes the hash command to delete all the entries in the hash table
2664except for functions.
2665.It Ic inputrc Ar file
2666Read the
2667.Ar file
2668to set key bindings as defined by
2669.Xr editrc 5 .
2670.It Ic jobid Oo Fl g Ns \&| Ns Fl j Ns \&| Ns Fl p Oc  Op Ar job
2671With no flags, print the process identifiers of the processes in the job.
2672If the
2673.Ar job
2674argument is omitted, the current job is used.
2675Any of the ways to select a job may be used for
2676.Ar job ,
2677including the
2678.Sq Li \&%
2679forms, or the process id of the job leader
2680.Po
2681.Dq Li \&$!
2682if the job was created in the background.
2683.Pc
2684.Pp
2685If one of the flags is given, then instead of the list of
2686process identifiers, the
2687.Ic jobid
2688command prints:
2689.Bl -tag -width ".Fl g"
2690.It Fl g
2691the process group, if one was created for this job,
2692or nothing otherwise (the job is in the same process
2693group as the shell.)
2694.It Fl j
2695the job identifier (using
2696.Dq Li \&% Ns Ar n
2697notation, where
2698.Ar n
2699is a number) is printed.
2700.It Fl p
2701only the process id of the process group leader is printed.
2702.El
2703.Pp
2704These flags are mutually exclusive.
2705.Pp
2706.Ic jobid
2707exits with status 2 if there is an argument error,
2708status 1, if with
2709.Fl g
2710the job had no separate process group,
2711or with
2712.Fl p
2713there is no process group leader (should not happen),
2714and otherwise exits with status 0.
2715.It Ic jobs Oo Fl l Ns \&| Ns Fl p Oc Op Ar job ...
2716Without
2717.Ar job
2718arguments,
2719this command lists out all the background processes
2720which are children of the current shell process.
2721With
2722.Ar job
2723arguments, the listed jobs are shown instead.
2724Without flags, the output contains the job
2725identifier (see
2726.Sx Job Control
2727below), an indicator character if the job is the current or previous job,
2728the current status of the job (running, suspended, or terminated successfully,
2729unsuccessfully, or by a signal)
2730and a (usually abbreviated) command string.
2731.Pp
2732With the
2733.Fl l
2734flag the output is in a longer form, with the process identifiers
2735of each process (run from the top level, as in a pipeline), and the
2736status of each process, rather than the job status.
2737.Pp
2738With the
2739.Fl p
2740flag, the output contains only the process identifier of the lead
2741process.
2742.Pp
2743In an interactive shell, each job shown as completed in the output
2744from the jobs command is implicitly waited for, and is removed from
2745the jobs table, never to be seen again.
2746In an interactive shell, when a background job terminates, the
2747.Ic jobs
2748command (with that job as an argument) is implicitly run just
2749before outputting the next PS1 command prompt, after the job
2750terminated.
2751This indicates that the job finished, shows its status,
2752and cleans up the job table entry for that job.
2753Non-interactive shells need to execute
2754.Ic wait
2755commands to clean up terminated background jobs.
2756.It Ic local Oo Fl INx Oc Oo Ar variable | \- Oc ...
2757Define local variables for a function.
2758Local variables have their attributes, and values,
2759as they were before the
2760.Ic local
2761declaration, restored when the function terminates.
2762.Pp
2763With the
2764.Fl N
2765flag, variables made local, are unset initially inside
2766the function.
2767Unless the
2768.Fl x
2769flag is also given, such variables are also unexported.
2770The
2771.Fl I
2772flag, which is the default in this shell, causes
2773the initial value and exported attribute
2774of local variables
2775to be inherited from the variable
2776with the same name in the surrounding
2777scope, if there is one.
2778If there is not, the variable is initially unset,
2779and not exported.
2780The
2781.Fl N
2782and
2783.Fl I
2784flags are mutually exclusive, if both are given, the last specified applies.
2785The read-only and unexportable attributes are always
2786inherited, if a variable with the same name already exists.
2787.Pp
2788The
2789.Fl x
2790flag (lower case) causes the local variable to be exported,
2791while the function runs, unless it has the unexportable attribute.
2792This can also be accomplished by using the
2793.Ic export
2794command, giving the same
2795.Ar variable
2796names, after the
2797.Ic local
2798command.
2799.Pp
2800Making an existing read-only variable local is possible,
2801but pointless.
2802If an attempt is made to assign an initial value to such
2803a variable, the
2804.Ic local
2805command fails, as does any later attempted assignment.
2806If the
2807.Ic readonly
2808command is applied to a variable that has been declared local,
2809the variable cannot be (further) modified within the function,
2810or any other functions it calls, however when the function returns,
2811the previous status (and value) of the variable is returned.
2812.Pp
2813Values may be given to local variables on the
2814.Ic local
2815command line in a similar fashion as used for
2816.Ic export
2817and
2818.Ic readonly .
2819These values are assigned immediately after the initialization
2820described above.
2821Note that any variable references on the command line will have
2822been expanded before
2823.Ic local
2824is executed, so expressions like
2825.Pp
2826.Dl "local -N X=${X}"
2827.Pp
2828are well defined, first $X is expanded, and then the command run is
2829.Pp
2830.Dl "local -N X=old-value-of-X"
2831.Pp
2832After arranging to preserve the old value and attributes, of
2833.Dv X
2834.Dq ( old-value-of X )
2835.Ic local
2836unsets
2837.Dv X ,
2838unexports it, and then assigns the
2839.Dq old-value-of-X
2840to
2841.Ev X .
2842.Pp
2843The shell uses dynamic scoping, so that if you make the variable
2844.Dv x
2845local to
2846function
2847.Dv f ,
2848which then calls function
2849.Dv g ,
2850references to the variable
2851.Dv x
2852made inside
2853.Dv g
2854will refer to the variable
2855.Dv x
2856declared inside
2857.Dv f ,
2858not to the global variable named
2859.Dv x .
2860.Pp
2861Another way to view this, is as if the shell just has one flat, global,
2862namespace, in which all variables exist.
2863The
2864.Ic local
2865command conceptually copies the variable(s) named to unnamed temporary
2866variables, and when the function ends, copies them back again.
2867All references to the variables reference the same global variables,
2868but while the function is active, after the
2869.Ic local
2870command has run, the values and attributes of the variables might
2871be altered, and later, when the function completes, be restored.
2872.Pp
2873Note that the positional parameters
2874.Dv 1 ,  \" $1
2875.Dv 2 ,  \" $2
2876\&... (see
2877.Sx Positional Parameters ) ,
2878and the special parameters
2879.Dv \&# , \" $#
2880.Dv \&*   \" $*
2881and
2882.Dv \&@   \" $@
2883(see
2884.Sx Special Parameters ) ,
2885are always made local in all functions, and are reset inside the
2886function to represent the options and arguments passed to the function.
2887Note that
2888.Li $0
2889however retains the value it had outside the function,
2890as do all the other special parameters.
2891.Pp
2892The only special parameter that can optionally be made local is
2893.Dq Li \- .
2894Making
2895.Dq Li \-
2896local causes any shell options that are changed via the set command inside the
2897function to be restored to their original values when the function
2898returns.
2899If
2900.Fl X
2901option is altered after
2902.Dq Li \-
2903has been made local, then when the function returns, the previous
2904destination for
2905.Cm xtrace
2906output (as of the time of the
2907.Ic local
2908command) will also be restored.
2909.Pp
2910It is an error to use
2911.Ic local
2912outside the scope of a function definition.
2913When used inside a function, it exits with status 0,
2914unless an undefined option is used, or an attempt is made to
2915assign a value to a read-only variable.
2916.Pp
2917Note that either
2918.Fl I
2919or
2920.Fl N
2921should always be used, or variables made local should always
2922be given a value, or explicitly unset, as the default behavior
2923(inheriting the earlier value, or starting unset after
2924.Ic local )
2925differs amongst shell implementations.
2926Using
2927.Dq Li local \&\-
2928is an extension not implemented by most shells.
2929.Pp
2930See the section
2931.Sx LINENO
2932below for details of the effects of making the variable
2933.Dv LINENO
2934local.
2935.It Ic pwd Op Fl \&LP
2936Print the current directory.
2937If
2938.Fl L
2939is specified the cached value (initially set from
2940.Ev PWD )
2941is checked to see if it refers to the current directory; if it does
2942the value is printed.
2943Otherwise the current directory name is found using
2944.Xr getcwd 3 .
2945The environment variable
2946.Ev PWD
2947is set to the printed value.
2948.Pp
2949The default is
2950.Ic pwd
2951.Fl L ,
2952but note that the built-in
2953.Ic cd
2954command doesn't support the
2955.Fl L
2956option and will cache (almost) the absolute path.
2957If
2958.Ic cd
2959is changed (as unlikely as that is),
2960.Ic pwd
2961may be changed to default to
2962.Ic pwd
2963.Fl P .
2964.Pp
2965If the current directory is renamed and replaced by a symlink to the
2966same directory, or the initial
2967.Ev PWD
2968value followed a symbolic link, then the cached value may not
2969be the absolute path.
2970.Pp
2971The built-in command may differ from the program of the same name because
2972the program will use
2973.Ev PWD
2974and the built-in uses a separately cached value.
2975.It Ic read Oo Fl p Ar prompt Oc Oo Fl r Oc Ar variable Op Ar ...
2976The
2977.Ar prompt
2978is printed if the
2979.Fl p
2980option is specified and the standard input is a terminal.
2981Then a line is read from the standard input.
2982The trailing newline is deleted from the
2983line and the line is split as described in the field splitting section of the
2984.Sx Word Expansions
2985section above, and the pieces are assigned to the variables in order.
2986If there are more pieces than variables, the remaining pieces
2987(along with the characters in
2988.Ev IFS
2989that separated them) are assigned to the last variable.
2990If there are more variables than pieces,
2991the remaining variables are assigned the null string.
2992The
2993.Ic read
2994built-in will indicate success unless EOF is encountered on input, in
2995which case failure is returned.
2996.Pp
2997By default, unless the
2998.Fl r
2999option is specified, the backslash
3000.Dq \e
3001acts as an escape character, causing the following character to be treated
3002literally.
3003If a backslash is followed by a newline, the backslash and the
3004newline will be deleted.
3005.It Ic readonly Ar name ...
3006.It Ic readonly Op Fl p
3007The specified names are marked as read only, so that they cannot be
3008subsequently modified or unset.
3009The shell allows the value of a variable
3010to be set at the same time it is marked read only by writing
3011.Pp
3012.Dl readonly name=value
3013.Pp
3014With no arguments the
3015.Ic readonly
3016command lists the names of all read only variables.
3017With the
3018.Fl p
3019option specified the output will be formatted suitably for non-interactive use.
3020.It Ic return Op Ar n
3021Stop executing the current function or a dot command with return value of
3022.Ar n
3023or the value of the last executed command, if not specified.
3024For portability,
3025.Ar n
3026should be in the range from 0 to 255.
3027.Pp
3028The POSIX standard says that the results of
3029.Ic return
3030outside a function or a dot command are unspecified.
3031This implementation treats such a return as a no-op with a return value of 0
3032(success, true).
3033Use the
3034.Ic exit
3035command instead, if you want to return from a script or exit
3036your shell.
3037.It Ic set Oo { Fl options | Cm +options | Cm \-- } Oc Ar arg ...
3038The
3039.Ic set
3040command performs four different functions.
3041.Pp
3042With no arguments, it lists the values of all shell variables.
3043.Pp
3044With a single option of either
3045.Dq Fl o
3046or
3047.Dq Cm +o
3048.Ic set
3049outputs the current values of the options.
3050In the
3051.Fl o
3052form, all options are listed, with their current values.
3053In the
3054.Cm +o
3055form, the shell outputs a string that can later be used
3056as a command to reset all options to their current values.
3057.Pp
3058If options are given, it sets the specified option
3059flags, or clears them as described in the
3060.Sx Argument List Processing
3061section.
3062In addition to the options listed there,
3063when the
3064.Dq "option name"
3065given to
3066.Ic set Fl o
3067is
3068.Cm default
3069all of the options are reset to the values they had immediately
3070after
3071.Nm
3072initialization, before any startup scripts, or other input, had been processed.
3073While this may be of use to users or scripts, its primary purpose
3074is for use in the output of
3075.Dq Ic set Cm +o ,
3076to avoid that command needing to list every available option.
3077There is no
3078.Cm +o default .
3079.Pp
3080The fourth use of the
3081.Ic set
3082command is to set the values of the shell's
3083positional parameters to the specified arguments.
3084To change the positional
3085parameters without changing any options, use
3086.Dq -\|-
3087as the first argument to
3088.Ic set .
3089If no following arguments are present, the
3090.Ic set
3091command
3092will clear all the positional parameters (equivalent to executing
3093.Dq Li shift $# . )
3094Otherwise the following arguments become
3095.Li \&$1 ,
3096.Li \&$2 ,
3097\&...,
3098and
3099.Li \&$#
3100is set to the number of arguments present.
3101.It Ic setvar Ar variable Ar value
3102Assigns
3103.Ar value
3104to
3105.Ar variable .
3106(In general it is better to write
3107.Li variable=value
3108rather than using
3109.Ic setvar .
3110.Ic setvar
3111is intended to be used in
3112functions that assign values to variables whose names are passed as
3113parameters.)
3114.It Ic shift Op Ar n
3115Shift the positional parameters
3116.Ar n
3117times.
3118If
3119.Ar n
3120is omitted, 1 is assumed.
3121Each
3122.Ic shift
3123sets the value of
3124.Li $1
3125to the previous value of
3126.Li $2 ,
3127the value of
3128.Li $2
3129to the previous value of
3130.Li $3 ,
3131and so on, decreasing
3132the value of
3133.Li $#
3134by one.
3135The shift count must be less than or equal to the number of
3136positional parameters (
3137.Dq Li $# )
3138before the shift.
3139.It Ic times
3140Prints two lines to standard output.
3141Each line contains two accumulated time values, expressed
3142in minutes and seconds (including fractions of a second.)
3143The first value gives the user time consumed, the second the system time.
3144.Pp
3145The first output line gives the CPU and system times consumed by the
3146shell itself.
3147The second line gives the accumulated times for children of this
3148shell (and their descendants) which have exited, and then been
3149successfully waited for by the relevant parent.
3150See
3151.Xr times 3
3152for more information.
3153.Pp
3154.Ic times
3155has no parameters, and exits with an exit status of 0 unless
3156an attempt is made to give it an option.
3157.It Ic trap Ar action signal ...
3158.It Ic trap \-
3159.It Ic trap Op Fl l
3160.It Ic trap Oo Fl p Oc Ar signal ...
3161.It Ic trap Ar N signal ...
3162.Pp
3163Cause the shell to parse and execute action when any of the specified
3164signals are received.
3165The signals are specified by signal number or as the name of the signal.
3166If
3167.Ar signal
3168is
3169.Li 0 \" $0
3170or its equivalent,
3171.Li EXIT ,
3172the action is executed when the shell exits.
3173The
3174.Ar action
3175may be a null (empty) string,
3176which causes the specified signals to be ignored.
3177With
3178.Ar action
3179set to
3180.Sq Li -
3181the specified signals are set to their default actions.
3182If the first
3183.Ar signal
3184is specified in its numeric form, then
3185.Ar action
3186can be omitted to achieve the same effect.
3187This archaic,
3188but still standard,
3189form should not be relied upon, use the explicit
3190.Sq Li -
3191action.
3192If no signals are specified with an action of
3193.Sq Li - ,
3194all signals are reset.
3195.Pp
3196When the shell forks off a sub-shell, it resets trapped (but not ignored)
3197signals to the default action.
3198On non-interactive shells, the
3199.Ic trap
3200command has no effect on signals that were
3201ignored on entry to the shell.
3202On interactive shells, the
3203.Ic trap
3204command will catch or reset signals ignored on entry.
3205.Pp
3206Issuing
3207.Ic trap
3208with option
3209.Fl l
3210will print a list of valid signal names.
3211.Ic trap
3212without any arguments causes it to write a list of signals and their
3213associated non-default actions to the standard output in a format
3214that is suitable as an input to the shell that achieves the same
3215trapping results.
3216With the
3217.Fl p
3218flag, trap prints the same information for the signals specified,
3219or if none are given, for all signals, including those where the
3220action is the default.
3221.Pp
3222Examples:
3223.Pp
3224.Dl trap
3225.Pp
3226List trapped signals and their corresponding actions.
3227.Pp
3228.Dl trap -l
3229.Pp
3230Print a list of valid signals.
3231.Pp
3232.Dl trap '' INT QUIT tstp 30
3233.Pp
3234Ignore signals INT QUIT TSTP USR1.
3235.Pp
3236.Dl trap date INT
3237.Pp
3238Run the
3239.Dq date
3240command (print the date) upon receiving signal INT.
3241.Pp
3242.Dl trap HUP INT
3243.Pp
3244Run the
3245.Dq HUP
3246command, or function, upon receiving signal INT.
3247.Pp
3248.Dl trap 1 2
3249.Pp
3250Reset the actions for signals 1 (HUP) and 2 (INT) to their defaults.
3251.Bd -literal -offset indent
3252traps=$(trap -p)
3253   # more commands ...
3254trap 'action' SIG
3255   # more commands ...
3256eval "$traps"
3257.Ed
3258.Pp
3259Save the trap status, execute commands, changing some traps,
3260and then reset all traps to their values at the start of the sequence.
3261The
3262.Fl p
3263option is required in the first command here,
3264or any signals that were previously
3265untrapped (in their default states)
3266and which were altered during the intermediate code,
3267would not be reset by the final
3268.Ic eval .
3269.It Ic type Op Ar name ...
3270Interpret each
3271.Ar name
3272as a command and print the resolution of the command search.
3273Possible resolutions are:
3274shell keyword, alias, shell built-in,
3275command, tracked alias and not found.
3276For aliases the alias expansion is
3277printed; for commands and tracked aliases the complete pathname of the
3278command is printed.
3279.It Ic ulimit Oo Fl H Ns \*(Ba Ns Fl S Oc Op Fl a \*(Ba Fl btfdscmlrpnv Op Ar value
3280Inquire about or set the hard or soft limits on processes or set new
3281limits.
3282The choice between hard limit (which no process is allowed to
3283violate, and which may not be raised once it has been lowered) and soft
3284limit (which causes processes to be signaled but not necessarily killed,
3285and which may be raised) is made with these flags:
3286.Pp
3287.Bl -tag -width Fl
3288.It Fl H
3289set or inquire about hard limits
3290.It Fl S
3291set or inquire about soft limits.
3292.El
3293.Pp
3294If neither
3295.Fl H
3296nor
3297.Fl S
3298is specified, the soft limit is displayed or both limits are set.
3299If both are specified, the last one wins.
3300.Pp
3301The limit to be interrogated or set, then, is chosen by specifying
3302any one of these flags:
3303.Pp
3304.Bl -tag -width Fl
3305.It Fl a
3306show all the current limits
3307.It Fl b
3308the socket buffer size of a process (bytes)
3309.It Fl c
3310the largest core dump size that can be produced
3311(512-byte blocks)
3312.It Fl d
3313the data segment size of a process (kilobytes)
3314.It Fl f
3315the largest file that can be created
3316(512-byte blocks)
3317.It Fl l
3318how much memory a process can lock with
3319.Xr mlock 2
3320(kilobytes)
3321.It Fl m
3322the total physical memory that can be
3323in use by a process (kilobytes)
3324.It Fl n
3325the number of files a process can have open at once
3326.It Fl p
3327the number of processes this user can
3328have at one time
3329.It Fl r
3330the number of threads this user can
3331have at one time
3332.It Fl s
3333the stack size of a process (kilobytes)
3334.It Fl t
3335CPU time (seconds)
3336.It Fl v
3337how large a process address space can be
3338.El
3339.Pp
3340If none of these is specified, it is the limit on file size that is shown
3341or set.
3342If value is specified, the limit is set to that number; otherwise
3343the current limit is displayed.
3344.Pp
3345Limits of an arbitrary process can be displayed or set using the
3346.Xr sysctl 8
3347utility.
3348.It Ic umask Oo Fl S Oc Op Ar mask
3349Set the value of umask (see
3350.Xr umask 2 )
3351to the specified octal value.
3352If the argument is omitted, the umask value is printed.
3353With
3354.Fl S
3355a symbolic form is used instead of an octal number.
3356.It Ic unalias Oo Fl a Oc Op Ar name
3357If
3358.Ar name
3359is specified, the shell removes that alias.
3360If
3361.Fl a
3362is specified, all aliases are removed.
3363.It Ic unset Oo Fl efvx Oc Ar name ...
3364If
3365.Fl v
3366is specified, the specified variables are unset and unexported.
3367Readonly variables cannot be unset.
3368If
3369.Fl f
3370is specified, the specified functions are undefined.
3371If
3372.Fl e
3373is given, the specified variables are unexported, but otherwise unchanged,
3374alternatively, if
3375.Fl x
3376is given, the exported status of the variable will be retained,
3377even after it is unset.
3378.Pp
3379If no flags are provided
3380.Fl v
3381is assumed.
3382If
3383.Fl f
3384is given with one of the other flags,
3385then the named variables will be unset, or unexported, and functions
3386of the same names will be undefined.
3387The
3388.Fl e
3389and
3390.Fl x
3391flags both imply
3392.Fl v .
3393If
3394.Fl e
3395is given, the
3396.Fl x
3397flag is ignored.
3398.Pp
3399The exit status is 0, unless an attempt was made to unset
3400a readonly variable, in which case the exit status is 1.
3401It is not an error to unset (or undefine) a variable (or function)
3402that is not currently set (or defined.)
3403.It Ic wait Oo Fl n Oc Oo Fl p Ar var Oc Op Ar job ...
3404Wait for the specified jobs to complete
3405and return the exit status of the last job in the parameter list,
3406or 127 if that job is not a current child of the shell.
3407.Pp
3408If no
3409.Ar job
3410arguments are given, wait for all jobs to
3411complete and then return an exit status of zero
3412(including when there were no jobs, and so nothing exited.)
3413.Pp
3414With the
3415.Fl n
3416option, wait instead for any one of the given
3417.Ar job Ns s,
3418or if none are given, any job, to complete, and
3419return the exit status of that job.
3420If none of the given
3421.Ar job
3422arguments is a current child of the shell,
3423or if no
3424.Ar job
3425arguments are given and the shell has no unwaited for children,
3426then the exit status will be 127.
3427.Pp
3428The
3429.Fl p Ar var
3430option allows the process (or job) identifier of the
3431job for which the exit status is returned to be obtained.
3432The variable named (which must not be readonly) will be
3433unset initially, then if a job has exited and its status is
3434being returned, set to the identifier from the
3435arg list (if given) of that job,
3436or the lead process identifier of the job to exit when used with
3437.Fl n
3438and no job arguments.
3439Note that
3440.Fl p
3441with neither
3442.Fl n
3443nor
3444.Ar job
3445arguments is useless, as in that case no job status is
3446returned, the variable named is simply unset.
3447.Pp
3448If the wait is interrupted by a signal,
3449its exit status will be greater than 128,
3450and
3451.Ar var ,
3452if given, will remain unset.
3453.Pp
3454Once waited upon, by specific process number or job-id,
3455or by a
3456.Ic wait
3457with no arguments,
3458knowledge of the child is removed from the system,
3459and it cannot be waited upon again.
3460.Pp
3461Note than when a list of jobs are given, more that
3462one argument might refer to the same job.
3463In that case, if the final argument represents a job
3464that is also given earlier in the list, it is not
3465defined whether the status returned will be the
3466exit status of the job, or 127 indicating that
3467the child no longer existed when the wait command
3468reached the later argument in the list.
3469In this
3470.Nm
3471the exit status will be that from the job.
3472.Nm
3473waits for each job exactly once, regardless of
3474how many times (or how many different ways) it
3475is listed in the arguments to
3476.Ic wait .
3477That is
3478.Bd -literal -offset indent -compact
3479wait 100 100 100
3480.Ed
3481is identical to
3482.Bd -literal -offset indent -compact
3483wait 100
3484.Ed
3485.El
3486.\"
3487.\"
3488.Ss Job Control
3489.\"
3490Each process (or set of processes) started by
3491.Nm
3492is created as a
3493.Dq job
3494and added to the jobs table.
3495When enabled by the
3496.Fl m
3497option
3498.Pq aka Fl o Cm monitor
3499when the job is created,
3500.Nm
3501places each job (if run from the top level shell)
3502into a process group of its own, which allows control
3503of the process(es), and its/their descendants, as a unit.
3504When the
3505.Fl m
3506option is off, or when started from a sub-shell environment,
3507jobs share the same process group as the parent shell.
3508The
3509.Fl m
3510option is enabled by default in interactive shells with
3511a terminal as standard input and standard error.
3512.Pp
3513Jobs with separate process groups may be stopped, and then later
3514resumed in the foreground (with access to the terminal)
3515or in the background (where attempting to read from the
3516terminal will result in the job stopping.)
3517A list of current jobs can be obtained using the
3518.Ic jobs
3519built-in command.
3520Jobs are identified using either the process identifier
3521of the lead process of the job (the value available in
3522the special parameter
3523.Dq Dv \&!
3524if the job is started in the background), or using percent
3525notation.
3526Each job is given a
3527.Dq job number
3528which is a small integer, starting from 1, and can be
3529referenced as
3530.Dq Li \&% Ns Ar n
3531where
3532.Ar n
3533is that number.
3534Note that this applies to jobs both with and without their own process groups.
3535Job numbers are shown in the output from the
3536.Ic jobs
3537command enclosed in brackets
3538.Po
3539.Sq Li \&[
3540and
3541.Sq Li \&]
3542.Pc .
3543Whenever the job table becomes empty, the numbers begin at one again.
3544In addition, there is the concept of a current, and a previous job,
3545identified by
3546.Dq Li \&%+
3547.Po
3548or
3549.Dq Li \&%%
3550or even just
3551.Dq Li \&%
3552.Pc ,
3553and a previous job, identified by
3554.Dq Li \&%\- .
3555Whenever a background job is started,
3556or a job is resumed in the background,
3557it becomes the current job.
3558The job that was the current job
3559(prepare for a big surprise here, drum roll..., wait for it...\&)
3560becomes the previous job.
3561When the current job terminates, the previous job is
3562promoted to be the current job.
3563In addition the form
3564.Dq Li \&% Ns Ar string\^
3565finds the job for which the command starts with
3566.Ar string
3567and the form
3568.Dq Li \&%? Ns Ar string\^
3569finds the job which contains the
3570.Ar string
3571in its command somewhere.
3572Both forms require the result to be unambiguous.
3573For this purpose the
3574.Dq command
3575is that shown in the output from the
3576.Ic jobs
3577command, not the original command line.
3578.Pp
3579The
3580.Ic bg ,
3581.Ic fg ,
3582.Ic jobid ,
3583.Ic jobs ,
3584.Ic kill ,
3585and
3586.Ic wait
3587commands all accept job identifiers as arguments, in addition to
3588process identifiers (larger integers).
3589See the
3590.Sx Built-ins
3591section above, and
3592.Xr kill 1 ,
3593for more details of those commands.
3594In addition, a job identifier
3595(using one of the
3596.Dq \&% forms )
3597issued as a command, without arguments, is interpreted as
3598if it had been given as the argument to the
3599.Ic fg
3600command.
3601.Pp
3602To cause a foreground process to stop, enter the terminal's
3603.Ic stop
3604character (usually control-Z).
3605To cause a background process to stop, send it a
3606.Dv STOP
3607signal, using the kill command.
3608A useful function to define is
3609.Bd -literal -offset indent
3610stop() { kill -s STOP "${@:-%%}"; }
3611.Ed
3612.Pp
3613The
3614.Ic fg
3615command resumes a stopped job, placing it in the foreground,
3616and
3617.Ic bg
3618resumes a stopped job in the background.
3619The
3620.Ic jobid
3621command provides information about process identifiers, job identifiers,
3622and the process group identifier, for a job.
3623.Pp
3624Whenever a sub-shell is created, the jobs table becomes invalid
3625(the sub-shell has no children.)
3626However, to enable uses like
3627.Bd -literal -offset indent
3628PID=$(jobid -p %1)
3629.Ed
3630.Pp
3631the table is only actually cleared in a sub-shell when needed to
3632create the first job there (built-in commands run in the foreground
3633do not create jobs.)
3634Note that in this environment, there is no useful current job
3635.Dq ( Li \&%%
3636actually refers to the sub-shell itself, but is not accessible)
3637but the job which is the current job in the parent can be accessed as
3638.Dq Li \&%\- .
3639.\"
3640.\"
3641.Ss Command Line Editing
3642.\"
3643When
3644.Nm
3645is being used interactively from a terminal, the current command
3646and the command history (see
3647.Ic fc
3648in the
3649.Sx Built-ins
3650section)
3651can be edited using emacs-mode or vi-mode command-line editing.
3652The command
3653.Ql set -o emacs
3654(or
3655.Fl E
3656option)
3657enables emacs-mode editing.
3658The command
3659.Ql set -o vi
3660(or
3661.Fl V
3662option)
3663enables vi-mode editing and places the current shell process into
3664vi insert mode.
3665(See the
3666.Sx Argument List Processing
3667section above.)
3668.Pp
3669The vi-mode uses commands similar to a subset of those described in the
3670.Xr vi 1
3671man page.
3672With vi-mode
3673enabled,
3674.Nm sh
3675can be switched between insert mode and command mode.
3676It's similar to
3677.Ic vi :
3678pressing the
3679.Aq ESC
3680key will throw you into vi command mode.
3681Pressing the
3682.Aq return
3683key while in command mode will pass the line to the shell.
3684.Pp
3685The emacs-mode uses commands similar to a subset available in the
3686.Ic emacs
3687editor.
3688With emacs-mode enabled, special keys can be used to modify the text
3689in the buffer using the control key.
3690.Pp
3691.Nm
3692uses the
3693.Xr editline 3
3694library.
3695See
3696.Xr editline 7
3697for a list of the possible command bindings,
3698and the default settings in emacs and vi modes.
3699Also see
3700.Xr editrc 5
3701for the commands that can be given to configure
3702.Xr editline 7
3703in the file named by the
3704.Ev EDITRC
3705parameter,
3706or a file used with the
3707.Ic inputrc
3708built-in command,
3709or using
3710.Xr editline 7 Ap s
3711configuration command line.
3712.Pp
3713When command line editing is enabled, the
3714.Xr editline 7
3715functions control printing of the
3716.Ev PS1
3717and
3718.Ev PS2
3719prompts when required.
3720As, in this mode, the command line editor needs to
3721keep track of what characters are in what position on
3722the command line, care needs to be taken when setting
3723the prompts.
3724Normal printing characters are handled automatically,
3725however mode setting sequences, which do not actually display
3726on the terminal, need to be identified to
3727.Xr editline 7 .
3728This is done, when needed, by choosing a character that
3729is not needed anywhere in the prompt, including in the mode
3730setting sequences, any single character is acceptable,
3731and assigning it to the shell parameter
3732.Dv PSlit .
3733Then that character should be used, in pairs, in the
3734prompt string.
3735Between each pair of
3736.Dv PSlit
3737characters are mode setting sequences, which affect the printing
3738attributes of the following (normal) characters of the prompt,
3739but do not themselves appear visibly, nor change the terminal's
3740cursor position.
3741.Pp
3742Each such sequence, that is
3743.Dv PSlit
3744character, mode setting character sequence, and another
3745.Dv PSlit
3746character, must currently be followed by at least one following
3747normal prompt character, or it will be ignored.
3748That is, a
3749.Dv PSlit
3750character cannot be the final character of
3751.Ev PS1
3752or
3753.Ev PS2 ,
3754nor may two
3755.Dv PSlit
3756delimited sequences appear adjacent to each other.
3757Each sequence can contain as many mode altering sequences as are
3758required however.
3759Only the first character from
3760.Dv PSlit
3761will be used.
3762When set
3763.Dv PSlit
3764should usually be set to a string containing just one
3765character, then it can simply be embedded in
3766.Ev PS1
3767(or
3768.Ev PS2 )
3769as in
3770.Pp
3771.D1 Li PS1=\*q${PSlit} Ns Ar mset\^ Ns Li ${PSlit}XYZ${PSlit} Ns Ar mclr\^ Ns Li ${PSlit}ABC\*q
3772.Pp
3773The prompt visible will be
3774.Dq XYZABC
3775with the
3776.Dq XYZ
3777part shown according as defined by the mode setting characters
3778.Ar mset ,
3779and then cleared again by
3780.Ar mclr .
3781See
3782.Xr tput 1
3783for one method to generate appropriate mode sequences.
3784Note that both parts, XYZ and ABC, must each contain at least one
3785character.
3786.Pp
3787If
3788.Dv PSlit
3789is unset, which is its initial state, or set to a null string,
3790no literal character will be defined,
3791and all characters of the prompt strings will be assumed
3792to be visible characters (which includes spaces etc.)
3793To allow smooth use of prompts, without needing redefinition, when
3794.Xr editline 7
3795is disabled, the character chosen should be one which will be
3796ignored by the terminal if received, as when
3797.Xr editline 7
3798is not in use, the prompt strings are simply written to the terminal.
3799For example, setting:
3800.\" XXX: PS1 line is too long for -offset indent
3801.Bd -literal -offset left
3802  PSlit="$(printf\ '\e1')"
3803  PS1="${PSlit}$(tput\ bold\ blink)${PSlit}\e$${PSlit}$(tput\ sgr0)${PSlit}\ "
3804.Ed
3805.Pp
3806will arrange for the primary prompt to be a bold blinking dollar sign,
3807if supported by the current terminal, followed by an (ordinary) space,
3808and, as the SOH (control-A) character
3809.Pq Sq \e1
3810will not normally affect
3811a terminal, this same prompt will usually work with
3812.Xr editline 7
3813enabled or disabled.
3814.Sh ENVIRONMENT
3815.Bl -tag -width MAILCHECK
3816.It Ev CDPATH
3817The search path used with the
3818.Ic cd
3819built-in.
3820.It Ev EDITRC
3821Gives the name of the file containing commands for
3822.Xr editline 7 .
3823See
3824.Xr editrc 5
3825for possible content and format.
3826The file is processed, when in interactive mode with
3827command line editing enabled, whenever
3828.Ev EDITRC
3829is set (even with no actual value change,)
3830and if command line editing changes from disabled to enabled,
3831or the editor style used is changed.
3832(See the
3833.Fl E
3834and
3835.Fl V
3836options of the
3837.Ic set
3838built-in command, described in
3839.Sx Built-ins
3840above, which are documented further above in
3841.Sx Argument List Processing . )
3842If unset
3843.Dq $HOME/.editrc
3844is used.
3845.It Ev HISTSIZE
3846The number of lines in the history buffer for the shell.
3847.It Ev HOME
3848Set automatically by
3849.Xr login 1
3850from the user's login directory in the password file
3851.Pq Xr passwd 5 .
3852This environment variable also functions as the default argument for the
3853.Ic cd
3854built-in.
3855.It Ev HOSTNAME
3856Set to the current hostname of the system, as returned by
3857.Xr gethostname 3 .
3858This is obtained each time
3859.Ev HOSTNAME
3860is expanded, so changes to the system's name are reflected
3861without further action.
3862If unset, it returns nothing.
3863Setting it does nothing except reverse the effect of an earlier
3864.Ic unset .
3865.It Ev IFS
3866Input Field Separators.
3867This is normally set to
3868.Aq space ,
3869.Aq tab ,
3870and
3871.Aq newline .
3872See the
3873.Sx White Space Splitting
3874section for more details.
3875.It Ev LANG
3876The string used to specify localization information that allows users
3877to work with different culture-specific and language conventions.
3878See
3879.Xr nls 7 .
3880.It Dv LINENO
3881The current line number in the script or function.
3882See the section
3883.Sx LINENO
3884below for more details.
3885.It Ev MAIL
3886The name of a mail file, that will be checked for the arrival of new mail.
3887Overridden by
3888.Ev MAILPATH .
3889The check occurs just before
3890.Ev PS1
3891is written, immediately after reporting jobs which have changed status,
3892in interactive shells only.
3893New mail is considered to have arrived if the monitored file
3894has increased in size since the last check.
3895.\" .It Ev MAILCHECK
3896.\" The frequency in seconds that the shell checks for the arrival of mail
3897.\" in the files specified by the
3898.\" .Ev MAILPATH
3899.\" or the
3900.\" .Ev MAIL
3901.\" file.
3902.\" If set to 0, the check will occur at each prompt.
3903.It Ev MAILPATH
3904A colon
3905.Dq \&:
3906separated list of file names, for the shell to check for incoming mail.
3907This environment setting overrides the
3908.Ev MAIL
3909setting.
3910There is a maximum of 10 mailboxes that can be monitored at once.
3911.It Ev PATH
3912The default search path for executables.
3913See the
3914.Sx Path Search
3915section above.
3916.It Ev PPID
3917The process identified of the parent process of the
3918current shell.
3919This value is set at shell startup, ignoring
3920any value in the environment, and then made readonly.
3921.It Ev PS1
3922The primary prompt string, which defaults to
3923.Dq Li "$ " ,
3924unless you are the superuser, in which case it defaults to
3925.Dq Li "# " .
3926This string is subject to parameter, arithmetic, and if
3927enabled by setting the
3928.Ic promptcmds
3929option, command substitution before being output.
3930During execution of commands used by command substitution,
3931execution tracing, the
3932.Ic xtrace
3933.Ic ( set Fl x )
3934option is temporarily disabled.
3935If
3936.Ic promptcmds
3937is not set and the prompt string uses command substitution,
3938the prompt used will be an appropriate error string.
3939For other expansion errors, a message will be output,
3940and the unexpanded string will then be used as the prompt.
3941.It Ev PS2
3942The secondary prompt string, which defaults to
3943.Dq Li "> " .
3944After expansion (as for
3945.Ev PS1 )
3946it is written whenever more input is required to complete the
3947current command.
3948.It Ev PS4
3949Output, after expansion like
3950.Ev PS1 ,
3951before each line when execution trace
3952.Ic ( set Fl x )
3953is enabled.
3954.Ev PS4
3955defaults to
3956.Dq Li "+ " .
3957.It Ev PSc
3958Initialized by the shell, ignoring any value from the environment,
3959to a single character string, either
3960.Sq \&#
3961or
3962.Sq \&$ ,
3963depending upon whether the current user is the superuser or not.
3964This is intended for use when building a custom
3965.Ev PS1 .
3966.It Ev PSlit
3967Defines the character which may be embedded in pairs, in
3968.Ev PS1
3969or
3970.Ev PS2
3971to indicate to
3972.Xr editline 7
3973that the characters between each pair of occurrences of the
3974.Dv PSlit
3975character will not appear in the visible prompt, and will not
3976cause the terminal's cursor to change position, but rather set terminal
3977attributes for the following prompt character(s) at least one of
3978which must be present.
3979See
3980.Sx Command Line Editing
3981above for more information.
3982.It Ev RANDOM
3983Returns a different pseudo-random integer,
3984in the range [0,32767] each time it is accessed.
3985.Ev RANDOM
3986can be assigned an integer value to seed the PRNG.
3987If the value assigned is a constant, then the
3988sequence of values produces on subsequent references of
3989.Ev RANDOM
3990will repeat after the next time the same constant is assigned.
3991Note, this is not guaranteed to remain constant from one version
3992of the shell to another \(en the PRNG algorithm, or seeding
3993method is subject to change.
3994If
3995.Ev RANDOM
3996is assigned an empty value (null string) then the next time
3997.Ev RANDOM
3998is accessed, it will be seeded from a more genuinely random source.
3999The sequence of pseudo-random numbers generated will not be able to
4000be generated again (except by luck, whether good or bad, depends!)
4001This is also how the initial seed is generated, if none has been
4002assigned before
4003.Ev RANDOM
4004is first accessed after shell initialization.
4005.It Ev SECONDS
4006Returns the number of seconds since the current shell was started.
4007Attempts to set this variable are ignored.
4008If unset, it remains unset, and returns nothing, unless set again.
4009.It Ev START_TIME
4010Initialized by the shell to the number of seconds since the Epoch
4011(see
4012.Xr localtime 3 )
4013when the shell was started.
4014The value of
4015.Dl $(( Ns Ev START_TIME + Ev SECONDS Ns ))
4016represents the current time, if
4017.Ev START_TIME
4018has not been modified, and
4019.Ev SECONDS
4020is not unset.
4021.It Ev TERM
4022The default terminal setting for the shell.
4023This is inherited by
4024children of the shell, and is used in the history editing modes.
4025.\" This is explicitly last, not in sort order - please leave!
4026.It Ev ToD
4027When referenced, uses the value of
4028.Ev ToD_FORMAT
4029(or
4030.Dq \&%T
4031if
4032.Ev ToD_FORMAT
4033is unset) as the format argument to
4034.Xr strftime 3
4035to encode the current time of day, in the time zone
4036defined by
4037.Ev TZ
4038if set, or current local time if not, and returns the result.
4039If unset
4040.Ev ToD
4041returns nothing.
4042Setting it has no effect, other than to reverse the effect of an earlier
4043.Ic unset .
4044.It Ev ToD_FORMAT
4045Can be set to the
4046.Xr strftime 3
4047format string to be used when expanding
4048.Ev ToD .
4049Initially unset.
4050.It Ev TZ
4051If set, gives the time zone
4052(see
4053.Xr localtime 3 ,
4054.Xr environ 7 )
4055to use when formatting
4056.Ev ToD
4057and if exported, other utilities that deal with times.
4058If unset, the system's local wall clock time zone is used.
4059.It Ev NETBSD_SHELL
4060Unlike the variables previously mentioned,
4061this variable is somewhat strange,
4062in that it cannot be set,
4063inherited from the environment,
4064modified, or exported from the shell.
4065If set, by the shell, it indicates that the shell is the
4066.Ic sh
4067defined by this manual page, and gives its version information.
4068It can also give information in additional space separated words,
4069after the version string.
4070If the shell was built as part of a reproducible build,
4071the relevant date that was used for that build will be included.
4072Finally, any non-standard compilation options,
4073which may affect features available,
4074that were used when building the shell will be listed.
4075.Ev NETBSD_SHELL
4076behaves like any other variable that has the read-only
4077and un-exportable attributes set.
4078.El
4079.Ss Dv LINENO
4080.Dv LINENO
4081is in many respects a normal shell variable, containing an
4082integer value. and can be expanded using any of the forms
4083mentioned above which can be used for any other variable.
4084.Pp
4085.Dv LINENO
4086can be exported, made readonly, or unset, as with any other
4087variable, with similar effects.
4088Note that while being readonly prevents later attempts to
4089set, or unset,
4090.Dv LINENO ,
4091it does not prevent its value changing.
4092References to
4093.Dv LINENO
4094.Pq "when not unset"
4095always obtain the current line number.
4096However,
4097.Dv LINENO
4098should normally not ever be set or unset.
4099In this shell setting
4100.Dv LINENO
4101reverses the effect of an earlier
4102.Ic unset ,
4103but does not otherwise affect the value obtained.
4104If unset,
4105.Dv LINENO
4106should not normally be set again, doing so is not portable.
4107If
4108.Dv LINENO
4109is set or unset, different shells act differently.
4110The value of
4111.Dv LINENO
4112is never imported from the environment when the shell is
4113started, though if present there, as with any other variable,
4114.Dv LINENO
4115will be exported by this shell.
4116.Pp
4117.Dv LINENO
4118is set automatically by the shell to be the number of the source
4119line on which it occurs.
4120When exported,
4121.Dv LINENO
4122is exported with its value set to the line number it would have
4123had had it been referenced on the command line of the command to
4124which it is exported.
4125Line numbers are counted from 1, which is the first line the shell
4126reads from any particular file.
4127For this shell, standard input, including in an interactive shell,
4128the user's terminal, is just another file and lines are counted
4129there as well.
4130However note that not all shells count interactive
4131lines this way, it is not wise to rely upon
4132.Dv LINENO
4133having a useful value, except in a script, or a function.
4134.Pp
4135The role of
4136.Dv LINENO
4137in functions is less clear.
4138In some shells,
4139.Dv LINENO
4140continues to refer to the line number in the script which defines
4141the function,
4142in others lines count from one within the function, always (and
4143resume counting normally once the function definition is complete)
4144and others count in functions from one if the function is defined
4145interactively, but otherwise just reference the line number in the
4146script in which the function is defined.
4147This shell gives the user the option to choose.
4148If the
4149.Fl L
4150flag (the
4151.Ic local_lineno
4152option, see
4153.Sx Argument List Processing )
4154is set, when the function is defined, then the function
4155defaults to counting lines with one being the first line of the
4156function.
4157When the
4158.Fl L
4159flag is not set, the shell counts lines in a function definition
4160in the same continuous sequence as the lines that surround the
4161function definition.
4162Further, if
4163.Dv LINENO
4164is made local
4165(see
4166.Sx Built-ins
4167above)
4168inside the function, the function can decide which
4169behavior it prefers.
4170If
4171.Dv LINENO
4172is made local and inherited, and not given a value, as in
4173.Dl local Fl I Dv LINENO
4174then from that point in the function,
4175.Dv LINENO
4176will give the line number as if lines are counted in sequence
4177with the lines that surround the function definition (and
4178any other function definitions in which this is nested.)
4179If
4180.Dv LINENO
4181is made local, and in that same command, given a value, as
4182.Dl local Oo Fl I Ns | Ns Fl N Oc Dv LINENO Ns = Ns Ar value
4183then
4184.Dv LINENO
4185will give the line number as if lines are counted from one
4186from the beginning of the function.
4187The value nominally assigned in this case is irrelevant, and ignored.
4188For completeness, if lineno is made local and unset, as in
4189.Dl local Fl N Dv LINENO
4190then
4191.Dv LINENO
4192is simply unset inside the function, and gives no value at all.
4193.Pp
4194Now for some technical details.
4195The line on which
4196.Dv LINENO
4197occurs in a parameter expansion, is the line that contains the
4198.Sq \&$
4199that begins the expansion of
4200.Dv LINENO .
4201In the case of nested expansions, that
4202.Sq \&$
4203is the one that actually has
4204.Dv LINENO
4205as its parameter.
4206In an arithmetic expansion, where no
4207.Sq \&$
4208is used to evaluate
4209.Dv LINENO
4210but
4211.Dv LINENO
4212is simply referenced as a variable, then the value is the
4213line number of the line that contains the
4214.Sq L
4215of
4216.Dv LINENO .
4217For functions line one of the function definition (when relevant)
4218is the line that contains the first character of the
4219function name in the definition.
4220When exported, the line number of the command is the line number
4221where the first character of the word which becomes the command name occurs.
4222.Pp
4223When the shell opens a new file, for any reason,
4224it counts lines from one in that file,
4225and then resumes its original counting once it resumes reading the
4226previous input stream.
4227When handling a string passed to
4228.Ic eval
4229the line number starts at the line on which the string starts,
4230and then if the string contains internal newline characters,
4231those characters increase the line number.
4232This means that references to
4233.Dv LINENO
4234in such a case can produce values larger than would be
4235produced by a reference on the line after the
4236.Ic eval .
4237.Sh FILES
4238.Bl -item
4239.It
4240.Pa $HOME/.profile
4241.It
4242.Pa /etc/profile
4243.El
4244.Sh EXIT STATUS
4245Errors that are detected by the shell, such as a syntax error, will cause the
4246shell to exit with a non-zero exit status.
4247If the shell is not an
4248interactive shell, the execution of the shell file will be aborted.
4249Otherwise
4250the shell will return the exit status of the last command executed, or
4251if the exit built-in is used with a numeric argument, it will return the
4252argument.
4253.Sh SEE ALSO
4254.Xr csh 1 ,
4255.Xr echo 1 ,
4256.Xr getopt 1 ,
4257.Xr ksh 1 ,
4258.Xr login 1 ,
4259.Xr printf 1 ,
4260.Xr test 1 ,
4261.Xr editline 3 ,
4262.Xr getopt 3 ,
4263.\" .Xr profile 4 ,
4264.Xr editrc 5 ,
4265.Xr passwd 5 ,
4266.Xr editline 7 ,
4267.Xr environ 7 ,
4268.Xr nls 7 ,
4269.Xr sysctl 8
4270.Sh HISTORY
4271A
4272.Nm
4273command appeared in
4274.At v1 .
4275It was replaced in
4276.At v7
4277with a version that introduced the basis of the current syntax.
4278That was, however, unmaintainable so we wrote this one.
4279.Sh BUGS
4280Setuid shell scripts should be avoided at all costs, as they are a
4281significant security risk.
4282.Pp
4283The characters generated by filename completion should probably be quoted
4284to ensure that the filename is still valid after the input line has been
4285processed.
4286.Pp
4287The
4288.Ic trap
4289command cannot usefully be used, yet, within a command substitution,
4290to obtain the current trap values,
4291as all command substitutions are currently executed within a
4292sub-shell environment,
4293and in sub-shells all non-ignored, non-default, traps are reset.
4294As a workaround, it is possible to redirect output from
4295.Dq trap
4296or
4297.Dq trap -p
4298to a file, and then read the file later using the
4299.Dq \&.
4300command.
4301.Pp
4302Job control of compound statements (loops, etc) is a complete mess.
4303.Pp
4304Many, many, more.
4305(But less than there were...)
4306