$ ); echo $? echoed 0,all other shells echo 2.
[unix-history] / usr / src / bin / sh / sh.1
CommitLineData
b7aa7294
KB
1.\" Copyright (c) 1991, 1993
2.\" The Regents of the University of California. All rights reserved.
65622797
KB
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Kenneth Almquist.
6.\"
7.\" %sccs.include.redist.man%
8.\"
34a1a05a 9.\" @(#)sh.1 8.6 (Berkeley) %G%
65622797 10.\"
7ab274c2 11.na
dd00107c
MT
12.TH SH 1
13.SH NAME
376e2f43 14sh \- command interpreter (shell)
dd00107c 15.SH SYNOPSIS
376e2f43 16sh [-/+aCefnuvxIimsVEb] [-/+o longname] [arg ...]
dd00107c
MT
17.SH DESCRIPTION
18.LP
af143eb3
KM
19Sh is the standard command interpreter for the system.
20The current version of sh is in the process of being changed to
21conform with the POSIX 1003.2 and 1003.2a specifications for
dd00107c
MT
22the shell. This version has many features which make it appear
23similar in some respects to the Korn shell, but it is not a Korn
24shell clone (run GNU's bash if you want that). Only features
af143eb3 25designated by POSIX, plus a few Berkeley extensions, are being
dd00107c 26incorporated into this shell. We expect POSIX conformance by the
af143eb3 27time 4.4 BSD is released.
dd00107c
MT
28This man page is not intended to be a tutorial or a complete
29specification of the shell.
30.sp 2
31.B Overview
65622797 32.sp
dd00107c
MT
33.LP
34The shell is a command that reads lines from
af143eb3
KM
35either a file or the terminal, interprets them, and
36generally executes other commands. It is the program that is running
dd00107c
MT
37when a user logs into the system (although a user can select
38a different shell with the chsh(1) command).
af143eb3 39The shell
de40fd0b 40implements a language that has flow control constructs,
dd00107c
MT
41a macro facility that provides a variety of features in
42addition to data storage, along with built in history and line
43editing capabilities. It incorporates many features to
44aid interactive use and has the advantage that the interpretative
45language is common to both interactive and non-interactive
46use (shell scripts). That is, commands can be typed directly
7ab274c2 47to the running shell or can be put into a file and the file
dd00107c
MT
48can be executed directly by the shell.
49.sp 2
50.B Invocation
65622797 51.sp
dd00107c
MT
52.LP
53If no args are present and if the standard input of the shell
54is connected to a terminal (or if the -i flag is set), the shell
55is considered an interactive shell. An interactive shell
56generally prompts before each command and handles programming
57and command errors differently (as described below).
58When first starting, the shell inspects argument 0, and
7ab274c2 59if it begins with a dash '-', the shell is also considered
af143eb3
KM
60a login shell. This is normally done automatically by the system
61when the user first logs in. A login shell first reads commands
dd00107c
MT
62from the files /etc/profile and .profile if they exist.
63If the environment variable ENV is set on entry to a shell,
64or is set in the .profile of a login shell, the shell next reads
65commands from the file named in ENV. Therefore, a user should
66place commands that are to be executed only at login time in
67the .profile file, and commands that are executed for every
68shell inside the ENV file. To set the ENV variable to some
69file, place the following line in your .profile of your home
70directory
71.nf
72
73 ENV=$HOME/.shinit; export ENV
74
75.fi
7ab274c2 76substituting for ``.shinit'' any filename you wish.
dd00107c
MT
77Since the ENV file is read for
78every invocation of the shell, including shell scripts and
79non-interactive shells, the following paradigm is useful
80for restricting commands in the ENV file to interactive invocations.
81Place commands within the ``case'' and ``esac'' below (these
82commands are described later):
65622797 83.nf
dd00107c
MT
84
85 case $- in *i*)
86 # commands for interactive use only
87 ...
88 esac
89
65622797 90.fi
dd00107c
MT
91If command line arguments besides the options have been
92specified, then the shell treats the first argument as the
93name of a file from which to read commands (a shell script), and
94the remaining arguments are set as the positional parameters
95of the shell ($1, $2, etc). Otherwise, the shell reads commands
96from its standard input.
97.sp 2
98.B Argument List Processing
65622797 99.sp
dd00107c
MT
100.LP
101All of the single letter options have a corresponding name
102that can be used as an argument to the '-o' option. The
af143eb3 103set -o name is provided next to the single letter option in
dd00107c 104the description below.
7ab274c2
MT
105Specifying a dash ``-'' turns the option on, while using a plus ``+''
106disables the option.
dd00107c
MT
107The following options can be set from the command line or
108with the set(1) builtin (described later).
109.TP
110-a allexport
7ab274c2 111Export all variables assigned to.
dd00107c
MT
112(UNIMPLEMENTED for 4.4alpha)
113.TP
114-C noclobber
51f8b609 115Don't overwrite existing files with ``>''.
dd00107c
MT
116(UNIMPLEMENTED for 4.4alpha)
117.TP
118-e errexit
51f8b609 119If not interactive, exit immediately if any
dd00107c 120untested command fails.
af143eb3
KM
121The exit status of a command is considered to be
122explicitly tested if the command is used to control
123an if, elif, while, or until; or if the command is the left
7ab274c2
MT
124hand operand of an ``&&'' or ``||'' operator.
125
dd00107c
MT
126.TP
127-f noglob
128Disable pathname expansion.
129.TP
130-n noexec
131If not interactive, read commands but do not
132execute them. This is useful for checking the
133syntax of shell scripts.
134.TP
135-u nounset
136Write a message to standard error when attempting
137to expand a variable that is not set, and if the
51f8b609 138shell is not interactive, exit immediately.
dd00107c
MT
139(UNIMPLEMENTED for 4.4alpha)
140.TP
141-v verbose
142The shell writes its input to standard error
143as it is read. Useful for debugging.
144.TP
145-x xtrace
7ab274c2
MT
146Write each command to standard error (preceded
147by a '+ ') before it is executed. Useful for
dd00107c
MT
148debugging.
149.TP
150-I ignoreeof
151Ignore EOF's from input when interactive.
152.TP
153-i interactive
154Force the shell to behave interactively.
155.TP
156-m monitor
157Turn on job control (set automatically when
158interactive).
159.TP
160-s stdin
161Read commands from standard input (set automatically
162if no file arguments are present). This option has
163no effect when set after the shell has already started
164running (i.e. with set(1)).
165.TP
166-V vi
167Enable the builtin vi(1) command line editor (disables
168-E if it has been set).
169.TP
170-E emacs
171Enable the builtin emacs(1) command line editor (disables
172-V if it has been set).
173.TP
174-b notify
af143eb3 175Enable asynchronous notification of background job
dd00107c
MT
176completion.
177(UNIMPLEMENTED for 4.4alpha)
178.LP
179.sp 2
180.B Lexical Structure
181.sp
182.LP
af143eb3 183The shell reads input in terms of lines from a file and breaks
7ab274c2
MT
184it up into words at whitespace (blanks and tabs), and at
185certain sequences of
dd00107c
MT
186characters that are special to the shell called ``operators''.
187There are two types of operators: control operators and
188redirection operators (their meaning is discussed later).
189Following is a list of operators:
190.nf
191.sp
192Control operators: & && ( ) ; ;; | || <newline>
af143eb3 193.sp
dd00107c
MT
194Redirection operator: < > >| << >> <& >& <<- <>
195.sp
196.fi
197.sp 2
198.B Quoting
199.sp
200.LP
201Quoting is used to remove the special meaning of certain characters
202or words to the shell, such as operators, whitespace, or
203keywords. There are three types of quoting: matched single quotes,
204matched double quotes, and backslash.
205.sp 2
206.B Backslash
207.sp
208.LP
209A backslash preserves the literal meaning of the following
210character, with the exception of <newline>. A backslash preceding
211a <newline> is treated as a line continuation.
212.sp 2
213.B Single Quotes
214.sp
215.LP
216Enclosing characters in single quotes preserves the literal
217meaning of all the characters.
218.sp 2
219.B Double Quotes
220.sp
221.LP
222Enclosing characters within double quotes preserves the literal
223meaning of all characters except dollarsign ($), backquote (`),
7ab274c2 224and backslash (\\). The backslash inside double quotes is
dd00107c 225historically weird, and serves to quote only the following
7ab274c2 226characters: $ ` " \\ <newline>.
dd00107c
MT
227Otherwise it remains literal.
228.sp 2
229.B Reserved Words
230.sp
231.LP
232Reserved words are words that have special meaning to the
af143eb3 233shell and are recognized at the beginning of a line and
dd00107c
MT
234after a control operator. The following are reserved words:
235.nf
236
237 ! elif fi while case
238 else for then { }
239 do done until if esac
240
241.fi
7ab274c2 242Their meaning is discussed later.
dd00107c
MT
243.sp 2
244.B Aliases
245.sp
246.LP
7ab274c2 247An alias is a name and corresponding value set using the alias(1)
dd00107c
MT
248builtin command. Whenever a reserved word may occur (see above),
249and after checking for reserved words, the shell
250checks the word to see if it matches an alias. If it does,
251it replaces it in the input stream with its value. For example,
252if there is an alias called ``lf'' with the value ``ls -F'',
253then the input
254.nf
255
256 lf foobar <return>
257
258 would become
259
260 ls -F foobar <return>
261
262.fi
263.LP
264Aliases provide a convenient way for naive users to
265create shorthands for commands without having to learn how
266to create functions with arguments. They can also be
267used to create lexically obscure code. This use is discouraged.
268.sp 2
269.B Commands
270.sp
271.LP
af143eb3 272The shell interprets the words it reads according to a
dd00107c
MT
273language, the specification of which is outside the scope
274of this man page (refer to the BNF in the POSIX 1003.2
275document). Essentially though, a line is read and if
276the first word of the line (or after a control operator)
277is not a reserved word, then the shell has recognized a
278simple command. Otherwise, a complex command or some
279other special construct may have been recognized.
280.sp 2
281.B Simple Commands
282.sp
283.LP
284If a simple command has been recognized, the shell performs
285the following actions:
286.sp
2871) Leading words of the form ``name=value'' are
288stripped off and assigned to the environment of
289the simple command. Redirection operators and
290their arguments (as described below) are stripped
291off and saved for processing.
292.sp
2932) The remaining words are expanded as described in
294the section called ``Expansions'', and the
295first remaining word is considered the command
296name and the command is located. The remaining
297words are considered the arguments of the command.
298If no command name resulted, then the ``name=value''
299variable assignments recognized in 1) affect the
300current shell.
301.sp
3023) Redirections are performed as described in
303the next section.
304.sp 2
305.B Redirections
306.sp
307.LP
308Redirections are used to change where a command reads its input
309or sends its output. In general, redirections open, close, or
310duplicate an existing reference to a file. The overall format
311used for redirection is:
312.nf
313
314 [n] redir-op file
315
316.fi
7ab274c2
MT
317where redir-op is one of the redirection operators mentioned
318previously. Following is a list of the possible redirections.
dd00107c
MT
319The [n] is an optional number, as in '3' (not '[3]'), that
320refers to a file descriptor.
321.TP
322[n]> file
323Redirect standard output (or n) to file.
324.TP
325[n]>| file
326Same, but override the -C option.
327.TP
328[n]>> file
329Append standard output (or n) to file.
330.TP
331[n]< file
332Redirect standard input (or n) from file.
333.TP
334[n1]<&n2
335Duplicate standard input (or n1) from
336file descriptor n2.
337.TP
338[n]<&-
339Close standard input (or n).
340.TP
341[n1]>&n2
342Duplicate standard output (or n) from
343n2.
344.TP
345[n]>&-
346Close standard output (or n).
347.TP
348[n]<> file
349Open file for reading and writing on
af143eb3 350standard input (or n).
dd00107c 351.LP
af143eb3 352The following redirection is often called a ``here-document''.
dd00107c
MT
353.nf
354
355 [n]<< delimiter
356 here-doc-text...
357 delimiter
358
359.fi
360All the text on successive lines up to the delimiter is
361saved away and made available to the command on standard
362input, or file descriptor n if it is specified. If the delimiter
363as specified on the initial line is quoted, then the here-doc-text
364is treated literally, otherwise the text is subjected to
365parameter expansion, command substitution, and arithmetic
366expansion (as described in the section on ``Expansions''). If
367the operator is ``<<-'' instead of ``<<'', then leading tabs
368in the here-doc-text are stripped.
369.sp 2
370.B Search and Execution
371.sp
372.LP
af143eb3 373There are three types of commands: shell functions, builtin commands, and normal programs -- and the
dd00107c
MT
374command is searched for (by name) in that order. They
375each are executed in a different way.
376.LP
af143eb3
KM
377When a shell function is executed, all of the shell positional parameters (except $0, which remains unchanged) are
378set to the arguments of the shell function.
379The variables which are explicitly placed in the environment of
380the command (by placing assignments to them before the
dd00107c 381function name) are made local to the function and are set
af143eb3
KM
382to the values given. Then the command given in the function
383definition is executed. The positional parameters are
384restored to their original values when the command completes.
dd00107c 385.LP
7ab274c2 386Shell builtins are executed internally to the shell, without spawning a new process.
dd00107c
MT
387.LP
388Otherwise, if the command name doesn't match a function
389or builtin, the command is searched for as a normal
390program in the filesystem (as described in the next section).
af143eb3
KM
391When a normal program is executed, the shell runs the program,
392passing the arguments and the environment to the
393program. If the program is a shell procedure, the shell
394will interpret the program in a subshell. The shell will
395reinitialize itself in this case, so that the effect will
396be as if a new shell had been invoked to handle the shell
dd00107c 397procedure, except that the location of commands located in
af143eb3 398the parent shell will be remembered by the child.
dd00107c
MT
399.sp 2
400.B Path Search
401.sp
402.LP
af143eb3
KM
403When locating a command, the shell first looks to see if
404it has a shell function by that name. Then it looks for a
405builtin command by that name.
dd00107c
MT
406Finally, it searches each
407entry in PATH in turn for the command.
408.LP
af143eb3
KM
409The value of the PATH variable should be a series of
410entries separated by colons. Each entry consists of a
7ab274c2
MT
411directory name.
412The current directory
413may be indicated by an empty directory name.
414.LP
415Command names containing a slash are simply executed without performing any of the above searches.
dd00107c
MT
416.sp 2
417.B Command Exit Status
418.sp
419.LP
420Each command has an exit status that can influence the behavior
421of other shell commands. The paradigm is that a command exits
422with zero for normal or success, and non-zero for failure,
423error, or a false indication. The man page for each command
51f8b609 424should indicate the various exit codes and what they mean.
dd00107c
MT
425Additionally, the builtin commands return exit codes, as does
426an executed function.
427.sp 2
428.B Complex Commands
429.sp
430.LP
7ab274c2
MT
431Complex commands are combinations of simple commands
432with control operators or reserved words, together creating a larger complex
dd00107c
MT
433command. More generally, a command is one of the following:
434.nf
435
436 - simple command
af143eb3 437
dd00107c
MT
438 - pipeline
439
440 - list or compound-list
441
442 - compound command
443
af143eb3 444 - function definition
dd00107c
MT
445
446.fi
447.LP
448Unless otherwise stated, the exit status of a command is
449that of the last simple command executed by the command.
450.sp 2
451.B Pipeline
452.sp
453.LP
454A pipeline is a sequence of one or more commands separated
455by the control operator |. The standard output of all but
456the last command is connected to the standard input
457of the next command.
458.LP
459The format for a pipeline is:
460.nf
461
462[!] command1 [ | command2 ...]
463
464.fi
465.LP
466The standard output of command1 is connected to the standard
467input of command2. The standard input, standard output, or
468both of a command is considered to be assigned by the
469pipeline before any redirection specified by redirection
470operators that are part of the command.
471.LP
472If the pipeline is not in the background (discussed later),
473the shell waits for all commands to complete.
474.LP
475If the reserved word ! does not precede the pipeline, the
476exit status is the exit status of the last command specified
477in the pipeline. Otherwise, the exit status is the logical
478NOT of the exit status of the last command. That is, if
479the last command returns zero, the exit status is 1; if
480the last command returns greater than zero, the exit status
65622797 481is zero.
dd00107c
MT
482.LP
483Because pipeline assignment of standard input or standard
484output or both takes place before redirection, it can be
485modified by redirection. For example:
486.nf
487
488$ command1 2>&1 | command2
489
dcd7db01 490.fi
dd00107c
MT
491sends both the standard output and standard error of command1
492to the standard input of command2.
493.LP
494A ; or <newline> terminator causes the preceding
7ab274c2 495AND-OR-list (described next) to be executed sequentially; a & causes
dd00107c
MT
496asynchronous execution of the preceding AND-OR-list.
497.sp 2
498.B Background Commands -- &
499.sp
500.LP
501If a command is terminated by the control operator ampersand
502(&), the shell executes the command asynchronously -- that is,
503the shell does not wait for
504the command to finish before executing the next command.
505.LP
506The format for running a command in background is:
507.nf
508
509command1 & [command2 & ...]
510
511.fi
af143eb3
KM
512If the shell is not interactive, the standard input of an
513asynchronous command is set to /dev/null.
dd00107c
MT
514.sp 2
515.B Lists -- Generally Speaking
516.sp
517.LP
518A list is a sequence of zero or more commands separated by
af143eb3
KM
519newlines, semicolons, or ampersands,
520and optionally terminated by one of these three characters.
521The commands in a
522list are executed in the order they are written.
523If command is followed by an ampersand, the shell starts the
524command and immediately proceed onto the next command;
525otherwise it waits for the command to terminate before
dd00107c
MT
526proceeding to the next one.
527.LP
af143eb3
KM
528``&&'' and ``||'' are AND-OR list operators. ``&&'' executes
529the first command, and then executes the second command
530iff the exit status of the first command is zero. ``||''
531is similar, but executes the second command iff the exit
dd00107c
MT
532status of the first command is nonzero. ``&&'' and ``||''
533both have the same priority.
534.LP
dcd7db01 535The syntax of the if command is
dd00107c
MT
536.nf
537
538 if list
539 then list
540 [ elif list
541 then list ] ...
542 [ else list ]
543 fi
544
545.fi
dcd7db01 546The syntax of the while command is
dd00107c
MT
547.nf
548
549 while list
550 do list
551 done
552
553.fi
af143eb3
KM
554The two lists are executed repeatedly while the exit status of the first list is zero. The until command is similar, but has the word until in place of while
555repeat until the exit status of the first list is zero.
dd00107c 556.LP
dcd7db01 557The syntax of the for command is
dd00107c 558.nf
dcd7db01 559
dd00107c
MT
560 for variable in word...
561 do list
562 done
563
564.fi
af143eb3 565The words are expanded, and then the list is executed
dd00107c
MT
566repeatedly with the variable set to each word in turn. do
567and done may be replaced with ``{'' and ``}''.
568.LP
dcd7db01 569The syntax of the break and continue command is
dd00107c
MT
570.nf
571
572 break [ num ]
573 continue [ num ]
574
575.fi
af143eb3
KM
576Break terminates the num innermost for or while loops.
577Continue continues with the next iteration of the innermost loop. These are implemented as builtin commands.
dd00107c 578.LP
dcd7db01 579The syntax of the case command is
dd00107c
MT
580.nf
581
582 case word in
583 pattern) list ;;
584 ...
585 esac
586
587.fi
588.LP
af143eb3 589The pattern can actually be one or more patterns (see Shell
dd00107c
MT
590Patterns described later), separated by ``|'' characters.
591
592.LP
65622797 593Commands may be grouped by writing either
dd00107c
MT
594.nf
595
596 (list)
597
598.fi
65622797 599or
dd00107c
MT
600.nf
601
602 { list; }
603
604.fi
65622797 605The first of these executes the commands in a subshell.
7ab274c2
MT
606.sp 2
607.B Functions
608.sp
dd00107c 609.LP
dcd7db01 610The syntax of a function definition is
65622797 611.nf
dd00107c
MT
612
613 name ( ) command
614
615.fi
616.LP
af143eb3 617A function definition is an executable statement; when
dd00107c 618executed it installs a function named name and returns an
af143eb3 619exit status of zero. The command is normally a list
dd00107c
MT
620enclosed between ``{'' and ``}''.
621.LP
af143eb3
KM
622Variables may be declared to be local to a function by
623using a local command. This should appear as the first
624statement of a function, and the syntax is
dd00107c
MT
625.nf
626
627 local [ variable | - ] ...
628
629.fi
630Local is implemented as a builtin command.
631.LP
af143eb3
KM
632When a variable is made local, it inherits the initial
633value and exported and readonly flags from the variable
634with the same name in the surrounding scope, if there is
635one. Otherwise, the variable is initially unset. The shell
636uses dynamic scoping, so that if you make the variable x
637local to function f, which then calls function g, references to the variable x made inside g will refer to the
638variable x declared inside f, not to the global variable
dd00107c
MT
639named x.
640.LP
af143eb3
KM
641The only special parameter than can be made local is
642``-''. Making ``-'' local any shell options that are
643changed via the set command inside the function to be
644restored to their original values when the function
dd00107c
MT
645returns.
646.LP
dcd7db01 647The syntax of the return command is
dd00107c
MT
648.nf
649
650 return [ exitstatus ]
651
652.fi
653It terminates the currently executing function. Return is
654implemented as a builtin command.
655.sp 2
656.B Variables and Parameters
657.sp
658.LP
659The shell maintains a set of parameters. A parameter
660denoted by a name is called a variable. When starting up,
661the shell turns all the environment variables into shell
662variables. New variables can be set using the form
663.nf
664
665 name=value
666
65622797 667.fi
dd00107c
MT
668.LP
669Variables set by the user must have a name consisting solely
670of alphabetics, numerics, and underscores - the first of which
671must not be numeric. A parameter can also be denoted by a number
672or a special character as explained below.
673.sp 2
674.B Positional Parameters
65622797 675.sp
dd00107c
MT
676.LP
677A positional parameter is a parameter denoted by a number (n > 0).
678The shell sets these initially to the values of its command
af143eb3 679line arguments that follow the name of the shell script.
dd00107c
MT
680The set(1) builtin can also be used to set or reset them.
681.sp 2
7ab274c2 682.B Special Parameters
65622797 683.sp
dd00107c
MT
684.LP
685A special parameter is a parameter denoted by one of the following
686special characters. The value of the parameter is listed
687next to its character.
688.TP
689*
690Expands to the positional parameters, starting from one. When
691the expansion occurs within a double-quoted string
692it expands to a single field with the value of each parameter
693separated by the first character of the IFS variable, or by a
694<space> if IFS is unset.
695.TP
696@
697Expands to the positional parameters, starting from one. When
698the expansion occurs within double-quotes, each positional
699parameter expands as a separate argument.
700If there are no positional parameters, the
7ab274c2 701expansion of @ generates zero arguments, even when @ is
dd00107c 702double-quoted. What this basically means, for example, is
af143eb3 703if $1 is ``abc'' and $2 is ``def ghi'', then "$@" expands to
dd00107c
MT
704the two arguments:
705
706"abc" "def ghi"
707.TP
708#
709Expands to the number of positional parameters.
710.TP
711?
712Expands to the exit status of the most recent pipeline.
713.TP
af143eb3 714- (Hyphen)
dd00107c
MT
715Expands to the current option flags (the single-letter
716option names concatenated into a string) as specified on
717invocation, by the set builtin command, or implicitly
718by the shell.
719.TP
720$
721Expands to the process ID of the invoked shell. A subshell
722retains the same value of $ as its parent.
723.TP
724!
725Expands to the process ID of the most recent background
726command executed from the current shell. For a
727pipeline, the process ID is that of the last command in the
728pipeline.
729.TP
af143eb3
KM
7300 (Zero.)
731Expands to the name of the shell or shell script.
dd00107c
MT
732.LP
733.sp 2
734.B Word Expansions
65622797 735.sp
dd00107c
MT
736.LP
737This clause describes the various expansions that are
738performed on words. Not all expansions are performed on
739every word, as explained later.
740.LP
741Tilde expansions, parameter expansions, command substitutions,
742arithmetic expansions, and quote removals that occur within
743a single word expand to a single field. It is only field
744splitting or pathname expansion that can create multiple
745fields from a single word. The single exception to this
746rule is the expansion of the special parameter @ within
7ab274c2 747double-quotes, as was described above.
dd00107c
MT
748.LP
749The order of word expansion is:
750.LP
751(1) Tilde Expansion, Parameter Expansion, Command Substitution,
752Arithmetic Expansion (these all occur at the same time).
753.LP
754(2) Field Splitting is performed on fields
755generated by step (1) unless the IFS variable is null.
756.LP
757(3) Pathname Expansion (unless set -f is in effect).
758.LP
759(4) Quote Removal.
760.LP
761The $ character is used to introduce parameter expansion, command
af143eb3 762substitution, or arithmetic evaluation.
dd00107c 763.sp 2
af143eb3 764.B Tilde Expansion (substituting a user's home directory)
dd00107c
MT
765.sp
766.LP
767A word beginning with an unquoted tilde character (~) is
768subjected to tilde expansion. All the characters up to
af143eb3
KM
769a slash (/) or the end of the word are treated as a username
770and are replaced with the user's home directory. If the
7ab274c2 771username is missing (as in ~/foobar), the tilde is replaced
af143eb3 772with the value of the HOME variable (the current user's
dd00107c
MT
773home directory).
774
775.sp 2
776.B Parameter Expansion
65622797 777.sp
dd00107c
MT
778.LP
779The format for parameter expansion is as follows:
780.nf
781
782 ${expression}
783
784.fi
785where expression consists of all characters until the matching }. Any }
786escaped by a backslash or within a quoted string, and characters in
787embedded arithmetic expansions, command substitutions, and variable
788expansions, are not examined in determining the matching }.
789.LP
790The simplest form for parameter expansion is:
791.nf
792
793 ${parameter}
794
795.fi
796The value, if any, of parameter is substituted.
797.LP
798The parameter name or symbol can be enclosed in braces, which are
799optional except for positional parameters with more than one digit or
800when parameter is followed by a character that could be interpreted as
af143eb3 801part of the name.
7ab274c2 802If a parameter expansion occurs inside
dd00107c
MT
803double-quotes:
804.LP
7ab274c2 8051) Pathname expansion is not performed on the results of the
dd00107c
MT
806expansion.
807.LP
7ab274c2 8082) Field splitting is not performed on the results of the
dd00107c
MT
809expansion, with the exception of @.
810.LP
811In addition, a parameter expansion can be modified by using one of the
af143eb3 812following formats.
65622797 813.sp
dd00107c 814.TP
af143eb3 815${parameter:-word}
dd00107c
MT
816Use Default Values. If parameter is unset or
817null, the expansion of word is
818substituted; otherwise, the value of
819parameter is substituted.
820.TP
af143eb3 821${parameter:=word}
dd00107c
MT
822Assign Default Values. If parameter is unset
823or null, the expansion of word is
824assigned to parameter. In all cases, the
825final value of parameter is
826substituted. Only variables, not positional
827parameters or special parameters, can be
828assigned in this way.
829.TP
830${parameter:?[word]}
831Indicate Error if Null or Unset. If
832parameter is unset or null, the expansion of
833word (or a message indicating it is unset if
834word is omitted) is written to standard
835error and the shell exits with a nonzero
836exit status. Otherwise, the value of
837parameter is substituted. An
838interactive shell need not exit.
839.TP
840${parameter:+word}
841Use Alternate Value. If parameter is unset
842or null, null is substituted;
843otherwise, the expansion of word is
844substituted.
845.LP
846In the parameter expansions shown previously, use of the colon in the
847format results in a test for a parameter that is unset or null; omission
848of the colon results in a test for a parameter that is only unset.
849.TP
850${#parameter}
851String Length. The length in characters of
af143eb3 852the value of parameter.
dd00107c
MT
853.LP
854The following four varieties of parameter expansion provide for substring
855processing. In each case, pattern matching notation (see Shell Patterns), rather
856than regular expression notation, is used to evaluate the patterns.
857If parameter is * or @, the result of the expansion is unspecified.
858Enclosing the full parameter expansion string in double-quotes does not
859cause the following four varieties of pattern characters to be quoted,
860whereas quoting characters within the braces has this effect.
dd00107c
MT
861.TP
862${parameter%word}
863Remove Smallest Suffix Pattern. The word
864is expanded to produce a pattern. The
865parameter expansion then results in
866parameter, with the smallest portion of the
867suffix matched by the pattern deleted.
868
869.TP
870${parameter%%word}
871Remove Largest Suffix Pattern. The word
872is expanded to produce a pattern. The
873parameter expansion then results in
874parameter, with the largest portion of the
875suffix matched by the pattern deleted.
876.TP
877${parameter#word}
878Remove Smallest Prefix Pattern. The word
879is expanded to produce a pattern. The
880parameter expansion then results in
881parameter, with the smallest portion of the
882prefix matched by the pattern deleted.
883.TP
884${parameter##word}
885Remove Largest Prefix Pattern. The word
886is expanded to produce a pattern. The
887parameter expansion then results in
888parameter, with the largest portion of the
889prefix matched by the pattern deleted.
890.LP
891.sp 2
892.B Command Substitution
65622797 893.sp
dd00107c
MT
894.LP
895Command substitution allows the output of a command to be substituted in
896place of the command name itself. Command substitution occurs when
897the command is enclosed as follows:
898.nf
899
900 $(command)
901
902.fi
903or (``backquoted'' version):
904.nf
905
906 `command`
907
908.fi
909.LP
910The shell expands the command substitution by executing command in a
911subshell environment and replacing the command substitution
7ab274c2 912with the
dd00107c
MT
913standard output of the command, removing sequences of one or more
914<newline>s at the end of the substitution. (Embedded <newline>s before
915the end of the output are not removed; however, during field
916splitting, they may be translated into <space>s, depending on the value
917of IFS and quoting that is in effect.)
918
919.sp 2
920.B Arithmetic Expansion
65622797 921.sp
dd00107c
MT
922.LP
923Arithmetic expansion provides a mechanism for evaluating an arithmetic
924expression and substituting its value. The format for arithmetic
925expansion is as follows:
926.nf
927
928 $((expression))
929
930.fi
931The expression is treated as if it were in double-quotes, except
932that a double-quote inside the expression is not treated specially. The
933shell expands all tokens in the expression for parameter expansion,
934command substitution, and quote removal.
935.LP
936Next, the shell treats this as an arithmetic expression and
af143eb3 937substitutes the value of the expression.
dd00107c
MT
938
939.sp 2
940.B White Space Splitting (Field Splitting)
65622797 941.sp
dd00107c
MT
942.LP
943After parameter expansion, command substitution, and
944arithmetic expansion the shell scans the results of
945expansions and substitutions that did not occur in double-quotes for
946field splitting and multiple fields can result.
947.LP
948The shell treats each character of the IFS as a delimiter and use
949the delimiters to split the results of parameter expansion and command
950substitution into fields.
951
952.sp 2
953.B Pathname Expansion (File Name Generation)
65622797 954.sp
dd00107c 955.LP
af143eb3 956Unless the -f flag is set, file name generation is performed after word splitting is complete. Each word is
dd00107c 957viewed as a series of patterns, separated by slashes. The
af143eb3
KM
958process of expansion replaces the word with the names of
959all existing files whose names can be formed by replacing
960each pattern with a string that matches the specified pattern.
961There are two restrictions on this: first, a pattern cannot match a string containing a slash, and second,
962a pattern cannot match a string starting with a period
dd00107c
MT
963unless the first character of the pattern is a period.
964The next section describes the patterns used for both
965Pathname Expansion and the case(1) command.
966
967.sp 2
968.B Shell Patterns
65622797 969.sp
dd00107c 970.LP
af143eb3
KM
971A pattern consists of normal characters, which match themselves, and meta-characters. The meta-characters are
972``!'', ``*'', ``?'', and ``[''. These characters lose
973there special meanings if they are quoted. When command
974or variable substitution is performed and the dollar sign
975or back quotes are not double quoted, the value of the
dd00107c
MT
976variable or the output of the command is scanned for these
977characters and they are turned into meta-characters.
978.LP
af143eb3
KM
979An asterisk (``*'') matches any string of characters. A
980question mark matches any single character. A left
981bracket (``['') introduces a character class. The end of
982the character class is indicated by a ``]''; if the ``]''
983is missing then the ``['' matches a ``['' rather than
984introducing a character class. A character class matches
985any of the characters between the square brackets. A
986range of characters may be specified using a minus sign.
987The character class may be complemented by making an
988exclamation point the first character of the character
dd00107c
MT
989class.
990.LP
991To include a ``]'' in a character class, make it the first
af143eb3 992character listed (after the ``!'', if any). To include a
dd00107c
MT
993minus sign, make it the first or last character listed
994
995.sp 2
996.B Builtins
65622797 997.sp
dd00107c 998.LP
af143eb3
KM
999This section lists the builtin commands which
1000are builtin because they need to perform some operation
1001that can't be performed by a separate process. In addition to these, there are several other commands that may
dd00107c
MT
1002be builtin for efficiency (e.g. printf(1), echo(1), test(1),
1003etc).
1004.TP
34a1a05a
CZ
1005:
1006A null command that returns a 0 (true) exit value.
1007.TP
1008\&. file
1009The commands in the specified file are read and executed by the shell.
1010.TP
dd00107c
MT
1011alias [ name[=string] ... ]
1012If name=string is specified, the shell defines the
1013alias ``name'' with value ``string''. If just ``name''
1014is specified, the value of the alias ``name'' is printed.
1015With no arguments, the alias builtin prints the
1016names and values of all defined aliases (see unalias).
1017.TP
1018bg [ job ] ...
1019Continue the specified jobs (or the current job if no
af143eb3 1020jobs are given) in the background.
dd00107c
MT
1021.TP
1022command command arg...
7ab274c2 1023Execute the specified builtin command. (This is useful when you have a shell function with the same name
dd00107c
MT
1024as a builtin command.)
1025.TP
1026cd [ directory ]
af143eb3 1027Switch to the specified directory (default $HOME).
dd00107c
MT
1028If the an entry for CDPATH appears in the environment
1029of the cd command or the shell variable CDPATH is set
af143eb3
KM
1030and the directory name does not begin with a slash,
1031then the directories listed in CDPATH will be
1032searched for the specified directory. The format of
1033CDPATH is the same as that of PATH. In an interactive shell, the cd command will print out the name of
dd00107c 1034the directory that it actually switched to if this is
af143eb3
KM
1035different from the name that the user gave. These
1036may be different either because the CDPATH mechanism
dd00107c
MT
1037was used or because a symbolic link was crossed.
1038.TP
dd00107c
MT
1039eval string...
1040Concatenate all the arguments with spaces. Then
1041re-parse and execute the command.
1042.TP
1043exec [ command arg... ]
af143eb3
KM
1044Unless command is omitted, the shell process is
1045replaced with the specified program (which must be a
dd00107c 1046real program, not a shell builtin or function). Any
af143eb3 1047redirections on the exec command are marked as permanent, so that they are not undone when the exec command finishes.
dd00107c
MT
1048.TP
1049exit [ exitstatus ]
af143eb3 1050Terminate the shell process. If exitstatus is given
dd00107c
MT
1051it is used as the exit status of the shell; otherwise
1052the exit status of the preceding command is used.
1053.TP
1054export name...
af143eb3
KM
1055The specified names are exported so that they will
1056appear in the environment of subsequent commands.
1057The only way to un-export a variable is to unset it.
1058The shell allows the value of a variable to be set at the
dd00107c
MT
1059same time it is exported by writing
1060.nf
1061
1062 export name=value
1063
1064.fi
af143eb3 1065With no arguments the export command lists the names
dd00107c
MT
1066of all exported variables.
1067.TP
1068fc [-e editor] [first [last]]
1069.TP
1070fc -l [-nr] [first [last]]
1071.TP
1072fc -s [old=new] [first]
1073The fc builtin lists, or edits and re-executes, commands
1074previously entered to an interactive shell.
1075.RS +.5i
1076.TP 2
af143eb3 1077-e editor
dd00107c
MT
1078Use the editor named by editor to edit the commands. The
1079editor string is a command name, subject to search via the
1080PATH variable. The value in the FCEDIT variable
1081is used as a default when -e is not specified. If
1082FCEDIT is null or unset, the value of the EDITOR
1083variable is used. If EDITOR is null or unset,
1084ed(1) is used as the editor.
1085.TP 2
1086-l (ell)
1087List the commands rather than invoking
1088an editor on them. The commands are written in the
1089sequence indicated by the first and last operands, as
1090affected by -r, with each command preceded by the command
1091number.
1092.TP 2
1093-n
1094Suppress command numbers when listing with -l.
1095.TP 2
1096-r
1097Reverse the order of the commands listed (with -l) or
1098edited (with neither -l nor -s).
1099.TP 2
af143eb3 1100-s
dd00107c
MT
1101Re-execute the command without invoking an editor.
1102.TP 2
1103first
1104.TP 2
af143eb3 1105last
dd00107c
MT
1106Select the commands to list or edit. The number of
1107previous commands that can be accessed are determined
1108by the value of the HISTSIZE variable. The value of first
1109or last or both are one of the following:
1110.TP 2
af143eb3 1111[+]number
dd00107c
MT
1112A positive number representing a command
1113number; command numbers can be displayed
1114with the -l option.
1115.TP 2
af143eb3 1116-number
dd00107c
MT
1117A negative decimal number representing the
1118command that was executed number of
1119commands previously. For example, -1 is
1120the immediately previous command.
1121.TP 2
af143eb3 1122string
dd00107c
MT
1123A string indicating the most recently
1124entered command that begins with that
1125string. If the old=new operand is not also
1126specified with -s, the string form of the
1127first operand cannot contain an embedded
1128equal sign.
1129.TP
1130The following environment variables affect the execution of fc:
1131.TP 2
af143eb3 1132FCEDIT
dd00107c
MT
1133Name of the editor to use.
1134.TP 2
af143eb3
KM
1135HISTSIZE
1136The number of previous commands that are accessable.
dd00107c
MT
1137.RE
1138.TP
1139fg [ job ]
af143eb3
KM
1140Move the specified job or the current job to the
1141foreground.
dd00107c
MT
1142.TP
1143getopts optstring var
1144The POSIX getopts command.
1145.TP
1146hash -rv command...
1147The shell maintains a hash table which remembers the
1148locations of commands. With no arguments whatsoever,
af143eb3
KM
1149the hash command prints out the contents of this
1150table. Entries which have not been looked at since
1151the last cd command are marked with an asterisk; it
dd00107c 1152is possible for these entries to be invalid.
65622797 1153.sp
af143eb3
KM
1154With arguments, the hash command removes the specified commands from the hash table (unless they are
1155functions) and then locates them. With the -v
1156option, hash prints the locations of the commands as
dd00107c 1157it finds them. The -r option causes the hash command
af143eb3 1158to delete all the entries in the hash table except
dd00107c
MT
1159for functions.
1160.TP
1161jobid [ job ]
af143eb3
KM
1162Print the process id's of the processes in the job.
1163If the job argument is omitted, use the current job.
dd00107c
MT
1164.TP
1165jobs
af143eb3 1166This command lists out all the background processes
dd00107c
MT
1167which are children of the current shell process.
1168.TP
1169pwd
1170Print the current directory. The builtin command may
af143eb3
KM
1171differ from the program of the same name because the
1172builtin command remembers what the current directory
1173is rather than recomputing it each time. This makes
1174it faster. However, if the current directory is
1175renamed, the builtin version of pwd will continue to
dd00107c
MT
1176print the old name for the directory.
1177.TP
1178read [ -p prompt ] [ -e ] variable...
af143eb3 1179The prompt is printed if the -p option is specified
dd00107c 1180and the standard input is a terminal. Then a line is
af143eb3
KM
1181read from the standard input. The trailing newline
1182is deleted from the line and the line is split as
dd00107c 1183described in the section on word splitting above, and
af143eb3
KM
1184the pieces are assigned to the variables in order.
1185If there are more pieces than variables, the remaining pieces (along with the characters in IFS that
1186separated them) are assigned to the last variable.
1187If there are more variables than pieces, the remaining variables are assigned the null string.
65622797 1188.sp
af143eb3
KM
1189The -e option causes any backslashes in the input to
1190be treated specially. If a backslash is followed by
1191a newline, the backslash and the newline will be
1192deleted. If a backslash is followed by any other
1193character, the backslash will be deleted and the following character will be treated as though it were
dd00107c
MT
1194not in IFS, even if it is.
1195.TP
1196readonly name...
af143eb3
KM
1197The specified names are marked as read only, so that
1198they cannot be subsequently modified or unset. The shell
1199allows the value of a variable to be set at the same
dd00107c
MT
1200time it is marked read only by writing
1201.TP
1202readonly name=value
af143eb3 1203With no arguments the readonly command lists the
dd00107c
MT
1204names of all read only variables.
1205.TP
1206set [ { -options | +options | -- } ] arg...
1207The set command performs three different functions.
65622797 1208.sp
af143eb3 1209With no arguments, it lists the values of all shell
dd00107c 1210variables.
65622797 1211.sp
af143eb3 1212If options are given, it sets the specified option
dd00107c
MT
1213flags, or clears them as described in the section
1214called ``Argument List Processing''.
1215.sp
1216The third use of the set command is to set the values
1217of the shell's positional parameters to the specified
af143eb3
KM
1218args. To change the positional parameters without
1219changing any options, use ``--'' as the first argument to set. If no args are present, the set command
dd00107c
MT
1220will clear all the positional parameters (equivalent
1221to executing ``shift $#''.
1222.TP
1223setvar variable value
af143eb3
KM
1224Assigns value to variable. (In general it is better
1225to write variable=value rather than using setvar.
1226Setvar is intended to be used in functions that
1227assign values to variables whose names are passed as
dd00107c
MT
1228parameters.)
1229.TP
1230shift [ n ]
af143eb3 1231Shift the positional parameters n times. A shift
dd00107c 1232sets the value of $1 to the value of $2, the value of
af143eb3
KM
1233$2 to the value of $3, and so on, decreasing the
1234value of $# by one. If there are zero positional
dd00107c
MT
1235parameters, shifting doesn't do anything.
1236.TP
1237trap [ action ] signal...
af143eb3
KM
1238Cause the shell to parse and execute action when any
1239of the specified signals are received. The signals
1240are specified by signal number. Action may be null
dd00107c 1241or omitted; the former causes the specified signal to
af143eb3
KM
1242be ignored and the latter causes the default action
1243to be taken. When the shell forks off a subshell, it
1244resets trapped (but not ignored) signals to the
1245default action. The trap command has no effect on
dd00107c
MT
1246signals that were ignored on entry to the shell.
1247.TP
1248umask [ mask ]
af143eb3
KM
1249Set the value of umask (see umask(2)) to the specified octal value.
1250If the argument is omitted, the
dd00107c
MT
1251umask value is printed.
1252.TP
1253unalias [-a] [name]
1254If ``name'' is specified, the shell removes that alias.
1255If ``-a'' is specified, all aliases are removed.
1256.TP
1257unset name...
af143eb3
KM
1258The specified variables and functions are unset and
1259unexported. If a given name corresponds to both a
1260variable and a function, both the variable and the
dd00107c
MT
1261function are unset.
1262.TP
1263wait [ job ]
1264Wait for the specified job to complete and return the
af143eb3
KM
1265exit status of the last process in the job. If the
1266argument is omitted, wait for all jobs to complete
dd00107c 1267and the return an exit status of zero.
7ab274c2 1268.LP
dd00107c
MT
1269.sp 2
1270.B Command Line Editing
1271.sp
1272.LP
1273When sh is being used interactively from a terminal, the current command
1274and the command history (see fc in Builtins) can be edited using vi-mode
af143eb3 1275command-line editing. This mode uses commands, described below, similar
dd00107c
MT
1276to a subset of those described in the vi man page.
1277The command set -o vi enables vi-mode editing and place sh into vi
1278insert mode.
1279With vi-mode enabled, sh can be switched between insert mode and command
af143eb3
KM
1280mode. The editor is not described in full here, but will be in a later
1281document. It's similar to vi: typing <ESC> will throw you into
1282command VI command mode. Hitting <return> while in command mode
dd00107c 1283will pass the line to the shell.