1.\" $NetBSD: csh.1,v 1.53 2016/08/10 17:16:47 sevan 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 August 8, 2016 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 49809 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 bg 1099.It Ic bg \&% Ns Ar job ... 1100Puts the current or specified jobs into the background, continuing them 1101if they were stopped. 1102.Pp 1103.It Ic break 1104Causes execution to resume after the 1105.Ic end 1106of the nearest enclosing 1107.Ic foreach 1108or 1109.Ic while . 1110The remaining commands on the current line are executed. 1111Multi-level breaks are thus possible by writing them all on one line. 1112.Pp 1113.It Ic breaksw 1114Causes a break from a 1115.Ic switch , 1116resuming after the 1117.Ic endsw . 1118.Pp 1119.It Ic case Ar label : 1120A label in a 1121.Ic switch 1122statement as discussed below. 1123.Pp 1124.It Ic cd 1125.It Ic cd Ar name 1126.It Ic chdir 1127.It Ic chdir Ar name 1128Change the shell's working directory to directory 1129.Ar name . 1130If no argument is given then change to the home directory of the user. 1131If 1132.Ar name 1133is not found as a subdirectory of the current directory (and does not begin 1134with `/', `./' or `../'), then each 1135component of the variable 1136.Ic cdpath 1137is checked to see if it has a subdirectory 1138.Ar name . 1139Finally, if all else fails but 1140.Ar name 1141is a shell variable whose value begins with `/', then this 1142is tried to see if it is a directory. 1143.Pp 1144.It Ic continue 1145Continue execution of the nearest enclosing 1146.Ic while 1147or 1148.Ic foreach . 1149The rest of the commands on the current line are executed. 1150.Pp 1151.It Ic default : 1152Labels the default case in a 1153.Ic switch 1154statement. 1155The default should come after all 1156.Ic case 1157labels. 1158.Pp 1159.It Ic dirs 1160Prints the directory stack; the top of the stack is at the left, 1161the first directory in the stack being the current directory. 1162.Pp 1163.It Ic echo Ar wordlist 1164.It Ic echo Fl n Ar wordlist 1165The specified words are written to the shell's standard output, separated 1166by spaces, and terminated with a newline unless the 1167.Fl n 1168option is specified. 1169.Pp 1170.It Ic else 1171.It Ic end 1172.It Ic endif 1173.It Ic endsw 1174See the description of the 1175.Ic foreach , 1176.Ic if , 1177.Ic switch , 1178and 1179.Ic while 1180statements below. 1181.Pp 1182.It Ic eval Ar arg ... 1183(As in 1184.Xr sh 1 . ) 1185The arguments are read as input to the shell and the resulting 1186command(s) executed in the context of the current shell. 1187This is usually used to execute commands 1188generated as the result of command or variable substitution, since 1189parsing occurs before these substitutions. 1190See 1191.Xr tset 1 1192for an example of using 1193.Ic eval . 1194.Pp 1195.It Ic exec Ar command 1196The specified command is executed in place of the current shell. 1197.Pp 1198.It Ic exit 1199.It Ic exit Ar ( expr ) 1200The shell exits either with the value of the 1201.Ic status 1202variable (first form) or with the value of the specified 1203.Ic expr 1204(second form). 1205.Pp 1206.It Ic fg 1207.It Ic fg % Ns Ar job ... 1208Brings the current or specified jobs into the foreground, continuing them if 1209they were stopped. 1210.Pp 1211.It Ic foreach Ar name ( wordlist ) 1212.It ... 1213.It Ic end 1214The variable 1215.Ic name 1216is successively set to each member of 1217.Ic wordlist 1218and the sequence of commands between this command and the matching 1219.Ic end 1220are executed. 1221(Both 1222.Ic foreach 1223and 1224.Ic end 1225must appear alone on separate lines.) 1226The builtin command 1227.Ic continue 1228may be used to continue the loop prematurely and the builtin 1229command 1230.Ic break 1231to terminate it prematurely. 1232When this command is read from the terminal, the loop is read once 1233prompting with `?' before any statements in the loop are executed. 1234If you make a mistake typing in a loop at the terminal you can rub it out. 1235.Pp 1236.It Ic glob Ar wordlist 1237Like 1238.Ic echo 1239but no `\e' escapes are recognized and words are delimited 1240by null characters in the output. 1241Useful for programs that wish to use the shell to filename expand a list 1242of words. 1243.Pp 1244.It Ic goto Ar word 1245The specified 1246.Ic word 1247is filename and command expanded to yield a string of the form `label'. 1248The shell rewinds its input as much as possible 1249and searches for a line of the form `label:' 1250possibly preceded by blanks or tabs. 1251Execution continues after the specified line. 1252.Pp 1253.It Ic hashstat 1254Print a statistics line showing how effective the internal hash 1255table has been at locating commands (and avoiding 1256.Ic exec Ns \'s ) . 1257An 1258.Ic exec 1259is attempted for each component of the 1260.Em path 1261where the hash function indicates a possible hit, and in each component 1262that does not begin with a `/'. 1263.Pp 1264.It Ic history 1265.It Ic history Ar n 1266.It Ic history Fl r Ar n 1267.It Ic history Fl h Ar n 1268Displays the history event list; if 1269.Ar n 1270is given only the 1271.Ar n 1272most recent events are printed. 1273The 1274.Fl r 1275option reverses the order of printout to be most recent first 1276instead of oldest first. 1277The 1278.Fl h 1279option causes the history list to be printed without leading numbers. 1280This format produces files suitable for sourcing using the \-h 1281option to 1282.Ic source . 1283.Pp 1284.It Ic if Ar ( expr ) No command 1285If the specified expression evaluates true, then the single 1286.Ar command 1287with arguments is executed. 1288Variable substitution on 1289.Ar command 1290happens early, at the same 1291time it does for the rest of the 1292.Ic if 1293command. 1294.Ar Command 1295must be a simple command, not 1296a pipeline, a command list, or a parenthesized command list. 1297Input/output redirection occurs even if 1298.Ar expr 1299is false, i.e., when command is 1300.Em not 1301executed (this is a bug). 1302.Pp 1303.It Ic if Ar ( expr ) Ic then 1304.It ... 1305.It Ic else if Ar ( expr2 ) Ic then 1306.It ... 1307.It Ic else 1308.It ... 1309.It Ic endif 1310If the specified 1311.Ar expr 1312is true then the commands up to the first 1313.Ic else 1314are executed; otherwise if 1315.Ar expr2 1316is true then the commands up to the 1317second 1318.Ic else 1319are executed, etc. 1320Any number of 1321.Ic else-if 1322pairs are possible; only one 1323.Ic endif 1324is needed. 1325The 1326.Ic else 1327part is likewise optional. 1328(The words 1329.Ic else 1330and 1331.Ic endif 1332must appear at the beginning of input lines; 1333the 1334.Ic if 1335must appear alone on its input line or after an 1336.Ic else . ) 1337.Pp 1338.It Ic jobs 1339.It Ic jobs Fl l 1340Lists the active jobs; the 1341.Fl l 1342option lists process id's in addition to the normal information. 1343.Pp 1344.It Ic kill % Ns Ar job 1345.It Ic kill Ar pid ... 1346.It Ic kill Fl l Op Ar exit_status 1347.It Ic kill Fl s Ar signal_name pid ... 1348.It Ic kill Fl Ar signal_name Ar pid ... 1349.It Ic kill Fl Ar signal_number Ar pid ... 1350Sends either the TERM (terminate) signal or the 1351specified signal to the specified jobs or processes. 1352Signals are either given by number or by names (as given in 1353.In signal.h , 1354stripped of the prefix ``SIG''). 1355The signal names are listed by ``kill \-l''; 1356if an 1357.Ar exit_status 1358is specified, only the corresponding signal name will be written. 1359There is no default, just saying `kill' does not 1360send a signal to the current job. 1361If the signal being sent is TERM (terminate) or HUP (hangup), 1362then the job or process will be sent a CONT (continue) signal as well. 1363.Pp 1364.It Ic limit 1365.It Ic limit Ar resource 1366.It Ic limit Ar resource maximum-use 1367.It Ic limit Fl h 1368.It Ic limit Fl h Ar resource 1369.It Ic limit Fl h Ar resource maximum-use 1370Manipulates per-process system resource limits via the 1371.Xr getrlimit 2 1372and 1373.Xr setrlimit 2 1374system calls; this 1375limits the consumption by the current process and each process 1376it creates to not individually exceed 1377.Ar maximum-use 1378on the 1379specified 1380.Ar resource . 1381If no 1382.Ar maximum-use 1383is given, then 1384the current limit is printed; if no 1385.Ar resource 1386is given, then 1387all limitations are given. 1388.Pp 1389If the 1390.Fl h 1391flag is given, the hard limits are used instead of the current 1392limits. 1393The hard limits impose a ceiling on the values of the current limits. 1394Only the super-user may raise the hard limits, 1395but a user may lower or raise the current limits within the legal range. 1396.Pp 1397Resources controllable currently include: 1398.Bl -tag -width coredumpsize 1399.It Ar cputime 1400The maximum number of CPU-seconds to be used by each process. 1401.It Ar filesize 1402The largest single file (in bytes) that can be created. 1403.It Ar datasize 1404The maximum growth of the data+stack region via 1405.Xr sbrk 2 1406beyond the end of the program text. 1407.It Ar stacksize 1408The maximum 1409size of the automatically-extended stack region. 1410.It Ar coredumpsize 1411The size of the largest core dump (in bytes) that will be created. 1412.It Ar memoryuse 1413The maximum size (in bytes) to which a process's resident set 1414size (RSS) may grow. 1415.It Ar memorylocked 1416The maximum size (in bytes) which a process may lock into memory using the 1417.Xr mlock 2 1418function. 1419.It Ar maxproc 1420The maximum number of simultaneous processes for this user id. 1421.It Ar openfiles 1422The maximum number of simultaneous open files for this user id. 1423.It Ar sbsize 1424The maximum socket buffer size of a process (in bytes). 1425.It Ar vmemoryuse 1426The maximum size (in bytes) which a process can obtain. 1427.El 1428.Pp 1429The 1430.Ar maximum-use 1431may be given as a (floating point or integer) 1432number followed by a scale factor. 1433For all limits other than 1434.Ar cputime 1435the default scale is `k' or `kilobytes' (1024 bytes); 1436a scale factor of `m' or `megabytes' may also be used. 1437For 1438.Ar cputime 1439the default scale is `seconds'; 1440a scale factor of `m' for minutes 1441or `h' for hours, or a time of the form `mm:ss' giving minutes 1442and seconds also may be used. 1443.Pp 1444For both 1445.Ar resource 1446names and scale factors, unambiguous prefixes 1447of the names suffice. 1448.Pp 1449Limits of an arbitrary process can be displayed or set using the 1450.Xr sysctl 8 1451utility. 1452See the 1453.Xr getrlimit 2 1454and 1455.Xr setrlimit 2 1456man pages for an additional description of system resource limits. 1457.Pp 1458.It Ic login 1459Terminate a login shell, replacing it with an instance of 1460.Pa /usr/bin/login . 1461This is one way to log off, included for compatibility with 1462.Xr sh 1 . 1463.Pp 1464.It Ic logout 1465Terminate a login shell. 1466Especially useful if 1467.Ic ignoreeof 1468is set. 1469.Pp 1470.It Ic nice 1471.It Ic nice Ar +number 1472.It Ic nice Ar command 1473.It Ic nice Ar +number command 1474The first form sets the 1475scheduling priority 1476for this shell to 4. 1477The second form sets the 1478priority 1479to the given 1480.Ar number . 1481The final two forms run command at priority 4 and 1482.Ar number 1483respectively. 1484The greater the number, the less CPU the process will get. 1485The super-user may specify negative priority by using `nice \-number ...'. 1486.Ar Command 1487is always executed in a sub-shell, and the restrictions 1488placed on commands in simple 1489.Ic if 1490statements apply. 1491.Pp 1492.It Ic nohup 1493.It Ic nohup Ar command 1494The first form can be used in shell scripts to cause hangups to be 1495ignored for the remainder of the script. 1496The second form causes the specified command to be run with hangups 1497ignored. 1498All processes detached with `\*[Am]' are effectively 1499.Ic nohup Ns \'ed . 1500.Pp 1501.It Ic notify 1502.It Ic notify % Ns Ar job ... 1503Causes the shell to notify the user asynchronously when the status of the 1504current or specified jobs change; normally notification is presented 1505before a prompt. 1506This is automatic if the shell variable 1507.Ic notify 1508is set. 1509.Pp 1510.It Ic onintr 1511.It Ic onintr Fl 1512.It Ic onintr Ar label 1513Control the action of the shell on interrupts. 1514The first form restores the default action of the shell on interrupts 1515which is to terminate shell scripts or to return to the terminal command 1516input level. 1517The second form `onintr \-' causes all interrupts to be ignored. 1518The final form causes the shell to execute a `goto label' when 1519an interrupt is received or a child process terminates because 1520it was interrupted. 1521.Pp 1522In any case, if the shell is running detached and interrupts are 1523being ignored, all forms of 1524.Ic onintr 1525have no meaning and interrupts 1526continue to be ignored by the shell and all invoked commands. 1527Finally 1528.Ic onintr 1529statements are ignored in the system startup files where interrupts 1530are disabled (/etc/csh.cshrc, /etc/csh.login). 1531.Pp 1532.It Ic popd 1533.It Ic popd Ar +n 1534Pops the directory stack, returning to the new top directory. 1535With an argument 1536.Ns \`+ Ar n Ns \' 1537discards the 1538.Ar n Ns \'th 1539entry in the stack. 1540The members of the directory stack are numbered from the top starting at 0. 1541.Pp 1542.It Ic pushd 1543.It Ic pushd Ar name 1544.It Ic pushd Ar +n 1545With no arguments, 1546.Ic pushd 1547exchanges the top two elements of the directory stack. 1548Given a 1549.Ar name 1550argument, 1551.Ic pushd 1552changes to the new directory (ala 1553.Ic cd ) 1554and pushes the old current working directory 1555(as in 1556.Ic cwd ) 1557onto the directory stack. 1558With a numeric argument, 1559.Ic pushd 1560rotates the 1561.Ar n Ns \'th 1562argument of the directory 1563stack around to be the top element and changes to it. 1564The members 1565of the directory stack are numbered from the top starting at 0. 1566.Pp 1567.It Ic rehash 1568Causes the internal hash table of the contents of the directories in 1569the 1570.Ic path 1571variable to be recomputed. 1572This is needed if new commands are added to directories in the 1573.Ic path 1574while you are logged in. 1575This should only be necessary if you add 1576commands to one of your own directories, or if a systems programmer 1577changes the contents of a system directory. 1578.Pp 1579.It Ic repeat Ar count command 1580The specified 1581.Ar command , 1582which is subject to the same restrictions 1583as the 1584.Ar command 1585in the one line 1586.Ic if 1587statement above, 1588is executed 1589.Ar count 1590times. 1591I/O redirections occur exactly once, even if 1592.Ar count 1593is 0. 1594.Pp 1595.It Ic set 1596.It Ic set Ar name 1597.It Ic set Ar name Ns =word 1598.It Ic set Ar name[index] Ns =word 1599.It Ic set Ar name Ns =(wordlist) 1600The first form of the command shows the value of all shell variables. 1601Variables that have other than a single word as their 1602value print as a parenthesized word list. 1603The second form sets 1604.Ar name 1605to the null string. 1606The third form sets 1607.Ar name 1608to the single 1609.Ar word . 1610The fourth form sets 1611the 1612.Ar index Ns 'th 1613component of 1614.Ar name 1615to 1616.Ar word ; 1617this component must already exist. 1618The final form sets 1619.Ar name 1620to the list of words in 1621.Ar wordlist . 1622The value is always command and filename expanded. 1623.Pp 1624These arguments may be repeated to set multiple values in a single set command. 1625Note however, that variable expansion happens for all arguments before any 1626setting occurs. 1627.Pp 1628.It Ic setenv 1629.It Ic setenv Ar name 1630.It Ic setenv Ar name value 1631The first form lists all current environment variables. 1632It is equivalent to 1633.Xr printenv 1 . 1634The last form sets the value of environment variable 1635.Ar name 1636to be 1637.Ar value , 1638a single string. 1639The second form sets 1640.Ar name 1641to an empty string. 1642The most commonly used environment variables 1643.Ev USER , 1644.Ev TERM , 1645and 1646.Ev PATH 1647are automatically imported to and exported from the 1648.Nm 1649variables 1650.Ar user , 1651.Ar term , 1652and 1653.Ar path ; 1654there is no need to use 1655.Ic setenv 1656for these. 1657.Pp 1658.It Ic shift 1659.It Ic shift Ar variable 1660The members of 1661.Ic argv 1662are shifted to the left, discarding 1663.Ic argv Ns Bq 1 . 1664It is an error for 1665.Ic argv 1666not to be set or to have less than one word as value. 1667The second form performs the same function on the specified variable. 1668.Pp 1669.It Ic source Ar name 1670.It Ic source Fl h Ar name 1671The shell reads commands from 1672.Ar name . 1673.Ic Source 1674commands may be nested; if they are nested too deeply the shell may 1675run out of file descriptors. 1676An error in a 1677.Ic source 1678at any level terminates all nested 1679.Ic source 1680commands. 1681Normally input during 1682.Ic source 1683commands is not placed on the history list; 1684the \-h option causes the commands to be placed on the 1685history list without being executed. 1686.Pp 1687.It Ic stop 1688.It Ic stop % Ns Ar job ... 1689Stops the current or specified jobs that are executing in the background. 1690.Pp 1691.It Ic suspend 1692Causes the shell to stop in its tracks, much as if it had been sent a stop 1693signal with 1694.Ic ^Z . 1695This is most often used to stop shells started by 1696.Xr su 1 . 1697.Pp 1698.It Ic switch Ar ( string ) 1699.It Ic case Ar str1 : 1700.It \ \ \ \ \&... 1701.It Ic \ \ \ \ breaksw 1702.It \ \ \ \ \&... 1703.It Ic default : 1704.It \ \ \ \ \&... 1705.It Ic \ \ \ \ breaksw 1706.It Ic endsw 1707Each case label is successively matched against the specified 1708.Ar string 1709which is first command and filename expanded. 1710The file metacharacters `*', `?' and `[...]' 1711may be used in the case labels, 1712which are variable expanded. 1713If none of the labels match before the `default' label is found, then 1714the execution begins after the default label. 1715Each case label and the default label must appear at the beginning of a line. 1716The command 1717.Ic breaksw 1718causes execution to continue after the 1719.Ic endsw . 1720Otherwise control may fall through case labels and the default label as in C. 1721If no label matches and there is no default, execution continues after 1722the 1723.Ic endsw . 1724.Pp 1725.It Ic time 1726.It Ic time Ar command 1727With no argument, a summary of time used by this shell and its children 1728is printed. 1729If arguments are given 1730the specified simple command is timed and a time summary 1731as described under the 1732.Ic time 1733variable is printed. 1734If necessary, an extra shell is created to print the time 1735statistic when the command completes. 1736.Pp 1737.It Ic umask 1738.It Ic umask Ar value 1739The file creation mask is displayed (first form) or set to the specified 1740value (second form). 1741The mask is given in octal. 1742Common values for 1743the mask are 002 giving all access to the group and read and execute 1744access to others or 022 giving all access except write access for 1745users in the group or others. 1746.Pp 1747.It Ic unalias Ar pattern 1748All aliases whose names match the specified pattern are discarded. 1749Thus all aliases are removed by `unalias *'. 1750It is not an error for nothing to be 1751.Ic unaliased . 1752.Pp 1753.It Ic unhash 1754Use of the internal hash table to speed location of executed programs 1755is disabled. 1756.Pp 1757.It Ic unlimit 1758.It Ic unlimit Ar resource 1759.It Ic unlimit Fl h 1760.It Ic unlimit Fl h Ar resource 1761Removes the limitation on 1762.Ar resource . 1763If no 1764.Ar resource 1765is specified, then all 1766.Ar resource 1767limitations are removed. 1768If 1769.Fl h 1770is given, the corresponding hard limits are removed. 1771Only the 1772super-user may do this. 1773.Pp 1774.It Ic unset Ar pattern 1775All variables whose names match the specified pattern are removed. 1776Thus all variables are removed by `unset *'; this has noticeably 1777distasteful side-effects. 1778It is not an error for nothing to be 1779.Ic unset . 1780.Pp 1781.It Ic unsetenv Ar pattern 1782Removes all variables whose name match the specified pattern from the 1783environment. 1784See also the 1785.Ic setenv 1786command above and 1787.Xr printenv 1 . 1788.Pp 1789.It Ic wait 1790Wait for all background jobs. 1791If the shell is interactive, then an interrupt can disrupt the wait. 1792After the interrupt, the shell prints names and job numbers of all jobs 1793known to be outstanding. 1794.Pp 1795.It Ic which Ar command 1796Displays the resolved command that will be executed by the shell. 1797.Pp 1798.It Ic while Ar ( expr ) 1799.It \&... 1800.It Ic end 1801While the specified expression evaluates non-zero, the commands between 1802the 1803.Ic while 1804and the matching 1805.Ic end 1806are evaluated. 1807.Ic Break 1808and 1809.Ic continue 1810may be used to terminate or continue the loop prematurely. 1811(The 1812.Ic while 1813and 1814.Ic end 1815must appear alone on their input lines.) 1816Prompting occurs here the first time through the loop as for the 1817.Ic foreach 1818statement if the input is a terminal. 1819.Pp 1820.It Ic % Ns Ar job 1821Brings the specified job into the foreground. 1822.Pp 1823.It Ic % Ns Ar job Ic \*[Am] 1824Continues the specified job in the background. 1825.Pp 1826.It Ic @ 1827.It Ic @ Ar name Ns = expr 1828.It Ic @ Ar name[index] Ns = expr 1829The first form prints the values of all the shell variables. 1830The second form sets the specified 1831.Ar name 1832to the value of 1833.Ar expr . 1834If the expression contains `\*[Lt]', `\*[Gt]', `\*[Am]' or `|' then at least 1835this part of the expression must be placed within `(' `)'. 1836The third form assigns the value of 1837.Ar expr 1838to the 1839.Ar index Ns 'th 1840argument of 1841.Ar name . 1842Both 1843.Ar name 1844and its 1845.Ar index Ns 'th 1846component must already exist. 1847.El 1848.Pp 1849The operators `*=', `+=', etc are available as in C. 1850The space separating the name from the assignment operator is optional. 1851Spaces are, however, mandatory in separating components of 1852.Ar expr 1853which would otherwise be single words. 1854.Pp 1855Special postfix `+\|+' and `\-\|\-' operators increment and decrement 1856.Ar name 1857respectively, i.e., `@ i++'. 1858.Ss Pre-defined and environment variables 1859The following variables have special meaning to the shell. 1860Of these, 1861.Ar argv , 1862.Ar cwd , 1863.Ar home , 1864.Ar path , 1865.Ar prompt , 1866.Ar shell 1867and 1868.Ar status 1869are always set by the shell. 1870Except for 1871.Ar cwd 1872and 1873.Ar status , 1874this setting occurs only at initialization; 1875these variables will not then be modified unless done 1876explicitly by the user. 1877.Pp 1878The shell copies the environment variable 1879.Ev USER 1880into the variable 1881.Ar user , 1882.Ev TERM 1883into 1884.Ar term , 1885and 1886.Ev HOME 1887into 1888.Ar home , 1889and copies these back into the environment whenever the normal 1890shell variables are reset. 1891The environment variable 1892.Ev PATH 1893is likewise handled; it is not 1894necessary to worry about its setting other than in the file 1895.Ar \&.cshrc 1896as inferior 1897.Nm 1898processes will import the definition of 1899.Ar path 1900from the environment, and re-export it if you then change it. 1901.Bl -tag -width histchars 1902.It Ic argv 1903Set to the arguments to the shell, it is from this variable that 1904positional parameters are substituted, i.e., `$1' is replaced by 1905`$argv[1]', 1906etc. 1907.It Ic cdpath 1908Gives a list of alternative directories searched to find subdirectories 1909in 1910.Ar chdir 1911commands. 1912.It Ic cwd 1913The full pathname of the current directory. 1914.It Ic echo 1915Set when the 1916.Fl x 1917command line option is given. 1918Causes each command and its arguments 1919to be echoed just before it is executed. 1920For non-builtin commands all expansions occur before echoing. 1921Builtin commands are echoed before command and filename substitution, 1922since these substitutions are then done selectively. 1923.It Ic filec 1924Enable file name completion. 1925.It Ic histchars 1926Can be given a string value to change the characters used in history 1927substitution. 1928The first character of its value is used as the 1929history substitution character, replacing the default character `!'. 1930The second character of its value replaces the character `^' in 1931quick substitutions. 1932.It Ic histfile 1933Can be set to the pathname where history is going to be saved/restored. 1934.It Ic history 1935Can be given a numeric value to control the size of the history list. 1936Any command that has been referenced in this many events will not be 1937discarded. 1938Too large values of 1939.Ar history 1940may run the shell out of memory. 1941The last executed command is always saved on the history list. 1942.It Ic home 1943The home directory of the invoker, initialized from the environment. 1944The filename expansion of 1945.Sq Pa ~ 1946refers to this variable. 1947.It Ic ignoreeof 1948If set the shell ignores 1949end-of-file from input devices which are terminals. 1950This prevents shells from accidentally being killed by control-D's. 1951.It Ic mail 1952The files where the shell checks for mail. 1953This checking is done after each command completion that will 1954result in a prompt, 1955if a specified interval has elapsed. 1956The shell says `You have new mail.' 1957if the file exists with an access time not greater than its modify time. 1958.Pp 1959If the first word of the value of 1960.Ar mail 1961is numeric it specifies a different mail checking interval, in seconds, 1962than the default, which is 10 minutes. 1963.Pp 1964If multiple mail files are specified, then the shell says 1965`New mail in 1966.Ar name Ns ' 1967when there is mail in the file 1968.Ar name . 1969.It Ic noclobber 1970As described in the section on 1971.Sx Input/output , 1972restrictions are placed on output redirection to ensure that 1973files are not accidentally destroyed, and that `\*[Gt]\*[Gt]' redirections 1974refer to existing files. 1975.It Ic noglob 1976If set, filename expansion is inhibited. 1977This inhibition is most useful in shell scripts that 1978 are not dealing with filenames, 1979or after a list of filenames has been obtained and further expansions 1980are not desirable. 1981.It Ic nonomatch 1982If set, it is not an error for a filename expansion to not match any 1983existing files; instead the primitive pattern is returned. 1984It is still an error for the primitive pattern to be malformed, i.e., 1985`echo [' 1986still gives an error. 1987.It Ic notify 1988If set, the shell notifies asynchronously of job completions; 1989the default is to present job completions just before printing 1990a prompt. 1991.It Ic path 1992Each word of the path variable specifies a directory in which 1993commands are to be sought for execution. 1994A null word specifies the current directory. 1995If there is no 1996.Ar path 1997variable then only full path names will execute. 1998The usual search path is `.', `/bin' and `/usr/bin', but this 1999may vary from system to system. 2000For the super-user the default search path is `/etc', `/bin' and `/usr/bin'. 2001A shell that is given neither the 2002.Fl c 2003nor the 2004.Fl t 2005option will normally hash the contents of the directories in the 2006.Ar path 2007variable after reading 2008.Ar \&.cshrc , 2009and each time the 2010.Ar path 2011variable is reset. 2012If new commands are added to these directories 2013while the shell is active, it may be necessary to do a 2014.Ic rehash 2015or the commands may not be found. 2016.It Ic prompt 2017The string that is printed before each command is read from 2018an interactive terminal input. 2019If a `!' appears in the string it will be replaced by the current event number 2020unless a preceding `\e' is given. 2021Default is `% ', or `# ' for the super-user. 2022.It Ic savehist 2023Is given a numeric value to control the number of entries of the 2024history list that are saved in ~/.history when the user logs out. 2025Any command that has been referenced in this many events will be saved. 2026During start up the shell sources ~/.history into the history list 2027enabling history to be saved across logins. 2028Too large values of 2029.Ar savehist 2030will slow down the shell during start up. 2031If 2032.Ar savehist 2033is just set, the shell will use the value of 2034.Ar history . 2035.It Ic shell 2036The file in which the shell resides. 2037This variable is used in forking shells to interpret files that have execute 2038bits set, but which are not executable by the system. 2039(See the description of 2040.Sx Non-builtin command execution 2041below.) 2042Initialized to the (system-dependent) home of the shell. 2043.It Ic status 2044The status returned by the last command. 2045If it terminated abnormally, then 0200 is added to the status. 2046Builtin commands that fail return exit status `1', 2047all other builtin commands set status to `0'. 2048.It Ic time 2049Controls automatic timing of commands. 2050This setting allows two parameters. 2051The first specifies the CPU time threshold at which reporting should be done 2052for a process, and the optional second specifies the output format. 2053The following format strings are available: 2054.Pp 2055.Bl -tag -width Ds -compact -offset indent 2056.It Li \&%c 2057Number of involuntary context switches. 2058.It Li \&%D 2059Average unshared data size. 2060.It Li \&%E 2061Elapsed (wall\-clock) time. 2062.It Li \&%F 2063Page faults. 2064.It Li \&%I 2065Filesystem blocks in. 2066.It Li \&%K 2067Average total data memory used. 2068.It Li \&%k 2069Number of signals received. 2070.It Li \&%M 2071Maximum Resident Set Size. 2072.It Li \&%O 2073Filesystem blocks out. 2074.It Li \&%P 2075Total percent time spent running. 2076.It Li \&%R 2077Page reclaims. 2078.It Li \&%r 2079Socket messages received. 2080.It Li \&%S 2081Total system CPU time used. 2082.It Li \&%s 2083Socket messages sent. 2084.It Li \&%U 2085Total user CPU time used. 2086.It Li \&%W 2087Number of swaps. 2088.It Li \&%w 2089Number of voluntary context switches (waits). 2090.It Li \&%X 2091Average shared text size. 2092.El 2093.Pp 2094The default summary is "%Uu %Ss %E %P %X+%Dk %I+%Oio %Fpf+%Ww" 2095.It Ic verbose 2096Set by the 2097.Fl v 2098command line option, causes the words of each command to be printed 2099after history substitution. 2100.El 2101.Ss Non-builtin command execution 2102When a command to be executed is found to not be a builtin command 2103the shell attempts to execute the command via 2104.Xr execve 2 . 2105Each word in the variable 2106.Ar path 2107names a directory from which the shell will attempt to execute the command. 2108If it is given neither a 2109.Fl c 2110nor a 2111.Fl t 2112option, the shell will hash the names in these directories into an internal 2113table so that it will only try an 2114.Ic exec 2115in a directory if there is a possibility that the command resides there. 2116This shortcut greatly speeds command location when many directories 2117are present in the search path. 2118If this mechanism has been turned off (via 2119.Ic unhash ) , 2120or if the shell was given a 2121.Fl c 2122or 2123.Fl t 2124argument, and in any case for each directory component of 2125.Ar path 2126that does not begin with a `/', 2127the shell concatenates with the given command name to form a path name 2128of a file which it then attempts to execute. 2129.Pp 2130Parenthesized commands are always executed in a subshell. 2131Thus 2132.Pp 2133.Dl (cd ; pwd) ; pwd 2134.Pp 2135prints the 2136.Ar home 2137directory; leaving you where you were (printing this after the home directory), 2138while 2139.Pp 2140.Dl cd ; pwd 2141.Pp 2142leaves you in the 2143.Ar home 2144directory. 2145Parenthesized commands are most often used to prevent 2146.Ic chdir 2147from affecting the current shell. 2148.Pp 2149If the file has execute permissions but is not an 2150executable binary to the system, then it is assumed to be a 2151file containing shell commands and a new shell is spawned to read it. 2152.Pp 2153If there is an 2154.Ic alias 2155for 2156.Ic shell 2157then the words of the alias will be prepended to the argument list to form 2158the shell command. 2159The first word of the 2160.Ic alias 2161should be the full path name of the shell 2162(e.g., `$shell'). 2163Note that this is a special, late occurring, case of 2164.Ic alias 2165substitution, 2166and only allows words to be prepended to the argument list without change. 2167.Ss Signal handling 2168The shell normally ignores 2169.Ar quit 2170signals. 2171Jobs running detached (either by 2172.Ic \&\*[Am] 2173or the 2174.Ic bg 2175or 2176.Ic %... \*[Am] 2177commands) are immune to signals generated from the keyboard, including 2178hangups. 2179Other signals have the values which the shell inherited from its parent. 2180The shell's handling of interrupts and terminate signals 2181in shell scripts can be controlled by 2182.Ic onintr . 2183Login shells catch the 2184.Ar terminate 2185signal; otherwise this signal is passed on to children from the state in the 2186shell's parent. 2187Interrupts are not allowed when a login shell is reading the file 2188.Pa \&.logout . 2189.Sh FILES 2190.Bl -tag -width /etc/passwd -compact 2191.It Pa ~/.cshrc 2192Read at beginning of execution by each shell. 2193.It Pa ~/.login 2194Read by login shell, after `.cshrc' at login. 2195.It Pa ~/.logout 2196Read by login shell, at logout. 2197.It Pa /bin/sh 2198Standard shell, for shell scripts not starting with a `#'. 2199.It Pa /tmp/sh* 2200Temporary file for `\*[Lt]\*[Lt]'. 2201.It Pa /etc/passwd 2202Source of home directories for `~name'. 2203.El 2204.Sh LIMITATIONS 2205Word lengths \- 2206Words can be no longer than 1024 characters. 2207The system limits argument lists to 10240 characters. 2208The number of arguments to a command that involves filename expansion 2209is limited to 1/6'th the number of characters allowed in an argument list. 2210Command substitutions may substitute no more characters than are 2211allowed in an argument list. 2212To detect looping, the shell restricts the number of 2213.Ic alias 2214substitutions on a single line to 20. 2215.Sh SEE ALSO 2216.Xr sh 1 , 2217.Xr access 2 , 2218.Xr execve 2 , 2219.Xr fork 2 , 2220.Xr pipe 2 , 2221.Xr setrlimit 2 , 2222.Xr sigaction 2 , 2223.Xr umask 2 , 2224.Xr wait 2 , 2225.Xr killpg 3 , 2226.Xr tty 4 , 2227.Xr a.out 5 , 2228.Xr environ 7 , 2229.Xr sysctl 8 2230.Rs 2231.%T "An introduction to the C shell" 2232.Re 2233.Sh HISTORY 2234.Nm 2235appeared in 2236.Bx 2 . 2237It was a first implementation of a command language interpreter 2238incorporating a history mechanism (see 2239.Sx History substitutions ) , 2240job control facilities (see 2241.Sx Jobs ) , 2242interactive file name 2243and user name completion (see 2244.Sx File Name Completion ) , 2245and a C-like syntax. 2246There are now many shells that also have these mechanisms, plus 2247a few more (and maybe some bugs too), which are available through the 2248usenet. 2249.Sh AUTHORS 2250William Joy. 2251Job control and directory stack features first implemented by J.E. Kulp of 2252IIASA, Laxenburg, Austria, 2253with different syntax than that used now. 2254File name completion code written by Ken Greer, HP Labs. 2255Eight-bit implementation Christos S. Zoulas, Cornell University. 2256.Sh BUGS 2257When a command is restarted from a stop, 2258the shell prints the directory it started in if this is different 2259from the current directory; this can be misleading (i.e., wrong) 2260as the job may have changed directories internally. 2261.Pp 2262Shell builtin functions are not stoppable/restartable. 2263Command sequences of the form `a ; b ; c' are also not handled gracefully 2264when stopping is attempted. 2265If you suspend `b', the shell will immediately execute `c'. 2266This is especially noticeable if this expansion results from an 2267.Ar alias . 2268It suffices to place the sequence of commands in ()'s to force it to 2269a subshell, i.e., `( a ; b ; c )'. 2270.Pp 2271Control over tty output after processes are started is primitive; 2272perhaps this will inspire someone to work on a good virtual 2273terminal interface. 2274In a virtual terminal interface much more 2275interesting things could be done with output control. 2276.Pp 2277Alias substitution is most often used to clumsily simulate shell procedures; 2278shell procedures should be provided instead of aliases. 2279.Pp 2280Commands within loops, prompted for by `?', are not placed on the 2281.Ic history 2282list. 2283Control structure should be parsed instead of being recognized as built-in 2284commands. 2285This would allow control commands to be placed anywhere, 2286to be combined with `\&|', and to be used with `\*[Am]' and `;' metasyntax. 2287.Pp 2288It should be possible to use the `:' modifiers on the output of command 2289substitutions. 2290.Pp 2291The way the 2292.Ic filec 2293facility is implemented is ugly and expensive. 2294