.HTML "Rc — The Plan 9 Shell. \" /*% refer -k -e -n -l3,2 -s < % | tbl | troff -ms | lp -dfn.Tm shell programming language g.de TP \" An indented paragraph describing some command, tagged with the command name.IP "\\f(CW\\$1\\fR" 5.if \\w'\\f(CW\\$1\\fR'-4n .br...de CI.nr Sf \\n(.f\%\&\\$3\f(CW\\$1\fI\&\\$2\f\\n(Sf...TLRc \(em The Plan 9 Shell.AUTom Dufftd@plan9.bell-labs.com.AB.I Rcis a command interpreter for Plan 9 thatprovides similar facilities to UNIX'sBourne shell,with some small additions and less idiosyncratic syntax.This paper uses numerous examples to describe.I rc 'sfeatures, and contrasts.I rcwith the Bourne shell, a model that many readers will be familiar with..AE.NHIntroduction.PP.I Rcis similar in spirit but different in detail from UNIX'sBourne shell. This paper describes.I rc 'sprincipal features with many small examples and a few larger ones.It assumes familiarity with the Bourne shell..NHSimple commands.PPFor the simplest uses.I rchas syntax familiar to Bourne-shell users.All of the following behave as expected:.P1datecat /lib/news/buildwho >user.nameswho >>user.nameswc <fileecho [a-f]*.cwho | wcwho; datevc *.c &mk && v.out /*/bin/fb/*rm -r junk || echo rm failed!.P2.NHQuotation.PPAn argument that contains a space or one of.I rc 'sother syntax characters must be enclosed in apostrophes.CW ' ): (.P1rm 'odd file name'.P2An apostrophe in a quoted argument must be doubled:.P1echo 'How''s your father?'.P2.NHPatterns.PPAn unquoted argument that contains any of the characters.CW *.CW ?.CW [is a pattern to be matched against file names.A.CW *character matches any sequence of characters,.CW ?matches any single character, and.CW [\fIclass\fP]matches any character in the.CW class ,unless the first character of.I classis.CW ~ ,in which case the class is complemented.The.I classmay also contain pairs of characters separated by.CW - ,standing for all characters lexically between the two.The character.CW /must appear explicitly in a pattern, as must the path name components.CW .and.CW .. .A pattern is replaced by a list of arguments, one for each path name matched,except that a pattern matching no names is not replaced by the empty list;rather it stands for itself..NHVariables.PPUNIX's Bourne shell offers string-valued variables..I Rcprovides variables whose values are lists of arguments \(emthat is, arrays of strings. This is the principal differencebetween.I rcand traditional UNIX command interpreters.Variables may be given values by typing, for example:.P1path=(. /bin)user=tdfont=/lib/font/bit/pelm/ascii.9.font.P2The parentheses indicate that the value assigned to.CW pathis a list of two strings. The variables.CW userand.CW fontare assigned lists containing a single string..PPThe value of a variable can be substituted into a command bypreceding its name with a.CW $ ,like this:.P1echo $path.P2If.CW pathhad been set as above, this would be equivalent to.P1echo . /bin.P2Variables may be subscripted by numbers or lists of numbers,like this:.P1echo $path(2)echo $path(2 1 2).P2These are equivalent to.P1echo /binecho /bin . /bin.P2There can be no space separating the variable's name from theleft parenthesis; otherwise, the subscript would be considereda separate parenthesized list..PPThe number of strings in a variable can be determined by the.CW $#operator. For example,.P1echo $#path.P2would print 2 for this example..PPThe following two assignments are subtly different:.P1empty=()null=''.P2The first sets.CW emptyto a list containing no strings.The second sets.CW nullto a list containing a single string,but the string contains no characters..PPAlthough these may seem like more or lessthe same thing (in Bourne's shell, they areindistinguishable), they behave differentlyin almost all circumstances.Among other things.P1echo $#empty.P2prints 0, whereas.P1echo $#null.P2prints 1..PPAll variables that have never been set have the value.CW () ..PPOccasionally, it is convenient to treat a variable's valueas a single string. The elements of a string are concatenatedinto a single string, with spaces between the elements, bythe.CW $"operator.Thus, if we set.P1list=(How now brown cow)string=$"list.P2then both.P1echo $list.P2and.P1echo $string.P2cause the same output, viz:.P1How now brown cow.P2but.P1echo $#list $#string.P2will output.P14 1.P2because.CW $listhas four members, but.CW $stringhas a single member, with three spaces separating its words..NHArguments.PPWhen.I rcis reading its input from a file, the file has accessto the arguments supplied on.I rc 'scommand line. The variable.CW $*initially has the list of arguments assigned to it.The names.CW $1 ,.CW $2 ,etc. are synonyms for.CW $*(1) ,.CW $*(2) ,etc.In addition,.CW $0is the name of the file from which.I rc 'sinput is being read..NHConcatenation.PP.I Rchas a string concatenation operator, the caret .CW ^ ,to build arguments out of pieces..P1echo hully^gully.P2is exactly equivalent to.P1echo hullygully.P2Suppose variable.CW icontains the name of a command.Then.P1vc $i^.cvl -o $1 $i^.v.P2might compile the command's source code, leaving theresult in the appropriate file..PPConcatenation distributes over lists. The following.P1echo (a b c)^(1 2 3)src=(main subr io)cc $src^.c.P2are equivalent to.P1echo a1 b2 c3cc main.c subr.c io.c.P2In detail, the rule is: if both operands of.CW ^are lists of the same non-zero number of strings, they are concatenatedpairwise. Otherwise, if one of the operands is a single string,it is concatenated with each member of the other operand in turn.Any other combination of operands is an error..NHFree carets.PPUser demand has dictated that.I rcinsert carets in certain places, to make the syntaxlook more like the Bourne shell. For example, this:.P1cc -$flags $stems.c.P2is equivalent to.P1cc -^$flags $stems^.c.P2In general,.I rcwill insert.CW ^between two arguments that are not separated by white space.Specifically, whenever one of.CW "$'`follows a quoted or unquoted word, or an unquoted word followsa quoted word with no intervening blanks or tabs, an implicit.CW ^is inserted between the two. If an unquoted word immediately following a.CW $contains a character other than an alphanumeric, underscore or.CW * ,a.CW ^is inserted before the first such character..NHCommand substitution.PPIt is often useful to build an argument list from the output of a command..I Rcallows a command, enclosed in braces and preceded by a left quote,.CW "`{...}" ,anywhere that an argument is required. The command is executed and itsstandard output captured.The characters stored in the variable.CW ifsare used to split the output into arguments.For example,.P1cat `{ls -tr|sed 10q}.P2will concatenate the ten oldest files in the current directory in temporal order, given thedefault.CW ifssetting of space, tab, and newline..NHPipeline branching.PPThe normal pipeline notation is general enough for almost all cases.Very occasionally it is useful to have pipelines that are not linear.Pipeline topologies more general than trees can require arbitrarily large pipe buffers,or worse, can cause deadlock..I Rchas syntax for some kinds of non-linear but treelike pipelines.For example,.P1 cmp <{old} <{new}.P2will regression-test a new version of a command..CW <or.CW >followed by a command in braces causes the command to be run withits standard output or input attached to a pipe. The parent command.CW cmp "" (in the example)is started with the other end of the pipe attached to some file descriptoror other, and with an argument that will connect to the pipe when opened(e.g.,.CW /dev/fd/6 ).Some commands are unprepared to deal with input files that turn out not to be seekable.For example.CW diffneeds to read its input twice..NHExit status.PPWhen a command exits it returns status to the program that executed it.On Plan 9 status is a character string describing an error condition.On normal termination it is empty..PP.I Rccaptures command exit status in the variable.CW $status .For a simple command the value of.CW $statusis just as described above. For a pipeline.CW $statusis set to the concatenation of the statuses of the pipeline components with.CW |characters for separators..PP.I Rchas a several kinds of control flow,many of them conditioned by the status returned from previouslyexecuted commands. Any.CW $statuscontaining only.CW 0 'sand.CW | 'shas boolean value.I true .Any other status is.I false ..NHCommand grouping.PPA sequence of commands enclosed in.CW {}may be used anywhere a command is required.For example:.P1{sleep 3600;echo 'Time''s up!'}&.P2will wait an hour in the background, then print a message.Without the braces,.P1sleep 3600;echo 'Time''s up!'&.P2would lock up the terminal for an hour,then print the message in the background..NHControl flow \(em \f(CWfor\fP.PPA command may be executed once for each member of a listby typing, for example:.P1for(i in printf scanf putchar) look $i /usr/td/lib/dw.dat.P2This looks for each of the words.CW printf ,.CW scanfand.CW putcharin the given file.The general form is.P1for(\fIname\fP in \fIlist\fP) \fIcommand\fP.P2or.P1for(\fIname\fP) \fIcommand\fP.P2In the first case.I commandis executed once for each member of.I listwith that member assigned to variable.I name .If the clause.CW in "" ``.I list ''is missing,.CW in "" ``.CW $* ''is assumed..NHConditional execution \(em \f(CWif\fP.PP.I Rcalso provides a general if-statement. For example:.P1for(i in *.c) if(cpp $i >/tmp/$i) vc /tmp/$i.P2runs the C compiler on each C source program thatcpp processes without error.An `if not' statement provides a two-tailed conditional.For example:.P1for(i){ if(test -f /tmp/$i) echo $i already in /tmp if not cp $i /tmp}.P2This loops over each file in.CW $* ,copying to.CW /tmpthose that do not already appear there, andprinting a message for those that do..NHControl flow \(em \f(CWwhile\fP.PP.I Rc 'swhile statement looks like this:.P1while(newer subr.v subr.c) sleep 5.P2This waits until.CW subr.vis newer than.CW subr.c ,presumably because the C compiler finished with it..PPIf the controlling command is empty, the loop will not terminate.Thus,Lines 1–500 of 1589. Load more
.HTML "Rc — The Plan 9 Shell . \" /*% refer -k -e -n -l3,2 -s < % | tbl | troff -ms | lp -dfn .Tm shell programming language g .de TP \" An indented paragraph describing some command, tagged with the command name .IP "\\f(CW\\$1\\fR" 5 .if \\w'\\f(CW\\$1\\fR'-4n .br .. .de CI .nr Sf \\n(.f \%\&\\$3\f(CW\\$1\fI\&\\$2\f\\n(Sf .. .TL Rc \(em The Plan 9 Shell .AU Tom Duff td@plan9.bell-labs.com .AB .I Rc is a command interpreter for Plan 9 that provides similar facilities to UNIX's Bourne shell, with some small additions and less idiosyncratic syntax. This paper uses numerous examples to describe .I rc 's features, and contrasts .I rc with the Bourne shell, a model that many readers will be familiar with. .AE .NH Introduction .PP .I Rc is similar in spirit but different in detail from UNIX's Bourne shell. This paper describes .I rc 's principal features with many small examples and a few larger ones. It assumes familiarity with the Bourne shell. .NH Simple commands .PP For the simplest uses .I rc has syntax familiar to Bourne-shell users. All of the following behave as expected: .P1 date cat /lib/news/build who >user.names who >>user.names wc <file echo [a-f]*.c who | wc who; date vc *.c & mk && v.out /*/bin/fb/* rm -r junk || echo rm failed! .P2 .NH Quotation .PP An argument that contains a space or one of .I rc 's other syntax characters must be enclosed in apostrophes .CW ' ): ( .P1 rm 'odd file name' .P2 An apostrophe in a quoted argument must be doubled: .P1 echo 'How''s your father?' .P2 .NH Patterns .PP An unquoted argument that contains any of the characters .CW * .CW ? .CW [ is a pattern to be matched against file names. A .CW * character matches any sequence of characters, .CW ? matches any single character, and .CW [\fIclass\fP] matches any character in the .CW class , unless the first character of .I class is .CW ~ , in which case the class is complemented. The .I class may also contain pairs of characters separated by .CW - , standing for all characters lexically between the two. The character .CW / must appear explicitly in a pattern, as must the path name components .CW . and .CW .. . A pattern is replaced by a list of arguments, one for each path name matched, except that a pattern matching no names is not replaced by the empty list; rather it stands for itself. .NH Variables .PP UNIX's Bourne shell offers string-valued variables. .I Rc provides variables whose values are lists of arguments \(em that is, arrays of strings. This is the principal difference between .I rc and traditional UNIX command interpreters. Variables may be given values by typing, for example: .P1 path=(. /bin) user=td font=/lib/font/bit/pelm/ascii.9.font .P2 The parentheses indicate that the value assigned to .CW path is a list of two strings. The variables .CW user and .CW font are assigned lists containing a single string. .PP The value of a variable can be substituted into a command by preceding its name with a .CW $ , like this: .P1 echo $path .P2 If .CW path had been set as above, this would be equivalent to .P1 echo . /bin .P2 Variables may be subscripted by numbers or lists of numbers, like this: .P1 echo $path(2) echo $path(2 1 2) .P2 These are equivalent to .P1 echo /bin echo /bin . /bin .P2 There can be no space separating the variable's name from the left parenthesis; otherwise, the subscript would be considered a separate parenthesized list. .PP The number of strings in a variable can be determined by the .CW $# operator. For example, .P1 echo $#path .P2 would print 2 for this example. .PP The following two assignments are subtly different: .P1 empty=() null='' .P2 The first sets .CW empty to a list containing no strings. The second sets .CW null to a list containing a single string, but the string contains no characters. .PP Although these may seem like more or less the same thing (in Bourne's shell, they are indistinguishable), they behave differently in almost all circumstances. Among other things .P1 echo $#empty .P2 prints 0, whereas .P1 echo $#null .P2 prints 1. .PP All variables that have never been set have the value .CW () . .PP Occasionally, it is convenient to treat a variable's value as a single string. The elements of a string are concatenated into a single string, with spaces between the elements, by the .CW $" operator. Thus, if we set .P1 list=(How now brown cow) string=$"list .P2 then both .P1 echo $list .P2 and .P1 echo $string .P2 cause the same output, viz: .P1 How now brown cow .P2 but .P1 echo $#list $#string .P2 will output .P1 4 1 .P2 because .CW $list has four members, but .CW $string has a single member, with three spaces separating its words. .NH Arguments .PP When .I rc is reading its input from a file, the file has access to the arguments supplied on .I rc 's command line. The variable .CW $* initially has the list of arguments assigned to it. The names .CW $1 , .CW $2 , etc. are synonyms for .CW $*(1) , .CW $*(2) , etc. In addition, .CW $0 is the name of the file from which .I rc 's input is being read. .NH Concatenation .PP .I Rc has a string concatenation operator, the caret .CW ^ , to build arguments out of pieces. .P1 echo hully^gully .P2 is exactly equivalent to .P1 echo hullygully .P2 Suppose variable .CW i contains the name of a command. Then .P1 vc $i^.c vl -o $1 $i^.v .P2 might compile the command's source code, leaving the result in the appropriate file. .PP Concatenation distributes over lists. The following .P1 echo (a b c)^(1 2 3) src=(main subr io) cc $src^.c .P2 are equivalent to .P1 echo a1 b2 c3 cc main.c subr.c io.c .P2 In detail, the rule is: if both operands of .CW ^ are lists of the same non-zero number of strings, they are concatenated pairwise. Otherwise, if one of the operands is a single string, it is concatenated with each member of the other operand in turn. Any other combination of operands is an error. .NH Free carets .PP User demand has dictated that .I rc insert carets in certain places, to make the syntax look more like the Bourne shell. For example, this: .P1 cc -$flags $stems.c .P2 is equivalent to .P1 cc -^$flags $stems^.c .P2 In general, .I rc will insert .CW ^ between two arguments that are not separated by white space. Specifically, whenever one of .CW "$'` follows a quoted or unquoted word, or an unquoted word follows a quoted word with no intervening blanks or tabs, an implicit .CW ^ is inserted between the two. If an unquoted word immediately following a .CW $ contains a character other than an alphanumeric, underscore or .CW * , a .CW ^ is inserted before the first such character. .NH Command substitution .PP It is often useful to build an argument list from the output of a command. .I Rc allows a command, enclosed in braces and preceded by a left quote, .CW "`{...}" , anywhere that an argument is required. The command is executed and its standard output captured. The characters stored in the variable .CW ifs are used to split the output into arguments. For example, .P1 cat `{ls -tr|sed 10q} .P2 will concatenate the ten oldest files in the current directory in temporal order, given the default .CW ifs setting of space, tab, and newline. .NH Pipeline branching .PP The normal pipeline notation is general enough for almost all cases. Very occasionally it is useful to have pipelines that are not linear. Pipeline topologies more general than trees can require arbitrarily large pipe buffers, or worse, can cause deadlock. .I Rc has syntax for some kinds of non-linear but treelike pipelines. For example, .P1 cmp <{old} <{new} .P2 will regression-test a new version of a command. .CW < or .CW > followed by a command in braces causes the command to be run with its standard output or input attached to a pipe. The parent command .CW cmp "" ( in the example) is started with the other end of the pipe attached to some file descriptor or other, and with an argument that will connect to the pipe when opened (e.g., .CW /dev/fd/6 ). Some commands are unprepared to deal with input files that turn out not to be seekable. For example .CW diff needs to read its input twice. .NH Exit status .PP When a command exits it returns status to the program that executed it. On Plan 9 status is a character string describing an error condition. On normal termination it is empty. .PP .I Rc captures command exit status in the variable .CW $status . For a simple command the value of .CW $status is just as described above. For a pipeline .CW $status is set to the concatenation of the statuses of the pipeline components with .CW | characters for separators. .PP .I Rc has a several kinds of control flow, many of them conditioned by the status returned from previously executed commands. Any .CW $status containing only .CW 0 's and .CW | 's has boolean value .I true . Any other status is .I false . .NH Command grouping .PP A sequence of commands enclosed in .CW {} may be used anywhere a command is required. For example: .P1 {sleep 3600;echo 'Time''s up!'}& .P2 will wait an hour in the background, then print a message. Without the braces, .P1 sleep 3600;echo 'Time''s up!'& .P2 would lock up the terminal for an hour, then print the message in the background. .NH Control flow \(em \f(CWfor\fP .PP A command may be executed once for each member of a list by typing, for example: .P1 for(i in printf scanf putchar) look $i /usr/td/lib/dw.dat .P2 This looks for each of the words .CW printf , .CW scanf and .CW putchar in the given file. The general form is .P1 for(\fIname\fP in \fIlist\fP) \fIcommand\fP .P2 or .P1 for(\fIname\fP) \fIcommand\fP .P2 In the first case .I command is executed once for each member of .I list with that member assigned to variable .I name . If the clause .CW in "" `` .I list '' is missing, .CW in "" `` .CW $* '' is assumed. .NH Conditional execution \(em \f(CWif\fP .PP .I Rc also provides a general if-statement. For example: .P1 for(i in *.c) if(cpp $i >/tmp/$i) vc /tmp/$i .P2 runs the C compiler on each C source program that cpp processes without error. An `if not' statement provides a two-tailed conditional. For example: .P1 for(i){ if(test -f /tmp/$i) echo $i already in /tmp if not cp $i /tmp } .P2 This loops over each file in .CW $* , copying to .CW /tmp those that do not already appear there, and printing a message for those that do. .NH Control flow \(em \f(CWwhile\fP .PP .I Rc 's while statement looks like this: .P1 while(newer subr.v subr.c) sleep 5 .P2 This waits until .CW subr.v is newer than .CW subr.c , presumably because the C compiler finished with it. .PP If the controlling command is empty, the loop will not terminate. Thus,