1.\" $NetBSD: csh.1,v 1.43 2004/04/20 01:43:03 jschauma Exp $ 2.\" 3.\" Copyright (c) 1980, 1990, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)csh.1 8.2 (Berkeley) 1/21/94 31.\" 32.Dd April 17, 2004 33.Dt CSH 1 34.Os 35.Sh NAME 36.Nm csh 37.Nd a shell (command interpreter) with C-like syntax 38.Sh SYNOPSIS 39.Nm 40.Op Fl bcefinstvVxX 41.Op arg ... 42.Nm 43.Op Fl l 44.Sh DESCRIPTION 45The 46.Nm 47is a command language interpreter 48incorporating a history mechanism (see 49.Sx History Substitutions ) , 50job control facilities (see 51.Sx Jobs ) , 52interactive file name 53and user name completion (see 54.Sx File Name Completion ) , 55and a C-like syntax. 56It is used both as an interactive 57login shell and a shell script command processor. 58.Ss Argument list processing 59If the first argument (argument 0) to the shell is 60.Ql Fl \& , 61then this is a login shell. 62A login shell also can be specified by invoking the shell with the 63.Ql Fl l 64flag as the only argument. 65.Pp 66The rest of the flag arguments are interpreted as follows: 67.Bl -tag -width 5n 68.It Fl b 69This flag forces a ``break'' from option processing, causing any further 70shell arguments to be treated as non-option arguments. 71The remaining arguments will not be interpreted as shell options. 72This may be used to pass options to a shell script without confusion 73or possible subterfuge. 74The shell will not run a set-user ID script without this option. 75.It Fl c 76Commands are read from the (single) following argument which must 77be present. 78Any remaining arguments are placed in 79.Ar argv . 80.It Fl e 81The shell exits if any invoked command terminates abnormally 82or yields a non-zero exit status. 83.It Fl f 84The shell will start faster, because it will neither search for nor 85execute commands from the file 86.Pa \&.cshrc 87in the invoker's home directory. 88.It Fl i 89The shell is interactive and prompts for its top-level input, 90even if it appears not to be a terminal. 91Shells are interactive without this option if their inputs 92and outputs are terminals. 93.It Fl l 94The shell is a login shell (only applicable if 95.Fl l 96is the only flag specified). 97.It Fl m 98Read 99.Pa \&.cshrc 100even if not owned by the user. 101This flag is normally given only by 102.Xr su 1 . 103.It Fl n 104Commands are parsed, but not executed. 105This aids in syntactic checking of shell scripts. 106.It Fl s 107Command input is taken from the standard input. 108.It Fl t 109A single line of input is read and executed. 110A 111.Ql \e 112may be used to escape the newline at the end of this 113line and continue onto another line. 114.It Fl v 115Causes the 116.Ar verbose 117variable to be set, with the effect 118that command input is echoed after history substitution. 119.It Fl x 120Causes the 121.Ar echo 122variable to be set, so that commands are echoed immediately before execution. 123.It Fl V 124Causes the 125.Ar verbose 126variable to be set even before 127.Pa .cshrc 128is executed. 129.It Fl X 130Is to 131.Fl x 132as 133.Fl V 134is to 135.Fl v . 136.El 137.Pp 138After processing of flag arguments, if arguments remain but none of the 139.Fl c , 140.Fl i , 141.Fl s , 142or 143.Fl t 144options were given, the first argument is taken as the name of a file of 145commands to be executed. 146The shell opens this file, and saves its name for possible resubstitution 147by `$0'. 148Since many systems use either the standard version 6 or version 7 shells 149whose shell scripts are not compatible with this shell, the shell will 150execute such a `standard' shell if the first character of a script 151is not a `#', i.e., if the script does not start with a comment. 152Remaining arguments initialize the variable 153.Ar argv . 154.Pp 155An instance of 156.Nm 157begins by executing commands from the file 158.Pa /etc/csh.cshrc 159and, 160if this is a login shell, 161.Pa \&/etc/csh.login . 162It then executes 163commands from 164.Pa \&.cshrc 165in the 166.Ar home 167directory of the invoker, and, if this is a login shell, the file 168.Pa \&.login 169in the same location. 170It is typical for users on crt's to put the command ``stty crt'' 171in their 172.Pa \&.login 173file, and to also invoke 174.Xr tset 1 175there. 176.Pp 177In the normal case, the shell will begin reading commands from the 178terminal, prompting with `% '. 179Processing of arguments and the use of the shell to process files 180containing command scripts will be described later. 181.Pp 182The shell repeatedly performs the following actions: 183a line of command input is read and broken into 184.Ar words . 185This sequence of words is placed on the command history list and parsed. 186Finally each command in the current line is executed. 187.Pp 188When a login shell terminates it executes commands from the files 189.Pa .logout 190in the user's 191.Ar home 192directory and 193.Pa /etc/csh.logout . 194.Ss Lexical structure 195The shell splits input lines into words at blanks and tabs with the 196following exceptions. 197The characters 198`\*[Am]' `\&|' `;' `\*[Lt]' `\*[Gt]' `(' `)' 199form separate words. 200If doubled in `\*[Am]\*[Am]', 201`\&|\&|', `\*[Lt]\*[Lt]' or `\*[Gt]\*[Gt]' these pairs form single words. 202These parser metacharacters may be made part of other words, or prevented their 203special meaning, by preceding them with `\e'. 204A newline preceded by a `\e' is equivalent to a blank. 205.Pp 206Strings enclosed in matched pairs of quotations, 207`'\|', `\*(ga' or `"', 208form parts of a word; metacharacters in these strings, including blanks 209and tabs, do not form separate words. 210These quotations have semantics to be described later. 211Within pairs of `\'' or `"' characters, a newline preceded by a `\e' gives 212a true newline character. 213.Pp 214When the shell's input is not a terminal, 215the character `#' introduces a comment that continues to the end of the 216input line. 217It is prevented this special meaning when preceded by `\e' 218and in quotations using `\`', `\'', and `"'. 219.Ss Commands 220A simple command is a sequence of words, the first of which 221specifies the command to be executed. 222A simple command or 223a sequence of simple commands separated by `\&|' characters 224forms a pipeline. 225The output of each command in a pipeline is connected to the input of the next. 226Sequences of pipelines may be separated by `;', and are then executed 227sequentially. 228A sequence of pipelines may be executed without immediately 229waiting for it to terminate by following it with an `\*[Am]'. 230.Pp 231Any of the above may be placed in `(' `)' to form a simple command (that 232may be a component of a pipeline, etc.). 233It is also possible to separate pipelines with `\&|\&|' 234or `\*[Am]\*[Am]' showing, as in the C language, 235that the second is to be executed only if the first fails or succeeds 236respectively. 237(See 238.Sx Expressions . ) 239.Ss Jobs 240The shell associates a 241.Ar job 242with each pipeline. 243It keeps 244a table of current jobs, printed by the 245.Ar jobs 246command, and assigns them small integer numbers. 247When a job is started asynchronously with `\*[Am]', 248the shell prints a line that looks like: 249.Bd -filled -offset indent 250.Op 1 2511234 252.Ed 253.Pp 254showing that the job which was started asynchronously was job number 2551 and had one (top-level) process, whose process id was 1234. 256.Pp 257If you are running a job and wish to do something else you may hit the key 258.Ic ^Z 259(control-Z) which sends a STOP signal to the current job. 260The shell will then normally show that the job has been `Stopped', 261and print another prompt. 262You can then manipulate the state of this job, putting it in the 263.Em background 264with the 265.Ar bg 266command, or run some other 267commands and eventually bring the job back into the foreground with 268the 269.Em foreground 270command 271.Ar fg . 272A 273.Ic ^Z 274takes effect immediately and 275is like an interrupt in that pending output and unread input are discarded 276when it is typed. 277There is another special key 278.Ic ^Y 279that does not generate a STOP signal until a program attempts to 280.Xr read 2 281it. 282This request can usefully be typed ahead when you have prepared some commands 283for a job that you wish to stop after it has read them. 284.Pp 285A job being run in the background will stop if it tries to read 286from the terminal. 287Background jobs are normally allowed to produce output, 288but this can be disabled by giving the command ``stty tostop''. 289If you set this 290tty option, then background jobs will stop when they try to produce 291output like they do when they try to read input. 292.Pp 293There are several ways to refer to jobs in the shell. 294The character `%' introduces a job name. 295If you wish to refer to job number 1, you can name it as `%1'. 296Just naming a job brings it to the foreground; thus 297`%1' is a synonym for `fg %1', bringing job number 1 back into the foreground. 298Similarly saying `%1 \*[Am]' resumes job number 1 in the background. 299Jobs can also be named by prefixes of the string typed in to start them, 300if these prefixes are unambiguous, thus `%ex' would normally restart 301a suspended 302.Xr ex 1 303job, if there were only one suspended job whose name began with 304the string `ex'. 305It is also possible to say `%?string' 306which specifies a job whose text contains 307.Ar string , 308if there is only one such job. 309.Pp 310The shell maintains a notion of the current and previous jobs. 311In output about jobs, the current job is marked with a `+' 312and the previous job with a `\-'. 313The abbreviation `%+' refers 314to the current job and `%\-' refers to the previous job. 315For close analogy with the syntax of the 316.Ar history 317mechanism (described below), 318`%%' is also a synonym for the current job. 319.Pp 320The job control mechanism requires that the 321.Xr stty 1 322option 323.Ic new 324be set. 325It is an artifact from a 326.Em new 327implementation 328of the 329tty driver that allows generation of interrupt characters from 330the keyboard to tell jobs to stop. 331See 332.Xr stty 1 333for details on setting options in the new tty driver. 334.Ss Status reporting 335The shell learns immediately whenever a process changes state. 336It normally informs you whenever a job becomes blocked so that 337no further progress is possible, but only just before it prints 338a prompt. 339This is done so that it does not otherwise disturb your work. 340If, however, you set the shell variable 341.Ar notify , 342the shell will notify you immediately of changes of status in background 343jobs. 344There is also a shell command 345.Ar notify 346that marks a single process so that its status changes will be immediately 347reported. 348By default 349.Ar notify 350marks the current process; 351simply say `notify' after starting a background job to mark it. 352.Pp 353When you try to leave the shell while jobs are stopped, you will 354be warned that `You have stopped jobs.' 355You may use the 356.Ar jobs 357command to see what they are. 358If you try to exit again immediately, 359the shell will not warn you a second time, and the suspended 360jobs will be terminated. 361.Ss File Name Completion 362When the file name completion feature is enabled by setting 363the shell variable 364.Ar filec 365(see 366.Ic set ) , 367.Nm 368will 369interactively complete file names and user names from unique 370prefixes, when they are input from the terminal followed by 371the escape character (the escape key, or control-[) 372For example, 373if the current directory looks like 374.Bd -literal -offset indent 375DSC.OLD bin cmd lib xmpl.c 376DSC.NEW chaosnet cmtest mail xmpl.o 377bench class dev mbox xmpl.out 378.Ed 379.Pp 380and the input is 381.Pp 382.Dl % vi ch\*[Lt]escape\*[Gt] 383.Pp 384.Nm 385will complete the prefix ``ch'' 386to the only matching file name ``chaosnet'', changing the input 387line to 388.Pp 389.Dl % vi chaosnet 390.Pp 391However, given 392.Pp 393.Dl % vi D\*[Lt]escape\*[Gt] 394.Pp 395.Nm 396will only expand the input to 397.Pp 398.Dl % vi DSC. 399.Pp 400and will sound the terminal bell to indicate that the expansion is 401incomplete, since there are two file names matching the prefix ``D''. 402.Pp 403If a partial file name is followed by the end-of-file character 404(usually control-D), then, instead of completing the name, 405.Nm 406will list all file names matching the prefix. 407For example, 408the input 409.Pp 410.Dl % vi D\*[Lt]control-D\*[Gt] 411.Pp 412causes all files beginning with ``D'' to be listed: 413.Pp 414.Dl DSC.NEW DSC.OLD 415.Pp 416while the input line remains unchanged. 417.Pp 418The same system of escape and end-of-file can also be used to 419expand partial user names, if the word to be completed 420(or listed) begins with the character ``~''. 421For example, typing 422.Pp 423.Dl cd ~ro\*[Lt]escape\*[Gt] 424.Pp 425may produce the expansion 426.Pp 427.Dl cd ~root 428.Pp 429The use of the terminal bell to signal errors or multiple matches 430can be inhibited by setting the variable 431.Ar nobeep . 432.Pp 433Normally, all files in the particular directory are candidates 434for name completion. 435Files with certain suffixes can be excluded 436from consideration by setting the variable 437.Ar fignore 438to the 439list of suffixes to be ignored. 440Thus, if 441.Ar fignore 442is set by 443the command 444.Pp 445.Dl % set fignore = (.o .out) 446.Pp 447then typing 448.Pp 449.Dl % vi x\*[Lt]escape\*[Gt] 450.Pp 451would result in the completion to 452.Pp 453.Dl % vi xmpl.c 454.Pp 455ignoring the files "xmpl.o" and "xmpl.out". 456However, if the only completion possible requires not ignoring these 457suffixes, then they are not ignored. 458In addition, 459.Ar fignore 460does not affect the listing of file names by control-D. 461All files 462are listed regardless of their suffixes. 463.Ss Substitutions 464We now describe the various transformations the shell performs on the 465input in the order in which they occur. 466.Ss History substitutions 467History substitutions place words from previous command input as portions 468of new commands, making it easy to repeat commands, repeat arguments 469of a previous command in the current command, or fix spelling mistakes 470in the previous command with little typing and a high degree of confidence. 471History substitutions begin with the character `!' and may begin 472.Ar anywhere 473in the input stream (with the proviso that they 474.Em do not 475nest.) 476This `!' may be preceded by a `\e' to prevent its special meaning; for 477convenience, an `!' is passed unchanged when it is followed by a blank, 478tab, newline, `=' or `('. 479(History substitutions also occur when an input line begins with `\*(ua'. 480This special abbreviation will be described later.) 481Any input line that contains history substitution is echoed on the terminal 482before it is executed as it would have been typed without history substitution. 483.Pp 484Commands input from the terminal that consist of one or more words 485are saved on the history list. 486The history substitutions reintroduce sequences of words from these 487saved commands into the input stream. 488The size of the history list is controlled by the 489.Ar history 490variable; the previous command is always retained, 491regardless of the value of the history variable. 492Commands are numbered sequentially from 1. 493.Pp 494For example, consider the following output from the 495.Ar history 496command: 497.Bd -literal -offset indent 498\09 write michael 49910 ex write.c 50011 cat oldwrite.c 50112 diff *write.c 502.Ed 503.Pp 504The commands are shown with their event numbers. 505It is not usually necessary to use event numbers, but the current event 506number can be made part of the 507.Ar prompt 508by placing an `!' in the prompt string. 509.Pp 510With the current event 13 we can refer to previous events by event 511number `!11', relatively as in `!\-2' (referring to the same event), 512by a prefix of a command word 513as in `!d' for event 12 or `!wri' for event 9, or by a string contained in 514a word in the command as in `!?mic?' also referring to event 9. 515These forms, without further change, simply reintroduce the words 516of the specified events, each separated by a single blank. 517As a special case, `!!' refers to the previous command; thus `!!' alone is a 518.Ar redo . 519.Pp 520To select words from an event we can follow the event specification by 521a `:' and a designator for the desired words. 522The words of an input line are numbered from 0, 523the first (usually command) word being 0, the second word (first argument) 524being 1, etc. 525The basic word designators are: 526.Pp 527.Bl -tag -width Ds -compact -offset indent 528.It \&0 529first (command) word 530.It Ar n 531.Ar n Ns 'th 532argument 533.It \*(ua 534first argument, i.e., `1' 535.It $ 536last argument 537.It % 538word matched by (immediately preceding) 539.No \&? Ns Ar s Ns \&? 540search 541.It Ar \&x\-y 542range of words 543.It Ar \&\-y 544abbreviates 545.Ar `\&0\-y\' 546.It * 547abbreviates `\*(ua\-$', or nothing if only 1 word in event 548.It Ar x* 549abbreviates 550.Ar `x\-$\' 551.It Ar x\- 552like 553.Ar `x*\' 554but omitting word `$' 555.El 556.Pp 557The `:' separating the event specification from the word designator 558can be omitted if the argument selector begins with a `\*(ua', `$', `*', 559`\-' or `%'. 560After the optional word designator can be 561placed a sequence of modifiers, each preceded by a `:'. 562The following modifiers are defined: 563.Pp 564.Bl -tag -width Ds -compact -offset indent 565.It h 566Remove a trailing pathname component, leaving the head. 567.It r 568Remove a trailing `.xxx' component, leaving the root name. 569.It e 570Remove all but the extension `.xxx' part. 571.It s Ns Ar /l/r/ 572Substitute 573.Ar l 574for 575.Ar r 576.It t 577Remove all leading pathname components, leaving the tail. 578.It \&\*[Am] 579Repeat the previous substitution. 580.It g 581Apply the change once on each word, prefixing the above, e.g., `g\*[Am]'. 582.It a 583Apply the change as many times as possible on a single word, prefixing 584the above. 585It can be used together with `g' to apply a substitution 586globally. 587.It p 588Print the new command line but do not execute it. 589.It q 590Quote the substituted words, preventing further substitutions. 591.It x 592Like q, but break into words at blanks, tabs and newlines. 593.El 594.Pp 595Unless preceded by a `g' the change is applied only to the first 596modifiable word. 597With substitutions, it is an error for no word to be applicable. 598.Pp 599The left hand side of substitutions are not regular expressions in the sense 600of the editors, but instead strings. 601Any character may be used as the delimiter in place of `/'; 602a `\e' quotes the delimiter into the 603.Ar l 604and 605.Ar r 606strings. 607The character `\*[Am]' in the right hand side is replaced by the text from 608the left. 609A `\e' also quotes `\*[Am]'. 610A null 611.Ar l 612(`//') 613uses the previous string either from an 614.Ar l 615or from a 616contextual scan string 617.Ar s 618in 619.No \&`!? Ns Ar s Ns \e?' . 620The trailing delimiter in the substitution may be omitted if a newline 621follows immediately as may the trailing `?' in a contextual scan. 622.Pp 623A history reference may be given without an event specification, e.g., `!$'. 624Here, the reference is to the previous command unless a previous 625history reference occurred on the same line in which case this form repeats 626the previous reference. 627Thus `!?foo?\*(ua !$' gives the first and last arguments 628from the command matching `?foo?'. 629.Pp 630A special abbreviation of a history reference occurs when the first 631non-blank character of an input line is a `\*(ua'. 632This is equivalent to `!:s\*(ua' providing a convenient 633shorthand for substitutions on the text of the previous line. 634Thus `\*(ualb\*(ualib' fixes the spelling of 635`lib' 636in the previous command. 637Finally, a history substitution may be surrounded with `{' and `}' 638if necessary to insulate it from the characters that follow. 639Thus, after `ls \-ld ~paul' we might do `!{l}a' to do `ls \-ld ~paula', 640while `!la' would look for a command starting with `la'. 641.Ss Quotations with \' and \&" 642The quotation of strings by `\'' and `"' can be used 643to prevent all or some of the remaining substitutions. 644Strings enclosed in `\'' are prevented any further interpretation. 645Strings enclosed in `"' may be expanded as described below. 646.Pp 647In both cases the resulting text becomes (all or part of) a single word; 648only in one special case (see 649.Em Command Substitution 650below) does a `"' quoted string yield parts of more than one word; 651`\'' quoted strings never do. 652.Ss Alias substitution 653The shell maintains a list of aliases that can be established, displayed 654and modified by the 655.Ar alias 656and 657.Ar unalias 658commands. 659After a command line is scanned, it is parsed into distinct commands and 660the first word of each command, left-to-right, is checked to see if it 661has an alias. 662If it does, then the text that is the alias for that command is reread 663with the history mechanism available 664as though that command were the previous input line. 665The resulting words replace the 666command and argument list. 667If no reference is made to the history list, then the argument list is 668left unchanged. 669.Pp 670Thus if the alias for `ls' is `ls \-l' the command `ls /usr' would map to 671`ls \-l /usr', the argument list here being undisturbed. 672Similarly if the alias for `lookup' was `grep !\*(ua /etc/passwd' then 673`lookup bill' would map to `grep bill /etc/passwd'. 674.Pp 675If an alias is found, the word transformation of the input text 676is performed and the aliasing process begins again on the reformed input line. 677Looping is prevented if the first word of the new text is the same as the old 678by flagging it to prevent further aliasing. 679Other loops are detected and cause an error. 680.Pp 681Note that the mechanism allows aliases to introduce parser metasyntax. 682Thus, we can `alias print \'pr \e!* \&| lpr\'' to make a command that 683.Ar pr Ns 's 684its arguments to the line printer. 685.Ss Variable substitution 686The shell maintains a set of variables, each of which has as value a list 687of zero or more words. 688Some of these variables are set by the shell or referred to by it. 689For instance, the 690.Ar argv 691variable is an image of the shell's argument list, and words of this 692variable's value are referred to in special ways. 693.Pp 694The values of variables may be displayed and changed by using the 695.Ar set 696and 697.Ar unset 698commands. 699Of the variables referred to by the shell a number are toggles; 700the shell does not care what their value is, 701only whether they are set or not. 702For instance, the 703.Ar verbose 704variable is a toggle that causes command input to be echoed. 705The setting of this variable results from the 706.Fl v 707command line option. 708.Pp 709Other operations treat variables numerically. 710The `@' command permits numeric calculations to be performed and the result 711assigned to a variable. 712Variable values are, however, always represented as (zero or more) strings. 713For the purposes of numeric operations, the null string is considered to be 714zero, and the second and additional words of multiword values are ignored. 715.Pp 716After the input line is aliased and parsed, and before each command 717is executed, variable substitution 718is performed keyed by `$' characters. 719This expansion can be prevented by preceding the `$' with a `\e' except 720within `"'s where it 721.Em always 722occurs, and within `\''s where it 723.Em never 724occurs. 725Strings quoted by `\*(ga' are interpreted later (see 726.Sx Command substitution 727below), so `$' substitution does not occur there until later, if at all. 728A `$' is passed unchanged if followed by a blank, tab, or end-of-line. 729.Pp 730Input/output redirections are recognized before variable expansion, 731and are variable expanded separately. 732Otherwise, the command name and entire argument list are expanded together. 733It is thus possible for the first (command) word (to this point) to generate 734more than one word, the first of which becomes the command name, 735and the rest of which become arguments. 736.Pp 737Unless enclosed in `"' or given the `:q' modifier the results of variable 738substitution may eventually be command and filename substituted. 739Within `"', a variable whose value consists of multiple words expands to a 740(portion of) a single word, with the words of the variable's value 741separated by blanks. 742When the `:q' modifier is applied to a substitution 743the variable will expand to multiple words with each word separated 744by a blank and quoted to prevent later command or filename substitution. 745.Pp 746The following metasequences are provided for introducing variable values into 747the shell input. 748Except as noted, it is an error to reference a variable that is not set. 749.Pp 750.Bl -tag -width Ds -compact -offset indent 751.It $name 752.It ${name} 753Are replaced by the words of the value of variable 754.Ar name , 755each separated by a blank. 756Braces insulate 757.Ar name 758from following characters that would otherwise be part of it. 759Shell variables have names consisting of up to 20 letters and digits 760starting with a letter. 761The underscore character is considered a letter. 762If 763.Ar name 764is not a shell variable, but is set in the environment, then 765that value is returned (but `:' modifiers and the other forms 766given below are not available here). 767.It $name Ns Op selector 768.It ${name Ns [ selector ] Ns } 769May be used to select only some of the words from the value of 770.Ar name . 771The selector is subjected to `$' substitution and may consist of a single 772number or two numbers separated by a `\-'. 773The first word of a variable's value is numbered `1'. 774If the first number of a range is omitted it defaults to `1'. 775If the last number of a range is omitted it defaults to `$#name'. 776The selector `*' selects all words. 777It is not an error for a range to be empty if the second argument is omitted 778or in range. 779.It $#name 780.It ${#name} 781Gives the number of words in the variable. 782This is useful for later use in a 783`$argv[selector]'. 784.It $0 785Substitutes the name of the file from which command input is being read. 786An error occurs if the name is not known. 787.It $number 788.It ${number} 789Equivalent to 790`$argv[number]'. 791.It $* 792Equivalent to 793`$argv[*]'. 794.El 795.Pp 796The modifiers `:e', `:h', `:t', `:r', `:q' and `:x' may be applied to 797the substitutions above as may `:gh', `:gt' and `:gr'. 798If braces `{' '}' appear in the command form then the modifiers 799must appear within the braces. 800The current implementation allows only one `:' modifier on each `$' expansion. 801.Pp 802The following substitutions may not be modified with `:' modifiers. 803.Bl -tag -width Ds -compact -offset indent 804.It $?name 805.It ${?name} 806Substitutes the string `1' if name is set, `0' if it is not. 807.It $?0 808Substitutes `1' if the current input filename is known, `0' if it is not. 809.It \&$\&$\& 810Substitute the (decimal) process number of the (parent) shell. 811.It $! 812Substitute the (decimal) process number of the last background process 813started by this shell. 814.It $\*[Lt] 815Substitutes a line from the standard 816input, with no further interpretation. 817It can be used to read from the keyboard in a shell script. 818.El 819.Ss Command and filename substitution 820The remaining substitutions, command and filename substitution, 821are applied selectively to the arguments of builtin commands. 822By selectively, we mean that portions of expressions which are 823not evaluated are not subjected to these expansions. 824For commands that are not internal to the shell, the command 825name is substituted separately from the argument list. 826This occurs very late, 827after input-output redirection is performed, and in a child 828of the main shell. 829.Ss Command substitution 830Command substitution is shown by a command enclosed in `\*(ga'. 831The output from such a command is normally broken into separate words 832at blanks, tabs and newlines, with null words being discarded; 833this text then replaces the original string. 834Within `"'s, only newlines force new words; blanks and tabs are preserved. 835.Pp 836In any case, the single final newline does not force a new word. 837Note that it is thus possible for a command substitution to yield 838only part of a word, even if the command outputs a complete line. 839.Ss Filename substitution 840If a word contains any of the characters `*', `?', `[' or `{' 841or begins with the character `~', then that word is a candidate for 842filename substitution, also known as `globbing'. 843This word is then regarded as a pattern, and replaced with an alphabetically 844sorted list of file names that match the pattern. 845In a list of words specifying filename substitution it is an error for 846no pattern to match an existing file name, but it is not required 847for each pattern to match. 848Only the metacharacters `*', `?' and `[' imply pattern matching, 849the characters `~' and `{' being more akin to abbreviations. 850.Pp 851In matching filenames, the character `.' at the beginning of a filename 852or immediately following a `/', as well as the character `/' must 853be matched explicitly. 854The character `*' matches any string of characters, including the null 855string. 856The character `?' matches any single character. 857The sequence 858.Sq Op ... 859matches any one of the characters enclosed. 860Within 861.Sq Op ... , 862a pair of characters separated by `\-' matches any character lexically between 863the two (inclusive). 864.Pp 865The character `~' at the beginning of a filename refers to home 866directories. 867Standing alone, i.e., `~' it expands to the invoker's home directory as reflected 868in the value of the variable 869.Ar home . 870When followed by a name consisting of letters, digits and `\-' characters, 871the shell searches for a user with that name and substitutes their 872home directory; thus `~ken' might expand to `/usr/ken' and `~ken/chmach' 873to `/usr/ken/chmach'. 874If the character `~' is followed by a character other than a letter or `/' 875or does not appear at the beginning of a word, 876it is left undisturbed. 877.Pp 878The metanotation `a{b,c,d}e' is a shorthand for `abe ace ade'. 879Left to right order is preserved, with results of matches being sorted 880separately at a low level to preserve this order. 881This construct may be nested. 882Thus, `~source/s1/{oldls,ls}.c' expands to 883`/usr/source/s1/oldls.c /usr/source/s1/ls.c' 884without chance of error 885if the home directory for `source' is `/usr/source'. 886Similarly `../{memo,*box}' might expand to `../memo ../box ../mbox'. 887(Note that `memo' was not sorted with the results of the match to `*box'.) 888As a special case `{', `}' and `{}' are passed undisturbed. 889.Ss Input/output 890The standard input and the standard output of a command may be redirected 891with the following syntax: 892.Pp 893.Bl -tag -width Ds -compact -offset indent 894.It \*[Lt] name 895Open file 896.Ar name 897(which is first variable, command and filename expanded) as the standard 898input. 899.It \*[Lt]\*[Lt] word 900Read the shell input up to a line that is identical to 901.Ar word . 902.Ar Word 903is not subjected to variable, filename or command substitution, 904and each input line is compared to 905.Ar word 906before any substitutions are done on the input line. 907Unless a quoting `\e', `"', `\*(aa' or `\*(ga' appears in 908.Ar word , 909variable and command substitution is performed on the intervening lines, 910allowing `\e' to quote `$', `\e' and `\*(ga'. 911Commands that are substituted have all blanks, tabs, and newlines 912preserved, except for the final newline which is dropped. 913The resultant text is placed in an anonymous temporary file that 914is given to the command as its standard input. 915.It \*[Gt] name 916.It \*[Gt]! name 917.It \*[Gt]\*[Am] name 918.It \*[Gt]\*[Am]! name 919The file 920.Ar name 921is used as the standard output. 922If the file does not exist then it is created; 923if the file exists, it is truncated; its previous contents are lost. 924.Pp 925If the variable 926.Ar noclobber 927is set, then the file must not exist or be a character special file (e.g., a 928terminal or `/dev/null') or an error results. 929This helps prevent accidental destruction of files. 930Here, the `!' forms can be used to suppress this check. 931.Pp 932The forms involving `\*[Am]' route the standard error output into the specified 933file as well as the standard output. 934.Ar Name 935is expanded in the same way as `\*[Lt]' input filenames are. 936.It \*[Gt]\*[Gt] name 937.It \*[Gt]\*[Gt]\*[Am] name 938.It \*[Gt]\*[Gt]! name 939.It \*[Gt]\*[Gt]\*[Am]! name 940Uses file 941.Ar name 942as the standard output; 943like `\*[Gt]' but places output at the end of the file. 944If the variable 945.Ar noclobber 946is set, then it is an error for the file not to exist unless 947one of the `!' forms is given. 948Otherwise similar to `\*[Gt]'. 949.El 950.Pp 951A command receives the environment in which the shell was 952invoked as modified by the input-output parameters and 953the presence of the command in a pipeline. 954Thus, unlike some previous shells, commands run from a file of shell commands 955have no access to the text of the commands by default; 956instead they receive the original standard input of the shell. 957The `\*[Lt]\*[Lt]' mechanism should be used to present inline data. 958This permits shell command scripts to function as components of pipelines 959and allows the shell to block read its input. 960Note that the default standard input for a command run detached is 961.Ar not 962modified to be the empty file 963.Pa /dev/null ; 964instead the standard input 965remains as the original standard input of the shell. 966If this is a terminal 967and if the process attempts to read from the terminal, then the process 968will block and the user will be notified (see 969.Sx Jobs 970above). 971.Pp 972The standard error output may be directed through 973a pipe with the standard output. 974Simply use the form `\&|\*[Am]' instead of just `\&|'. 975.Ss Expressions 976Several of the builtin commands (to be described later) 977take expressions, in which the operators are similar to those of C, with 978the same precedence, but with the 979.Em opposite grouping : 980right to left. 981These expressions appear in the 982.Ar @ , 983.Ar exit , 984.Ar if , 985and 986.Ar while 987commands. 988The following operators are available: 989.Bd -ragged -offset indent 990\&|\&| \*[Am]\*[Am] \&| \*(ua \*[Am] == != =~ !~ \*[Le] \*[Ge] 991\*[Lt] \*[Gt] \*[Lt]\*[Lt] \*[Gt]\*[Gt] + \- * / % ! ~ ( ) 992.Ed 993.Pp 994Here the precedence increases to the right, 995`==' `!=' `=~' and `!~', `\*[Le]' `\*[Ge]' `\*[Lt]' 996and `\*[Gt]', `\*[Lt]\*[Lt]' and `\*[Gt]\*[Gt]', `+' and `\-', 997`*' `/' and `%' being, in groups, at the same level. 998The `==' `!=' `=~' and `!~' operators compare their arguments as strings; 999all others operate on numbers. 1000The operators `=~' and `!~' are like `!=' and `==' except that the right 1001hand side is a 1002.Ar pattern 1003(containing, e.g., `*'s, `?'s and instances of `[...]') 1004against which the left hand operand is matched. 1005This reduces the need for use of the 1006.Ar switch 1007statement in shell scripts when all that is really needed is pattern matching. 1008.Pp 1009Strings that begin with `0' are considered octal numbers. 1010Null or missing arguments are considered `0'. 1011The result of all expressions are strings, 1012which represent decimal numbers. 1013It is important to note that no two components of an expression can appear 1014in the same word; except when adjacent to components of expressions that 1015are syntactically significant to the parser 1016(`\*[Am]' `\&|' `\*[Lt]' `\*[Gt]' `(' `)'), 1017they should be surrounded by spaces. 1018.Pp 1019Also available in expressions as primitive operands are command executions 1020enclosed in `{' and `}' 1021and file enquiries of the form 1022.Fl l 1023.Ar name 1024where 1025.Ic l 1026is one of: 1027.Bd -literal -offset indent 1028r read access 1029w write access 1030x execute access 1031e existence 1032o ownership 1033z zero size 1034f plain file 1035d directory 1036.Ed 1037.Pp 1038The specified name is command and filename expanded and then tested 1039to see if it has the specified relationship to the real user. 1040If the file does not exist or is inaccessible then all enquiries return 1041false, i.e., `0'. 1042Command executions succeed, returning true, i.e., `1', 1043if the command exits with status 0, otherwise they fail, returning 1044false, i.e., `0'. 1045If more detailed status information is required then the command 1046should be executed outside an expression and the variable 1047.Ar status 1048examined. 1049.Ss Control flow 1050The shell contains several commands that can be used to regulate the 1051flow of control in command files (shell scripts) and 1052(in limited but useful ways) from terminal input. 1053These commands all operate by forcing the shell to reread or skip in its 1054input and, because of the implementation, restrict the placement of some 1055of the commands. 1056.Pp 1057The 1058.Ic foreach , 1059.Ic switch , 1060and 1061.Ic while 1062statements, as well as the 1063.Ic if\-then\-else 1064form of the 1065.Ic if 1066statement require that the major keywords appear in a single simple command 1067on an input line as shown below. 1068.Pp 1069If the shell's input is not seekable, 1070the shell buffers up input whenever a loop is being read 1071and performs seeks in this internal buffer to accomplish the rereading 1072implied by the loop. 1073(To the extent that this allows, backward goto's will succeed on 1074non-seekable inputs.) 1075.Ss Builtin commands 1076Builtin commands are executed within the shell. 1077If a builtin command occurs as any component of a pipeline 1078except the last then it is executed in a subshell. 1079.Pp 1080.Bl -tag -width Ds -compact -offset indent 1081.It Ic alias 1082.It Ic alias Ar name 1083.It Ic alias Ar name wordlist 1084The first form prints all aliases. 1085The second form prints the alias for name. 1086The final form assigns the specified 1087.Ar wordlist 1088as the alias of 1089.Ar name ; 1090.Ar wordlist 1091is command and filename substituted. 1092.Ar Name 1093is not allowed to be 1094.Ar alias 1095or 1096.Ar unalias . 1097.Pp 1098.It Ic alloc 1099Shows the amount of dynamic memory acquired, broken down into used and 1100free memory. 1101With an argument shows the number of free and used blocks in each size 1102category. 1103The categories start at size 8 and double at each step. 1104This command's output may vary across system types, since 1105systems other than the VAX may use a different memory allocator. 1106.Pp 1107.It Ic bg 1108.It Ic bg \&% Ns Ar job ... 1109Puts the current or specified jobs into the background, continuing them 1110if they were stopped. 1111.Pp 1112.It Ic break 1113Causes execution to resume after the 1114.Ic end 1115of the nearest enclosing 1116.Ic foreach 1117or 1118.Ic while . 1119The remaining commands on the current line are executed. 1120Multi-level breaks are thus possible by writing them all on one line. 1121.Pp 1122.It Ic breaksw 1123Causes a break from a 1124.Ic switch , 1125resuming after the 1126.Ic endsw . 1127.Pp 1128.It Ic case Ar label : 1129A label in a 1130.Ic switch 1131statement as discussed below. 1132.Pp 1133.It Ic cd 1134.It Ic cd Ar name 1135.It Ic chdir 1136.It Ic chdir Ar name 1137Change the shell's working directory to directory 1138.Ar name . 1139If no argument is given then change to the home directory of the user. 1140If 1141.Ar name 1142is not found as a subdirectory of the current directory (and does not begin 1143with `/', `./' or `../'), then each 1144component of the variable 1145.Ic cdpath 1146is checked to see if it has a subdirectory 1147.Ar name . 1148Finally, if all else fails but 1149.Ar name 1150is a shell variable whose value begins with `/', then this 1151is tried to see if it is a directory. 1152.Pp 1153.It Ic continue 1154Continue execution of the nearest enclosing 1155.Ic while 1156or 1157.Ic foreach . 1158The rest of the commands on the current line are executed. 1159.Pp 1160.It Ic default : 1161Labels the default case in a 1162.Ic switch 1163statement. 1164The default should come after all 1165.Ic case 1166labels. 1167.Pp 1168.It Ic dirs 1169Prints the directory stack; the top of the stack is at the left, 1170the first directory in the stack being the current directory. 1171.Pp 1172.It Ic echo Ar wordlist 1173.It Ic echo Fl n Ar wordlist 1174The specified words are written to the shell's standard output, separated 1175by spaces, and terminated with a newline unless the 1176.Fl n 1177option is specified. 1178.Pp 1179.It Ic else 1180.It Ic end 1181.It Ic endif 1182.It Ic endsw 1183See the description of the 1184.Ic foreach , 1185.Ic if , 1186.Ic switch , 1187and 1188.Ic while 1189statements below. 1190.Pp 1191.It Ic eval Ar arg ... 1192(As in 1193.Xr sh 1 . ) 1194The arguments are read as input to the shell and the resulting 1195command(s) executed in the context of the current shell. 1196This is usually used to execute commands 1197generated as the result of command or variable substitution, since 1198parsing occurs before these substitutions. 1199See 1200.Xr tset 1 1201for an example of using 1202.Ic eval . 1203.Pp 1204.It Ic exec Ar command 1205The specified command is executed in place of the current shell. 1206.Pp 1207.It Ic exit 1208.It Ic exit Ar ( expr ) 1209The shell exits either with the value of the 1210.Ic status 1211variable (first form) or with the value of the specified 1212.Ic expr 1213(second form). 1214.Pp 1215.It Ic fg 1216.It Ic fg % Ns Ar job ... 1217Brings the current or specified jobs into the foreground, continuing them if 1218they were stopped. 1219.Pp 1220.It Ic foreach Ar name ( wordlist ) 1221.It ... 1222.It Ic end 1223The variable 1224.Ic name 1225is successively set to each member of 1226.Ic wordlist 1227and the sequence of commands between this command and the matching 1228.Ic end 1229are executed. 1230(Both 1231.Ic foreach 1232and 1233.Ic end 1234must appear alone on separate lines.) 1235The builtin command 1236.Ic continue 1237may be used to continue the loop prematurely and the builtin 1238command 1239.Ic break 1240to terminate it prematurely. 1241When this command is read from the terminal, the loop is read once 1242prompting with `?' before any statements in the loop are executed. 1243If you make a mistake typing in a loop at the terminal you can rub it out. 1244.Pp 1245.It Ic glob Ar wordlist 1246Like 1247.Ic echo 1248but no `\e' escapes are recognized and words are delimited 1249by null characters in the output. 1250Useful for programs that wish to use the shell to filename expand a list 1251of words. 1252.Pp 1253.It Ic goto Ar word 1254The specified 1255.Ic word 1256is filename and command expanded to yield a string of the form `label'. 1257The shell rewinds its input as much as possible 1258and searches for a line of the form `label:' 1259possibly preceded by blanks or tabs. 1260Execution continues after the specified line. 1261.Pp 1262.It Ic hashstat 1263Print a statistics line showing how effective the internal hash 1264table has been at locating commands (and avoiding 1265.Ic exec Ns \'s ) . 1266An 1267.Ic exec 1268is attempted for each component of the 1269.Em path 1270where the hash function indicates a possible hit, and in each component 1271that does not begin with a `/'. 1272.Pp 1273.It Ic history 1274.It Ic history Ar n 1275.It Ic history Fl r Ar n 1276.It Ic history Fl h Ar n 1277Displays the history event list; if 1278.Ar n 1279is given only the 1280.Ar n 1281most recent events are printed. 1282The 1283.Fl r 1284option reverses the order of printout to be most recent first 1285instead of oldest first. 1286The 1287.Fl h 1288option causes the history list to be printed without leading numbers. 1289This format produces files suitable for sourcing using the \-h 1290option to 1291.Ic source . 1292.Pp 1293.It Ic if Ar ( expr ) No command 1294If the specified expression evaluates true, then the single 1295.Ar command 1296with arguments is executed. 1297Variable substitution on 1298.Ar command 1299happens early, at the same 1300time it does for the rest of the 1301.Ic if 1302command. 1303.Ar Command 1304must be a simple command, not 1305a pipeline, a command list, or a parenthesized command list. 1306Input/output redirection occurs even if 1307.Ar expr 1308is false, i.e., when command is 1309.Em not 1310executed (this is a bug). 1311.Pp 1312.It Ic if Ar ( expr ) Ic then 1313.It ... 1314.It Ic else if Ar ( expr2 ) Ic then 1315.It ... 1316.It Ic else 1317.It ... 1318.It Ic endif 1319If the specified 1320.Ar expr 1321is true then the commands up to the first 1322.Ic else 1323are executed; otherwise if 1324.Ar expr2 1325is true then the commands up to the 1326second 1327.Ic else 1328are executed, etc. 1329Any number of 1330.Ic else-if 1331pairs are possible; only one 1332.Ic endif 1333is needed. 1334The 1335.Ic else 1336part is likewise optional. 1337(The words 1338.Ic else 1339and 1340.Ic endif 1341must appear at the beginning of input lines; 1342the 1343.Ic if 1344must appear alone on its input line or after an 1345.Ic else . ) 1346.Pp 1347.It Ic jobs 1348.It Ic jobs Fl l 1349Lists the active jobs; the 1350.Fl l 1351option lists process id's in addition to the normal information. 1352.Pp 1353.It Ic kill % Ns Ar job 1354.It Ic kill Ar pid ... 1355.It Ic kill Fl l Op Ar exit_status 1356.It Ic kill Fl s Ar signal_name pid ... 1357.It Ic kill Fl Ar signal_name Ar pid ... 1358.It Ic kill Fl Ar signal_number Ar pid ... 1359Sends either the TERM (terminate) signal or the 1360specified signal to the specified jobs or processes. 1361Signals are either given by number or by names (as given in 1362.Aq Pa signal.h , 1363stripped of the prefix ``SIG''). 1364The signal names are listed by ``kill \-l''; 1365if an 1366.Ar exit_status 1367is specified, only the corresponding signal name will be written. 1368There is no default, just saying `kill' does not 1369send a signal to the current job. 1370If the signal being sent is TERM (terminate) or HUP (hangup), 1371then the job or process will be sent a CONT (continue) signal as well. 1372.Pp 1373.It Ic limit 1374.It Ic limit Ar resource 1375.It Ic limit Ar resource maximum-use 1376.It Ic limit Fl h 1377.It Ic limit Fl h Ar resource 1378.It Ic limit Fl h Ar resource maximum-use 1379Manipulates per-process system resource limits via the 1380.Xr getrlimit 2 1381and 1382.Xr setrlimit 2 1383system calls; this 1384limits the consumption by the current process and each process 1385it creates to not individually exceed 1386.Ar maximum-use 1387on the 1388specified 1389.Ar resource . 1390If no 1391.Ar maximum-use 1392is given, then 1393the current limit is printed; if no 1394.Ar resource 1395is given, then 1396all limitations are given. 1397.Pp 1398If the 1399.Fl h 1400flag is given, the hard limits are used instead of the current 1401limits. 1402The hard limits impose a ceiling on the values of the current limits. 1403Only the super-user may raise the hard limits, 1404but a user may lower or raise the current limits within the legal range. 1405.Pp 1406Resources controllable currently include: 1407.Bl -tag -width coredumpsize 1408.It Ar cputime 1409The maximum number of CPU-seconds to be used by each process. 1410.It Ar filesize 1411The largest single file (in bytes) that can be created. 1412.It Ar datasize 1413The maximum growth of the data+stack region via 1414.Xr sbrk 2 1415beyond the end of the program text. 1416.It Ar stacksize 1417The maximum 1418size of the automatically-extended stack region. 1419.It Ar coredumpsize 1420The size of the largest core dump (in bytes) that will be created. 1421.It Ar memoryuse 1422The maximum size (in bytes) to which a process's resident set 1423size (RSS) may grow. 1424.It Ar memorylocked 1425The maximum size (in bytes) which a process may lock into memory using the 1426.Xr mlock 2 1427function. 1428.It Ar maxproc 1429The maximum number of simultaneous processes for this user id. 1430.It Ar openfiles 1431The maximum number of simultaneous open files for this user id. 1432.It Ar sbsize 1433The maximum socket buffer size of a process (in bytes). 1434.El 1435.Pp 1436The 1437.Ar maximum-use 1438may be given as a (floating point or integer) 1439number followed by a scale factor. 1440For all limits other than 1441.Ar cputime 1442the default scale is `k' or `kilobytes' (1024 bytes); 1443a scale factor of `m' or `megabytes' may also be used. 1444For 1445.Ar cputime 1446the default scale is `seconds'; 1447a scale factor of `m' for minutes 1448or `h' for hours, or a time of the form `mm:ss' giving minutes 1449and seconds also may be used. 1450.Pp 1451For both 1452.Ar resource 1453names and scale factors, unambiguous prefixes 1454of the names suffice. 1455.Pp 1456Limits of an arbitrary process can be displayed or set using the 1457.Xr sysctl 8 1458utility. 1459See the 1460.Xr getrlimit 2 1461and 1462.Xr setrlimit 2 1463man pages for an additional description of system resource limits. 1464.Pp 1465.It Ic login 1466Terminate a login shell, replacing it with an instance of 1467.Pa /usr/bin/login . 1468This is one way to log off, included for compatibility with 1469.Xr sh 1 . 1470.Pp 1471.It Ic logout 1472Terminate a login shell. 1473Especially useful if 1474.Ic ignoreeof 1475is set. 1476.Pp 1477.It Ic nice 1478.It Ic nice Ar +number 1479.It Ic nice Ar command 1480.It Ic nice Ar +number command 1481The first form sets the 1482scheduling priority 1483for this shell to 4. 1484The second form sets the 1485priority 1486to the given 1487.Ar number . 1488The final two forms run command at priority 4 and 1489.Ar number 1490respectively. 1491The greater the number, the less CPU the process will get. 1492The super-user may specify negative priority by using `nice \-number ...'. 1493.Ar Command 1494is always executed in a sub-shell, and the restrictions 1495placed on commands in simple 1496.Ic if 1497statements apply. 1498.Pp 1499.It Ic nohup 1500.It Ic nohup Ar command 1501The first form can be used in shell scripts to cause hangups to be 1502ignored for the remainder of the script. 1503The second form causes the specified command to be run with hangups 1504ignored. 1505All processes detached with `\*[Am]' are effectively 1506.Ic nohup Ns \'ed . 1507.Pp 1508.It Ic notify 1509.It Ic notify % Ns Ar job ... 1510Causes the shell to notify the user asynchronously when the status of the 1511current or specified jobs change; normally notification is presented 1512before a prompt. 1513This is automatic if the shell variable 1514.Ic notify 1515is set. 1516.Pp 1517.It Ic onintr 1518.It Ic onintr Fl 1519.It Ic onintr Ar label 1520Control the action of the shell on interrupts. 1521The first form restores the default action of the shell on interrupts 1522which is to terminate shell scripts or to return to the terminal command 1523input level. 1524The second form `onintr \-' causes all interrupts to be ignored. 1525The final form causes the shell to execute a `goto label' when 1526an interrupt is received or a child process terminates because 1527it was interrupted. 1528.Pp 1529In any case, if the shell is running detached and interrupts are 1530being ignored, all forms of 1531.Ic onintr 1532have no meaning and interrupts 1533continue to be ignored by the shell and all invoked commands. 1534Finally 1535.Ic onintr 1536statements are ignored in the system startup files where interrupts 1537are disabled (/etc/csh.cshrc, /etc/csh.login). 1538.Pp 1539.It Ic popd 1540.It Ic popd Ar +n 1541Pops the directory stack, returning to the new top directory. 1542With an argument 1543.Ns \`+ Ar n Ns \' 1544discards the 1545.Ar n Ns \'th 1546entry in the stack. 1547The members of the directory stack are numbered from the top starting at 0. 1548.Pp 1549.It Ic pushd 1550.It Ic pushd Ar name 1551.It Ic pushd Ar +n 1552With no arguments, 1553.Ic pushd 1554exchanges the top two elements of the directory stack. 1555Given a 1556.Ar name 1557argument, 1558.Ic pushd 1559changes to the new directory (ala 1560.Ic cd ) 1561and pushes the old current working directory 1562(as in 1563.Ic cwd ) 1564onto the directory stack. 1565With a numeric argument, 1566.Ic pushd 1567rotates the 1568.Ar n Ns \'th 1569argument of the directory 1570stack around to be the top element and changes to it. 1571The members 1572of the directory stack are numbered from the top starting at 0. 1573.Pp 1574.It Ic rehash 1575Causes the internal hash table of the contents of the directories in 1576the 1577.Ic path 1578variable to be recomputed. 1579This is needed if new commands are added to directories in the 1580.Ic path 1581while you are logged in. 1582This should only be necessary if you add 1583commands to one of your own directories, or if a systems programmer 1584changes the contents of a system directory. 1585.Pp 1586.It Ic repeat Ar count command 1587The specified 1588.Ar command , 1589which is subject to the same restrictions 1590as the 1591.Ar command 1592in the one line 1593.Ic if 1594statement above, 1595is executed 1596.Ar count 1597times. 1598I/O redirections occur exactly once, even if 1599.Ar count 1600is 0. 1601.Pp 1602.It Ic set 1603.It Ic set Ar name 1604.It Ic set Ar name Ns =word 1605.It Ic set Ar name[index] Ns =word 1606.It Ic set Ar name Ns =(wordlist) 1607The first form of the command shows the value of all shell variables. 1608Variables that have other than a single word as their 1609value print as a parenthesized word list. 1610The second form sets 1611.Ar name 1612to the null string. 1613The third form sets 1614.Ar name 1615to the single 1616.Ar word . 1617The fourth form sets 1618the 1619.Ar index Ns 'th 1620component of 1621.Ar name 1622to 1623.Ar word ; 1624this component must already exist. 1625The final form sets 1626.Ar name 1627to the list of words in 1628.Ar wordlist . 1629The value is always command and filename expanded. 1630.Pp 1631These arguments may be repeated to set multiple values in a single set command. 1632Note however, that variable expansion happens for all arguments before any 1633setting occurs. 1634.Pp 1635.It Ic setenv 1636.It Ic setenv Ar name 1637.It Ic setenv Ar name value 1638The first form lists all current environment variables. 1639It is equivalent to 1640.Xr printenv 1 . 1641The last form sets the value of environment variable 1642.Ar name 1643to be 1644.Ar value , 1645a single string. 1646The second form sets 1647.Ar name 1648to an empty string. 1649The most commonly used environment variables 1650.Ev USER , 1651.Ev TERM , 1652and 1653.Ev PATH 1654are automatically imported to and exported from the 1655.Nm 1656variables 1657.Ar user , 1658.Ar term , 1659and 1660.Ar path ; 1661there is no need to use 1662.Ic setenv 1663for these. 1664.Pp 1665.It Ic shift 1666.It Ic shift Ar variable 1667The members of 1668.Ic argv 1669are shifted to the left, discarding 1670.Ic argv Ns Bq 1 . 1671It is an error for 1672.Ic argv 1673not to be set or to have less than one word as value. 1674The second form performs the same function on the specified variable. 1675.Pp 1676.It Ic source Ar name 1677.It Ic source Fl h Ar name 1678The shell reads commands from 1679.Ar name . 1680.Ic Source 1681commands may be nested; if they are nested too deeply the shell may 1682run out of file descriptors. 1683An error in a 1684.Ic source 1685at any level terminates all nested 1686.Ic source 1687commands. 1688Normally input during 1689.Ic source 1690commands is not placed on the history list; 1691the \-h option causes the commands to be placed on the 1692history list without being executed. 1693.Pp 1694.It Ic stop 1695.It Ic stop % Ns Ar job ... 1696Stops the current or specified jobs that are executing in the background. 1697.Pp 1698.It Ic suspend 1699Causes the shell to stop in its tracks, much as if it had been sent a stop 1700signal with 1701.Ic ^Z . 1702This is most often used to stop shells started by 1703.Xr su 1 . 1704.Pp 1705.It Ic switch Ar ( string ) 1706.It Ic case Ar str1 : 1707.It \ \ \ \ \&... 1708.It Ic \ \ \ \ breaksw 1709.It \ \ \ \ \&... 1710.It Ic default : 1711.It \ \ \ \ \&... 1712.It Ic \ \ \ \ breaksw 1713.It Ic endsw 1714Each case label is successively matched against the specified 1715.Ar string 1716which is first command and filename expanded. 1717The file metacharacters `*', `?' and `[...]' 1718may be used in the case labels, 1719which are variable expanded. 1720If none of the labels match before the `default' label is found, then 1721the execution begins after the default label. 1722Each case label and the default label must appear at the beginning of a line. 1723The command 1724.Ic breaksw 1725causes execution to continue after the 1726.Ic endsw . 1727Otherwise control may fall through case labels and the default label as in C. 1728If no label matches and there is no default, execution continues after 1729the 1730.Ic endsw . 1731.Pp 1732.It Ic time 1733.It Ic time Ar command 1734With no argument, a summary of time used by this shell and its children 1735is printed. 1736If arguments are given 1737the specified simple command is timed and a time summary 1738as described under the 1739.Ic time 1740variable is printed. 1741If necessary, an extra shell is created to print the time 1742statistic when the command completes. 1743.Pp 1744.It Ic umask 1745.It Ic umask Ar value 1746The file creation mask is displayed (first form) or set to the specified 1747value (second form). 1748The mask is given in octal. 1749Common values for 1750the mask are 002 giving all access to the group and read and execute 1751access to others or 022 giving all access except write access for 1752users in the group or others. 1753.Pp 1754.It Ic unalias Ar pattern 1755All aliases whose names match the specified pattern are discarded. 1756Thus all aliases are removed by `unalias *'. 1757It is not an error for nothing to be 1758.Ic unaliased . 1759.Pp 1760.It Ic unhash 1761Use of the internal hash table to speed location of executed programs 1762is disabled. 1763.Pp 1764.It Ic unlimit 1765.It Ic unlimit Ar resource 1766.It Ic unlimit Fl h 1767.It Ic unlimit Fl h Ar resource 1768Removes the limitation on 1769.Ar resource . 1770If no 1771.Ar resource 1772is specified, then all 1773.Ar resource 1774limitations are removed. 1775If 1776.Fl h 1777is given, the corresponding hard limits are removed. 1778Only the 1779super-user may do this. 1780.Pp 1781.It Ic unset Ar pattern 1782All variables whose names match the specified pattern are removed. 1783Thus all variables are removed by `unset *'; this has noticeably 1784distasteful side-effects. 1785It is not an error for nothing to be 1786.Ic unset . 1787.Pp 1788.It Ic unsetenv Ar pattern 1789Removes all variables whose name match the specified pattern from the 1790environment. 1791See also the 1792.Ic setenv 1793command above and 1794.Xr printenv 1 . 1795.Pp 1796.It Ic wait 1797Wait for all background jobs. 1798If the shell is interactive, then an interrupt can disrupt the wait. 1799After the interrupt, the shell prints names and job numbers of all jobs 1800known to be outstanding. 1801.Pp 1802.It Ic which Ar command 1803Displays the resolved command that will be executed by the shell. 1804.Pp 1805.It Ic while Ar ( expr ) 1806.It \&... 1807.It Ic end 1808While the specified expression evaluates non-zero, the commands between 1809the 1810.Ic while 1811and the matching 1812.Ic end 1813are evaluated. 1814.Ic Break 1815and 1816.Ic continue 1817may be used to terminate or continue the loop prematurely. 1818(The 1819.Ic while 1820and 1821.Ic end 1822must appear alone on their input lines.) 1823Prompting occurs here the first time through the loop as for the 1824.Ic foreach 1825statement if the input is a terminal. 1826.Pp 1827.It Ic % Ns Ar job 1828Brings the specified job into the foreground. 1829.Pp 1830.It Ic % Ns Ar job Ic \*[Am] 1831Continues the specified job in the background. 1832.Pp 1833.It Ic @ 1834.It Ic @ Ar name Ns = expr 1835.It Ic @ Ar name[index] Ns = expr 1836The first form prints the values of all the shell variables. 1837The second form sets the specified 1838.Ar name 1839to the value of 1840.Ar expr . 1841If the expression contains `\*[Lt]', `\*[Gt]', `\*[Am]' or `|' then at least 1842this part of the expression must be placed within `(' `)'. 1843The third form assigns the value of 1844.Ar expr 1845to the 1846.Ar index Ns 'th 1847argument of 1848.Ar name . 1849Both 1850.Ar name 1851and its 1852.Ar index Ns 'th 1853component must already exist. 1854.El 1855.Pp 1856The operators `*=', `+=', etc are available as in C. 1857The space separating the name from the assignment operator is optional. 1858Spaces are, however, mandatory in separating components of 1859.Ar expr 1860which would otherwise be single words. 1861.Pp 1862Special postfix `+\|+' and `\-\|\-' operators increment and decrement 1863.Ar name 1864respectively, i.e., `@ i++'. 1865.Ss Pre-defined and environment variables 1866The following variables have special meaning to the shell. 1867Of these, 1868.Ar argv , 1869.Ar cwd , 1870.Ar home , 1871.Ar path , 1872.Ar prompt , 1873.Ar shell 1874and 1875.Ar status 1876are always set by the shell. 1877Except for 1878.Ar cwd 1879and 1880.Ar status , 1881this setting occurs only at initialization; 1882these variables will not then be modified unless done 1883explicitly by the user. 1884.Pp 1885The shell copies the environment variable 1886.Ev USER 1887into the variable 1888.Ar user , 1889.Ev TERM 1890into 1891.Ar term , 1892and 1893.Ev HOME 1894into 1895.Ar home , 1896and copies these back into the environment whenever the normal 1897shell variables are reset. 1898The environment variable 1899.Ev PATH 1900is likewise handled; it is not 1901necessary to worry about its setting other than in the file 1902.Ar \&.cshrc 1903as inferior 1904.Nm 1905processes will import the definition of 1906.Ar path 1907from the environment, and re-export it if you then change it. 1908.Bl -tag -width histchars 1909.It Ic argv 1910Set to the arguments to the shell, it is from this variable that 1911positional parameters are substituted, i.e., `$1' is replaced by 1912`$argv[1]', 1913etc. 1914.It Ic cdpath 1915Gives a list of alternative directories searched to find subdirectories 1916in 1917.Ar chdir 1918commands. 1919.It Ic cwd 1920The full pathname of the current directory. 1921.It Ic echo 1922Set when the 1923.Fl x 1924command line option is given. 1925Causes each command and its arguments 1926to be echoed just before it is executed. 1927For non-builtin commands all expansions occur before echoing. 1928Builtin commands are echoed before command and filename substitution, 1929since these substitutions are then done selectively. 1930.It Ic filec 1931Enable file name completion. 1932.It Ic histchars 1933Can be given a string value to change the characters used in history 1934substitution. 1935The first character of its value is used as the 1936history substitution character, replacing the default character `!'. 1937The second character of its value replaces the character `^' in 1938quick substitutions. 1939.It Ic histfile 1940Can be set to the pathname where history is going to be saved/restored. 1941.It Ic history 1942Can be given a numeric value to control the size of the history list. 1943Any command that has been referenced in this many events will not be 1944discarded. 1945Too large values of 1946.Ar history 1947may run the shell out of memory. 1948The last executed command is always saved on the history list. 1949.It Ic home 1950The home directory of the invoker, initialized from the environment. 1951The filename expansion of 1952.Sq Pa ~ 1953refers to this variable. 1954.It Ic ignoreeof 1955If set the shell ignores 1956end-of-file from input devices which are terminals. 1957This prevents shells from accidentally being killed by control-D's. 1958.It Ic mail 1959The files where the shell checks for mail. 1960This checking is done after each command completion that will 1961result in a prompt, 1962if a specified interval has elapsed. 1963The shell says `You have new mail.' 1964if the file exists with an access time not greater than its modify time. 1965.Pp 1966If the first word of the value of 1967.Ar mail 1968is numeric it specifies a different mail checking interval, in seconds, 1969than the default, which is 10 minutes. 1970.Pp 1971If multiple mail files are specified, then the shell says 1972`New mail in 1973.Ar name Ns ' 1974when there is mail in the file 1975.Ar name . 1976.It Ic noclobber 1977As described in the section on 1978.Sx input/output , 1979restrictions are placed on output redirection to ensure that 1980files are not accidentally destroyed, and that `\*[Gt]\*[Gt]' redirections 1981refer to existing files. 1982.It Ic noglob 1983If set, filename expansion is inhibited. 1984This inhibition is most useful in shell scripts that 1985 are not dealing with filenames, 1986or after a list of filenames has been obtained and further expansions 1987are not desirable. 1988.It Ic nonomatch 1989If set, it is not an error for a filename expansion to not match any 1990existing files; instead the primitive pattern is returned. 1991It is still an error for the primitive pattern to be malformed, i.e., 1992`echo [' 1993still gives an error. 1994.It Ic notify 1995If set, the shell notifies asynchronously of job completions; 1996the default is to present job completions just before printing 1997a prompt. 1998.It Ic path 1999Each word of the path variable specifies a directory in which 2000commands are to be sought for execution. 2001A null word specifies the current directory. 2002If there is no 2003.Ar path 2004variable then only full path names will execute. 2005The usual search path is `.', `/bin' and `/usr/bin', but this 2006may vary from system to system. 2007For the super-user the default search path is `/etc', `/bin' and `/usr/bin'. 2008A shell that is given neither the 2009.Fl c 2010nor the 2011.Fl t 2012option will normally hash the contents of the directories in the 2013.Ar path 2014variable after reading 2015.Ar \&.cshrc , 2016and each time the 2017.Ar path 2018variable is reset. 2019If new commands are added to these directories 2020while the shell is active, it may be necessary to do a 2021.Ic rehash 2022or the commands may not be found. 2023.It Ic prompt 2024The string that is printed before each command is read from 2025an interactive terminal input. 2026If a `!' appears in the string it will be replaced by the current event number 2027unless a preceding `\e' is given. 2028Default is `% ', or `# ' for the super-user. 2029.It Ic savehist 2030Is given a numeric value to control the number of entries of the 2031history list that are saved in ~/.history when the user logs out. 2032Any command that has been referenced in this many events will be saved. 2033During start up the shell sources ~/.history into the history list 2034enabling history to be saved across logins. 2035Too large values of 2036.Ar savehist 2037will slow down the shell during start up. 2038If 2039.Ar savehist 2040is just set, the shell will use the value of 2041.Ar history . 2042.It Ic shell 2043The file in which the shell resides. 2044This variable is used in forking shells to interpret files that have execute 2045bits set, but which are not executable by the system. 2046(See the description of 2047.Sx Non-builtin Command Execution 2048below.) 2049Initialized to the (system-dependent) home of the shell. 2050.It Ic status 2051The status returned by the last command. 2052If it terminated abnormally, then 0200 is added to the status. 2053Builtin commands that fail return exit status `1', 2054all other builtin commands set status to `0'. 2055.It Ic time 2056Controls automatic timing of commands. 2057This setting allows two parameters. 2058The first specifies the CPU time threshold at which reporting should be done 2059for a process, and the optional second specifies the output format. 2060The following format strings are available: 2061.Pp 2062.Bl -tag -width Ds -compact -offset indent 2063.It Li \&%c 2064Number of involuntary context switches. 2065.It Li \&%D 2066Average unshared data size. 2067.It Li \&%E 2068Elapsed (wall\-clock) time. 2069.It Li \&%F 2070Page faults. 2071.It Li \&%I 2072Filesystem blocks in. 2073.It Li \&%K 2074Average total data memory used. 2075.It Li \&%k 2076Number of signals received. 2077.It Li \&%M 2078Maximum Resident Set Size. 2079.It Li \&%O 2080Filesystem blocks out. 2081.It Li \&%P 2082Total percent time spent running. 2083.It Li \&%R 2084Page reclaims. 2085.It Li \&%r 2086Socket messages received. 2087.It Li \&%S 2088Total system CPU time used. 2089.It Li \&%s 2090Socket messages sent. 2091.It Li \&%U 2092Total user CPU time used. 2093.It Li \&%W 2094Number of swaps. 2095.It Li \&%w 2096Number of voluntary context switches (waits). 2097.It Li \&%X 2098Average shared text size. 2099.El 2100.Pp 2101The default summary is "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww" 2102.It Ic verbose 2103Set by the 2104.Fl v 2105command line option, causes the words of each command to be printed 2106after history substitution. 2107.El 2108.Ss Non-builtin command execution 2109When a command to be executed is found to not be a builtin command 2110the shell attempts to execute the command via 2111.Xr execve 2 . 2112Each word in the variable 2113.Ar path 2114names a directory from which the shell will attempt to execute the command. 2115If it is given neither a 2116.Fl c 2117nor a 2118.Fl t 2119option, the shell will hash the names in these directories into an internal 2120table so that it will only try an 2121.Ic exec 2122in a directory if there is a possibility that the command resides there. 2123This shortcut greatly speeds command location when many directories 2124are present in the search path. 2125If this mechanism has been turned off (via 2126.Ic unhash ) , 2127or if the shell was given a 2128.Fl c 2129or 2130.Fl t 2131argument, and in any case for each directory component of 2132.Ar path 2133that does not begin with a `/', 2134the shell concatenates with the given command name to form a path name 2135of a file which it then attempts to execute. 2136.Pp 2137Parenthesized commands are always executed in a subshell. 2138Thus 2139.Pp 2140.Dl (cd ; pwd) ; pwd 2141.Pp 2142prints the 2143.Ar home 2144directory; leaving you where you were (printing this after the home directory), 2145while 2146.Pp 2147.Dl cd ; pwd 2148.Pp 2149leaves you in the 2150.Ar home 2151directory. 2152Parenthesized commands are most often used to prevent 2153.Ic chdir 2154from affecting the current shell. 2155.Pp 2156If the file has execute permissions but is not an 2157executable binary to the system, then it is assumed to be a 2158file containing shell commands and a new shell is spawned to read it. 2159.Pp 2160If there is an 2161.Ic alias 2162for 2163.Ic shell 2164then the words of the alias will be prepended to the argument list to form 2165the shell command. 2166The first word of the 2167.Ic alias 2168should be the full path name of the shell 2169(e.g., `$shell'). 2170Note that this is a special, late occurring, case of 2171.Ic alias 2172substitution, 2173and only allows words to be prepended to the argument list without change. 2174.Ss Signal handling 2175The shell normally ignores 2176.Ar quit 2177signals. 2178Jobs running detached (either by 2179.Ic \&\*[Am] 2180or the 2181.Ic bg 2182or 2183.Ic %... \*[Am] 2184commands) are immune to signals generated from the keyboard, including 2185hangups. 2186Other signals have the values which the shell inherited from its parent. 2187The shell's handling of interrupts and terminate signals 2188in shell scripts can be controlled by 2189.Ic onintr . 2190Login shells catch the 2191.Ar terminate 2192signal; otherwise this signal is passed on to children from the state in the 2193shell's parent. 2194Interrupts are not allowed when a login shell is reading the file 2195.Pa \&.logout . 2196.Sh FILES 2197.Bl -tag -width /etc/passwd -compact 2198.It Pa ~/.cshrc 2199Read at beginning of execution by each shell. 2200.It Pa ~/.login 2201Read by login shell, after `.cshrc' at login. 2202.It Pa ~/.logout 2203Read by login shell, at logout. 2204.It Pa /bin/sh 2205Standard shell, for shell scripts not starting with a `#'. 2206.It Pa /tmp/sh* 2207Temporary file for `\*[Lt]\*[Lt]'. 2208.It Pa /etc/passwd 2209Source of home directories for `~name'. 2210.El 2211.Sh LIMITATIONS 2212Word lengths \- 2213Words can be no longer than 1024 characters. 2214The system limits argument lists to 10240 characters. 2215The number of arguments to a command that involves filename expansion 2216is limited to 1/6'th the number of characters allowed in an argument list. 2217Command substitutions may substitute no more characters than are 2218allowed in an argument list. 2219To detect looping, the shell restricts the number of 2220.Ic alias 2221substitutions on a single line to 20. 2222.Sh SEE ALSO 2223.Xr sh 1 , 2224.Xr access 2 , 2225.Xr execve 2 , 2226.Xr fork 2 , 2227.Xr pipe 2 , 2228.Xr setrlimit 2 , 2229.Xr sigaction 2 , 2230.Xr umask 2 , 2231.Xr wait 2 , 2232.Xr killpg 3 , 2233.Xr tty 4 , 2234.Xr a.out 5 , 2235.Xr environ 7 , 2236.Xr sysctl 8 2237.br 2238.Em "An introduction to the C shell" 2239.Sh HISTORY 2240.Nm 2241appeared in 2242.Bx 3 . 2243It was a first implementation of a command language interpreter 2244incorporating a history mechanism (see 2245.Sx History Substitutions ) , 2246job control facilities (see 2247.Sx Jobs ) , 2248interactive file name 2249and user name completion (see 2250.Sx File Name Completion ) , 2251and a C-like syntax. 2252There are now many shells that also have these mechanisms, plus 2253a few more (and maybe some bugs too), which are available through the 2254usenet. 2255.Sh AUTHORS 2256William Joy. 2257Job control and directory stack features first implemented by J.E. Kulp of 2258IIASA, Laxenburg, Austria, 2259with different syntax than that used now. 2260File name completion code written by Ken Greer, HP Labs. 2261Eight-bit implementation Christos S. Zoulas, Cornell University. 2262.Sh BUGS 2263When a command is restarted from a stop, 2264the shell prints the directory it started in if this is different 2265from the current directory; this can be misleading (i.e., wrong) 2266as the job may have changed directories internally. 2267.Pp 2268Shell builtin functions are not stoppable/restartable. 2269Command sequences of the form `a ; b ; c' are also not handled gracefully 2270when stopping is attempted. 2271If you suspend `b', the shell will immediately execute `c'. 2272This is especially noticeable if this expansion results from an 2273.Ar alias . 2274It suffices to place the sequence of commands in ()'s to force it to 2275a subshell, i.e., `( a ; b ; c )'. 2276.Pp 2277Control over tty output after processes are started is primitive; 2278perhaps this will inspire someone to work on a good virtual 2279terminal interface. 2280In a virtual terminal interface much more 2281interesting things could be done with output control. 2282.Pp 2283Alias substitution is most often used to clumsily simulate shell procedures; 2284shell procedures should be provided instead of aliases. 2285.Pp 2286Commands within loops, prompted for by `?', are not placed on the 2287.Ic history 2288list. 2289Control structure should be parsed instead of being recognized as built-in 2290commands. 2291This would allow control commands to be placed anywhere, 2292to be combined with `\&|', and to be used with `\*[Am]' and `;' metasyntax. 2293.Pp 2294It should be possible to use the `:' modifiers on the output of command 2295substitutions. 2296.Pp 2297The way the 2298.Ic filec 2299facility is implemented is ugly and expensive. 2300