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