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