home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / unix / riscbsd / datafile / usd / 03_shell / t1_txt next >
Encoding:
Text File  |  1996-10-12  |  10.6 KB  |  463 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.              An Introduction to the UNIX Shell
  11.  
  12.                         S. R. Bourne
  13.  
  14.             (Updated for 4.3BSD by Mark Seiden)
  15.  
  16.  
  17.                           ABSTRACT
  18.  
  19.  
  20.  
  21.      The shell= is a command programming language  that
  22.      provides  an  interface  to  the UNIX(R) operating
  23.      system.  Its features include control-flow  primi-
  24.      tives,  parameter  passing,  variables  and string
  25.      substitution.  Constructs such as while,  if  then
  26.      else,  case and for are available.  Two-way commu-
  27.      nication is possible between the  shell  and  com-
  28.      mands.   String-valued  parameters, typically file
  29.      names or flags, may be passed  to  a  command.   A
  30.      return code is set by commands that may be used to
  31.      determine control-flow, and  the  standard  output
  32.      from a command may be used as shell input.
  33.  
  34.      The shell can modify the environment in which com-
  35.      mands run.  Input and output can be redirected  to
  36.      files,  and  processes  that  communicate  through
  37.      `pipes' can be invoked.   Commands  are  found  by
  38.      searching  directories  in  the  file  system in a
  39.      sequence that can be defined by  the  user.   Com-
  40.      mands can be read either from the terminal or from
  41.      a file, which  allows  command  procedures  to  be
  42.      stored for later use.
  43.  
  44.  
  45. 1.0 Introduction
  46.  
  47. The  shell is both a command language and a programming lan-
  48. guage that provides an interface to the UNIX operating  sys-
  49. tem.   This  memorandum  describes,  with examples, the UNIX
  50. shell.  The  first  section  covers  most  of  the  everyday
  51. requirements  of terminal users.  Some familiarity with UNIX
  52. is an advantage when reading this section; see, for example,
  53. "UNIX  for  beginners".   unix beginn kernigh 1978 Section 2
  54. describes those features of the shell primarily intended for
  55. use within shell procedures.  These include the control-flow
  56. -----------
  57. =  This paper describes sh(1). If it's the c shell
  58. (csh) you're interested in, a good place to  begin
  59. is  William  Joy's paper "An Introduction to the C
  60. shell" (USD:4).
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. USD:3-2                    An Introduction to the UNIX Shell
  71.  
  72.  
  73. primitives  and  string-valued  variables  provided  by  the
  74. shell.   A  knowledge  of  a programming language would be a
  75. help when reading this section.  The last section  describes
  76. the  more advanced features of the shell.  References of the
  77. form "see pipe (2)" are to a section  of  the  UNIX  manual.
  78. seventh 1978 ritchie thompson
  79.  
  80. 1.1 Simple commands
  81.  
  82. Simple  commands  consist  of one or more words separated by
  83. blanks.  The first word is the name of  the  command  to  be
  84. executed; any remaining words are passed as arguments to the
  85. command.  For example,
  86.  
  87.           who
  88.  
  89. is a command that prints the names of users logged in.   The
  90. command
  91.  
  92.           ls -l
  93.  
  94. prints  a list of files in the current directory.  The argu-
  95. ment -l tells ls to print status information, size  and  the
  96. creation date for each file.
  97.  
  98. 1.2 Background commands
  99.  
  100. To  execute  a command the shell normally creates a new pro-
  101. cess and waits for it to finish.  A command may be run with-
  102. out waiting for it to finish.  For example,
  103.  
  104.           cc pgm.c &
  105.  
  106. calls  the C compiler to compile the file pgm.c.  The trail-
  107. ing & is an operator that instructs the shell  not  to  wait
  108. for  the  command  to  finish.  To help keep track of such a
  109. process the shell reports its process number  following  its
  110. creation.   A  list  of  currently  active  processes may be
  111. obtained using the ps command.
  112.  
  113. 1.3 Input output redirection
  114.  
  115. Most commands produce output on the standard output that  is
  116. initially  connected  to  the  terminal.  This output may be
  117. sent to a file by writing, for example,
  118.  
  119.           ls -l >file
  120.  
  121. The notation >file is interpreted by the shell  and  is  not
  122. passed  as  an  argument to ls.  If file does not exist then
  123. the shell creates it; otherwise  the  original  contents  of
  124. file  are  replaced  with the output from ls.  Output may be
  125. appended to a file using the notation
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. An Introduction to the UNIX Shell                    USD:3-3
  137.  
  138.  
  139.           ls -l file
  140.  
  141. In this case file is also created if  it  does  not  already
  142. exist.
  143.  
  144. The  standard  input  of  a command may be taken from a file
  145. instead of the terminal by writing, for example,
  146.  
  147.           wc <file
  148.  
  149. The command wc reads its standard input (in this case  redi-
  150. rected from file) and prints the number of characters, words
  151. and lines found.  If only the number of  lines  is  required
  152. then
  153.  
  154.           wc -l <file
  155.  
  156. could be used.
  157.  
  158. 1.4 Pipelines and filters
  159.  
  160. The  standard  output of one command may be connected to the
  161. standard input of another by writing  the  `pipe'  operator,
  162. indicated by , as in,
  163.  
  164.           ls -l  wc
  165.  
  166. Two commands connected in this way constitute a pipeline and
  167. the overall effect is the same as
  168.  
  169.           ls -l >file; wc <file
  170.  
  171. except that no file is used.  Instead the two processes  are
  172. connected  by a pipe (see pipe (2)) and are run in parallel.
  173. Pipes are unidirectional and synchronization is achieved  by
  174. halting wc when there is nothing to read and halting ls when
  175. the pipe is full.
  176.  
  177. A filter is a command that reads its standard input,  trans-
  178. forms  it in some way, and prints the result as output.  One
  179. such filter, grep, selects from its input those  lines  that
  180. contain some specified string.  For example,
  181.  
  182.           ls  grep old
  183.  
  184. prints  those lines, if any, of the output from ls that con-
  185. tain the string old.  Another useful filter  is  sort.   For
  186. example,
  187.  
  188.           who  sort
  189.  
  190. will print an alphabetically sorted list of logged in users.
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. USD:3-4                    An Introduction to the UNIX Shell
  203.  
  204.  
  205. A pipeline may consist of more than two commands, for  exam-
  206. ple,
  207.  
  208.           ls  grep old  wc -l
  209.  
  210. prints  the  number  of  file names in the current directory
  211. containing the string old.
  212.  
  213. 1.5 File name generation
  214.  
  215. Many commands accept arguments which are  file  names.   For
  216. example,
  217.  
  218.           ls -l main.c
  219.  
  220. prints information relating to the file main.c.
  221.  
  222. The shell provides a mechanism for generating a list of file
  223. names that match a pattern.  For example,
  224.  
  225.           ls -l *.c
  226.  
  227. generates, as arguments to ls, all file names in the current
  228. directory that end in .c.  The character * is a pattern that
  229. will match any string including the null string.  In general
  230. patterns are specified as follows.
  231.  
  232.      *       Matches  any string of characters including the
  233.              null string.
  234.  
  235.      ?       Matches any single character.
  236.  
  237.      []      Matches any one of the characters enclosed.   A
  238.              pair  of  characters  separated by a minus will
  239.              match any character lexically between the pair.
  240.  
  241. For example,
  242.  
  243.           [a-z]*
  244.  
  245. matches  all  names  in the current directory beginning with
  246. one of the letters a through z.
  247.  
  248.           /usr/fred/test/?
  249.  
  250. matches all names in the directory /usr/fred/test that  con-
  251. sist  of  a single character.  If no file name is found that
  252. matches the pattern then the pattern is  passed,  unchanged,
  253. as an argument.
  254.  
  255. This  mechanism  is useful both to save typing and to select
  256. names according to some pattern.  It may  also  be  used  to
  257. find files.  For example,
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. An Introduction to the UNIX Shell                    USD:3-5
  269.  
  270.  
  271.           echo /usr/fred/*/core
  272.  
  273. finds  and  prints  the  names  of  all  core  files in sub-
  274. directories of /usr/fred.  (echo is a standard UNIX  command
  275. that  prints its arguments, separated by blanks.)  This last
  276. feature can be expensive,  requiring  a  scan  of  all  sub-
  277. directories of /usr/fred.
  278.  
  279. There  is  one exception to the general rules given for pat-
  280. terns.  The character `.'  at the start of a file name  must
  281. be explicitly matched.
  282.  
  283.           echo *
  284.  
  285. will  therefore echo all file names in the current directory
  286. not beginning with `.'.
  287.  
  288.           echo .*
  289.  
  290. will echo all those file names that begin  with  `.'.   This
  291. avoids inadvertent matching of the names `.' and `..'  which
  292. mean `the current  directory'  and  `the  parent  directory'
  293. respectively.   (Notice  that  ls suppresses information for
  294. the files `.' and `..'.)
  295.  
  296. 1.6 Quoting
  297.  
  298. Characters that have a special meaning to the shell, such as
  299. <  >  * ?  &, are called metacharacters.  A complete list of
  300. metacharacters is given in appendix B.  Any  character  pre-
  301. ceded  by  a  \  is quoted and loses its special meaning, if
  302. any.  The \ is elided so that
  303.  
  304.           echo \\?
  305.  
  306. will echo a single ?, and
  307.  
  308.           echo \\\\
  309.  
  310. will echo a single \.  To allow long strings to be continued
  311. over more than one line the sequence \newline is ignored.
  312.  
  313. \  is  convenient  for quoting single characters.  When more
  314. than one character needs  quoting  the  above  mechanism  is
  315. clumsy  and  error  prone.   A  string  of characters may be
  316. quoted by enclosing the string between single  quotes.   For
  317. example,
  318.  
  319.           echo xx'****'xx
  320.  
  321. will echo
  322.  
  323.           xx****xx
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. USD:3-6                    An Introduction to the UNIX Shell
  335.  
  336.  
  337. The  quoted  string  may  not contain a single quote but may
  338. contain newlines, which are preserved.  This quoting  mecha-
  339. nism is the most simple and is recommended for casual use.
  340.  
  341. A third quoting mechanism using double quotes is also avail-
  342. able that  prevents  interpretation  of  some  but  not  all
  343. metacharacters.   Discussion  of  the details is deferred to
  344. section 3.4.
  345.  
  346. 1.7 Prompting
  347.  
  348. When the shell is used from  a  terminal  it  will  issue  a
  349. prompt  before reading a command.  By default this prompt is
  350. `$ '.  It may be changed by saying, for example,
  351.  
  352.           PS1=yesdear
  353.  
  354. that sets the prompt to be the string yesdear.  If a newline
  355. is  typed  and  further  input is needed then the shell will
  356. issue the prompt `> '.  Sometimes  this  can  be  caused  by
  357. mistyping  a quote mark.  If it is unexpected then an inter-
  358. rupt (DEL) will return the shell to  read  another  command.
  359. This prompt may be changed by saying, for example,
  360.  
  361.           PS2=more
  362.  
  363.  
  364. 1.8 The shell and login
  365.  
  366. Following  login (1) the shell is called to read and execute
  367. commands typed at the terminal.  If the user's login  direc-
  368. tory  contains  the file .profile then it is assumed to con-
  369. tain commands and is read by the shell  before  reading  any
  370. commands from the terminal.
  371.  
  372. 1.9 Summary
  373.  
  374.  
  375.      o    ls
  376.           Print the names of files in the current directory.
  377.  
  378.      o    ls >file
  379.           Put the output from ls into file.
  380.  
  381.      o    ls  wc -l
  382.           Print the number of files in  the  current  direc-
  383.           tory.
  384.  
  385.      o    ls  grep old
  386.           Print  those file names containing the string old.
  387.  
  388.      o    ls  grep old  wc -l
  389.           Print the number of files whose name contains  the
  390.           string old.
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. An Introduction to the UNIX Shell                    USD:3-7
  401.  
  402.  
  403.      o    cc pgm.c &
  404.           Run cc in the background.
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.