|
|
1.1 root 1:
2:
3: sh Command sh
4:
5:
6:
7:
8: Command language interpreter
9:
10: sshh [-cceeiikknnssttuuvvxx] _t_o_k_e_n ...
11:
12: sh, also called the shell, is the default COHERENT command lan-
13: guage interpreter. Other command languages can be provided on a
14: per-user basis. The tutorial included in this manual describes
15: the shell in detail.
16:
17: The shell reads commands from the terminal or from a file and in-
18: terprets them. A command may be either a program or a text file
19: containing other commands. Shell constructs provide control flow
20: logic. Commands can contain patterns, which the shell expands
21: into file names. The shell can redirect input and output.
22:
23: ***** Commands *****
24:
25: A command consists of a command name and optional command ar-
26: guments, called tokens. A token is a string of graphic charac-
27: ters separated by spaces or tabs. Normally, the first token in a
28: command is the command name.
29:
30: Commands are combined with the pipe operator `|' to form
31: pipelines. In the pipeline
32:
33:
34: a | b
35:
36:
37: the shell passes the standard output of a to the standard input
38: of b. The shell runs each command in the pipeline as a separate
39: process, and waits for the last command to finish before con-
40: tinuing.
41:
42: Commands and pipelines can be joined into a sequence by using the
43: tokens `;', `&', `&&', and `||', in addition to newlines. The
44: shell executes commands or pipelines separated by newlines or by
45: `;' sequentially. For example,
46:
47:
48: a | b ; c | d
49:
50:
51: first executes the pipeline a | b and then executes the pipeline
52: c | d. The shell executes any command followed by `&'
53: asynchronously as a background (or detached) process and prints
54: its process id. The shell executes the command following the
55: token `&&' only if the preceding command returns a zero exit
56: status, signifying success. Similarly, it executes the command
57: following `||' only if the preceding command returns a nonzero
58: exit status, signifying failure. Newline, `;' and `&' bind less
59: tightly than `&&' and `||'; the shell parses command lines from
60: left to right if separators bind equally.
61:
62:
63:
64: COHERENT Lexicon Page 1
65:
66:
67:
68:
69: sh Command sh
70:
71:
72:
73: ***** I/O Redirection *****
74:
75: The standard input, standard output, and standard error streams
76: are normally connected to the terminal. A pipeline attaches the
77: output of one command to the input of another command. In addi-
78: tion, the operators `>', `>>', `<', and `<<' redirect the stan-
79: dard output and the standard input.
80:
81: Output redirection sends standard output to file rather than to
82: the terminal:
83:
84:
85: >file
86:
87:
88: creates file if it does not exist, and destroys its previous con-
89: tents if it does exist. The operator
90:
91:
92: >>file
93:
94:
95: appends standard output to an existing file, or creates file if
96: it does not exist.
97:
98: In input redirection,
99:
100:
101: <file
102:
103:
104: accepts standard input from file rather than from the terminal.
105: The input redirection operator
106:
107:
108: <<token
109:
110:
111: accepts standard input from the shell input until the next line
112: containing only token in the shell input. The shell input be-
113: tween tokens is called a here document. The shell will perform
114: parameter substitution on the here document unless the leading
115: token is quoted; parameter substitution and quoting are described
116: below.
117:
118: The standard input and output may also be redirected to duplicate
119: other file descriptors. The operator `<&_n' duplicates the stan-
120: dard input from file descriptor n, and `>&_n' duplicates the stan-
121: dard output. The operators `<&-' and `>&-' close the standard
122: input and output.
123:
124: Other descriptors may be redirected by preceding the `<' or `>'
125: with the digit of the descriptor to be redirected. For example,
126:
127:
128:
129:
130: COHERENT Lexicon Page 2
131:
132:
133:
134:
135: sh Command sh
136:
137:
138:
139:
140: 2>&1
141:
142:
143: redirects file descriptor 2 (the standard error) to file descrip-
144: tor 1 (the standard output). The system call dup performs file
145: descriptor duplication.
146:
147: Each command executed as a foreground process inherits the file
148: descriptors and signal traps (described below) of the invoking
149: shell, modified by any specified redirection. Background proces-
150: ses take input from the null device /dev/null (unless redirected)
151: and ignore interrupt and quit signals.
152:
153: ***** File Name Patterns *****
154:
155: The shell interprets an input token containing any of the special
156: characters `?', `*', or `[' as a file name pattern. The question
157: mark `?' matches any single character except newline. The as-
158: terisk `*' matches a string of non-newline characters of any
159: length (including zero). Square brackets `[ ]' enclose alter-
160: natives to match a single character; as in ed, ranges of charac-
161: ters can be separated by `-'. The slash `/' and leading period
162: `.' must be matched explicitly in a pattern. The shell generates
163: an alphabetized list of file names matching the pattern to
164: replace the token. It passes the token unchanged if it finds no
165: match.
166:
167: In addition, the characters `\', `"', and `'' remove the special
168: meaning of other characters. The backslash `\' quotes the
169: following character. The shell ignores a backslash immediately
170: followed by a newline, called a concealed newline. A pair of
171: apostrophes ' ' prevents interpretation of any enclosed special
172: characters. A pair of quotation marks " " has the same effect,
173: except that parameter substitution and command output substitu-
174: tion (described below) occur within quotation marks.
175:
176: ***** Scripts *****
177:
178: Shell commands can be stored in a file, or script. The command
179:
180:
181: sh _s_c_r_i_p_t [ _p_a_r_a_m_e_t_e_r ... ]
182:
183:
184: executes the commands in script with a new subshell sh. Each
185: parameter is a value for a positional parameter, as described
186: below. If script has been made executable with the chmod com-
187: mand, the sh may be omitted.
188:
189: Formal parameters of the form `$_n', where n ranges from zero
190: through nine, represent positional parameters in a script. The
191: parameter `$0' gives the name of the script. If no corresponding
192: actual parameter is given on the command line, the shell sub-
193: stitutes the null string for the positional parameter. The shell
194:
195:
196: COHERENT Lexicon Page 3
197:
198:
199:
200:
201: sh Command sh
202:
203:
204:
205: substitutes the actual values of all positional parameters for
206: the reference `$*'.
207:
208: Commands in a script can also be executed with the . (dot) com-
209: mand. It resembles the sh command, but the current shell ex-
210: ecutes the script commands without creating a new subshell or a
211: new environment; positional parameters are not allowed.
212:
213: ***** Variables *****
214:
215: Shell variables are names which may be assigned string values on
216: a command line, in the form
217:
218:
219: _n_a_m_e=_v_a_l_u_e
220:
221:
222: The name must begin with a letter, and may contain letters,
223: digits, and underscores `_'. In shell input, `$_n_a_m_e' or
224: `${_n_a_m_e}' represents the value of the variable. If an assignment
225: precedes a command on the same command line, the effect of the
226: assignment is local to the command; otherwise, the effect is per-
227: manent. For example,
228:
229:
230: kp=one testproc
231:
232:
233: assigns variable kp the value one only for the execution of the
234: script testproc.
235:
236: The shell sets the following variables:
237:
238: # The number of actual positional parameters given to the cur-
239: rent command.
240:
241: @ The list of positional parameters ``$1 $2 ...''.
242:
243: * The list of positional parameters ``$1'' ``$2'' ... (the same
244: as `$@' unless quoted).
245:
246: - Options set in the invocation of the shell or by the set com-
247: mand.
248:
249: ? The exit status returned by the last command.
250:
251: ! The process number of the last command invoked with `&'.
252:
253: $ The process number of the current shell.
254:
255: The shell also references the following variables:
256:
257: HHOOMMEE Initial working directory; usually specified in the
258: password file /etc/passwd.
259:
260:
261:
262: COHERENT Lexicon Page 4
263:
264:
265:
266:
267: sh Command sh
268:
269:
270:
271: IIFFSS Delimiters for tokens; usually space, tab and newline.
272:
273: LLAASSTTEERRRROORR
274: Name of last command returning nonzero exit status.
275:
276: MMAAIILL Checked at the end of each command. If file specified in
277: this variable is new since last command, the shell prints
278: ``You have mail.'' on the user's terminal.
279:
280: PPAATTHH Colon-separated list of directories searched for com-
281: mands.
282:
283: PPSS11 First prompt string, usually `$'.
284:
285: PPSS22 Second prompt string, usually `>'. Used when the shell
286: expects more input, such as when an open quote has been
287: typed but a close quote has not been typed, or within a
288: shell construct.
289:
290: The special forms `${_n_a_m_e_c_t_o_k_e_n}', where c is one of the charac-
291: ters `-', `=', `+', or `?', perform conditional parameter sub-
292: stitution. The shell replaces the form `${_n_a_m_e-_t_o_k_e_n}' by the
293: value of name if it is set and by token otherwise. The `=' form
294: has the same effect, but also sets the value of name to token if
295: it was not set previously. The shell replaces the `+' form by
296: token if the given name is set. The shell replaces the `?' form
297: by the value of name if set, and otherwise prints token and exits
298: from the shell.
299:
300: ***** Command Output Substitution *****
301:
302: The shell can use the output of a command as shell input (as com-
303: mand arguments, for example) by enclosing the command in grave
304: characters ` `. To list directories given in a file dirs, use
305: the command
306:
307:
308: ls -l `cat dirs`
309:
310:
311: ***** Constructs *****
312:
313: The shell provides control over execution of commands by the
314: bbrreeaakk, ccaassee, ccoonnttiinnuuee, ffoorr, iiff, uunnttiill, and wwhhiillee constructs. The
315: shell recognizes each reserved word only if it occurs unquoted as
316: the first token of a command. This implies that a separator must
317: precede each reserved word in the following constructs. For ex-
318: ample, newline or `;' must precede do in the for construct.
319:
320: bbrreeaakk [_n]
321: Exit from for, until, or while. If n is given, exit from n
322: levels.
323:
324: ccaassee _t_o_k_e_n iinn [ _p_a_t_t_e_r_n [ | _p_a_t_t_e_r_n ] ...) _s_e_q_u_e_n_c_e;; ] ... eessaacc
325: Check the token against each pattern, and execute the se-
326:
327:
328: COHERENT Lexicon Page 5
329:
330:
331:
332:
333: sh Command sh
334:
335:
336:
337: quence associated with the first matching pattern.
338:
339: ccoonnttiinnuuee [_n]
340: Branch to the end of the nth enclosing for, until, or while
341: construct.
342:
343: ffoorr _n_a_m_e [ iinn _t_o_k_e_n ... ] ddoo _s_e_q_u_e_n_c_e ddoonnee
344: Execute sequence once for each member of the specified token
345: list. On each iteration, the name takes the value of the
346: next token in the list. If the in clause is omitted, $@ is
347: assumed. For example, to list all files ending with .c:
348:
349: for i in *.c
350: do cat $i
351: done
352:
353:
354: iiff _s_e_q_1 tthheenn _s_e_q_2 [ eelliiff _s_e_q_3 tthheenn _s_e_q_4 ] ... [ eellssee _s_e_q_5 ] ffii
355: Execute seq1. If the exit status is zero, execute seq2. If
356: not, execute the optional seq3 if given. If its exit status
357: is zero, execute seq4 and so on. If the exit status of each
358: tested sequence is nonzero, execute seq5.
359:
360: uunnttiill _s_e_q_u_e_n_c_e_1 [ ddoo _s_e_q_u_e_n_c_e_2 ] ddoonnee
361: Execute sequence2 until the execution of sequence1 results
362: in an exit status of zero.
363:
364: wwhhiillee _s_e_q_u_e_n_c_e_1 [ ddoo _s_e_q_u_e_n_c_e_2 ] ddoonnee
365: Execute sequence2 as long as the execution of sequence1
366: results in an exit status of zero.
367:
368: (_s_e_q_u_e_n_c_e)
369: Execute the sequence within a subshell. This allows the se-
370: quence to change the current directory, for example, and not
371: affect the enclosing environment.
372:
373: {_s_e_q_u_e_n_c_e}
374: Braces simply enclose a sequence.
375:
376: ***** Special Commands *****
377:
378: The shell usually executes commands by a fork system call, which
379: creates another process. However, the shell executes the com-
380: mands in this section either directly or with an exec system
381: call. The Lexicon describes fork and exec.
382:
383: . _s_c_r_i_p_t
384: Read and execute commands from script. Positional
385: parameters are not allowed. The shell searches PATH to find
386: the given script.
387:
388: : [_t_o_k_e_n ...]
389: A colon `:' indicates a ``partial comment''. The shell nor-
390: mally ignores all commands on a line that begins with a
391: colon, except for redirection and such symbols as $, {, ?,
392:
393:
394: COHERENT Lexicon Page 6
395:
396:
397:
398:
399: sh Command sh
400:
401:
402:
403: etc.
404:
405: # A complete comment: if # is the first character on a line,
406: the shell ignores all text that follows on that line.
407:
408: ccdd [_d_i_r]
409: Change the working directory to dir. If no argument is
410: given, change to the home directory.
411:
412: eevvaall [_t_o_k_e_n ...]
413: Evaluate each token and treat the result as shell input.
414:
415: eexxeecc [_c_o_m_m_a_n_d]
416: Execute command directly rather than performing fork. This
417: terminates the current shell.
418:
419: eexxiitt [_s_t_a_t_u_s]
420: Set the exit status to status, if given; otherwise, the pre-
421: vious status is unchanged. If the shell is not interactive,
422: terminate it.
423:
424: eexxppoorrtt [_n_a_m_e ...]
425: The shell executes each command in an environment, which is
426: essentially a set of shell variable names and corresponding
427: string values. The shell inherits an environment when in-
428: voked, and normally it passes the same environment to each
429: command it invokes. export specifies that the shell should
430: pass the modified value of each given name to the environ-
431: ment of subsequent commands. When no name is given, the
432: shell prints the name and value of each variable marked for
433: export.
434:
435: rreeaadd _n_a_m_e ...
436: Read a line from the standard input and assign each token of
437: the input to the corresponding shell variable name. If the
438: input contains fewer tokens than the name list, assign the
439: null string to extra variables. If the input contains more
440: tokens, assign the last name the remainder of the input.
441:
442: rreeaaddoonnllyy [_n_a_m_e ...]
443: Mark each shell variable name as a read only variable. Sub-
444: sequent assignments to read only variables will not be per-
445: mitted. With no arguments, print the name and value of each
446: read only variable.
447:
448: sseett [-cceeiikknnssttuuvvxx [_n_a_m_e ...] ]
449: Set listed flag. If name list is provided, set shell
450: variables name to values of positional parameters beginning
451: with $1.
452:
453: sshhiifftt
454: Rename positional parameter 1 to current value of $2, and so
455: on.
456:
457:
458:
459:
460: COHERENT Lexicon Page 7
461:
462:
463:
464:
465: sh Command sh
466:
467:
468:
469: ttiimmeess
470: Print the total user and system times for all executed
471: processes.
472:
473: ttrraapp [_c_o_m_m_a_n_d] [_n ...]
474: Execute command if the shell receives signal n. If command
475: is omitted, reset traps to original values. To ignore a
476: signal, pass null string as command. With n zero, execute
477: command when the shell exits. With no arguments, print the
478: current trap settings.
479:
480: uummaasskk [_n_n_n]
481: Set user file creation mask to nnn. If no argument is
482: given, print the current file creation mask.
483:
484: wwaaiitt [_p_i_d]
485: Hold execution of further commands until process pid ter-
486: minates. If pid is omitted, wait for all child processes.
487: If no children are active, this command finishes im-
488: mediately.
489:
490: ***** Options *****
491:
492:
493: -cc _s_t_r_i_n_g
494: Read shell commands from string.
495:
496: -ee Exit on any error (command not found or command returning
497: nonzero status) if the shell is not interactive.
498:
499: -ii The shell is interactive, even if the terminal is not at-
500: tached to it; print prompt strings. For a shell reading a
501: script, ignore the signals SIGTERM and SIGINT.
502:
503: -kk Place all keyword arguments into the environment. Normally,
504: the shell places only assignments to variables preceding the
505: command into the environment.
506:
507: -nn Read commands but do not execute them.
508:
509: -ss Read commands from the standard input and write shell output
510: to the standard error.
511:
512: -tt Read and execute one command rather than the entire file.
513:
514: -uu If the actual value of a shell variable is blank, report an
515: error rather than substituting the null string.
516:
517: -vv Print each line as it is read.
518:
519: -xx Print each command and its arguments as it is executed.
520:
521: - Cancel the -x and -v options.
522:
523:
524:
525:
526: COHERENT Lexicon Page 8
527:
528:
529:
530:
531: sh Command sh
532:
533:
534:
535: If the first character of argument 0 is `-', the shell reads and
536: executes the scripts /etc/profile and $HOME/.profile before
537: reading the standard input. /etc/profile is a convenient place
538: for initializing system-wide variables, such as TIMEZONE.
539:
540: ***** Examples *****
541:
542: The first example is a shell script that moves to the next al-
543: phabetical sibling directory.
544:
545:
546: # DEF_NAME is a command that defines the current directory name.
547: DEF_NAME="basename `pwd`"
548:
549:
550:
551: # CUR_DIR current directory name.
552: CUR_DIR=`$DEF_NAME`
553:
554:
555:
556: # If current directory is root exit, else
557: # go the to parent directory.
558: if [ $CUR_DIR = '/' ]; then
559: echo This is root directory.
560: exit
561: else
562: cd ..
563: fi
564:
565:
566:
567: # DIR_NUM is the alphabetical number of the current directory
568: # in the directory list of the parent directory.
569: DIR_NUM=`lc -d1 | sed -n -e "/ $CUR_DIR/="`
570:
571:
572:
573: # NEXT is the number of the next alphabetical directory.
574: NEXT=`expr $DIR_NUM + 1`
575:
576:
577:
578: # If next directory exists then come to this directory,
579: # else stay in parent directory.
580: if [ $NEXT -le `lc -d1 | wc -l` ]; then
581: cd `lc -d1 | sed -n -e $NEXT\p`
582: fi
583:
584:
585: The second example is a script that logs UUCP information to a
586: file. Usage is uuuuiinnffoo _o_u_t_f_i_l_e, where outfile is the file to hold
587: the logged information.
588:
589:
590:
591:
592: COHERENT Lexicon Page 9
593:
594:
595:
596:
597: sh Command sh
598:
599:
600:
601:
602: OUTFILE=$1
603:
604:
605:
606: > $OUTFILE
607: echo "Descriptive text for top of file, ended by Ctrl-D:"
608: echo "UUCP and Com Port Information." >> $OUTFILE
609: cat >> $OUTFILE
610: echo "===============================================" >> $OUTFILE
611:
612:
613:
614: echo "/usr/lib/uucp" >> $OUTFILE
615: (
616: cd /usr/lib/uucp
617: echo "==============================================="
618: ls -l L.sys L-devices Permissions
619: echo "==============================================="
620: echo "L.sys"
621: echo "==============================================="
622: cat L.sys
623: echo "==============================================="
624: echo "L-devices"
625: echo "==============================================="
626: cat L-devices
627: echo "==============================================="
628: echo "Permissions"
629: echo "==============================================="
630: cat Permissions
631: echo "==============================================="
632: ) >> $OUTFILE
633:
634:
635:
636: echo "/etc/ttys" >> $OUTFILE
637: echo "===============================================" >> $OUTFILE
638: ls -l /etc/ttys >> $OUTFILE
639: echo "===============================================" >> $OUTFILE
640: cat /etc/ttys >> $OUTFILE
641: echo "===============================================" >> $OUTFILE
642: echo "/dev/com*" >> $OUTFILE
643: echo "===============================================" >> $OUTFILE
644: ls -l /dev/com* >> $OUTFILE
645: echo "===============================================" >> $OUTFILE
646: echo "End of file." >> $OUTFILE
647: echo "$OUTFILE written."
648: echo "Remove confidential passwords & phone numbers from $OUTFILE."
649:
650:
651: ***** Files *****
652:
653: /eettcc/pprrooffiillee -- System-wide initial commands
654: $HHOOMMEE/.pprrooffiillee -- User-specific initial commands
655: /ddeevv/nnuullll -- For background input
656:
657:
658: COHERENT Lexicon Page 10
659:
660:
661:
662:
663: sh Command sh
664:
665:
666:
667: /ttmmpp/sshh* -- Temporaries
668:
669: ***** See Also *****
670:
671: commands, dup(), environ, exec, fork(), login, newgrp, signal(),
672: test
673: _I_n_t_r_o_d_u_c_t_i_o_n _t_o _s_h, _t_h_e _B_o_u_r_n_e _S_h_e_l_l, tutorial
674:
675: ***** Diagnostics *****
676:
677: The shell notes on the standard error syntax errors in commands
678: and commands which it cannot find. Syntax errors cause a nonin-
679: teractive shell to exit. It gives error messages if I/O redirec-
680: tion is incorrect. The shell returns the exit status of the last
681: command executed or the status specified by an exit command.
682:
683:
684:
685:
686:
687:
688:
689:
690:
691:
692:
693:
694:
695:
696:
697:
698:
699:
700:
701:
702:
703:
704:
705:
706:
707:
708:
709:
710:
711:
712:
713:
714:
715:
716:
717:
718:
719:
720:
721:
722:
723:
724: COHERENT Lexicon Page 11
725:
726:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.