<html> <title>data</title><body BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#330088" ALINK="#FF0044"><H1>Rc ­ The Plan 9 Shell</H1><DL><DD><I>Tom Duff<br>td@plan9.bell-labs.com<br></I></DL><DL><DD><H4>ABSTRACT</H4><I>Rc</I>is 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</I>'sfeatures, and contrasts<I>rc</I>with the Bourne shell, a model that many readers will be familiar with.</DL><H4>1 Introduction</H4><P><I>Rc</I>is similar in spirit but different in detail from UNIX'sBourne shell. This paper describes<I>rc</I>'sprincipal features with many small examples and a few larger ones.It assumes familiarity with the Bourne shell.</P><H4>2 Simple commands</H4><P>For the simplest uses<I>rc</I>has syntax familiar to Bourne-shell users.All of the following behave as expected:<DL><DT><DD><TT><PRE>datecat /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!</PRE></TT></DL></P><H4>3 Quotation</H4><P>An argument that contains a space or one of<I>rc</I>'sother syntax characters must be enclosed in apostrophes(<TT>'</TT>):<DL><DT><DD><TT><PRE>rm 'odd file name'</PRE></TT></DL>An apostrophe in a quoted argument must be doubled:<DL><DT><DD><TT><PRE>echo 'How''s your father?'</PRE></TT></DL></P><H4>4 Patterns</H4><P>An unquoted argument that contains any of the characters<TT>*</TT><TT>?</TT><TT>[</TT>is a pattern to be matched against file names.A<TT>*</TT>character matches any sequence of characters,<TT>?</TT>matches any single character, and<TT>[<I>class</I>]</TT>matches any character in the<TT>class</TT>,unless the first character of<I>class</I>is<TT>~</TT>,in which case the class is complemented.The<I>class</I>may also contain pairs of characters separated by<TT>-</TT>,standing for all characters lexically between the two.The character<TT>/</TT>must appear explicitly in a pattern, as must the path name components<TT>.</TT>and<TT>..</TT>.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.</P><H4>5 Variables</H4><P>UNIX's Bourne shell offers string-valued variables.<I>Rc</I>provides variables whose values are lists of arguments ­that is, arrays of strings. This is the principal differencebetween<I>rc</I>and traditional UNIX command interpreters.Variables may be given values by typing, for example:<DL><DT><DD><TT><PRE>path=(. /bin)user=tdfont=/lib/font/bit/pelm/ascii.9.font</PRE></TT></DL>The parentheses indicate that the value assigned to<TT>path</TT>is a list of two strings. The variables<TT>user</TT>and<TT>font</TT>are assigned lists containing a single string.</P><P>The value of a variable can be substituted into a command bypreceding its name with a<TT>$</TT>,like this:<DL><DT><DD><TT><PRE>echo $path</PRE></TT></DL>If<TT>path</TT>had been set as above, this would be equivalent to<DL><DT><DD><TT><PRE>echo . /bin</PRE></TT></DL>Variables may be subscripted by numbers or lists of numbers,like this:<DL><DT><DD><TT><PRE>echo $path(2)echo $path(2 1 2)</PRE></TT></DL>These are equivalent to<DL><DT><DD><TT><PRE>echo /binecho /bin . /bin</PRE></TT></DL>There can be no space separating the variable's name from theleft parenthesis; otherwise, the subscript would be considereda separate parenthesized list.</P><P>The number of strings in a variable can be determined by the<TT>$#</TT>operator. For example,<DL><DT><DD><TT><PRE>echo $#path</PRE></TT></DL>would print 2 for this example.</P><P>The following two assignments are subtly different:<DL><DT><DD><TT><PRE>empty=()null=''</PRE></TT></DL>The first sets<TT>empty</TT>to a list containing no strings.The second sets<TT>null</TT>to a list containing a single string,but the string contains no characters.</P><P>Although these may seem like more or lessthe same thing (in Bourne's shell, they areindistinguishable), they behave differentlyin almost all circumstances.Among other things<DL><DT><DD><TT><PRE>echo $#empty</PRE></TT></DL>prints 0, whereas<DL><DT><DD><TT><PRE>echo $#null</PRE></TT></DL>prints 1.</P><P>All variables that have never been set have the value<TT>()</TT>.</P><P>Occasionally, 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<TT>$"</TT>operator.Thus, if we set<DL><DT><DD><TT><PRE>list=(How now brown cow)string=$"list</PRE></TT></DL>then both<DL><DT><DD><TT><PRE>echo $list</PRE></TT></DL>and<DL><DT><DD><TT><PRE>echo $string</PRE></TT></DL>cause the same output, viz:<DL><DT><DD><TT><PRE>How now brown cow</PRE></TT></DL>but<DL><DT><DD><TT><PRE>echo $#list $#string</PRE></TT></DL>will output<DL><DT><DD><TT><PRE>4 1</PRE></TT></DL>because<TT>$list</TT>has four members, but<TT>$string</TT>has a single member, with three spaces separating its words.</P><H4>6 Arguments</H4><P>When<I>rc</I>is reading its input from a file, the file has accessto the arguments supplied on<I>rc</I>'scommand line. The variable<TT>$*</TT>initially has the list of arguments assigned to it.The names<TT>$1</TT>,<TT>$2</TT>,etc. are synonyms for<TT>$*(1)</TT>,<TT>$*(2)</TT>,etc.In addition,<TT>$0</TT>is the name of the file from which<I>rc</I>'sinput is being read.</P><H4>7 Concatenation</H4><P><I>Rc</I>has a string concatenation operator, the caret <TT>^</TT>,to build arguments out of pieces.<DL><DT><DD><TT><PRE>echo hully^gully</PRE></TT></DL>is exactly equivalent to<DL><DT><DD><TT><PRE>echo hullygully</PRE></TT></DL>Suppose variable<TT>i</TT>contains the name of a command.Then<DL><DT><DD><TT><PRE>vc $i^.cvl -o $1 $i^.v</PRE></TT></DL>might compile the command's source code, leaving theresult in the appropriate file.</P><P>Concatenation distributes over lists. The following<DL><DT><DD><TT><PRE>echo (a b c)^(1 2 3)src=(main subr io)cc $src^.c</PRE></TT></DL>are equivalent to<DL><DT><DD><TT><PRE>echo a1 b2 c3cc main.c subr.c io.c</PRE></TT></DL>In detail, the rule is: if both operands of<TT>^</TT>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.</P><H4>8 Free carets</H4><P>User demand has dictated that<I>rc</I>insert carets in certain places, to make the syntaxlook more like the Bourne shell. For example, this:<DL><DT><DD><TT><PRE>cc -$flags $stems.c</PRE></TT></DL>is equivalent to<DL><DT><DD><TT><PRE>cc -^$flags $stems^.c</PRE></TT></DL>In general,<I>rc</I>will insert<TT>^</TT>between two arguments that are not separated by white space.Specifically, whenever one of<TT>$'`</TT>follows a quoted or unquoted word, or an unquoted word followsa quoted word with no intervening blanks or tabs, an implicit<TT>^</TT>is inserted between the two. If an unquoted word immediately following a<TT>$</TT>contains a character other than an alphanumeric, underscore or<TT>*</TT>,a<TT>^</TT>is inserted before the first such character.</P><H4>9 Command substitution</H4><P>It is often useful to build an argument list from the output of a command.<I>Rc</I>allows a command, enclosed in braces and preceded by a left quote,<TT>`{...}</TT>,anywhere that an argument is required. The command is executed and itsstandard output captured.The characters stored in the variable<TT>ifs</TT>are used to split the output into arguments.For example,<DL><DT><DD><TT><PRE>cat `{ls -tr|sed 10q}</PRE></TT></DL>will concatenate the ten oldest files in the current directory in temporal order, given thedefault<TT>ifs</TT>setting of space, tab, and newline.</P><H4>10 Pipeline branching</H4><P>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</I>has syntax for some kinds of non-linear but treelike pipelines.For example,<DL><DT><DD><TT><PRE> cmp <{old} <{new}</PRE></TT></DL>will regression-test a new version of a command.<TT><</TT>or<TT>></TT>followed by a command in braces causes the command to be run withits standard output or input attached to a pipe. The parent command(<TT>cmp</TT>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.,<TT>/dev/fd/6</TT>).Some commands are unprepared to deal with input files that turn out not to be seekable.For example<TT>diff</TT>needs to read its input twice.</P><H4>11 Exit status</H4><P>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.</P><P><I>Rc</I>captures command exit status in the variable<TT>$status</TT>.For a simple command the value of<TT>$status</TT>is just as described above. For a pipeline<TT>$status</TT>is set to the concatenation of the statuses of the pipeline components with<TT>|</TT>characters for separators.</P><P><I>Rc</I>has a several kinds of control flow,many of them conditioned by the status returned from previouslyexecuted commands. Any<TT>$status</TT>containing only<TT>0</TT>'sand<TT>|</TT>'shas boolean value<I>true</I>.Any other status is<I>false</I>.</P><H4>12 Command grouping</H4><P>A sequence of commands enclosed in<TT>{}</TT>may be used anywhere a command is required.For example:<DL><DT><DD><TT><PRE>{sleep 3600;echo 'Time''s up!'}&</PRE></TT></DL>will wait an hour in the background, then print a message.Without the braces,<DL><DT><DD><TT><PRE>sleep 3600;echo 'Time''s up!'&</PRE></TT></DL>would lock up the terminal for an hour,then print the message in the background.</P><H4>13 Control flow ­ <TT>for</TT></H4><P>A command may be executed once for each member of a listby typing, for example:<DL><DT><DD><TT><PRE>for(i in printf scanf putchar) look $i /usr/td/lib/dw.dat</PRE></TT></DL>This looks for each of the words<TT>printf</TT>,<TT>scanf</TT>and<TT>putchar</TT>in the given file.The general form is<DL><DT><DD><TT><PRE>for(<I>name</I> in <I>list</I>) <I>command</I></PRE></TT></DL>or<DL><DT><DD><TT><PRE>for(<I>name</I>) <I>command</I></PRE></TT></DL>In the first case<I>command</I>is executed once for each member of<I>list</I>with that member assigned to variable<I>name</I>.If the clause``<TT>in</TT><I>list</I>''is missing,``<TT>in</TT><TT>$*</TT>''is assumed.</P><H4>14 Conditional execution ­ <TT>if</TT></H4><P><I>Rc</I>also provides a general if-statement. For example:<DL><DT><DD><TT><PRE>for(i in *.c) if(cpp $i >/tmp/$i) vc /tmp/$i</PRE></TT></DL>runs the C compiler on each C source program thatcpp processes without error.An `if not' statement provides a two-tailed conditional.For example:<DL><DT><DD><TT><PRE>for(i){ if(test -f /tmp/$i) echo $i already in /tmp if not cp $i /tmp}</PRE></TT></DL>This loops over each file in<TT>$*</TT>,copying to<TT>/tmp</TT>those that do not already appear there, andLines 1–500 of 1653. Load more
<html> <title> data </title> <body BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#330088" ALINK="#FF0044"> <H1>Rc ­ The Plan 9 Shell </H1> <DL><DD><I>Tom Duff<br> td@plan9.bell-labs.com<br> </I></DL> <DL><DD><H4>ABSTRACT</H4> <I>Rc</I> 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</I>'s features, and contrasts <I>rc</I> with the Bourne shell, a model that many readers will be familiar with. </DL> <H4>1 Introduction </H4> <P> <I>Rc</I> is similar in spirit but different in detail from UNIX's Bourne shell. This paper describes <I>rc</I>'s principal features with many small examples and a few larger ones. It assumes familiarity with the Bourne shell. </P> <H4>2 Simple commands </H4> <P> For the simplest uses <I>rc</I> has syntax familiar to Bourne-shell users. All of the following behave as expected: <DL><DT><DD><TT><PRE> 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! </PRE></TT></DL> </P> <H4>3 Quotation </H4> <P> An argument that contains a space or one of <I>rc</I>'s other syntax characters must be enclosed in apostrophes (<TT>'</TT>): <DL><DT><DD><TT><PRE> rm 'odd file name' </PRE></TT></DL> An apostrophe in a quoted argument must be doubled: <DL><DT><DD><TT><PRE> echo 'How''s your father?' </PRE></TT></DL> </P> <H4>4 Patterns </H4> <P> An unquoted argument that contains any of the characters <TT>*</TT> <TT>?</TT> <TT>[</TT> is a pattern to be matched against file names. A <TT>*</TT> character matches any sequence of characters, <TT>?</TT> matches any single character, and <TT>[<I>class</I>]</TT> matches any character in the <TT>class</TT>, unless the first character of <I>class</I> is <TT>~</TT>, in which case the class is complemented. The <I>class</I> may also contain pairs of characters separated by <TT>-</TT>, standing for all characters lexically between the two. The character <TT>/</TT> must appear explicitly in a pattern, as must the path name components <TT>.</TT> and <TT>..</TT>. 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. </P> <H4>5 Variables </H4> <P> UNIX's Bourne shell offers string-valued variables. <I>Rc</I> provides variables whose values are lists of arguments ­ that is, arrays of strings. This is the principal difference between <I>rc</I> and traditional UNIX command interpreters. Variables may be given values by typing, for example: <DL><DT><DD><TT><PRE> path=(. /bin) user=td font=/lib/font/bit/pelm/ascii.9.font </PRE></TT></DL> The parentheses indicate that the value assigned to <TT>path</TT> is a list of two strings. The variables <TT>user</TT> and <TT>font</TT> are assigned lists containing a single string. </P> <P> The value of a variable can be substituted into a command by preceding its name with a <TT>$</TT>, like this: <DL><DT><DD><TT><PRE> echo $path </PRE></TT></DL> If <TT>path</TT> had been set as above, this would be equivalent to <DL><DT><DD><TT><PRE> echo . /bin </PRE></TT></DL> Variables may be subscripted by numbers or lists of numbers, like this: <DL><DT><DD><TT><PRE> echo $path(2) echo $path(2 1 2) </PRE></TT></DL> These are equivalent to <DL><DT><DD><TT><PRE> echo /bin echo /bin . /bin </PRE></TT></DL> There can be no space separating the variable's name from the left parenthesis; otherwise, the subscript would be considered a separate parenthesized list. </P> <P> The number of strings in a variable can be determined by the <TT>$#</TT> operator. For example, <DL><DT><DD><TT><PRE> echo $#path </PRE></TT></DL> would print 2 for this example. </P> <P> The following two assignments are subtly different: <DL><DT><DD><TT><PRE> empty=() null='' </PRE></TT></DL> The first sets <TT>empty</TT> to a list containing no strings. The second sets <TT>null</TT> to a list containing a single string, but the string contains no characters. </P> <P> 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 <DL><DT><DD><TT><PRE> echo $#empty </PRE></TT></DL> prints 0, whereas <DL><DT><DD><TT><PRE> echo $#null </PRE></TT></DL> prints 1. </P> <P> All variables that have never been set have the value <TT>()</TT>. </P> <P> 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 <TT>$"</TT> operator. Thus, if we set <DL><DT><DD><TT><PRE> list=(How now brown cow) string=$"list </PRE></TT></DL> then both <DL><DT><DD><TT><PRE> echo $list </PRE></TT></DL> and <DL><DT><DD><TT><PRE> echo $string </PRE></TT></DL> cause the same output, viz: <DL><DT><DD><TT><PRE> How now brown cow </PRE></TT></DL> but <DL><DT><DD><TT><PRE> echo $#list $#string </PRE></TT></DL> will output <DL><DT><DD><TT><PRE> 4 1 </PRE></TT></DL> because <TT>$list</TT> has four members, but <TT>$string</TT> has a single member, with three spaces separating its words. </P> <H4>6 Arguments </H4> <P> When <I>rc</I> is reading its input from a file, the file has access to the arguments supplied on <I>rc</I>'s command line. The variable <TT>$*</TT> initially has the list of arguments assigned to it. The names <TT>$1</TT>, <TT>$2</TT>, etc. are synonyms for <TT>$*(1)</TT>, <TT>$*(2)</TT>, etc. In addition, <TT>$0</TT> is the name of the file from which <I>rc</I>'s input is being read. </P> <H4>7 Concatenation </H4> <P> <I>Rc</I> has a string concatenation operator, the caret <TT>^</TT>, to build arguments out of pieces. <DL><DT><DD><TT><PRE> echo hully^gully </PRE></TT></DL> is exactly equivalent to <DL><DT><DD><TT><PRE> echo hullygully </PRE></TT></DL> Suppose variable <TT>i</TT> contains the name of a command. Then <DL><DT><DD><TT><PRE> vc $i^.c vl -o $1 $i^.v </PRE></TT></DL> might compile the command's source code, leaving the result in the appropriate file. </P> <P> Concatenation distributes over lists. The following <DL><DT><DD><TT><PRE> echo (a b c)^(1 2 3) src=(main subr io) cc $src^.c </PRE></TT></DL> are equivalent to <DL><DT><DD><TT><PRE> echo a1 b2 c3 cc main.c subr.c io.c </PRE></TT></DL> In detail, the rule is: if both operands of <TT>^</TT> 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. </P> <H4>8 Free carets </H4> <P> User demand has dictated that <I>rc</I> insert carets in certain places, to make the syntax look more like the Bourne shell. For example, this: <DL><DT><DD><TT><PRE> cc -$flags $stems.c </PRE></TT></DL> is equivalent to <DL><DT><DD><TT><PRE> cc -^$flags $stems^.c </PRE></TT></DL> In general, <I>rc</I> will insert <TT>^</TT> between two arguments that are not separated by white space. Specifically, whenever one of <TT>$'`</TT> follows a quoted or unquoted word, or an unquoted word follows a quoted word with no intervening blanks or tabs, an implicit <TT>^</TT> is inserted between the two. If an unquoted word immediately following a <TT>$</TT> contains a character other than an alphanumeric, underscore or <TT>*</TT>, a <TT>^</TT> is inserted before the first such character. </P> <H4>9 Command substitution </H4> <P> It is often useful to build an argument list from the output of a command. <I>Rc</I> allows a command, enclosed in braces and preceded by a left quote, <TT>`{...}</TT>, anywhere that an argument is required. The command is executed and its standard output captured. The characters stored in the variable <TT>ifs</TT> are used to split the output into arguments. For example, <DL><DT><DD><TT><PRE> cat `{ls -tr|sed 10q} </PRE></TT></DL> will concatenate the ten oldest files in the current directory in temporal order, given the default <TT>ifs</TT> setting of space, tab, and newline. </P> <H4>10 Pipeline branching </H4> <P> 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</I> has syntax for some kinds of non-linear but treelike pipelines. For example, <DL><DT><DD><TT><PRE> cmp <{old} <{new} </PRE></TT></DL> will regression-test a new version of a command. <TT><</TT> or <TT>></TT> 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 (<TT>cmp</TT> 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., <TT>/dev/fd/6</TT>). Some commands are unprepared to deal with input files that turn out not to be seekable. For example <TT>diff</TT> needs to read its input twice. </P> <H4>11 Exit status </H4> <P> 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. </P> <P> <I>Rc</I> captures command exit status in the variable <TT>$status</TT>. For a simple command the value of <TT>$status</TT> is just as described above. For a pipeline <TT>$status</TT> is set to the concatenation of the statuses of the pipeline components with <TT>|</TT> characters for separators. </P> <P> <I>Rc</I> has a several kinds of control flow, many of them conditioned by the status returned from previously executed commands. Any <TT>$status</TT> containing only <TT>0</TT>'s and <TT>|</TT>'s has boolean value <I>true</I>. Any other status is <I>false</I>. </P> <H4>12 Command grouping </H4> <P> A sequence of commands enclosed in <TT>{}</TT> may be used anywhere a command is required. For example: <DL><DT><DD><TT><PRE> {sleep 3600;echo 'Time''s up!'}& </PRE></TT></DL> will wait an hour in the background, then print a message. Without the braces, <DL><DT><DD><TT><PRE> sleep 3600;echo 'Time''s up!'& </PRE></TT></DL> would lock up the terminal for an hour, then print the message in the background. </P> <H4>13 Control flow ­ <TT>for</TT> </H4> <P> A command may be executed once for each member of a list by typing, for example: <DL><DT><DD><TT><PRE> for(i in printf scanf putchar) look $i /usr/td/lib/dw.dat </PRE></TT></DL> This looks for each of the words <TT>printf</TT>, <TT>scanf</TT> and <TT>putchar</TT> in the given file. The general form is <DL><DT><DD><TT><PRE> for(<I>name</I> in <I>list</I>) <I>command</I> </PRE></TT></DL> or <DL><DT><DD><TT><PRE> for(<I>name</I>) <I>command</I> </PRE></TT></DL> In the first case <I>command</I> is executed once for each member of <I>list</I> with that member assigned to variable <I>name</I>. If the clause ``<TT>in</TT> <I>list</I>'' is missing, ``<TT>in</TT> <TT>$*</TT>'' is assumed. </P> <H4>14 Conditional execution ­ <TT>if</TT> </H4> <P> <I>Rc</I> also provides a general if-statement. For example: <DL><DT><DD><TT><PRE> for(i in *.c) if(cpp $i >/tmp/$i) vc /tmp/$i </PRE></TT></DL> 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: <DL><DT><DD><TT><PRE> for(i){ if(test -f /tmp/$i) echo $i already in /tmp if not cp $i /tmp } </PRE></TT></DL> This loops over each file in <TT>$*</TT>, copying to <TT>/tmp</TT> those that do not already appear there, and