3
rc, cd, eval, exec, exit, flag, rfork, shift, wait, whatis, ., ~ \- command language
20
It executes command lines read from a terminal or a file or, with the
26
A command line is a sequence of commands, separated by ampersands or semicolons
30
terminated by a newline.
31
The commands are executed in sequence
34
does not wait for a command followed by
36
to finish executing before starting
37
the following command.
38
Whenever a command followed by
40
is executed, its process id is assigned to the
48
exits or is terminated, the
52
gets the process's wait message (see
54
it will be the null string if the command was successful.
56
A long command line may be continued on subsequent lines by typing
59
followed by a newline.
60
This sequence is treated as though it were a blank.
61
Backslash is not otherwise a special character.
65
and any following characters up to (but not including) the next newline
66
are ignored, except in quotation marks.
68
A simple command is a sequence of arguments interspersed with I/O redirections.
69
If the first argument is the name of an
73
built-in commands, it is executed by
75
Otherwise if the name starts with a slash
77
it must be the path name of the program to be executed.
78
Names containing no initial slash are searched for in
79
a list of directory names stored in
81
The first executable file of the given name found
84
is the program to be executed.
85
To be executable, the user must have execute permission (see
87
and the file must be either an executable binary
88
for the current machine's CPU type, or a shell script.
89
Shell scripts begin with a line containing the full path name of a shell
95
The first word of a simple command cannot be a keyword unless it is
96
quoted or otherwise disguised.
99
for in while if not switch fn ~ ! @
101
.SS Arguments and Variables
102
A number of constructions may be used where
104
syntax requires an argument to appear.
105
In many cases a construction's
106
value will be a list of arguments rather than a single string.
108
The simplest kind of argument is the unquoted word:
109
a sequence of one or more characters none of which is a blank, tab,
110
newline, or any of the following:
112
# ; & | ^ $ = ` ' { } ( ) < >
114
An unquoted word that contains any of the characters
118
is a pattern for matching against file names.
121
matches any sequence of characters,
123
matches any single character, and
125
matches any character in the
127
If the first character of
131
the class is complemented.
134
may also contain pairs of characters separated by
136
standing for all characters lexically between the two.
139
must appear explicitly in a pattern, as must the
140
first character of the path name components
144
A pattern is replaced by a list of arguments, one for each path name matched,
145
except that a pattern matching no names is not replaced by the empty list,
146
but rather stands for itself.
147
Pattern matching is done after all other
160
A quoted word is a sequence of characters surrounded by single quotes
162
A single quote is represented in a quoted word by a pair of quotes
165
Each of the following is an argument.
170
The value of a sequence of arguments enclosed in parentheses is
171
a list comprising the members of each element of the sequence.
172
Argument lists have no recursive structure, although their syntax may
174
The following are entirely equivalent:
176
echo hi there everybody
177
((echo) (hi there) everybody)
182
.BI $ argument ( subscript )
188
is the name of a variable whose value is substituted.
190
of indirection are possible, but of questionable utility.
192
are lists of strings.
205
elements, in which case the value is empty.
208
is followed by a parenthesized list of subscripts, the
209
value substituted is a list composed of the requested elements (origin 1).
210
The parenthesis must follow the variable name with no spaces.
211
Assignments to variables are described below.
215
The value is the number of elements in the named variable.
217
never assigned a value has zero elements.
222
The value is a single string containing the components of the named variable
223
separated by spaces. A variable with zero elements yields the empty string.
230
and reads its standard output, splitting it into a list of arguments,
236
is not otherwise set, its value is
245
is executed asynchronously with its standard output or standard input
247
The value of the argument is the name of a file
248
referring to the other end of the pipe.
249
This allows the construction of
250
non-linear pipelines.
251
For example, the following runs two commands
257
to compare their outputs
262
.IB argument ^ argument
266
operator concatenates its two operands.
268
have the same number of components, they are concatenated pairwise.
270
then one operand must have one component, and the other must be non-empty,
271
and concatenation is distributive.
274
In most circumstances,
278
operator automatically between words that are not separated by white space.
283
follows a quoted or unquoted word or an unquoted word follows a quoted word
284
with no intervening blanks or tabs,
287
is inserted between the two.
288
If an unquoted word immediately follows a
290
and contains a character other than an alphanumeric, underscore,
295
is inserted before the first such character.
298
.B cc -$flags $stem.c
302
.B cc -^$flags $stem^.c
306
redirects the standard output file (file descriptor 1, normally the
307
terminal) to the named
310
appends standard output to the file.
311
The standard input file (file descriptor 0, also normally the terminal)
312
may be redirected from a file by the sequence
314
or from an inline `here document'
316
.BI << eof-marker\f1.
317
The contents of a here document are lines of text taken from the command
318
input stream up to a line containing nothing but the
320
which may be either a quoted or unquoted word.
323
is unquoted, variable names of the form
325
have their values substituted from
330
is followed by a caret
332
the caret is deleted.
335
is quoted, no substitution occurs.
337
Redirections may be applied to a file-descriptor other than standard input
338
or output by qualifying the redirection operator
339
with a number in square brackets.
340
For example, the diagnostic output (file descriptor 2)
341
may be redirected by writing
342
.BR "cc junk.c >[2]junk" .
344
A file descriptor may be redirected to an already open descriptor by writing
347
.BI <[ fd0 = fd1 ]\f1.
349
is a previously opened file descriptor and
351
becomes a new copy (in the sense of
354
A file descriptor may be closed by writing
359
Redirections are executed from left to right.
361
.B cc junk.c >/dev/null >[2=1]
363
.B cc junk.c >[2=1] >/dev/null
364
have different effects: the first puts standard output in
366
and then puts diagnostic output in the same place, where the second
367
directs diagnostic output to the terminal and sends standard output to
369
.SS Compound Commands
370
A pair of commands separated by a pipe operator
373
The standard output of the left command is sent through a pipe
374
to the standard input of the right command.
375
The pipe operator may be decorated
376
to use different file descriptors.
378
connects the output end of the pipe to file descriptor
384
of the left command and input to
386
of the right command.
388
A pair of commands separated by
393
In either case, the left command is executed and its exit status examined.
396
the right command is executed if the left command's status is null.
398
causes the right command to be executed if the left command's status is non-null.
400
The exit status of a command may be inverted (non-null is changed to null, null
401
is changed to non-null) by preceding it with a
406
operator has highest precedence, and is left-associative (i.e. binds tighter
407
to the left than the right).
409
has intermediate precedence, and
413
have the lowest precedence.
417
operator, with precedence equal to
419
causes its operand to be executed in a subshell.
421
Each of the following is a command.
431
is a sequence of commands, separated by
436
if its exit status is null, the
443
The immediately preceding command must have been
446
If its condition was non-zero, the
460
is executed once for each
462
with that argument assigned to
464
If the argument list is omitted,
473
is executed repeatedly until its exit status is non-null.
474
Each time it returns null status, the
479
is taken to give null status.
481
.BI "switch(" argument "){" list }
485
is searched for simple commands beginning with the word
487
(The search is only at the `top level' of the
491
in nested constructs are not found.)
493
is matched against each word following
495
using the pattern-matching algorithm described above, except that
497
and the first characters of
501
need not be matched explicitly.
502
When a match is found, commands in the list are executed up to the next
505
command (at the top level) or the closing brace.
509
Braces serve to alter the grouping of commands implied by operator
513
is a sequence of commands separated by
518
.BI "fn " name { list }
522
The first form defines a function with the given
524
Subsequently, whenever a command whose first argument is
526
is encountered, the current value of
527
the remainder of the command's argument list will be assigned to
529
after saving its current value, and
533
The second form removes
537
.BI "fn " note { list }
542
A function with a special name will be called when
544
receives a corresponding note; see
546
The valid note names (and corresponding notes) are
555
(floating point trap).
558
exits on receiving any signal, except when run interactively,
559
in which case interrupts and quits normally cause
561
to stop whatever it's doing and start reading a new command.
562
The second form causes
564
to handle a signal in the default manner.
566
recognizes an artificial note,
570
is about to finish executing.
572
.IB name = "argument command"
574
Any command may be preceded by a sequence of assignments
575
interspersed with redirections.
576
The assignments remain in effect until the end of the command, unless
577
the command is empty (i.e. the assignments stand alone), in which case
578
they are effective until rescinded by later assignments.
580
.SS Built-in Commands
581
These commands are executed internally by
583
usually because their execution changes or depends on
590
Execute commands from
593
is set for the duration to the remainder of the argument list following
596
is searched for using
599
.BI builtin " command ..."
603
as usual except that any function named
605
is ignored in favor of the built-in meaning.
609
Change the current directory to
611
The default argument is
614
is searched for in each of the directories mentioned in
617
.BI "eval [" "arg ..." "]"
619
The arguments are concatenated separated by spaces into a single string,
624
.BI "exec [" "command ..." "]"
628
replaces itself with the given (non-built-in)
631
.BI "flag " f " [+-]"
645
is a single character, one of the command line flags (see Invocation, below).
647
.BI "exit [" status "]"
649
Exit with the given exit status.
650
If none is given, the current value of
654
.BR "rfork " [ nNeEsfFm ]
656
Become a new process group using
660
is composed of the bitwise OR of the
662
flags specified by the option letters
667
are given, they default to
671
and their meanings are:
708
Wait for the process with the given
713
is given, all outstanding processes are waited for.
715
.BI whatis " name ..."
717
Print the value of each
719
in a form suitable for input to
722
an assignment to any variable,
723
the definition of any function,
726
for any built-in command, or
727
the completed pathname of any executable file.
729
.BI ~ " subject pattern ..."
733
is matched against each
736
If it matches any pattern,
742
Patterns are the same as for file name matching, except that
744
and the first character of
748
need not be matched explicitly.
752
file name matching before the
754
command is executed, so they need not be enclosed in quotation marks.
759
is a list of strings made available to executing binaries by the
762
creates an environment entry for each variable whose value is non-empty,
763
and for each function.
764
The string for a variable entry has the variable's name followed by
767
If the value has more than one component, these
768
are separated by SOH (001)
770
The string for a function is just the
772
input that defines the function.
773
The name of a function in the environment is the function name
779
starts executing it reads variable and function definitions from its
781
.SS Special Variables
782
The following variables are set or used by
785
.TP \w'\fL$promptXX'u
789
argument list during initialization.
792
command or a function is executed, the current value is saved and
794
receives the new argument list.
795
The saved value is restored on completion of the
800
Whenever a process is started asynchronously with
803
is set to its process id.
806
The default directory for
810
The input field separators used in backquote substitutions.
815
environment, it is initialized to blank, tab and newline.
818
The search path used to find commands and input files
822
If not set in the environment, it is initialized by
829
.BR "path=(.\ /bin)" .
834
are maintained together: changes to one will be reflected in the other.
835
.\" Its use is discouraged; instead use
839
.\" containing what's needed.
842
Set during initialization to
849
is run interactively, the first component of
851
is printed before reading each command.
852
The second component is printed whenever a newline is typed and more lines
853
are required to complete the command.
854
If not set in the environment, it is initialized by
855
.BR "prompt=('%\ '\ '\ ')" .
858
Set to the wait message of the last-executed program.
866
Its value is used to control execution in
875
exits at end-of-file of its input or on executing an
877
command with no argument,
884
is started with no arguments it reads commands from standard input.
885
Otherwise its first non-flag argument is the name of a file from which
886
to read commands (but see
889
Subsequent arguments become the initial value of
892
accepts the following command-line flags.
894
.TP \w'\fL-c\ \fIstring\fLXX'u
896
Commands are read from
900
Print out exit status after any command where the status is non-null.
905
is non-null after executing a simple command.
912
is given no arguments and its standard input is a terminal,
913
it runs interactively.
914
Commands are prompted for using
920
is not run interactively.
925
is given or the first character of argument zero is
929
.BR $home/lib/profile ,
930
if it exists, before reading its normal input.
939
Echo input on file descriptor 2 as it is read.
942
Print each simple command before executing it.
945
Print debugging information (internal form of commands
946
as they are executed).
952
``Rc \- The Plan 9 Shell''.
954
There should be a way to match patterns against whole lists rather than
959
to check the value of
964
Functions that use here documents don't work.
966
Free carets don't get inserted next to keywords.
970
syntax depends on the underlying operating system
971
providing a file descriptor device tree at
974
By default, FreeBSD 5
975
does not provide file descriptors greater than 2
981
/fdescfs /dev/fd fdescfs rw 0 0
991
ensures causes FreeBSD to mount the file system
992
automatically at boot time.)