home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / doc / shell / t1 < prev    next >
Encoding:
Text File  |  1979-01-10  |  10.0 KB  |  439 lines

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