xref: /freebsd/bin/sh/sh.1 (revision 11f0b352e05306cf6f1f85e9087022c0a92624a3)
1.\" Copyright (c) 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Kenneth Almquist.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. All advertising materials mentioning features or use of this software
16.\"    must display the following acknowledgement:
17.\"	This product includes software developed by the University of
18.\"	California, Berkeley and its contributors.
19.\" 4. Neither the name of the University nor the names of its contributors
20.\"    may be used to endorse or promote products derived from this software
21.\"    without specific prior written permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.\"	from: @(#)sh.1	8.6 (Berkeley) 5/4/95
36.\" $FreeBSD$
37.\"
38.Dd May 5, 1995
39.Dt SH 1
40.Os
41.Sh NAME
42.Nm sh
43.Nd command interpreter (shell)
44.Sh SYNOPSIS
45.Nm
46.Op Fl /+abCEefIimnpsTuVvx
47.Op Fl /+o Ar longname
48.Op Fl c Ar string
49.Op Ar arg ...
50.Sh DESCRIPTION
51The
52.Nm
53utility is the standard command interpreter for the system.
54The current version of
55.Nm
56is in the process of being changed to
57conform with the
58.St -p1003.2
59specification for the shell.  This version has many features which make
60it appear
61similar in some respects to the Korn shell, but it is not a Korn
62shell clone like
63.Xr pdksh 1 .
64Only features
65designated by POSIX, plus a few Berkeley extensions, are being
66incorporated into this shell.
67This man page is not intended to be a tutorial nor a complete
68specification of the shell.
69.Ss Overview
70The shell is a command that reads lines from
71either a file or the terminal, interprets them, and
72generally executes other commands.
73It is the program that is started when a user logs into the system,
74although a user can select a different shell with the
75.Xr chsh 1
76command.
77The shell
78implements a language that has flow control constructs,
79a macro facility that provides a variety of features in
80addition to data storage, along with builtin history and line
81editing capabilities.  It incorporates many features to
82aid interactive use and has the advantage that the interpretative
83language is common to both interactive and non-interactive
84use (shell scripts).  That is, commands can be typed directly
85to the running shell or can be put into a file,
86which can be executed directly by the shell.
87.Ss Invocation
88.\"
89.\" XXX This next sentence is incredibly confusing.
90.\"
91If no arguments are present and if the standard input of the shell
92is connected to a terminal
93(or if the
94.Fl i
95option is set),
96the shell is considered an interactive shell.  An interactive shell
97generally prompts before each command and handles programming
98and command errors differently (as described below).
99When first starting, the shell inspects argument 0, and
100if it begins with a dash
101.Pq Li - ,
102the shell is also considered a login shell.
103This is normally done automatically by the system
104when the user first logs in.  A login shell first reads commands
105from the files
106.Pa /etc/profile
107and then
108.Pa .profile
109if they exist.  If the environment variable
110.Ev ENV
111is set on entry to a shell, or is set in the
112.Pa .profile
113of a login shell, the shell then reads commands from the file named in
114.Ev ENV .
115Therefore, a user should place commands that are to be executed only
116at login time in the
117.Pa .profile
118file, and commands that are executed for every shell inside the
119.Ev ENV
120file.
121The user can set the
122.Ev ENV
123variable to some file by placing the following line in the file
124.Pa .profile
125in the home directory,
126substituting for
127.Pa .shinit
128the filename desired:
129.Pp
130.Dl ENV=$HOME/.shinit; export ENV
131.Pp
132The first non-option argument specified on the command line
133will be treated as the
134name of a file from which to read commands (a shell script), and
135the remaining arguments are set as the positional parameters
136of the shell ($1, $2, etc).  Otherwise, the shell reads commands
137from its standard input.
138.Pp
139Unlike older versions of
140.Nm
141the
142.Ev ENV
143script is only sourced on invocation of interactive shells.  This
144closes a well-known, and sometimes easily exploitable security
145hole related to poorly thought out
146.Ev ENV
147scripts.
148.Ss Argument List Processing
149All of the single letter options to
150.Nm
151have a corresponding long name,
152with the exception of
153.Fl c
154and
155.Fl /+o .
156These long names are provided next to the single letter options
157in the descriptions below.
158The long name for an option may be specified as an argument to the
159.Fl /+o
160option of
161.Xr sh 1 .
162Once the shell is running,
163the long name for an option may be specified as an argument to the
164.Fl /+o
165option of the
166.Ic set
167builtin command
168(described later in the section called
169.Sx Builtin Commands ) .
170Introducing an option with a dash
171.Pq Li -
172enables the option,
173while using a plus
174.Pq Li +
175disables the option.
176A
177.Dq Li --
178or plain
179.Dq Li -
180will stop option processing and will force the remaining
181words on the command line to be treated as arguments.
182The
183.Fl /+o
184and
185.Fl c
186options do not have long names.
187They take arguments and are described after the single letter options.
188.Bl -tag -width indent
189.It Fl a Li allexport
190Flag variables for export when assignments are made to them.
191.It Fl b Li notify
192Enable asynchronous notification of background job
193completion.
194(UNIMPLEMENTED)
195.It Fl C Li noclobber
196Do not overwrite existing files with
197.Dq Li > .
198.It Fl E Li emacs
199Enable the builtin
200.Xr emacs 1
201command line editor (disables the
202.Fl V
203option if it has been set).
204.It Fl e Li errexit
205Exit immediately if any untested command fails in non-interactive mode.
206The exit status of a command is considered to be
207explicitly tested if the command is used to control
208an if, elif, while, or until; or if the command is the left
209hand operand of an
210.Dq Li &&
211or
212.Dq Li ||
213operator.
214.It Fl f Li noglob
215Disable pathname expansion.
216.It Fl I Li ignoreeof
217Ignore
218.Dv EOF Ns ' Ns s
219from input when in interactive mode.
220.It Fl i Li interactive
221Force the shell to behave interactively.
222.It Fl m Li monitor
223Turn on job control (set automatically when interactive).
224.It Fl n Li noexec
225If not interactive, read commands but do not
226execute them.  This is useful for checking the
227syntax of shell scripts.
228.It Fl p Li privileged
229Turn on privileged mode.  This mode is enabled on startup
230if either the effective user or group id is not equal to the
231real user or group id.  Turning this mode off sets the
232effective user and group ids to the real user and group ids.
233When this mode is enabled for interactive shells, the file
234.Pa /etc/suid_profile
235is sourced instead of
236.Pa ~/.profile
237after
238.Pa /etc/profile
239is sourced, and the contents of the
240.Ev ENV
241variable are ignored.
242.It Fl s Li stdin
243Read commands from standard input (set automatically
244if no file arguments are present).  This option has
245no effect when set after the shell has already started
246running (i.e. when set with the
247.Ic set
248command).
249.It Fl T Li asynctraps
250When waiting for a child, execute traps immediately.
251If this option is not set,
252traps are executed after the child exits,
253as specified in
254.St -p1003.2
255This nonstandard option is useful for putting guarding shells around
256children that block signals.  The surrounding shell may kill the child
257or it may just return control to the tty and leave the child alone,
258like this:
259.Bd -literal -offset indent
260sh -T -c "trap 'exit 1' 2 ; some-blocking-program"
261.Ed
262.Pp
263.It Fl u Li nounset
264Write a message to standard error when attempting
265to expand a variable that is not set, and if the
266shell is not interactive, exit immediately.
267.It Fl V Li vi
268Enable the builtin
269.Xr vi 1
270command line editor (disables
271.Fl E
272if it has been set).
273.It Fl v Li verbose
274The shell writes its input to standard error
275as it is read.  Useful for debugging.
276.It Fl x Li xtrace
277Write each command
278(preceded by
279.Dq Li +\  )
280to standard error before it is executed.
281Useful for debugging.
282.El
283.Pp
284The
285.Fl c
286option may be used to pass its string argument to the shell
287to be interpreted as input.
288Keep in mind that this option only accepts a single string as its
289argument, hence multi-word strings must be quoted.
290.Pp
291The
292.Fl /+o
293option takes as its only argument the long name of an option
294to be enabled or disabled.
295For example, the following two invocations of
296.Nm
297both enable the builtin
298.Xr emacs 1
299command line editor:
300.Bd -literal -offset indent
301set -E
302set -o emacs
303.Ed
304.Pp
305If used without an argument, the
306.Fl o
307option displays the current option settings in a human-readable format.
308If
309.Cm +o
310is used without an argument, the current option settings are output
311in a format suitable for re-input into the shell.
312.Ss Lexical Structure
313The shell reads input in terms of lines from a file and breaks
314it up into words at whitespace (blanks and tabs), and at
315certain sequences of
316characters called
317.Dq operators ,
318which are special to the shell.
319There are two types of operators: control operators and
320redirection operators (their meaning is discussed later).
321The following is a list of valid operators:
322.Bl -tag -width indent
323.It Control operators:
324.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
325.It Li & Ta Li && Ta Li ( Ta Li ) Ta Li \en
326.It Li ;; Ta Li ; Ta Li | Ta Li ||
327.El
328.It Redirection operators:
329.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
330.It Li < Ta Li > Ta Li << Ta Li >> Ta Li <>
331.It Li <& Ta Li >& Ta Li <<- Ta Li >|
332.El
333.El
334.Ss Quoting
335Quoting is used to remove the special meaning of certain characters
336or words to the shell, such as operators, whitespace, or
337keywords.  There are three types of quoting: matched single quotes,
338matched double quotes, and backslash.
339.Bl -tag -width indent
340.It Single Quotes
341Enclosing characters in single quotes preserves the literal
342meaning of all the characters (except single quotes, making
343it impossible to put single-quotes in a single-quoted string).
344.It Double Quotes
345Enclosing characters within double quotes preserves the literal
346meaning of all characters except dollarsign
347.Pq Li $ ,
348backquote
349.Pq Li ` ,
350and backslash
351.Pq Li \e .
352The backslash inside double quotes is historically weird.
353It remains literal unless it precedes the following characters,
354which it serves to quote:
355.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact
356.It Li $ Ta Li ` Ta Li \&" Ta Li \e\  Ta Li \en
357.El
358.It Backslash
359A backslash preserves the literal meaning of the following
360character, with the exception of the newline character
361.Pq Li \en .
362A backslash preceding a newline is treated as a line continuation.
363.El
364.Ss Reserved Words
365Reserved words are words that have special meaning to the
366shell and are recognized at the beginning of a line and
367after a control operator.  The following are reserved words:
368.Bl -column "doneXX" "elifXX" "elseXX" "untilXX" "whileX" -offset center
369.It Li \&! Ta { Ta } Ta Ic case Ta Ic do
370.It Ic done Ta Ic elif Ta Ic else Ta Ic esac Ta Ic fi
371.It Ic for Ta Ic if Ta Ic then Ta Ic until Ta Ic while
372.El
373.Ss Aliases
374An alias is a name and corresponding value set using the
375.Ic alias
376builtin command.  Whenever a reserved word may occur (see above),
377and after checking for reserved words, the shell
378checks the word to see if it matches an alias.
379If it does, it replaces it in the input stream with its value.
380For example, if there is an alias called
381.Dq Li lf
382with the value
383.Dq Li ls -F ,
384then the input
385.Bd -literal -offset indent
386lf foobar
387.Ed
388.Pp
389would become
390.Bd -literal -offset indent
391ls -F foobar
392.Ed
393.Pp
394Aliases provide a convenient way for naive users to
395create shorthands for commands without having to learn how
396to create functions with arguments.  They can also be
397used to create lexically obscure code.  This use is discouraged.
398.Ss Commands
399The shell interprets the words it reads according to a
400language, the specification of which is outside the scope
401of this man page (refer to the BNF in the
402.St -p1003.2
403document).  Essentially though, a line is read and if
404the first word of the line (or after a control operator)
405is not a reserved word, then the shell has recognized a
406simple command.  Otherwise, a complex command or some
407other special construct may have been recognized.
408.Ss Simple Commands
409If a simple command has been recognized, the shell performs
410the following actions:
411.Bl -enum
412.It
413Leading words of the form
414.Dq Li name=value
415are stripped off and assigned to the environment of
416the simple command.  Redirection operators and
417their arguments (as described below) are stripped
418off and saved for processing.
419.It
420The remaining words are expanded as described in
421the section called
422.Sx Word Expansions ,
423and the first remaining word is considered the command
424name and the command is located.  The remaining
425words are considered the arguments of the command.
426If no command name resulted, then the
427.Dq Li name=value
428variable assignments recognized in 1) affect the
429current shell.
430.It
431Redirections are performed as described in
432the next section.
433.El
434.Ss Redirections
435Redirections are used to change where a command reads its input
436or sends its output.  In general, redirections open, close, or
437duplicate an existing reference to a file.  The overall format
438used for redirection is:
439.Pp
440.Dl [n] redir-op file
441.Pp
442The
443.Ql redir-op
444is one of the redirection operators mentioned
445previously.  The following gives some examples of how these
446operators can be used.
447Note that stdin and stdout are commonly used abbreviations
448for standard input and standard output respectively.
449.Bl -tag -width "1234567890XX" -offset indent
450.It Li [n]> file
451redirect stdout (or file descriptor n) to file
452.It Li [n]>| file
453same as above, but override the
454.Fl C
455option
456.It Li [n]>> file
457append stdout (or file descriptor n) to file
458.It Li [n]< file
459redirect stdin (or file descriptor n) from file
460.It Li [n]<> file
461redirect stdin (or file descriptor n) to and from file
462.It Li [n1]<&n2
463duplicate stdin (or file descriptor n1) from file descriptor n2
464.It Li [n]<&-
465close stdin (or file descriptor n)
466.It Li [n1]>&n2
467duplicate stdout (or file descriptor n1) to file descriptor n2
468.It Li [n]>&-
469close stdout (or file descriptor n)
470.El
471.Pp
472The following redirection is often called a
473.Dq here-document .
474.Bd -literal -offset indent
475[n]<< delimiter
476	here-doc-text
477	...
478delimiter
479.Ed
480.Pp
481All the text on successive lines up to the delimiter is
482saved away and made available to the command on standard
483input, or file descriptor n if it is specified.  If the delimiter
484as specified on the initial line is quoted, then the here-doc-text
485is treated literally, otherwise the text is subjected to
486parameter expansion, command substitution, and arithmetic
487expansion (as described in the section on
488.Sx Word Expansions ) .
489If the operator is
490.Dq Li <<-
491instead of
492.Dq Li << ,
493then leading tabs
494in the here-doc-text are stripped.
495.Ss Search and Execution
496There are three types of commands: shell functions,
497builtin commands, and normal programs.
498The command is searched for (by name) in that order.
499The three types of commands are all executed in a different way.
500.Pp
501When a shell function is executed, all of the shell positional
502parameters (except $0, which remains unchanged) are
503set to the arguments of the shell function.
504The variables which are explicitly placed in the environment of
505the command (by placing assignments to them before the
506function name) are made local to the function and are set
507to the values given.
508Then the command given in the function definition is executed.
509The positional parameters are restored to their original values
510when the command completes.
511This all occurs within the current shell.
512.Pp
513Shell builtin commands are executed internally to the shell, without
514spawning a new process.
515.Pp
516Otherwise, if the command name does not match a function
517or builtin command, the command is searched for as a normal
518program in the filesystem (as described in the next section).
519When a normal program is executed, the shell runs the program,
520passing the arguments and the environment to the program.
521If the program is not a normal executable file
522(i.e. if it does not begin with the
523.Qq magic number
524whose
525.Tn ASCII
526representation is
527.Qq #! ,
528resulting in an
529.Er ENOEXEC
530return value from
531.Xr execve 2 )
532the shell will interpret the program in a subshell.
533The child shell will reinitialize itself in this case,
534so that the effect will be
535as if a new shell had been invoked to handle the ad-hoc shell script,
536except that the location of hashed commands located in
537the parent shell will be remembered by the child.
538.Pp
539Note that previous versions of this document
540and the source code itself misleadingly and sporadically
541refer to a shell script without a magic number
542as a
543.Qq shell procedure .
544.Ss Path Search
545When locating a command, the shell first looks to see if
546it has a shell function by that name.  Then it looks for a
547builtin command by that name.  If a builtin command is not found,
548one of two things happen:
549.Bl -enum
550.It
551Command names containing a slash are simply executed without
552performing any searches.
553.It
554The shell searches each entry in
555.Ev PATH
556in turn for the command.  The value of the
557.Ev PATH
558variable should be a series of
559entries separated by colons.  Each entry consists of a
560directory name.
561The current directory
562may be indicated implicitly by an empty directory name,
563or explicitly by a single period.
564.El
565.Ss Command Exit Status
566Each command has an exit status that can influence the behavior
567of other shell commands.  The paradigm is that a command exits
568with zero for normal or success, and non-zero for failure,
569error, or a false indication.  The man page for each command
570should indicate the various exit codes and what they mean.
571Additionally, the builtin commands return exit codes, as does
572an executed shell function.
573.Pp
574If a command is terminated by a signal, its exit status is 128 plus
575the signal number.  Signal numbers are defined in the header file
576.Aq Pa sys/signal.h .
577.Ss Complex Commands
578Complex commands are combinations of simple commands
579with control operators or reserved words, together creating a larger complex
580command.  More generally, a command is one of the following:
581.Bl -item -offset indent
582.It
583simple command
584.It
585pipeline
586.It
587list or compound-list
588.It
589compound command
590.It
591function definition
592.El
593.Pp
594Unless otherwise stated, the exit status of a command is
595that of the last simple command executed by the command.
596.Ss Pipelines
597A pipeline is a sequence of one or more commands separated
598by the control operator |.  The standard output of all but
599the last command is connected to the standard input
600of the next command.  The standard output of the last
601command is inherited from the shell, as usual.
602.Pp
603The format for a pipeline is:
604.Pp
605.Dl [!] command1 [ | command2 ...]
606.Pp
607The standard output of command1 is connected to the standard
608input of command2.  The standard input, standard output, or
609both of a command is considered to be assigned by the
610pipeline before any redirection specified by redirection
611operators that are part of the command.
612.Pp
613If the pipeline is not in the background (discussed later),
614the shell waits for all commands to complete.
615.Pp
616If the reserved word ! does not precede the pipeline, the
617exit status is the exit status of the last command specified
618in the pipeline.  Otherwise, the exit status is the logical
619NOT of the exit status of the last command.  That is, if
620the last command returns zero, the exit status is 1; if
621the last command returns greater than zero, the exit status
622is zero.
623.Pp
624Because pipeline assignment of standard input or standard
625output or both takes place before redirection, it can be
626modified by redirection.  For example:
627.Pp
628.Dl $ command1 2>&1 | command2
629.Pp
630sends both the standard output and standard error of
631.Ql command1
632to the standard input of
633.Ql command2 .
634.Pp
635A
636.Dq Li \&;
637or newline terminator causes the preceding
638AND-OR-list
639(described below in the section called
640.Sx Short-Circuit List Operators )
641to be executed sequentially;
642an
643.Dq Li &
644causes asynchronous execution of the preceding AND-OR-list.
645.Pp
646Note that unlike some other shells,
647.Nm
648executes each process in the pipeline as a child of the
649.Nm
650process.
651Shell builtin commands are the exception to this rule.
652They are executed in the current shell, although they do not affect its
653environment when used in pipelines.
654.Ss Background Commands (&)
655If a command is terminated by the control operator ampersand
656.Pq Li & ,
657the shell executes the command asynchronously;
658the shell does not wait for the command to finish
659before executing the next command.
660.Pp
661The format for running a command in background is:
662.Bd -literal -offset indent
663command1 & [command2 & ...]
664.Ed
665.Pp
666If the shell is not interactive, the standard input of an
667asynchronous command is set to /dev/null.
668.Ss Lists (Generally Speaking)
669A list is a sequence of zero or more commands separated by
670newlines, semicolons, or ampersands,
671and optionally terminated by one of these three characters.
672The commands in a
673list are executed in the order they are written.
674If command is followed by an ampersand, the shell starts the
675command and immediately proceed onto the next command;
676otherwise it waits for the command to terminate before
677proceeding to the next one.
678.Ss Short-Circuit List Operators
679.Dq Li &&
680and
681.Dq Li ||
682are AND-OR list operators.
683.Dq Li &&
684executes the first command, and then executes the second command
685if the exit status of the first command is zero.
686.Dq Li ||
687is similar, but executes the second command if the exit
688status of the first command is nonzero.
689.Dq Li &&
690and
691.Dq Li ||
692both have the same priority.
693.Ss Flow-Control Constructs (if, while, for, case)
694The syntax of the
695.Ic if
696command is:
697.\"
698.\" XXX Use .Dl to work around broken handling of .Ic inside .Bd and .Ed .
699.\"
700.Dl Ic if Ar list
701.Dl Ic then Ar list
702.Dl [ Ic elif Ar list
703.Dl Ic then Ar list ] ...
704.Dl [ Ic else Ar list ]
705.Dl Ic fi
706.Pp
707The syntax of the
708.Ic while
709command is:
710.Dl Ic while Ar list
711.Dl Ic do Ar list
712.Dl Ic done
713.Pp
714The two lists are executed repeatedly while the exit status of the
715first list is zero.
716The
717.Ic until
718command is similar, but has the word
719.Ic until
720in place of
721.Ic while ,
722which causes it to
723repeat until the exit status of the first list is zero.
724.Pp
725The syntax of the
726.Ic for
727command is:
728.Dl Ic for Ar variable Ic in Ar word ...
729.Dl Ic do Ar list
730.Dl Ic done
731.Pp
732The words are expanded, and then the list is executed
733repeatedly with the variable set to each word in turn.
734The
735.Ic do
736and
737.Ic done
738commands may be replaced with
739.Dq Li {
740and
741.Dq Li } .
742.Pp
743The syntax of the
744.Ic break
745and
746.Ic continue
747commands is:
748.Dl Ic break Op Ar num
749.Dl Ic continue Op Ar num
750.Pp
751The
752.Ic break
753command terminates the
754.Ar num
755innermost
756.Ic for
757or
758.Ic while
759loops.
760The
761.Ic continue
762command continues with the next iteration of the innermost loop.
763These are implemented as builtin commands.
764.Pp
765The syntax of the
766.Ic case
767command is
768.Dl Ic case Ar word Ic in
769.Dl pattern) list ;;
770.Dl ...
771.Dl Ic esac
772.Pp
773The pattern can actually be one or more patterns
774(see
775.Sx Shell Patterns
776described later),
777separated by
778.Dq Li \&|
779characters.
780.Ss Grouping Commands Together
781Commands may be grouped by writing either
782.Bd -literal -offset indent
783(list)
784.Ed
785.Pp
786or
787.Bd -literal -offset indent
788{ list; }
789.Ed
790.Pp
791The first form executes the commands in a subshell.
792Note that builtin commands thus executed do not affect the current shell.
793The second form does not fork another shell,
794so it is slightly more efficient.
795Grouping commands together this way allows the user to
796redirect their output as though they were one program:
797.Bd -literal -offset indent
798{ echo -n "hello"; echo " world"; } > greeting
799.Ed
800.Ss Functions
801The syntax of a function definition is
802.Bd -literal -offset indent
803name ( ) command
804.Ed
805.Pp
806A function definition is an executable statement; when
807executed it installs a function named name and returns an
808exit status of zero.  The command is normally a list
809enclosed between
810.Dq Li {
811and
812.Dq Li } .
813.Pp
814Variables may be declared to be local to a function by
815using the
816.Ic local
817command.
818This should appear as the first statement of a function,
819and the syntax is:
820.Bd -ragged -offset indent
821.Ic local
822.Op Ar variable ...
823.Op Ar -
824.Ed
825.Pp
826The
827.Ic local
828command is implemented as a builtin command.
829.Pp
830When a variable is made local, it inherits the initial
831value and exported and readonly flags from the variable
832with the same name in the surrounding scope, if there is
833one.  Otherwise, the variable is initially unset.  The shell
834uses dynamic scoping, so that if the variable
835.Em x
836is made local to function
837.Em f ,
838which then calls function
839.Em g ,
840references to the variable
841.Em x
842made inside
843.Em g
844will refer to the variable
845.Em x
846declared inside
847.Em f ,
848not to the global variable named
849.Em x .
850.Pp
851The only special parameter than can be made local is
852.Dq Li - .
853Making
854.Dq Li -
855local causes any shell options that are
856changed via the set command inside the function to be
857restored to their original values when the function
858returns.
859.Pp
860The syntax of the
861.Ic return
862command is
863.Bd -ragged -offset indent
864.Ic return
865.Op Ar exitstatus
866.Ed
867.Pp
868It terminates the currently executing function.
869The
870.Ic return
871command is implemented as a builtin command.
872.Ss Variables and Parameters
873The shell maintains a set of parameters.  A parameter
874denoted by a name is called a variable.  When starting up,
875the shell turns all the environment variables into shell
876variables.  New variables can be set using the form
877.Bd -literal -offset indent
878name=value
879.Ed
880.Pp
881Variables set by the user must have a name consisting solely
882of alphabetics, numerics, and underscores.
883The first letter of a variable name must not be numeric.
884A parameter can also be denoted by a number
885or a special character as explained below.
886.Ss Positional Parameters
887A positional parameter is a parameter denoted by a number greater than zero.
888The shell sets these initially to the values of its command line
889arguments that follow the name of the shell script.  The
890.Ic set
891builtin command can also be used to set or reset them.
892.Ss Special Parameters
893A special parameter is a parameter denoted by one of the following
894special characters.  The value of the parameter is listed
895next to its character.
896.Bl -hang
897.It Li *
898Expands to the positional parameters, starting from one.  When
899the expansion occurs within a double-quoted string
900it expands to a single field with the value of each parameter
901separated by the first character of the
902.Ev IFS
903variable,
904or by a
905.Aq space
906if
907.Ev IFS
908is unset.
909.It Li @
910Expands to the positional parameters, starting from one.  When
911the expansion occurs within double-quotes, each positional
912parameter expands as a separate argument.
913If there are no positional parameters, the
914expansion of
915.Li @
916generates zero arguments, even when
917.Li @
918is double-quoted.  What this basically means, for example, is
919if $1 is
920.Dq abc
921and $2 is
922.Dq def ghi ,
923then
924.Qq Li $@
925expands to
926the two arguments:
927.Bd -literal -offset indent
928"abc"   "def ghi"
929.Ed
930.It Li #
931Expands to the number of positional parameters.
932.It Li \&?
933Expands to the exit status of the most recent pipeline.
934.It Li -
935(hyphen) Expands to the current option flags (the single-letter
936option names concatenated into a string) as specified on
937invocation, by the set builtin command, or implicitly
938by the shell.
939.It Li $
940Expands to the process ID of the invoked shell.  A subshell
941retains the same value of $ as its parent.
942.It Li \&!
943Expands to the process ID of the most recent background
944command executed from the current shell.  For a
945pipeline, the process ID is that of the last command in the
946pipeline.
947.It Li 0
948(zero) Expands to the name of the shell or shell script.
949.El
950.Ss Word Expansions
951This clause describes the various expansions that are
952performed on words.  Not all expansions are performed on
953every word, as explained later.
954.Pp
955Tilde expansions, parameter expansions, command substitutions,
956arithmetic expansions, and quote removals that occur within
957a single word expand to a single field.  It is only field
958splitting or pathname expansion that can create multiple
959fields from a single word.
960The single exception to this rule is
961the expansion of the special parameter
962.Li @
963within double-quotes,
964as was described above.
965.Pp
966The order of word expansion is:
967.Bl -enum
968.It
969Tilde Expansion, Parameter Expansion, Command Substitution,
970Arithmetic Expansion (these all occur at the same time).
971.It
972Field Splitting is performed on fields generated by step (1)
973unless the
974.Ev IFS
975variable is null.
976.It
977Pathname Expansion (unless the
978.Fl f
979option is in effect).
980.It
981Quote Removal.
982.El
983.Pp
984The
985.Dq Li $
986character is used to introduce parameter expansion, command
987substitution, or arithmetic evaluation.
988.Ss Tilde Expansion (substituting a user's home directory)
989A word beginning with an unquoted tilde character
990.Pq Li ~
991is
992subjected to tilde expansion.
993All the characters up to a slash
994.Pq Li /
995or the end of the word are treated as a username
996and are replaced with the user's home directory.  If the
997username is missing (as in ~/foobar), the tilde is replaced
998with the value of the HOME variable (the current user's
999home directory).
1000.Ss Parameter Expansion
1001The format for parameter expansion is as follows:
1002.Bd -literal -offset indent
1003${expression}
1004.Ed
1005.Pp
1006where expression consists of all characters until the matching
1007.Dq Li } .
1008Any
1009.Dq Li }
1010escaped by a backslash or within a quoted string, and characters in
1011embedded arithmetic expansions, command substitutions, and variable
1012expansions, are not examined in determining the matching
1013.Dq Li } .
1014.Pp
1015The simplest form for parameter expansion is:
1016.Bd -literal -offset indent
1017${parameter}
1018.Ed
1019.Pp
1020The value, if any, of parameter is substituted.
1021.Pp
1022The parameter name or symbol can be enclosed in braces, which are
1023optional except for positional parameters with more than one digit or
1024when parameter is followed by a character that could be interpreted as
1025part of the name.
1026If a parameter expansion occurs inside double-quotes:
1027.Bl -enum
1028.It
1029Pathname expansion is not performed on the results of the
1030expansion.
1031.It
1032Field splitting is not performed on the results of the
1033expansion, with the exception of the special parameter
1034.Li @ .
1035.El
1036.Pp
1037In addition, a parameter expansion can be modified by using one of the
1038following formats.
1039.Bl -tag -width indent
1040.It Li ${parameter:-word}
1041Use Default Values.  If parameter is unset or
1042null, the expansion of word is
1043substituted; otherwise, the value of
1044parameter is substituted.
1045.It Li ${parameter:=word}
1046Assign Default Values.  If parameter is unset
1047or null, the expansion of word is
1048assigned to parameter.  In all cases, the
1049final value of parameter is
1050substituted.  Only variables, not positional
1051parameters or special parameters, can be
1052assigned in this way.
1053.It Li ${parameter:?[word]}
1054Indicate Error if Null or Unset.  If
1055parameter is unset or null, the expansion of
1056word (or a message indicating it is unset if
1057word is omitted) is written to standard
1058error and the shell exits with a nonzero
1059exit status.
1060Otherwise, the value of
1061parameter is substituted.  An
1062interactive shell need not exit.
1063.It Li ${parameter:+word}
1064Use Alternate Value.  If parameter is unset
1065or null, null is substituted;
1066otherwise, the expansion of word is
1067substituted.
1068.Pp
1069In the parameter expansions shown previously, use of the colon in the
1070format results in a test for a parameter that is unset or null; omission
1071of the colon results in a test for a parameter that is only unset.
1072.It Li ${#parameter}
1073String Length.  The length in characters of
1074the value of parameter.
1075.El
1076.Pp
1077The following four varieties of parameter expansion provide for substring
1078processing.
1079In each case, pattern matching notation
1080(see
1081.Sx Shell Patterns ) ,
1082rather than regular expression notation,
1083is used to evaluate the patterns.
1084If parameter is one of the special parameters
1085.Li *
1086or
1087.Li @ ,
1088the result of the expansion is unspecified.
1089Enclosing the full parameter expansion string in double-quotes does not
1090cause the following four varieties of pattern characters to be quoted,
1091whereas quoting characters within the braces has this effect.
1092.Bl -tag -width indent
1093.It Li ${parameter%word}
1094Remove Smallest Suffix Pattern.  The word
1095is expanded to produce a pattern.  The
1096parameter expansion then results in
1097parameter, with the smallest portion of the
1098suffix matched by the pattern deleted.
1099.It Li ${parameter%%word}
1100Remove Largest Suffix Pattern.  The word
1101is expanded to produce a pattern.  The
1102parameter expansion then results in
1103parameter, with the largest portion of the
1104suffix matched by the pattern deleted.
1105.It Li ${parameter#word}
1106Remove Smallest Prefix Pattern.  The word
1107is expanded to produce a pattern.  The
1108parameter expansion then results in
1109parameter, with the smallest portion of the
1110prefix matched by the pattern deleted.
1111.It Li ${parameter##word}
1112Remove Largest Prefix Pattern.  The word
1113is expanded to produce a pattern.  The
1114parameter expansion then results in
1115parameter, with the largest portion of the
1116prefix matched by the pattern deleted.
1117.El
1118.Ss Command Substitution
1119Command substitution allows the output of a command to be substituted in
1120place of the command name itself.  Command substitution occurs when
1121the command is enclosed as follows:
1122.Bd -literal -offset indent
1123$(command)
1124.Ed
1125.Pp
1126or the backquoted version:
1127.Bd -literal -offset indent
1128`command`
1129.Ed
1130.Pp
1131The shell expands the command substitution by executing command in a
1132subshell environment and replacing the command substitution
1133with the standard output of the command,
1134removing sequences of one or more newlines at the end of the substitution.
1135Embedded newlines before the end of the output are not removed;
1136however, during field splitting, they may be translated into spaces
1137depending on the value of
1138.Ev IFS
1139and the quoting that is in effect.
1140.Ss Arithmetic Expansion
1141Arithmetic expansion provides a mechanism for evaluating an arithmetic
1142expression and substituting its value.
1143The format for arithmetic expansion is as follows:
1144.Bd -literal -offset indent
1145$((expression))
1146.Ed
1147.Pp
1148The expression is treated as if it were in double-quotes, except
1149that a double-quote inside the expression is not treated specially.  The
1150shell expands all tokens in the expression for parameter expansion,
1151command substitution, and quote removal.
1152.Pp
1153Next, the shell treats this as an arithmetic expression and
1154substitutes the value of the expression.
1155.Ss White Space Splitting (Field Splitting)
1156After parameter expansion, command substitution, and
1157arithmetic expansion the shell scans the results of
1158expansions and substitutions that did not occur in double-quotes for
1159field splitting and multiple fields can result.
1160.Pp
1161The shell treats each character of the
1162.Ev IFS
1163as a delimiter and uses
1164the delimiters to split the results of parameter expansion and command
1165substitution into fields.
1166.Ss Pathname Expansion (File Name Generation)
1167Unless the
1168.Fl f
1169option is set,
1170file name generation is performed
1171after word splitting is complete.  Each word is
1172viewed as a series of patterns, separated by slashes.  The
1173process of expansion replaces the word with the names of
1174all existing files whose names can be formed by replacing
1175each pattern with a string that matches the specified pattern.
1176There are two restrictions on this: first, a pattern cannot match
1177a string containing a slash, and second,
1178a pattern cannot match a string starting with a period
1179unless the first character of the pattern is a period.
1180The next section describes the patterns used for both
1181Pathname Expansion and the
1182.Ic case
1183command.
1184.Ss Shell Patterns
1185A pattern consists of normal characters, which match themselves,
1186and meta-characters.
1187The meta-characters are
1188.Dq Li \&! ,
1189.Dq Li * ,
1190.Dq Li \&? ,
1191and
1192.Dq Li [ .
1193These characters lose their special meanings if they are quoted.
1194When command or variable substitution is performed and the dollar sign
1195or back quotes are not double-quoted, the value of the
1196variable or the output of the command is scanned for these
1197characters and they are turned into meta-characters.
1198.Pp
1199An asterisk
1200.Pq Li *
1201matches any string of characters.
1202A question mark
1203.Pq Li \&?
1204matches any single character.
1205A left bracket
1206.Pq Li [
1207introduces a character class.
1208The end of the character class is indicated by a
1209.Dq Li \&] ;
1210if the
1211.Dq Li \&]
1212is missing then the
1213.Dq Li [
1214matches a
1215.Dq Li [
1216rather than introducing a character class.
1217A character class matches any of the characters between the square brackets.
1218A range of characters may be specified using a minus sign.
1219The character class may be complemented by making an exclamation point
1220.Pq Li !\&
1221the first character of the character class.
1222.Pp
1223To include a
1224.Dq Li \&]
1225in a character class, make it the first character listed
1226(after the
1227.Dq Li \&! ,
1228if any).
1229To include a
1230.Dq Li - ,
1231make it the first or last character listed.
1232.Ss Builtin Commands
1233This section lists the commands which
1234are builtin because they need to perform some operation
1235that cannot be performed by a separate process.  In addition to
1236these, a builtin version of the
1237.Xr test 1
1238command is provided for efficiency.
1239.Bl -tag -width indent
1240.It Ic \&:
1241A null command that returns a 0 (true) exit value.
1242.It Ic \&. Ar file
1243The commands in the specified file are read and executed by the shell.
1244If
1245.Ar file
1246contains any
1247.Dq /
1248characters, it is used as is.  Otherwise, the shell searches the
1249.Ev PATH
1250for the file.  If it is not found in the
1251.Ev PATH ,
1252it is sought in the current working directory.
1253.It Ic alias Op Ar name ...
1254.It Ic alias Op Ar name Ns = Ns Ar string ...
1255If
1256.Ar name Ns = Ns Ar string
1257is specified, the shell defines the alias
1258.Ar name
1259with value
1260.Ar string .
1261If just
1262.Ar name
1263is specified, the value of the alias
1264.Ar name
1265is printed.
1266With no arguments, the
1267.Ic alias
1268builtin command prints the names and values of all defined aliases
1269(see
1270.Ic unalias ) .
1271Alias values are written with appropriate quoting so that they are
1272suitable for reinput to the shell.
1273.It Ic bg Op Ar job ...
1274Continue the specified jobs
1275(or the current job if no jobs are given)
1276in the background.
1277.It Ic command Ar cmd Op Ar arg ...
1278Execute the specified builtin command,
1279.Ar cmd .
1280This is useful when the user wishes to override a shell function
1281with the same name as a builtin command.
1282.It Ic cd Oo Fl LP Oc Op Ar directory
1283Switch to the specified
1284.Ar directory ,
1285or to the directory specified in the
1286.Ev HOME
1287environment variable if no
1288.Ar directory
1289is specified.
1290If
1291.Ar directory
1292does not begin with
1293.Pa / , . ,
1294or
1295.Pa .. ,
1296then the directories listed in the
1297.Ev CDPATH
1298variable will be
1299searched for the specified
1300.Ar directory .
1301If
1302.Ev CDPATH
1303is unset, the current directory is searched.
1304The format of
1305.Ar CDPATH
1306is the same as that of
1307.Ev PATH .
1308In an interactive shell,
1309the
1310.Ic cd
1311command will print out the name of the directory
1312that it actually switched to
1313if this is different from the name that the user gave.
1314These may be different either because the
1315.Ev CDPATH
1316mechanism was used or because a symbolic link was crossed.
1317.Pp
1318If the
1319.Fl P
1320option is specified,
1321.Pa ..
1322is handled physically and symbolic links are resolved before
1323.Pa ..
1324components are processed.
1325If the
1326.Fl L
1327option is specified,
1328.Pa ..
1329is handled logically.
1330This is the default.
1331.It Ic chdir
1332A synonym for the
1333.Ic cd
1334builtin command.
1335.It Ic echo Oo Fl en Oc Ar string
1336Print
1337.Ar string
1338to the standard output with a newline appended.
1339.Bl -tag -width indent
1340.It Fl n
1341Suppress the output of the trailing newline.
1342.It Fl e
1343Process C-style backslash escape sequences.
1344.Ic echo
1345understands the following character escapes:
1346.Bl -tag -width indent
1347.It \ea
1348Alert (ring the terminal bell)
1349.It \eb
1350Backspace
1351.It \ec
1352Suppress the trailing newline (this has the side-effect of truncating the
1353line if it is not the last character)
1354.It \ee
1355The ESC character (ASCII 0x1b)
1356.It \ef
1357Formfeed
1358.It \en
1359Newline
1360.It \er
1361Carriage return
1362.It \et
1363Horizontal tab
1364.It \ev
1365Vertical tab
1366.It \e\e
1367Literal backslash
1368.It \e0nnn
1369(Zero) The character whose octal value is nnn
1370.El
1371.Pp
1372If
1373.Ar string
1374is not enclosed in quotes then the backslash itself must be escaped
1375with a backslash to protect it from the shell. For example
1376.Bd -literal -offset indent
1377$ echo -e "a\evb"
1378a
1379 b
1380$ echo -e a\e\evb
1381a
1382 b
1383$ echo -e "a\e\eb"
1384a\eb
1385$ echo -e a\e\e\e\eb
1386a\eb
1387.Ed
1388.El
1389.It Ic eval Ar string ...
1390Concatenate all the arguments with spaces.
1391Then re-parse and execute the command.
1392.It Ic exec Op Ar command Op arg ...
1393Unless
1394.Ar command
1395is omitted,
1396the shell process is replaced with the specified program
1397(which must be a real program, not a shell builtin command or function).
1398Any redirections on the
1399.Ic exec
1400command are marked as permanent,
1401so that they are not undone when the
1402.Ic exec
1403command finishes.
1404.It Ic exit Op Ar exitstatus
1405Terminate the shell process.
1406If
1407.Ar exitstatus
1408is given
1409it is used as the exit status of the shell;
1410otherwise the exit status of the preceding command is used.
1411.It Xo
1412.Ic export
1413.Op Fl p
1414.Op Ar name ...
1415.Xc
1416The specified names are exported so that they will
1417appear in the environment of subsequent commands.
1418The only way to un-export a variable is to
1419.Ic unset
1420it.
1421The shell allows the value of a variable to be set
1422at the same time as it is exported by writing
1423.Bd -literal -offset indent
1424export name=value
1425.Ed
1426.Pp
1427With no arguments the export command lists the names
1428of all exported variables.
1429If the
1430.Fl p
1431option is specified, the exported variables are printed as
1432.Dq Ic export Ar name Ns = Ns Ar value
1433lines, suitable for re-input to the shell.
1434.It Ic fc Oo Fl e Ar editor Oc Op Ar first Op Ar last
1435.It Ic fc Fl l Oo Fl nr Oc Op Ar first Op Ar last
1436.It Ic fc Fl s Oo Ar old Ns = Ns Ar new Oc Op Ar first
1437The
1438.Ic fc
1439builtin command lists, or edits and re-executes,
1440commands previously entered to an interactive shell.
1441.Bl -tag -width indent
1442.It Fl e Ar editor
1443Use the editor named by
1444.Ar editor
1445to edit the commands.
1446The editor string is a command name,
1447subject to search via the
1448.Ev PATH
1449variable.
1450The value in the
1451.Ev FCEDIT
1452variable is used as a default when
1453.Fl e
1454is not specified.
1455If
1456.Ev FCEDIT
1457is null or unset, the value of the
1458.Ev EDITOR
1459variable is used.
1460If
1461.Ev EDITOR
1462is null or unset,
1463.Xr ed 1
1464is used as the editor.
1465.It Fl l No (ell)
1466List the commands rather than invoking
1467an editor on them.  The commands are written in the
1468sequence indicated by the first and last operands, as
1469affected by
1470.Fl r ,
1471with each command preceded by the command number.
1472.It Fl n
1473Suppress command numbers when listing with
1474.Fl l .
1475.It Fl r
1476Reverse the order of the commands listed
1477(with
1478.Fl l )
1479or edited
1480(with neither
1481.Fl l
1482nor
1483.Fl s ) .
1484.It Fl s
1485Re-execute the command without invoking an editor.
1486.It Ar first
1487.It Ar last
1488Select the commands to list or edit.
1489The number of previous commands that can be accessed
1490are determined by the value of the
1491.Ev HISTSIZE
1492variable.
1493The value of
1494.Ar first
1495or
1496.Ar last
1497or both are one of the following:
1498.Bl -tag -width indent
1499.It Ar [+]num
1500A positive number representing a command number;
1501command numbers can be displayed with the
1502.Fl l
1503option.
1504.It Ar -num
1505A negative decimal number representing the
1506command that was executed
1507.Ar num
1508of
1509commands previously.
1510For example, -1 is the immediately previous command.
1511.It Ar string
1512A string indicating the most recently entered command
1513that begins with that string.
1514If the
1515.Ar old=new
1516operand is not also specified with
1517.Fl s ,
1518the string form of the first operand cannot contain an embedded equal sign.
1519.El
1520.El
1521.Pp
1522The following environment variables affect the execution of
1523.Ic fc :
1524.Bl -tag -width indent
1525.It Ev FCEDIT
1526Name of the editor to use.
1527.It Ev HISTSIZE
1528The number of previous commands that are accessible.
1529.El
1530.It Ic fg Op Ar job
1531Move the specified
1532.Ar job
1533or the current job to the foreground.
1534.It Ic getopts Ar optstring Ar var
1535The POSIX
1536.Ic getopts
1537command.
1538The
1539.Ic getopts
1540command deprecates the older
1541.Xr getopt 1
1542command.
1543The first argument should be a series of letters, each possibly
1544followed by a colon which indicates that the option takes an argument.
1545The specified variable is set to the parsed option.  The index of
1546the next argument is placed into the shell variable
1547.Ev OPTIND .
1548If an option takes an argument, it is placed into the shell variable
1549.Ev OPTARG .
1550If an invalid option is encountered,
1551.Ev var
1552is set to
1553.Dq Li \&? .
1554It returns a false value (1) when it encounters the end of the options.
1555.It Ic hash Oo Fl rv Oc Op Ar command ...
1556The shell maintains a hash table which remembers the locations of commands.
1557With no arguments whatsoever, the
1558.Ic hash
1559command prints out the contents of this table.
1560Entries which have not been looked at since the last
1561.Ic cd
1562command are marked with an asterisk;
1563it is possible for these entries to be invalid.
1564.Pp
1565With arguments, the
1566.Ic hash
1567command removes each specified
1568.Ar command
1569from the hash table (unless they are functions) and then locates it.
1570With the
1571.Fl v
1572option,
1573.Ic hash
1574prints the locations of the commands as it finds them.
1575The
1576.Fl r
1577option causes the
1578.Ic hash
1579command to delete all the entries in the hash table except for functions.
1580.It Ic jobid Op Ar job
1581Print the process id's of the processes in the specified
1582.Ar job .
1583If the
1584.Ar job
1585argument is omitted, use the current job.
1586.It Xo
1587.Ic jobs
1588.Op Fl ls
1589.Op Ar job ...
1590.Xc
1591Print information about the specified jobs, or all jobs if no
1592.Ar job
1593argument is given.
1594The information printed includes job ID, status and command name.
1595.Pp
1596If the
1597.Fl l
1598option is specified, the PID of each job is also printed.
1599If the
1600.Fl s
1601option is specified, only the PID's of the jobs are printed, one per line.
1602.It Ic pwd Op Fl LP
1603Print the path of the current directory.  The builtin command may
1604differ from the program of the same name because the
1605builtin command remembers what the current directory
1606is rather than recomputing it each time.  This makes
1607it faster.  However, if the current directory is
1608renamed,
1609the builtin version of
1610.Xr pwd 1
1611will continue to print the old name for the directory.
1612.Pp
1613If the
1614.Fl P
1615option is specified, symbolic links are resolved.
1616If the
1617.Fl L
1618option is specified, the shell's notion of the current directory
1619is printed (symbolic links are not resolved).
1620This is the default.
1621.It Ic read Oo Fl p Ar prompt Oc Oo Fl t Ar timeout Oc Oo Fl er Oc Ar variable ...
1622The
1623.Ar prompt
1624is printed if the
1625.Fl p
1626option is specified
1627and the standard input is a terminal.  Then a line is
1628read from the standard input.  The trailing newline
1629is deleted from the line and the line is split as
1630described in the section on
1631.Sx White Space Splitting (Field Splitting)
1632above, and
1633the pieces are assigned to the variables in order.
1634If there are more pieces than variables, the remaining
1635pieces (along with the characters in
1636.Ev IFS
1637that separated them)
1638are assigned to the last variable.
1639If there are more variables than pieces, the remaining
1640variables are assigned the null string.
1641.Pp
1642Backslashes are treated specially, unless the
1643.Fl r
1644option is
1645specified.  If a backslash is followed by
1646a newline, the backslash and the newline will be
1647deleted.  If a backslash is followed by any other
1648character, the backslash will be deleted and the following
1649character will be treated as though it were not in
1650.Ev IFS ,
1651even if it is.
1652.Pp
1653If the
1654.Fl t
1655option is specified and the
1656.Ar timeout
1657elapses before any input is supplied,
1658the
1659.Ic read
1660command will return without assigning any values.
1661The
1662.Ar timeout
1663value may optionally be followed by one of
1664.Dq s ,
1665.Dq m
1666or
1667.Dq h
1668to explicitly specify seconds, minutes or hours.
1669If none is supplied,
1670.Dq s
1671is assumed.
1672.Pp
1673The
1674.Fl e
1675option exists only for backward compatibility with older scripts.
1676.It Xo
1677.Ic readonly
1678.Op Fl p
1679.Op Ar name ...
1680.Xc
1681Each specified
1682.Ar name
1683is marked as read only,
1684so that it cannot be subsequently modified or unset.
1685The shell allows the value of a variable to be set
1686at the same time as it is marked read only
1687by using the following form:
1688.Bd -literal -offset indent
1689readonly name=value
1690.Ed
1691.Pp
1692With no arguments the
1693.Ic readonly
1694command lists the names of all read only variables.
1695If the
1696.Fl p
1697option is specified, the read-only variables are printed as
1698.Dq Ic readonly Ar name Ns = Ns Ar value
1699lines, suitable for re-input to the shell.
1700.It Ic set Oo Fl /+abCEefIimnpTuVvx Oc Oo Fl /+o Ar longname Oc Oo
1701.Fl c Ar string Oc Op Fl - Ar arg ...
1702The
1703.Ic set
1704command performs three different functions:
1705.Bl -item
1706.It
1707With no arguments, it lists the values of all shell variables.
1708.It
1709If options are given,
1710either in short form or using the long
1711.Dq Fl /+o Ar longname
1712form,
1713it sets or clears the specified options as described in the section called
1714.Sx Argument List Processing .
1715.It
1716If the
1717.Dq Fl -
1718option is specified,
1719.Ic set
1720will replace the shell's positional parameters with the subsequent
1721arguments.
1722If no arguments follow the
1723.Dq Fl -
1724option,
1725all the positional parameters will be cleared,
1726which is equivalent to executing the command
1727.Dq Li shift $# .
1728The
1729.Dq Fl -
1730flag may be ommitted when specifying arguments to be used
1731as positional replacement parameters.
1732This is not recommended,
1733because the first argument may begin with a dash
1734.Pq Li -
1735or a plus
1736.Pq Li + ,
1737which the
1738.Ic set
1739command will interpret as a request to enable or disable options.
1740.El
1741.It Ic setvar Ar variable Ar value
1742Assigns the specified
1743.Ar value
1744to the specified
1745.Ar variable .
1746.Ic Setvar
1747is intended to be used in functions that
1748assign values to variables whose names are passed as parameters.
1749In general it is better to write
1750.Bd -literal -offset indent
1751variable=value
1752.Ed
1753rather than using
1754.Ic setvar .
1755.It Ic shift Op Ar n
1756Shift the positional parameters
1757.Ar n
1758times, or once if
1759.Ar n
1760is not specified.
1761A shift sets the value of $1 to the value of $2,
1762the value of $2 to the value of $3, and so on,
1763decreasing the value of $# by one.
1764If there are zero positional parameters, shifting does not do anything.
1765.It Ic trap Oo Ar action Oc Ar signal ...
1766Cause the shell to parse and execute
1767.Ar action
1768when any specified
1769.Ar signal
1770is received.
1771The signals are specified by signal number.
1772The
1773.Ar action
1774may be null or omitted;
1775the former causes the specified signal to be ignored
1776and the latter causes the default action to be taken.
1777When the shell forks off a subshell,
1778it resets trapped (but not ignored) signals to the default action.
1779The
1780.Ic trap
1781command has no effect on signals that were ignored on entry to the shell.
1782.It Ic type Op Ar name ...
1783Interpret each
1784.Ar name
1785as a command and print the resolution of the command search.
1786Possible resolutions are:
1787shell keyword, alias, shell builtin command, command, tracked alias
1788and not found.
1789For aliases the alias expansion is printed;
1790for commands and tracked aliases
1791the complete pathname of the command is printed.
1792.It Ic ulimit Oo Fl HSabcdflmnstuv Oc Op Ar limit
1793Set or display resource limits (see
1794.Xr getrlimit 2 ) .
1795If
1796.Ar limit
1797is specified, the named resource will be set;
1798otherwise the current resource value will be displayed.
1799.Pp
1800If
1801.Fl H
1802is specified, the hard limits will be set or displayed.
1803While everybody is allowed to reduce a hard limit,
1804only the superuser can increase it.
1805The
1806.Fl S
1807option
1808specifies the soft limits instead.  When displaying limits,
1809only one of
1810.Fl S
1811or
1812.Fl H
1813can be given.
1814The default is to display the soft limits,
1815and to set both the hard and the soft limits.
1816.Pp
1817Option
1818.Fl a
1819causes the
1820.Ic ulimit
1821command to display all resources.
1822The parameter
1823.Ar limit
1824is not acceptable in this mode.
1825.Pp
1826The remaining options specify which resource value is to be
1827displayed or modified.
1828They are mutually exclusive.
1829.Bl -tag -width indent
1830.It Fl b Ar sbsize
1831The maximum size of socket buffer usage, in bytes.
1832.It Fl c Ar coredumpsize
1833The maximal size of core dump files, in 512-byte blocks.
1834.It Fl d Ar datasize
1835The maximal size of the data segment of a process, in kilobytes.
1836.It Fl f Ar filesize
1837The maximal size of a file, in 512-byte blocks.
1838.It Fl l Ar lockedmem
1839The maximal size of memory that can be locked by a process, in
1840kilobytes.
1841.It Fl m Ar memoryuse
1842The maximal resident set size of a process, in kilobytes.
1843.It Fl n Ar nofiles
1844The maximal number of descriptors that could be opened by a process.
1845.It Fl s Ar stacksize
1846The maximal size of the stack segment, in kilobytes.
1847.It Fl t Ar time
1848The maximal amount of CPU time to be used by each process, in seconds.
1849.It Fl u Ar userproc
1850The maximal number of simultaneous processes for this user ID.
1851.It Fl v Ar virtualmem
1852The maximal virtual size of a process, in kilobytes.
1853.El
1854.It Ic umask Op Ar mask
1855Set the file creation mask (see
1856.Xr umask 2 )
1857to the octal value specified by
1858.Ar mask .
1859If the argument is omitted, the current mask value is printed.
1860.It Ic unalias Oo Fl a Oc Op Ar name
1861If
1862.Ar name
1863is specified, the shell removes that alias.
1864If
1865.Fl a
1866is specified, all aliases are removed.
1867.It Xo
1868.Ic unset
1869.Op Fl fv
1870.Ar name ...
1871.Xc
1872The specified variables or functions are unset and unexported.
1873If the
1874.Fl v
1875option is specified or no options are given, the
1876.Ar name
1877arguments are treated as variable names.
1878If the
1879.Fl f
1880option is specified, the
1881.Ar name
1882arguments are treated as function names.
1883.It Ic wait Op Ar job
1884Wait for the specified
1885.Ar job
1886to complete and return the exit status of the last process in the
1887.Ar job .
1888If the argument is omitted, wait for all jobs to complete
1889and return an exit status of zero.
1890.El
1891.Ss Commandline Editing
1892When
1893.Nm
1894is being used interactively from a terminal, the current command
1895and the command history
1896(see
1897.Ic fc
1898in
1899.Sx Builtin Commands )
1900can be edited using vi-mode command line editing.
1901This mode uses commands similar
1902to a subset of those described in the vi man page.
1903The command
1904.Dq Li set -o vi
1905(or
1906.Dq Li set -V )
1907enables vi-mode editing and places
1908.Nm
1909into vi insert mode.  With vi-mode enabled,
1910.Nm
1911can be switched between insert mode and command mode by typing
1912.Aq ESC .
1913Hitting
1914.Aq return
1915while in command mode will pass the line to the shell.
1916.Pp
1917Similarly, the
1918.Dq Li set -o emacs
1919(or
1920.Dq Li set -E )
1921command can be used to enable a subset of
1922emacs-style command line editing features.
1923.Sh SEE ALSO
1924.Xr builtin 1 ,
1925.Xr echo 1 ,
1926.Xr expr 1 ,
1927.Xr pwd 1 ,
1928.Xr test 1
1929.Sh HISTORY
1930A
1931.Nm
1932command appeared in
1933.At v1 .
1934