3 rc, cd, eval, exec, exit, flag, rfork, shift, wait, whatis, ., ~ \- command language
25 It executes command lines read from a terminal or a file or, with the
31 A command line is a sequence of commands, separated by ampersands or semicolons
35 terminated by a newline.
36 The commands are executed in sequence
39 does not wait for a command followed by
41 to finish executing before starting
42 the following command.
43 Whenever a command followed by
45 is executed, its process id is assigned to the
53 exits or is terminated, the
57 gets the process's wait message (see
59 it will be the null string if the command was successful.
61 A long command line may be continued on subsequent lines by typing
64 followed by a newline.
65 This sequence is treated as though it were a blank.
66 Backslash is not otherwise a special character.
70 and any following characters up to (but not including) the next newline
71 are ignored, except in quotation marks.
73 A simple command is a sequence of arguments interspersed with I/O redirections.
74 If the first argument is the name of an
78 built-in commands, it is executed by
80 Otherwise if the name starts with a slash
82 it must be the path name of the program to be executed.
83 Names containing no initial slash are searched for in
84 a list of directory names stored in
86 The first executable file of the given name found
89 is the program to be executed.
90 To be executable, the user must have execute permission (see
92 and the file must be either an executable binary
93 for the current machine's CPU type, or a shell script.
94 Shell scripts begin with a line containing the full path name of a shell
100 The first word of a simple command cannot be a keyword unless it is
101 quoted or otherwise disguised.
104 for in while if not switch fn ~ ! @
106 .SS Arguments and Variables
107 A number of constructions may be used where
109 syntax requires an argument to appear.
110 In many cases a construction's
111 value will be a list of arguments rather than a single string.
113 The simplest kind of argument is the unquoted word:
114 a sequence of one or more characters none of which is a blank, tab,
115 newline, or any of the following:
117 # ; & | ^ $ = ` ' { } ( ) < >
119 An unquoted word that contains any of the characters
123 is a pattern for matching against file names.
126 matches any sequence of characters,
128 matches any single character, and
130 matches any character in the
132 If the first character of
136 the class is complemented.
139 may also contain pairs of characters separated by
141 standing for all characters lexically between the two.
144 must appear explicitly in a pattern, as must the
145 first character of the path name components
149 A pattern is replaced by a list of arguments, one for each path name matched,
150 except that a pattern matching no names is not replaced by the empty list,
151 but rather stands for itself.
152 Pattern matching is done after all other
165 A quoted word is a sequence of characters surrounded by single quotes
167 A single quote is represented in a quoted word by a pair of quotes
170 Each of the following is an argument.
175 The value of a sequence of arguments enclosed in parentheses is
176 a list comprising the members of each element of the sequence.
177 Argument lists have no recursive structure, although their syntax may
179 The following are entirely equivalent:
181 echo hi there everybody
182 ((echo) (hi there) everybody)
187 .BI $ argument ( subscript )
193 is the name of a variable whose value is substituted.
195 of indirection are possible, but of questionable utility.
197 are lists of strings.
210 elements, in which case the value is empty.
213 is followed by a parenthesized list of subscripts, the
214 value substituted is a list composed of the requested elements (origin 1).
215 The parenthesis must follow the variable name with no spaces.
216 Subscripts can also take the form
220 to indicate a sequence of elements.
221 Assignments to variables are described below.
225 The value is the number of elements in the named variable.
227 never assigned a value has zero elements.
232 The value is a single string containing the components of the named variable
233 separated by spaces. A variable with zero elements yields the empty string.
240 and reads its standard output, splitting it into a list of arguments,
246 is not otherwise set, its value is
255 is executed asynchronously with its standard output or standard input
257 The value of the argument is the name of a file
258 referring to the other end of the pipe.
259 This allows the construction of
260 non-linear pipelines.
261 For example, the following runs two commands
267 to compare their outputs
272 .IB argument ^ argument
276 operator concatenates its two operands.
278 have the same number of components, they are concatenated pairwise.
280 then one operand must have one component, and the other must be non-empty,
281 and concatenation is distributive.
284 In most circumstances,
288 operator automatically between words that are not separated by white space.
293 follows a quoted or unquoted word or an unquoted word follows a quoted word
294 with no intervening blanks or tabs,
297 is inserted between the two.
298 If an unquoted word immediately follows a
300 and contains a character other than an alphanumeric, underscore,
305 is inserted before the first such character.
308 .B cc -$flags $stem.c
312 .B cc -^$flags $stem^.c
316 redirects the standard output file (file descriptor 1, normally the
317 terminal) to the named
320 appends standard output to the file.
321 The standard input file (file descriptor 0, also normally the terminal)
322 may be redirected from a file by the sequence
324 or from an inline `here document'
326 .BI << eof-marker\f1.
327 The contents of a here document are lines of text taken from the command
328 input stream up to a line containing nothing but the
330 which may be either a quoted or unquoted word.
333 is unquoted, variable names of the form
335 have their values substituted from
340 is followed by a caret
342 the caret is deleted.
345 is quoted, no substitution occurs.
346 The standard input file
347 may also be redirected from a file by the sequence
351 exactly once, for reading and writing.
353 Redirections may be applied to a file-descriptor other than standard input
354 or output by qualifying the redirection operator
355 with a number in square brackets.
356 For example, the diagnostic output (file descriptor 2)
357 may be redirected by writing
358 .BR "cc junk.c >[2]junk" .
360 A file descriptor may be redirected to an already open descriptor by writing
364 .BI <[ fd0 = fd1 ]\f1.
366 is a previously opened file descriptor and
368 becomes a new copy (in the sense of
371 A file descriptor may be closed by writing
376 Redirections are executed from left to right.
378 .B cc junk.c >/dev/null >[2=1]
380 .B cc junk.c >[2=1] >/dev/null
381 have different effects: the first puts standard output in
383 and then puts diagnostic output in the same place, where the second
384 directs diagnostic output to the terminal and sends standard output to
387 .B newconn <>/net/tcp/clone >[1=0]
390 exactly once for reading and writing and puts it on standard input and output.
391 .B lpd <>[3]/net/tcp/42/data
394 exactly once for reading and writing and puts it on file descriptor 3.
395 .SS Compound Commands
396 A pair of commands separated by a pipe operator
399 The standard output of the left command is sent through a pipe
400 to the standard input of the right command.
401 The pipe operator may be decorated
402 to use different file descriptors.
404 connects the output end of the pipe to file descriptor
410 of the left command and input to
412 of the right command.
414 A pair of commands separated by
419 In either case, the left command is executed and its exit status examined.
422 the right command is executed if the left command's status is null.
424 causes the right command to be executed if the left command's status is non-null.
426 The exit status of a command may be inverted (non-null is changed to null, null
427 is changed to non-null) by preceding it with a
432 operator has highest precedence, and is left-associative (i.e. binds tighter
433 to the left than the right).
435 has intermediate precedence, and
439 have the lowest precedence.
443 operator, with precedence equal to
445 causes its operand to be executed in a subshell.
447 Each of the following is a command.
457 is a sequence of commands, separated by
462 if its exit status is null, the
469 The immediately preceding command must have been
472 If its condition was non-zero, the
486 is executed once for each
488 with that argument assigned to
490 If the argument list is omitted,
499 is executed repeatedly until its exit status is non-null.
500 Each time it returns null status, the
505 is taken to give null status.
507 .BI "switch(" argument "){" list }
511 is searched for simple commands beginning with the word
513 (The search is only at the `top level' of the
517 in nested constructs are not found.)
519 is matched against each word following
521 using the pattern-matching algorithm described above, except that
523 and the first characters of
527 need not be matched explicitly.
528 When a match is found, commands in the list are executed up to the next
531 command (at the top level) or the closing brace.
535 Braces serve to alter the grouping of commands implied by operator
539 is a sequence of commands separated by
544 .BI "fn " name { list }
548 The first form defines a function with the given
550 Subsequently, whenever a command whose first argument is
552 is encountered, the current value of
553 the remainder of the command's argument list will be assigned to
555 after saving its current value, and
559 The second form removes
563 .BI "fn " note { list }
568 A function with a special name will be called when
570 receives a corresponding note; see
572 The valid note names (and corresponding notes) are
581 (floating point trap).
584 exits on receiving any signal, except when run interactively,
585 in which case interrupts and quits normally cause
587 to stop whatever it's doing and start reading a new command.
588 The second form causes
590 to handle a signal in the default manner.
592 recognizes an artificial note,
596 is about to finish executing.
598 .IB name = "argument command"
600 Any command may be preceded by a sequence of assignments
601 interspersed with redirections.
602 The assignments remain in effect until the end of the command, unless
603 the command is empty (i.e. the assignments stand alone), in which case
604 they are effective until rescinded by later assignments.
606 .SS Built-in Commands
607 These commands are executed internally by
609 usually because their execution changes or depends on
616 Execute commands from
619 is set for the duration to the remainder of the argument list following
622 is searched for using
625 .BI builtin " command ..."
629 as usual except that any function named
631 is ignored in favor of the built-in meaning.
635 Change the current directory to
637 The default argument is
640 is searched for in each of the directories mentioned in
643 .BI "eval [" "arg ..." "]"
645 The arguments are concatenated separated by spaces into a single string,
650 .BI "exec [" "command ..." "]"
654 replaces itself with the given (non-built-in)
657 .BI "flag " f " [+-]"
671 is a single character, one of the command line flags (see Invocation, below).
673 .BI "exit [" status "]"
675 Exit with the given exit status.
676 If none is given, the current value of
680 .BR "rfork " [ nNeEsfFm ]
682 Become a new process group using
686 is composed of the bitwise OR of the
688 flags specified by the option letters
693 are given, they default to
697 and their meanings are:
734 Wait for the process with the given
739 is given, all outstanding processes are waited for.
741 .BI whatis " name ..."
743 Print the value of each
745 in a form suitable for input to
748 an assignment to any variable,
749 the definition of any function,
752 for any built-in command, or
753 the completed pathname of any executable file.
755 .BI ~ " subject pattern ..."
759 is matched against each
762 If it matches any pattern,
768 Patterns are the same as for file name matching, except that
770 and the first character of
774 need not be matched explicitly.
778 file name matching before the
780 command is executed, so they need not be enclosed in quotation marks.
785 is a list of strings made available to executing binaries by the
791 creates an environment entry for each variable whose value is non-empty,
792 and for each function.
793 The string for a variable entry has the variable's name followed by
796 If the value has more than one component, these
797 are separated by ctrl-a
800 The string for a function is just the
802 input that defines the function.
803 The name of a function in the environment is the function name
809 starts executing it reads variable and function definitions from its
811 .SS Special Variables
812 The following variables are set or used by
815 .TP \w'\fL$promptXX'u
819 argument list during initialization.
822 command or a function is executed, the current value is saved and
824 receives the new argument list.
825 The saved value is restored on completion of the
830 Whenever a process is started asynchronously with
833 is set to its process id.
836 The default directory for
840 The input field separators used in backquote substitutions.
845 environment, it is initialized to blank, tab and newline.
848 The search path used to find commands and input files
852 If not set in the environment, it is initialized by
853 .BR "path=(.\ /bin)" .
854 Its use is discouraged; instead use
858 containing what's needed.
861 Set during initialization to
868 is run interactively, the first component of
870 is printed before reading each command.
871 The second component is printed whenever a newline is typed and more lines
872 are required to complete the command.
873 If not set in the environment, it is initialized by
874 .BR "prompt=('%\ '\ '\ ')" .
877 Set to the wait message of the last-executed program.
885 Its value is used to control execution in
894 exits at end-of-file of its input or on executing an
896 command with no argument,
903 is started with no arguments it reads commands from standard input.
904 Otherwise its first non-flag argument is the name of a file from which
905 to read commands (but see
908 Subsequent arguments become the initial value of
911 accepts the following command-line flags.
913 .TP \w'\fL-c\ \fIstring\fLXX'u
915 Commands are read from
919 Print out exit status after any command where the status is non-null.
924 is non-null after executing a simple command.
931 is given no arguments and its standard input is a terminal,
932 it runs interactively.
933 Commands are prompted for using
939 is not run interactively.
944 is given or the first character of argument zero is
948 .BR $home/lib/profile ,
949 if it exists, before reading its normal input.
952 Read commands to initialize
966 Echo input on file descriptor 2 as it is read.
969 Print each simple command before executing it.
972 Print debugging information (internal form of commands
973 as they are executed).
979 ``Rc \- The Plan 9 Shell''.
981 There should be a way to match patterns against whole lists rather than
986 to check the value of
991 Functions containing here documents don't work.
993 Free carets don't get inserted next to keywords.