home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / share / doc / usd / 04.csh / csh.2 < prev    next >
Encoding:
Text File  |  1991-04-17  |  37.7 KB  |  1,306 lines

  1. .\" Copyright (c) 1980 The Regents of the University of California.
  2. .\" All rights reserved.
  3. .\"
  4. .\" Redistribution and use in source and binary forms, with or without
  5. .\" modification, are permitted provided that the following conditions
  6. .\" are met:
  7. .\" 1. Redistributions of source code must retain the above copyright
  8. .\"    notice, this list of conditions and the following disclaimer.
  9. .\" 2. Redistributions in binary form must reproduce the above copyright
  10. .\"    notice, this list of conditions and the following disclaimer in the
  11. .\"    documentation and/or other materials provided with the distribution.
  12. .\" 3. All advertising materials mentioning features or use of this software
  13. .\"    must display the following acknowledgement:
  14. .\"    This product includes software developed by the University of
  15. .\"    California, Berkeley and its contributors.
  16. .\" 4. Neither the name of the University nor the names of its contributors
  17. .\"    may be used to endorse or promote products derived from this software
  18. .\"    without specific prior written permission.
  19. .\"
  20. .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  21. .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24. .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. .\" SUCH DAMAGE.
  31. .\"
  32. .\"    @(#)csh.2    6.2 (Berkeley) 4/17/91
  33. .\"
  34. .nr H1 1
  35. .NH
  36. Details on the shell for terminal users
  37. .NH 2
  38. Shell startup and termination
  39. .PP
  40. When you login, the shell is started by the system in your
  41. .I home
  42. directory and begins by reading commands from a file
  43. .I \&.cshrc
  44. in this directory.
  45. All shells which you may start during your terminal session will
  46. read from this file.
  47. We will later see what kinds of commands are usefully placed there.
  48. For now we need not have this file and the shell does not complain about
  49. its absence.
  50. .PP
  51. .I "login shell" ,
  52. executed after you login to the system,
  53. will, after it reads commands from
  54. .I \&.cshrc,
  55. read commands from a file
  56. .I \&.login
  57. also in your home directory.
  58. This file contains commands which you wish to do each time you login
  59. to the \s-2UNIX\s0 system.
  60. My
  61. .I \&.login
  62. file looks something like:
  63. .DS
  64. set ignoreeof
  65. set mail=(/usr/spool/mail/bill)
  66. echo "${prompt}users" ; users
  67. alias ts \e
  68.     \'set noglob ; eval \`tset \-s \-m dialup:c100rv4pna \-m plugboard:?hp2621nl \!*\`\';
  69. ts; stty intr ^C kill ^U crt
  70. set time=15 history=10
  71. msgs \-f
  72. if (\-e $mail) then
  73.     echo "${prompt}mail"
  74.     mail
  75. endif
  76. .DE
  77. .PP
  78. This file contains several commands to be executed by \s-2UNIX\s0
  79. each time I login.
  80. The first is a
  81. .I set
  82. command which is interpreted directly by the shell.  It sets the shell
  83. variable
  84. .I ignoreeof
  85. which causes the shell to not log me off if I hit ^D.  Rather,
  86. I use the
  87. .I logout
  88. command to log off of the system.
  89. By setting the
  90. .I mail
  91. variable, I ask the shell to watch for incoming mail to me.  Every 5 minutes
  92. the shell looks for this file and tells me if more mail has arrived there.
  93. An alternative to this is to put the command
  94. .DS
  95. biff y
  96. .DE
  97. in place of this
  98. .I set;
  99. this will cause me to be notified immediately when mail arrives, and to
  100. be shown the first few lines of the new message.
  101. .PP
  102. Next I set the shell variable `time' to `15' causing the shell to automatically
  103. print out statistics lines for commands which execute for at least 15 seconds
  104. of \s-2CPU\s+2 time.  The variable `history' is set to 10 indicating that
  105. I want the shell to remember the last 10 commands I type in its
  106. .I "history list" ,
  107. (described later).
  108. .PP
  109. I create an
  110. .I alias
  111. ``ts'' which executes a
  112. \fItset\fR\|(1) command setting up the modes of the terminal.
  113. The parameters to
  114. .I tset
  115. indicate the kinds of terminal which I usually use when not on a hardwired
  116. port.  I then execute ``ts'' and also use the
  117. .I stty
  118. command to change the interrupt character to ^C and the line kill
  119. character to ^U.
  120. .PP
  121. I then run the `msgs' program, which provides me with any
  122. system messages which I have not seen before; the `\-f' option here prevents
  123. it from telling me anything if there are no new messages.
  124. Finally, if my mailbox file exists, then I run the `mail' program to
  125. process my mail.
  126. .PP
  127. When the `mail' and `msgs' programs finish, the shell will finish
  128. processing my
  129. .I \&.login
  130. file and begin reading commands from the terminal, prompting for each with
  131. `% '.
  132. When I log off (by giving the 
  133. .I logout
  134. command) the shell
  135. will print `logout' and execute commands from the file `.logout'
  136. if it exists in my home directory.
  137. After that the shell will terminate and \s-2UNIX\s0 will log
  138. me off the system.
  139. If the system is not going down, I will receive a new login message.
  140. In any case, after the `logout' message the shell is committed to terminating
  141. and will take no further input from my terminal.
  142. .NH 2
  143. Shell variables
  144. .PP
  145. The shell maintains a set of
  146. .I variables.
  147. We saw above the variables
  148. .I history
  149. and
  150. .I time
  151. which had values `10' and `15'.
  152. In fact, each shell variable has as value an array of
  153. zero or more
  154. .I strings.
  155. Shell variables may be assigned values by the set command.  It has
  156. several forms, the most useful of which was given above and is
  157. .DS
  158. set name=value
  159. .DE
  160. .PP
  161. Shell variables may be used to store values which are to
  162. be used in commands later through a substitution mechanism.
  163. The shell variables most commonly referenced are, however, those which the
  164. shell itself refers to.
  165. By changing the values of these variables one can directly affect the
  166. behavior of the shell.
  167. .PP
  168. One of the most important variables is the variable
  169. .I path.
  170. This variable contains a sequence of directory names where the shell
  171. searches for commands.
  172. The
  173. .I set
  174. command with no arguments
  175. shows the value of all variables currently defined (we usually say
  176. .I set)
  177. in the shell.
  178. The default value for path will be shown by
  179. .I set
  180. to be
  181. .DS
  182. % set
  183. .ta .75i
  184. argv    ()
  185. cwd    /usr/bill
  186. home    /usr/bill
  187. path    (. /usr/ucb /bin /usr/bin)
  188. prompt    %
  189. shell    /bin/csh
  190. status    0
  191. term    c100rv4pna
  192. user    bill
  193. %
  194. .so tabs
  195. .DE
  196. This output indicates that the variable path points to the current
  197. directory `.' and then `/usr/ucb', `/bin' and `/usr/bin'.
  198. Commands which you may write might be in `.' (usually one of
  199. your directories).
  200. Commands developed at Berkeley, live in `/usr/ucb'
  201. while commands developed at Bell Laboratories live in `/bin' and `/usr/bin'.
  202. .PP
  203. A number of locally developed programs on the system live in the directory
  204. `/usr/local'.
  205. If we wish that all shells which we invoke to have
  206. access to these new programs we can place the command
  207. .DS
  208. set path=(. /usr/ucb /bin /usr/bin /usr/local)
  209. .DE
  210. in our file
  211. .I \&.cshrc
  212. in our home directory.
  213. Try doing this and then logging out and back in and do
  214. .DS
  215. set
  216. .DE
  217. again to see that the value assigned to
  218. .I path
  219. has changed.
  220. .FS \(dg
  221. Another directory that might interest you is /usr/new, which contains
  222. many useful user-contributed programs provided with Berkeley Unix.
  223. .FE
  224. .PP
  225. One thing you should be aware of is that the shell examines each directory
  226. which you insert into your path and determines which commands are contained
  227. there.  Except for the current directory `.', which the shell treats specially,
  228. this means that if commands are added to a directory in your search path after
  229. you have started the shell, they will not necessarily be found by the shell.
  230. If you wish to use a command which has been added in this way, you should
  231. give the command
  232. .DS
  233. rehash
  234. .DE
  235. to the shell, which will cause it to recompute its internal table of command
  236. locations, so that it will find the newly added command.
  237. Since the shell has to look in the current directory `.' on each command,
  238. placing it at the end of the path specification usually works equivalently
  239. and reduces overhead.
  240. .PP
  241. Other useful built in variables are the variable
  242. .I home
  243. which shows your home directory,
  244. .I cwd
  245. which contains your current working directory,
  246. the variable
  247. .I ignoreeof
  248. which can be set in your
  249. .I \&.login
  250. file to tell the shell not to exit when it receives an end-of-file from
  251. a terminal (as described above).
  252. The variable `ignoreeof'
  253. is one of several variables which the shell does not care about the
  254. value of, only whether they are
  255. .I set
  256. or
  257. .I unset.
  258. Thus to set this variable you simply do
  259. .DS
  260. set ignoreeof
  261. .DE
  262. and to unset it do
  263. .DS
  264. unset ignoreeof
  265. .DE
  266. These give the variable `ignoreeof' no value, but none is desired or required.
  267. .PP
  268. Finally, some other built-in shell variables of use are the
  269. variables
  270. .I noclobber
  271. and
  272. .I mail.
  273. The metasyntax
  274. .DS
  275. > filename
  276. .DE
  277. which redirects the standard output of a command
  278. will overwrite and destroy the previous contents of the named file.
  279. In this way you may accidentally overwrite a file which is valuable.
  280. If you would prefer that the shell not overwrite files in this
  281. way you can
  282. .DS
  283. set noclobber
  284. .DE
  285. in your
  286. .I \&.login
  287. file.
  288. Then trying to do
  289. .DS
  290. date > now
  291. .DE
  292. would cause a diagnostic if `now' existed already.
  293. You could type
  294. .DS
  295. date >!  now
  296. .DE
  297. if you really wanted to overwrite the contents of `now'.
  298. The `>!' is a special metasyntax indicating that clobbering the
  299. file is ok.\(dg
  300. .FS
  301. \(dgThe space between the `!' and the word `now' is critical here, as `!now'
  302. would be an invocation of the
  303. .I history
  304. mechanism, and have a totally different effect.
  305. .FE
  306. .NH 2
  307. The shell's history list
  308. .PP
  309. The shell can maintain a
  310. .I "history list"
  311. into which it places the words
  312. of previous commands.
  313. It is possible to use a notation to reuse commands or words
  314. from commands in forming new commands.
  315. This mechanism can be used to repeat previous commands or to
  316. correct minor typing mistakes in commands.
  317. .PP
  318. The following figure gives a sample session involving typical usage of the
  319. history mechanism of the shell.
  320. .KF
  321. .DS
  322. % cat bug.c
  323. main()
  324.  
  325. {
  326.     printf("hello);
  327. }
  328. % cc !$
  329. cc bug.c 
  330. "bug.c", line 4: newline in string or char constant
  331. "bug.c", line 5: syntax error
  332. % ed !$
  333. ed bug.c 
  334. 29
  335. 4s/);/"&/p
  336.         printf("hello");
  337. w
  338. 30
  339. q
  340. % !c
  341. cc bug.c 
  342. % a.out
  343. hello% !e
  344. ed bug.c 
  345. 30
  346. 4s/lo/lo\e\en/p
  347.         printf("hello\en");
  348. w
  349. 32
  350. q
  351. % !c \-o bug
  352. cc bug.c \-o bug
  353. % size a.out bug
  354. a.out: 2784+364+1028 = 4176b = 0x1050b
  355. bug: 2784+364+1028 = 4176b = 0x1050b
  356. % ls \-l !*
  357. ls \-l a.out bug 
  358. \(mirwxr\(mixr\(mix 1 bill       3932 Dec 19 09:41 a.out
  359. \(mirwxr\(mixr\(mix 1 bill       3932 Dec 19 09:42 bug
  360. % bug
  361. hello
  362. % num bug.c | spp
  363. spp: Command not found.
  364. % ^spp^ssp
  365. num bug.c | ssp 
  366.     1    main()
  367.     3    {
  368.     4        printf("hello\en");
  369.     5    }
  370. % !! | lpr
  371. num bug.c | ssp | lpr
  372. .DE
  373. .KE
  374. In this example we have a very simple C program which has a bug (or two)
  375. in it in the file `bug.c', which we `cat' out on our terminal.  We then
  376. try to run the C compiler on it, referring to the file again as `!$',
  377. meaning the last argument to the previous command.  Here the `!' is the
  378. history mechanism invocation metacharacter, and the `$' stands for the last
  379. argument, by analogy to `$' in the editor which stands for the end of the line.
  380. The shell echoed the command, as it would have been typed without use of
  381. the history mechanism, and then executed it.
  382. The compilation yielded error diagnostics so we now run the editor on the
  383. file we were trying to compile, fix the bug, and run the C compiler again,
  384. this time referring to this command simply as `!c', which repeats the last
  385. command which started with the letter `c'.  If there were other
  386. commands starting with `c' done recently we could have said `!cc' or even
  387. `!cc:p' which would have printed the last command starting with `cc'
  388. without executing it.
  389. .PP
  390. After this recompilation, we ran the resulting `a.out' file, and then
  391. noting that there still was a bug, ran the editor again.  After fixing
  392. the program we ran the C compiler again, but tacked onto the command
  393. an extra `\-o bug' telling the compiler to place the resultant binary in
  394. the file `bug' rather than `a.out'.  In general, the history mechanisms
  395. may be used anywhere in the formation of new commands and other characters
  396. may be placed before and after the substituted commands.
  397. .PP
  398. We then ran the `size' command to see how large the binary program images
  399. we have created were, and then an `ls \-l' command with the same argument
  400. list, denoting the argument list `\!*'.
  401. Finally we ran the program `bug' to see that its output is indeed correct.
  402. .PP
  403. To make a numbered listing of the program we ran the `num' command on the file `bug.c'.
  404. In order to compress out blank lines in the output of `num' we ran the
  405. output through the filter `ssp', but misspelled it as spp.  To correct this
  406. we used a shell substitute, placing the old text and new text between `^'
  407. characters.  This is similar to the substitute command in the editor.
  408. Finally, we repeated the same command with `!!', but sent its output to the
  409. line printer.
  410. .PP
  411. There are other mechanisms available for repeating commands.  The
  412. .I history
  413. command prints out a number of previous commands with numbers by which
  414. they can be referenced.  There is a way to refer to a previous command
  415. by searching for a string which appeared in it, and there are other,
  416. less useful, ways to select arguments to include in a new command.
  417. A complete description of all these mechanisms
  418. is given in the C shell manual pages in the \s-2UNIX\s0 Programmer's Manual.
  419. .NH 2
  420. Aliases
  421. .PP
  422. The shell has an
  423. .I alias
  424. mechanism which can be used to make transformations on input commands.
  425. This mechanism can be used to simplify the commands you type,
  426. to supply default arguments to commands,
  427. or to perform transformations on commands and their arguments.
  428. The alias facility is similar to a macro facility.
  429. Some of the features obtained by aliasing can be obtained also
  430. using shell command files, but these take place in another instance
  431. of the shell and cannot directly affect the current shells environment
  432. or involve commands such as
  433. .I cd
  434. which must be done in the current shell.
  435. .PP
  436. As an example, suppose that there is a new version of the mail program
  437. on the system called `newmail'
  438. you wish to use, rather than the standard mail program which is called
  439. `mail'.
  440. If you place the shell command
  441. .DS
  442. alias mail newmail
  443. .DE
  444. in your
  445. .I \&.cshrc
  446. file, the shell will transform an input line of the form
  447. .DS
  448. mail bill
  449. .DE
  450. into a call on `newmail'.
  451. More generally, suppose we wish the command `ls' to always show
  452. sizes of files, that is to always do `\-s'.
  453. We can do
  454. .DS
  455. alias ls ls \-s
  456. .DE
  457. or even
  458. .DS
  459. alias dir ls \-s
  460. .DE
  461. creating a new command syntax `dir'
  462. which does an `ls \-s'.
  463. If we say
  464. .DS
  465. dir ~bill
  466. .DE
  467. then the shell will translate this to
  468. .DS
  469. ls \-s /mnt/bill
  470. .DE
  471. .PP
  472. Thus the
  473. .I alias
  474. mechanism can be used to provide short names for commands,
  475. to provide default arguments,
  476. and to define new short commands in terms of other commands.
  477. It is also possible to define aliases which contain multiple
  478. commands or pipelines, showing where the arguments to the original
  479. command are to be substituted using the facilities of the
  480. history mechanism.
  481. Thus the definition
  482. .DS
  483. alias cd \'cd \e!* ; ls \'
  484. .DE
  485. would do an
  486. .I ls
  487. command after each change directory
  488. .I cd
  489. command.
  490. We enclosed the entire alias definition in `\'' characters to prevent
  491. most substitutions from occurring and the character `;' from being
  492. recognized as a metacharacter.
  493. The `!' here is escaped with a `\e' to prevent it from being interpreted
  494. when the alias command is typed in.
  495. The `\e!*' here substitutes the entire argument list to the pre-aliasing
  496. .I cd
  497. command, without giving an error if there were no arguments.
  498. The `;' separating commands is used here
  499. to indicate that one command is to be done and then the next.
  500. Similarly the definition
  501. .DS
  502. alias whois \'grep \e!^ /etc/passwd\'
  503. .DE
  504. defines a command which looks up its first argument in the password file.
  505. .PP
  506. .B Warning:
  507. The shell currently reads the
  508. .I \&.cshrc
  509. file each time it starts up.  If you place a large number of commands
  510. there, shells will tend to start slowly.  A mechanism for saving the shell
  511. environment after reading the \fI\&.cshrc\fR file and quickly restoring it is
  512. under development, but for now you should try to limit the number of
  513. aliases you have to a reasonable number... 10 or 15 is reasonable,
  514. 50 or 60 will cause a noticeable delay in starting up shells, and make
  515. the system seem sluggish when you execute commands from within the editor
  516. and other programs.
  517. .NH 2
  518. More redirection; >> and >&
  519. .PP
  520. There are a few more notations useful to the terminal user
  521. which have not been introduced yet.
  522. .PP
  523. In addition to the standard output, commands also have a
  524. .I "diagnostic output"
  525. which is normally directed to the terminal even when the standard output
  526. is redirected to a file or a pipe.
  527. It is occasionally desirable to direct the diagnostic output along with
  528. the standard output.
  529. For instance if you want to redirect the output of a long running command
  530. into a file and wish to have a record of any error diagnostic it produces
  531. you can do
  532. .DS
  533. command >& file
  534. .DE
  535. The `>&' here tells the shell to route both the diagnostic output and the
  536. standard output into `file'.
  537. Similarly you can give the command
  538. .DS
  539. command |\|& lpr
  540. .DE
  541. to route both standard and diagnostic output through the pipe
  542. to the line printer daemon
  543. .I lpr.\(dd
  544. .FS
  545. \(dd A command of the form
  546. .br
  547. .ti +5
  548. command >&! file
  549. .br
  550. exists, and is used when
  551. .I noclobber
  552. is set and
  553. .I file
  554. already exists.
  555. .FE
  556. .PP
  557. Finally, it is possible to use the form
  558. .DS
  559. command >> file
  560. .DE
  561. to place output at the end of an existing file.\(dg
  562. .FS
  563. \(dg If
  564. .I noclobber
  565. is set, then an error will result if
  566. .I file
  567. does not exist, otherwise the shell will create
  568. .I file
  569. if it doesn't exist.
  570. A form
  571. .br
  572. .ti +5
  573. command >>! file
  574. .br
  575. makes it not be an error for file to not exist when
  576. .I noclobber
  577. is set.
  578. .FE
  579. .NH 2
  580. Jobs; Background, Foreground, or Suspended
  581. .PP
  582. When one or more commands
  583. are typed together as a pipeline or as a sequence of commands separated by
  584. semicolons, a single
  585. .I job
  586. is created by the shell consisting of these commands together as a unit.
  587. Single commands without pipes or semicolons create the simplest jobs.
  588. Usually, every line typed to the shell creates a job.
  589. Some lines that create jobs (one per line) are
  590. .DS
  591. sort < data
  592. ls \-s | sort \-n | head \-5
  593. mail harold
  594. .DE
  595. .PP
  596. If the metacharacter `&' is typed
  597. at the end of the commands, then the job is started as a
  598. .I background
  599. job.  This means that the shell does not wait for it to complete but
  600. immediately prompts and is ready for another command.  The job runs
  601. .I "in the background"
  602. at the same time that normal jobs, called
  603. .I foreground
  604. jobs, continue to be read and executed by the shell one at a time.
  605. Thus
  606. .DS
  607. du > usage &
  608. .DE
  609. would run the
  610. .I du
  611. program, which reports on the disk usage of your working directory (as well as
  612. any directories below it), put the output into the file `usage' and return
  613. immediately with a prompt for the next command without out waiting for
  614. .I du
  615. to finish.  The
  616. .I du
  617. program would continue executing in the background
  618. until it finished, even though you can type and execute more commands in the
  619. mean time.
  620. When a background
  621. job terminates, a message is typed by the shell just before the next prompt
  622. telling you that the job has completed.
  623. In the following example the
  624. .I du
  625. job finishes sometime during the
  626. execution of the
  627. .I mail
  628. command and its completion is reported just before
  629. the prompt after the
  630. .I mail
  631. job is finished.
  632. .DS
  633. % du > usage &
  634. [1] 503
  635. % mail bill
  636. How do you know when a background job is finished?
  637. EOT
  638. .ta 1.75i
  639. [1] \- Done    du > usage
  640. %
  641. .so tabs
  642. .DE
  643. If the job did not terminate normally the `Done' message might say
  644. something else like `Killed'.
  645. If you want the 
  646. terminations of background jobs to be reported at the time they occur
  647. (possibly interrupting the output of other foreground jobs), you can set
  648. the
  649. .I notify
  650. variable.  In the previous example this would mean that the
  651. `Done' message might have come right in the middle of the message to
  652. Bill.
  653. Background jobs are unaffected by any signals from the keyboard like
  654. the \s-2STOP\s0, \s-2INTERRUPT\s0, or \s-2QUIT\s0 signals mentioned earlier.
  655. .PP
  656. Jobs are recorded in a table inside the shell until they terminate.
  657. In this table, the shell remembers the command names, arguments and the
  658. .I "process numbers"
  659. of all commands in the job as well as the working directory where the job was
  660. started.
  661. Each job in the table is either running
  662. .I "in the foreground"
  663. with the shell waiting for it to terminate, running
  664. .I "in the background,"
  665. or
  666. .I suspended.
  667. Only one job can be running in the foreground at one time, but several
  668. jobs can be suspended or running in the background at once.  As each job
  669. is started, it is assigned a small identifying
  670. number called the
  671. .I "job number"
  672. which can be used later to refer to the job in the commands described below.
  673. Job numbers remain
  674. the same until the job terminates and then are re-used.
  675. .PP
  676. When a job is started in the backgound using `&', its number, as well
  677. as the process numbers of all its (top level) commands, is typed by the shell
  678. before prompting you for another command. For example,
  679. .DS
  680. % ls \-s | sort \-n > usage &
  681. [2] 2034 2035
  682. %
  683. .DE
  684. runs the `ls' program with the `\-s' options, pipes this output into
  685. the `sort' program with the `\-n' option which puts its output into the
  686. file `usage'.
  687. Since the `&' was at the end of the line, these two programs were started
  688. together as a background job.  After starting the job, the shell prints
  689. the job number in brackets (2 in this case) followed by the process number
  690. of each program started in the job.  Then the shell immediates prompts for
  691. a new command, leaving the job running simultaneously.
  692. .PP
  693. As mentioned in section 1.8, foreground jobs become
  694. .I suspended
  695. by typing ^Z
  696. which sends a \s-2STOP\s0 signal to the currently running
  697. foreground job.  A background job can become suspended by using the
  698. .I stop
  699. command described below.  When jobs are suspended they merely stop
  700. any further progress until started again, either in the foreground
  701. or the backgound.  The shell notices when a job becomes stopped and
  702. reports this fact, much like it reports the termination of background jobs.
  703. For foreground jobs this looks like
  704. .DS
  705. % du > usage
  706. ^Z
  707. Stopped
  708. %
  709. .DE
  710. `Stopped' message is typed by the shell when it notices that the
  711. .I du
  712. program stopped.
  713. For background jobs, using the
  714. .I stop
  715. command, it is
  716. .DS
  717. % sort usage &
  718. [1] 2345
  719. % stop %1
  720. .ta 1.75i
  721. [1] + Stopped (signal)    sort usage
  722. %
  723. .so tabs
  724. .DE
  725. Suspending foreground jobs can be very useful when you need to temporarily
  726. change what you are doing (execute other commands) and then return to
  727. the suspended job.  Also, foreground jobs can be suspended and then
  728. continued as background jobs using the
  729. .I bg
  730. command, allowing you to continue other work and
  731. stop waiting for the foreground job to finish.  Thus
  732. .DS
  733. % du > usage
  734. ^Z
  735. Stopped
  736. % bg
  737. [1] du > usage &
  738. %
  739. .DE
  740. starts `du' in the foreground, stops it before it finishes, then continues
  741. it in the background allowing more foreground commands to be executed.
  742. This is especially helpful
  743. when a foreground job ends up taking longer than you expected and you
  744. wish you had started it in the backgound in the beginning.
  745. .PP
  746. All
  747. .I "job control"
  748. commands can take an argument that identifies a particular
  749. job.
  750. All job name arguments begin with the character `%', since some of the
  751. job control commands also accept process numbers (printed by the
  752. .I ps
  753. command.)
  754. The default job (when no argument is given) is called the
  755. .I current
  756. job and is identified by a `+' in the output of the
  757. .I jobs
  758. command, which shows you which jobs you have.
  759. When only one job is stopped or running in the background (the usual case)
  760. it is always the current job thus no argument is needed.
  761. If a job is stopped while running in the foreground it becomes the
  762. .I current
  763. job and the existing current job becomes the
  764. .I previous
  765. job \- identified by a `\-' in the output of
  766. .I jobs.
  767. When the current job terminates, the previous job becomes the current job.
  768. When given, the argument is either `%\-' (indicating
  769. the previous job); `%#', where # is the job number; 
  770. `%pref' where pref is some unique prefix of the command name
  771. and arguments of one of the jobs; or `%?' followed by some string found
  772. in only one of the jobs.
  773. .PP
  774. The
  775. .I jobs
  776. command types the table of jobs, giving the job number,
  777. commands and status (`Stopped' or `Running') of each backgound or
  778. suspended job.  With the `\-l' option the process numbers are also
  779. typed.
  780. .DS
  781. % du > usage &
  782. [1] 3398
  783. % ls \-s | sort \-n > myfile &
  784. [2] 3405
  785. % mail bill
  786. ^Z
  787. Stopped
  788. % jobs
  789. .ta 1.75i
  790. [1] \(mi Running    du > usage
  791. [2]    Running    ls \-s | sort \-n > myfile
  792. [3] \(pl Stopped    mail bill
  793. % fg %ls
  794. ls \-s | sort \-n > myfile
  795. % more myfile
  796. .so tabs
  797. .DE
  798. .PP
  799. The
  800. .I fg
  801. command runs a suspended or background job in the foreground.  It is
  802. used to restart a previously suspended job or change a background job
  803. to run in the foreground (allowing signals or input from the terminal).
  804. In the above example we used
  805. .I fg
  806. to change the `ls' job from the
  807. background to the foreground since we wanted to wait for it to
  808. finish before looking at its output file.
  809. The
  810. .I bg
  811. command runs a suspended job in the background.  It is usually used
  812. after stopping the currently running foreground job with the
  813. \s-2STOP\s0 signal.  The combination of the \s-2STOP\s0 signal and the
  814. .I bg
  815. command changes a foreground job into a background job.
  816. The
  817. .I stop
  818. command suspends a background job.
  819. .PP
  820. The
  821. .I kill
  822. command terminates a background or suspended job immediately.
  823. In addition to jobs, it may be given process numbers as arguments,
  824. as printed by
  825. .I ps.
  826. Thus, in the example above, the running
  827. .I du
  828. command could have been terminated by the command
  829. .DS
  830. % kill %1
  831. .ta 1.75i
  832. [1]  Terminated    du > usage
  833. %
  834. .so tabs
  835. .DE
  836. .PP
  837. The
  838. .I notify
  839. command (not the variable mentioned earlier) indicates that the termination
  840. of a specific job should be
  841. reported at the time it finishes instead of waiting for the next prompt.
  842. .PP
  843. If a job running in the background tries to read input from the terminal
  844. it is automatically stopped.  When such a job is then run in the
  845. foreground, input can be given to the job.  If desired, the job can
  846. be run in the background again until it requests input again.
  847. This is illustrated in the following sequence where the `s' command in the
  848. text editor might take a long time.
  849. .ID
  850. .nf
  851. % ed bigfile
  852. 120000
  853. 1,$s/thisword/thatword/
  854. ^Z
  855. Stopped
  856. % bg
  857. [1] ed bigfile &
  858.  . . .  some foreground commands
  859. .ta 1.75i
  860. [1] Stopped (tty input)    ed bigfile
  861. % fg
  862. ed bigfile
  863. w
  864. 120000
  865. q
  866. .so tabs
  867. .DE
  868. So after the `s' command was issued, the `ed' job was stopped with ^Z
  869. and then put in the background using
  870. .I bg.
  871. Some time later when the `s' command was finished,
  872. .I ed
  873. tried to read another command and was stopped because jobs
  874. in the backgound cannot read from the terminal.  The
  875. .I fg
  876. command returned the `ed' job to the foreground where it could once again
  877. accept commands from the terminal.
  878. .PP
  879. The command
  880. .DS
  881. stty tostop
  882. .DE
  883. causes all background jobs run on your terminal to stop
  884. when they are about to
  885. write output to the terminal.  This prevents messages from background
  886. jobs from interrupting foreground job output and allows you to run
  887. a job in the background without losing terminal output.  It also
  888. can be used for interactive programs that sometimes have long
  889. periods without interaction.  Thus each time it outputs a prompt for more
  890. input it will stop before the prompt.  It can then be run in the
  891. foreground using
  892. .I fg,
  893. more input can be given and, if necessary stopped and returned to
  894. the background.  This
  895. .I stty
  896. command might be a good thing to put in your
  897. .I \&.login
  898. file if you do not like output from background jobs interrupting
  899. your work.  It also can reduce the need for redirecting the output
  900. of background jobs if the output is not very big:
  901. .DS
  902. % stty tostop
  903. % wc hugefile &
  904. [1] 10387
  905. % ed text
  906. \&. . . some time later
  907. q
  908. .ta 1.75i
  909. [1] Stopped (tty output)    wc hugefile
  910. % fg wc
  911. wc hugefile
  912.    13371   30123   302577
  913. % stty \-tostop
  914. .so tabs
  915. .DE
  916. Thus after some time the `wc' command, which counts the lines, words
  917. and characters in a file, had one line of output.  When it tried to
  918. write this to the terminal it stopped.  By restarting it in the
  919. foreground we allowed it to write on the terminal exactly when we were
  920. ready to look at its output.
  921. Programs which attempt to change the mode of the terminal will also
  922. block, whether or not
  923. .I tostop
  924. is set, when they are not in the foreground, as
  925. it would be very unpleasant to have a background job change the state
  926. of the terminal.
  927. .PP
  928. Since the
  929. .I jobs
  930. command only prints jobs started in the currently executing shell,
  931. it knows nothing about background jobs started in other login sessions
  932. or within shell files.  The
  933. .I ps
  934. can be used in this case to find out about background jobs not started
  935. in the current shell.
  936. .NH 2
  937. Working Directories
  938. .PP
  939. As mentioned in section 1.6, the shell is always in a particular
  940. .I "working directory."
  941. The `change directory' command
  942. .I chdir
  943. (its
  944. short form
  945. .I cd
  946. may also be used)
  947. changes the working directory of the shell,
  948. that is, changes the directory you
  949. are located in.
  950. .PP
  951. It is useful to make a directory for each project you wish to work on
  952. and to place all files related to that project in that directory.
  953. The `make directory' command,
  954. .I mkdir,
  955. creates a new directory.
  956. The
  957. .I pwd
  958. (`print working directory') command
  959. reports the absolute pathname of the working directory of the shell,
  960. that is, the directory you are
  961. located in.
  962. Thus in the example below:
  963. .DS
  964. % pwd
  965. /usr/bill
  966. % mkdir newpaper
  967. % chdir newpaper
  968. % pwd
  969. /usr/bill/newpaper
  970. %
  971. .DE
  972. the user has created and moved to the
  973. directory
  974. .I newpaper.
  975. where, for example, he might
  976. place a group of related files.
  977. .PP
  978. No matter where you have moved to in a directory hierarchy,
  979. you can return to your `home' login directory by doing just
  980. .DS
  981. cd
  982. .DE
  983. with no arguments.
  984. The name `..' always means the directory above the current one in
  985. the hierarchy, thus
  986. .DS
  987. cd ..
  988. .DE
  989. changes the shell's working directory to the one directly above the
  990. current one.
  991. The name `..' can be used in any
  992. pathname, thus,
  993. .DS
  994. cd ../programs
  995. .DE
  996. means
  997. change to the directory `programs' contained in the directory
  998. above the current one.
  999. If you have several directories for different
  1000. projects under, say, your home directory,
  1001. this shorthand notation
  1002. permits you to switch easily between them.
  1003. .PP
  1004. The shell always remembers the pathname of its current working directory in
  1005. the variable
  1006. .I cwd.
  1007. The shell can also be requested to remember the previous directory when
  1008. you change to a new working directory.  If the `push directory' command
  1009. .I pushd
  1010. is used in place of the
  1011. .I cd
  1012. command, the shell saves the name of the current working directory
  1013. on a
  1014. .I "directory stack"
  1015. before changing to the new one.
  1016. You can see this list at any time by typing the `directories'
  1017. command
  1018. .I dirs.
  1019. .ID
  1020. .nf
  1021. % pushd newpaper/references
  1022. ~/newpaper/references  ~
  1023. % pushd /usr/lib/tmac
  1024. /usr/lib/tmac  ~/newpaper/references  ~
  1025. % dirs
  1026. /usr/lib/tmac  ~/newpaper/references  ~
  1027. % popd
  1028. ~/newpaper/references  ~
  1029. % popd
  1030. ~
  1031. %
  1032. .DE
  1033. The list is printed in a horizontal line, reading left to right,
  1034. with a tilde (~) as
  1035. shorthand for your home directory\(emin this case `/usr/bill'.
  1036. The directory stack is printed whenever there is more than one
  1037. entry on it and it changes.
  1038. It is also printed by a
  1039. .I dirs
  1040. command.
  1041. .I Dirs
  1042. is usually faster and more informative than
  1043. .I pwd
  1044. since it shows the current working directory as well as any
  1045. other directories remembered in the stack.
  1046. .PP
  1047. The
  1048. .I pushd
  1049. command with no argument
  1050. alternates the current directory with the first directory in the
  1051. list.
  1052. The `pop directory'
  1053. .I popd
  1054. command without an argument returns you to the directory you were in prior to
  1055. the current one, discarding the previous current directory from the
  1056. stack (forgetting it).
  1057. Typing
  1058. .I popd
  1059. several times in a series takes you backward through the directories
  1060. you had been in (changed to) by
  1061. .I pushd
  1062. command.
  1063. There are other options to
  1064. .I pushd
  1065. and
  1066. .I popd
  1067. to manipulate the contents of the directory stack and to change
  1068. to directories not at the top of the stack; see the
  1069. .I csh
  1070. manual page for details.
  1071. .PP
  1072. Since the shell remembers the working directory in which each job
  1073. was started, it warns you when you might be confused by restarting
  1074. a job in the foreground which has a different working directory than the
  1075. current working directory of the shell.  Thus if you start a background
  1076. job, then change the shell's working directory and then cause the
  1077. background job to run in the foreground, the shell warns you that the
  1078. working directory of the currently running foreground job is different
  1079. from that of the shell.
  1080. .DS
  1081. % dirs \-l
  1082. /mnt/bill
  1083. % cd myproject
  1084. % dirs
  1085. ~/myproject
  1086. % ed prog.c
  1087. 1143
  1088. ^Z
  1089. Stopped
  1090. % cd ..
  1091. % ls
  1092. myproject
  1093. textfile
  1094. % fg
  1095. ed prog.c (wd: ~/myproject)
  1096. .DE
  1097. This way the shell warns you when there
  1098. is an implied change of working directory, even though no cd command was
  1099. issued.  In the above example the `ed' job was still in `/mnt/bill/project'
  1100. even though the shell had changed to `/mnt/bill'.
  1101. A similar warning is given when such a foreground job
  1102. terminates or is suspended (using the \s-2STOP\s0 signal) since
  1103. the return to the shell again implies a change of working directory.
  1104. .DS
  1105. % fg
  1106. ed prog.c (wd: ~/myproject)
  1107.  . . . after some editing
  1108. q
  1109. (wd now: ~)
  1110. %
  1111. .DE
  1112. These messages are sometimes confusing if you use programs that change
  1113. their own working directories, since the shell only remembers which
  1114. directory a job is started in, and assumes it stays there.
  1115. The `\-l' option of
  1116. .I jobs
  1117. will type the working directory
  1118. of suspended or background jobs when it is different
  1119. from the current working directory of the shell.
  1120. .NH 2
  1121. Useful built-in commands
  1122. .PP
  1123. We now give a few of the useful built-in commands of the shell describing
  1124. how they are used.
  1125. .PP
  1126. The
  1127. .I alias
  1128. command described above is used to assign new aliases and to show the
  1129. existing aliases.
  1130. With no arguments it prints the current aliases.
  1131. It may also be given only one argument such as
  1132. .DS
  1133. alias ls
  1134. .DE
  1135. to show the current alias for, e.g., `ls'.
  1136. .PP
  1137. The
  1138. .I echo
  1139. command prints its arguments.
  1140. It is often used in
  1141. .I "shell scripts"
  1142. or as an interactive command
  1143. to see what filename expansions will produce.
  1144. .PP
  1145. The
  1146. .I history
  1147. command will show the contents of the history list.
  1148. The numbers given with the history events can be used to reference
  1149. previous events which are difficult to reference using the
  1150. contextual mechanisms introduced above.
  1151. There is also a shell variable called
  1152. .I prompt.
  1153. By placing a `!' character in its value the shell will there substitute
  1154. the number of the current command in the history list.
  1155. You can use this number to refer to this command in a history substitution.
  1156. Thus you could
  1157. .DS
  1158. set prompt=\'\e! % \'
  1159. .DE
  1160. Note that the `!' character had to be
  1161. .I escaped
  1162. here even within `\'' characters.
  1163. .PP
  1164. The
  1165. .I limit
  1166. command is used to restrict use of resources.
  1167. With no arguments it prints the current limitations:
  1168. .DS
  1169. .ta 1i
  1170. cputime    unlimited
  1171. filesize    unlimited
  1172. datasize    5616 kbytes
  1173. stacksize    512 kbytes
  1174. coredumpsize    unlimited
  1175. .so tabs
  1176. .DE
  1177. Limits can be set, e.g.:
  1178. .DS
  1179. limit coredumpsize 128k
  1180. .DE
  1181. Most reasonable units abbreviations will work; see the
  1182. .I csh
  1183. manual page for more details.
  1184. .PP
  1185. The
  1186. .I logout
  1187. command can be used to terminate a login shell which has
  1188. .I ignoreeof
  1189. set.
  1190. .PP
  1191. The
  1192. .I rehash
  1193. command causes the shell to recompute a table of where commands are
  1194. located.  This is necessary if you add a command to a directory
  1195. in the current shell's search path and wish the shell to find it,
  1196. since otherwise the hashing algorithm may tell the shell that the
  1197. command wasn't in that directory when the hash table was computed.
  1198. .PP
  1199. The
  1200. .I repeat
  1201. command can be used to repeat a command several times.
  1202. Thus to make 5 copies of the file
  1203. .I one
  1204. in the file
  1205. .I five
  1206. you could do
  1207. .DS
  1208. repeat 5 cat one >> five
  1209. .DE
  1210. .PP
  1211. The
  1212. .I setenv
  1213. command can be used
  1214. to set variables in the environment.
  1215. Thus
  1216. .DS
  1217. setenv TERM adm3a
  1218. .DE
  1219. will set the value of the environment variable \s-2TERM\s0
  1220. to
  1221. `adm3a'.
  1222. A user program
  1223. .I printenv
  1224. exists which will print out the environment.
  1225. It might then show:
  1226. .DS
  1227. % printenv
  1228. HOME=/usr/bill
  1229. SHELL=/bin/csh
  1230. PATH=:/usr/ucb:/bin:/usr/bin:/usr/local
  1231. TERM=adm3a
  1232. USER=bill
  1233. %
  1234. .DE
  1235. .PP
  1236. The
  1237. .I source
  1238. command can be used to force the current shell to read commands from
  1239. a file.
  1240. Thus
  1241. .DS
  1242. source .cshrc
  1243. .DE
  1244. can be used after editing in a change to the
  1245. .I \&.cshrc
  1246. file which you wish to take effect right away.
  1247. .PP
  1248. The
  1249. .I time
  1250. command can be used to cause a command to be timed no matter how much
  1251. \s-2CPU\s0 time it takes.
  1252. Thus
  1253. .DS
  1254. % time cp /etc/rc /usr/bill/rc
  1255. 0.0u 0.1s 0:01 8% 2+1k 3+2io 1pf+0w
  1256. % time wc /etc/rc /usr/bill/rc
  1257.      52    178   1347 /etc/rc
  1258.      52    178   1347 /usr/bill/rc
  1259.     104    356   2694 total
  1260. 0.1u 0.1s 0:00 13% 3+3k 5+3io 7pf+0w
  1261. %
  1262. .DE
  1263. indicates that the
  1264. .I cp
  1265. command used a negligible amount of user time (u)
  1266. and about 1/10th of a system time (s); the elapsed time was 1 second (0:01),
  1267. there was an average memory usage of 2k bytes of program space and 1k
  1268. bytes of data space over the cpu time involved (2+1k); the program
  1269. did three disk reads and two disk writes (3+2io), and took one page fault
  1270. and was not swapped (1pf+0w).
  1271. The word count command
  1272. .I wc
  1273. on the other hand used 0.1 seconds of user time and 0.1 seconds of system
  1274. time in less than a second of elapsed time.
  1275. The percentage `13%' indicates that over the period when it was active
  1276. the command `wc' used an average of 13 percent of the available \s-2CPU\s0
  1277. cycles of the machine.
  1278. .PP
  1279. The
  1280. .I unalias
  1281. and
  1282. .I unset
  1283. commands can be used
  1284. to remove aliases and variable definitions from the shell, and
  1285. .I unsetenv
  1286. removes variables from the environment.
  1287. .NH 2
  1288. What else?
  1289. .PP
  1290. This concludes the basic discussion of the shell for terminal users.
  1291. There are more features of the shell to be discussed here, and all
  1292. features of the shell are discussed in its manual pages.
  1293. One useful feature which is discussed later is the
  1294. .I foreach
  1295. built-in command which can be used to run the same command
  1296. sequence with a number of different arguments.
  1297. .PP
  1298. If you intend to use \s-2UNIX\s0 a lot you you should look through
  1299. the rest of this document and the csh manual pages (section1) to become familiar
  1300. with the other facilities which are available to you.
  1301. .bp
  1302.