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