1.\"- 2.\" Copyright (c) 1991, 1993 3.\" The Regents of the University of California. All rights reserved. 4.\" 5.\" This code is derived from software contributed to Berkeley by 6.\" Kenneth Almquist. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 4. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95 33.\" $FreeBSD$ 34.\" 35.Dd December 31, 2009 36.Dt SH 1 37.Os 38.Sh NAME 39.Nm sh 40.Nd command interpreter (shell) 41.Sh SYNOPSIS 42.Nm 43.Op Fl /+abCEefIimnPpsTuVvx 44.Op Fl /+o Ar longname 45.Op Fl c Ar string 46.Op Ar arg ... 47.Sh DESCRIPTION 48The 49.Nm 50utility is the standard command interpreter for the system. 51The current version of 52.Nm 53is in the process of being changed to 54conform with the 55.St -p1003.2 56specification for the shell. 57This version has many features which make 58it appear 59similar in some respects to the Korn shell, but it is not a Korn 60shell clone like 61.Nm pdksh . 62Only features 63designated by 64.Tn POSIX , 65plus a few Berkeley extensions, are being 66incorporated into this shell. 67This man page is not intended to be a tutorial nor a complete 68specification of the shell. 69.Ss Overview 70The shell is a command that reads lines from 71either a file or the terminal, interprets them, and 72generally executes other commands. 73It is the program that is started when a user logs into the system, 74although a user can select a different shell with the 75.Xr chsh 1 76command. 77The shell 78implements a language that has flow control constructs, 79a macro facility that provides a variety of features in 80addition to data storage, along with built-in history and line 81editing capabilities. 82It incorporates many features to 83aid interactive use and has the advantage that the interpretative 84language is common to both interactive and non-interactive 85use (shell scripts). 86That is, commands can be typed directly 87to the running shell or can be put into a file, 88which can be executed directly by the shell. 89.Ss Invocation 90.\" 91.\" XXX This next sentence is incredibly confusing. 92.\" 93If no arguments are present and if the standard input of the shell 94is connected to a terminal 95(or if the 96.Fl i 97option is set), 98the shell is considered an interactive shell. 99An interactive shell 100generally prompts before each command and handles programming 101and command errors differently (as described below). 102When first starting, the shell inspects argument 0, and 103if it begins with a dash 104.Pq Ql - , 105the shell is also considered a login shell. 106This is normally done automatically by the system 107when the user first logs in. 108A login shell first reads commands 109from the files 110.Pa /etc/profile 111and then 112.Pa .profile 113in a user's home directory, 114if they exist. 115If the environment variable 116.Ev ENV 117is set on entry to a shell, or is set in the 118.Pa .profile 119of a login shell, the shell then reads commands from the file named in 120.Ev ENV . 121Therefore, a user should place commands that are to be executed only 122at login time in the 123.Pa .profile 124file, and commands that are executed for every shell inside the 125.Ev ENV 126file. 127The user can set the 128.Ev ENV 129variable to some file by placing the following line in the file 130.Pa .profile 131in the home directory, 132substituting for 133.Pa .shinit 134the filename desired: 135.Pp 136.Dl "ENV=$HOME/.shinit; export ENV" 137.Pp 138The first non-option argument specified on the command line 139will be treated as the 140name of a file from which to read commands (a shell script), and 141the remaining arguments are set as the positional parameters 142of the shell 143.Li ( $1 , $2 , 144etc.). 145Otherwise, the shell reads commands 146from its standard input. 147.Pp 148Unlike older versions of 149.Nm 150the 151.Ev ENV 152script is only sourced on invocation of interactive shells. 153This 154closes a well-known, and sometimes easily exploitable security 155hole related to poorly thought out 156.Ev ENV 157scripts. 158.Ss Argument List Processing 159All of the single letter options to 160.Nm 161have a corresponding long name, 162with the exception of 163.Fl c 164and 165.Fl /+o . 166These long names are provided next to the single letter options 167in the descriptions below. 168The long name for an option may be specified as an argument to the 169.Fl /+o 170option of 171.Nm . 172Once the shell is running, 173the long name for an option may be specified as an argument to the 174.Fl /+o 175option of the 176.Ic set 177built-in command 178(described later in the section called 179.Sx Built-in Commands ) . 180Introducing an option with a dash 181.Pq Ql - 182enables the option, 183while using a plus 184.Pq Ql + 185disables the option. 186A 187.Dq Li -- 188or plain 189.Ql - 190will stop option processing and will force the remaining 191words on the command line to be treated as arguments. 192The 193.Fl /+o 194and 195.Fl c 196options do not have long names. 197They take arguments and are described after the single letter options. 198.Bl -tag -width indent 199.It Fl a Li allexport 200Flag variables for export when assignments are made to them. 201.It Fl b Li notify 202Enable asynchronous notification of background job 203completion. 204(UNIMPLEMENTED) 205.It Fl C Li noclobber 206Do not overwrite existing files with 207.Ql > . 208.It Fl E Li emacs 209Enable the built-in 210.Xr emacs 1 211command line editor (disables the 212.Fl V 213option if it has been set). 214.It Fl e Li errexit 215Exit immediately if any untested command fails in non-interactive mode. 216The exit status of a command is considered to be 217explicitly tested if the command is part of the list used to control 218an 219.Ic if , elif , while , 220or 221.Ic until ; 222if the command is the left 223hand operand of an 224.Dq Li && 225or 226.Dq Li || 227operator; or if the command is a pipeline preceded by the 228.Ic !\& 229operator. 230If a shell function is executed and its exit status is explicitly 231tested, all commands of the function are considered to be tested as 232well. 233.It Fl f Li noglob 234Disable pathname expansion. 235.It Fl I Li ignoreeof 236Ignore 237.Dv EOF Ap s 238from input when in interactive mode. 239.It Fl i Li interactive 240Force the shell to behave interactively. 241.It Fl m Li monitor 242Turn on job control (set automatically when interactive). 243.It Fl n Li noexec 244If not interactive, read commands but do not 245execute them. 246This is useful for checking the 247syntax of shell scripts. 248.It Fl P Li physical 249Change the default for the 250.Ic cd 251and 252.Ic pwd 253commands from 254.Fl L 255(logical directory layout) 256to 257.Fl P 258(physical directory layout). 259.It Fl p Li privileged 260Turn on privileged mode. 261This mode is enabled on startup 262if either the effective user or group ID is not equal to the 263real user or group ID. 264Turning this mode off sets the 265effective user and group IDs to the real user and group IDs. 266When this mode is enabled for interactive shells, the file 267.Pa /etc/suid_profile 268is sourced instead of 269.Pa ~/.profile 270after 271.Pa /etc/profile 272is sourced, and the contents of the 273.Ev ENV 274variable are ignored. 275.It Fl s Li stdin 276Read commands from standard input (set automatically 277if no file arguments are present). 278This option has 279no effect when set after the shell has already started 280running (i.e., when set with the 281.Ic set 282command). 283.It Fl T Li trapsasync 284When waiting for a child, execute traps immediately. 285If this option is not set, 286traps are executed after the child exits, 287as specified in 288.St -p1003.2 . 289This nonstandard option is useful for putting guarding shells around 290children that block signals. 291The surrounding shell may kill the child 292or it may just return control to the tty and leave the child alone, 293like this: 294.Bd -literal -offset indent 295sh -T -c "trap 'exit 1' 2 ; some-blocking-program" 296.Ed 297.It Fl u Li nounset 298Write a message to standard error when attempting 299to expand a variable, a positional parameter or 300the special parameter 301.Va \&! 302that is not set, and if the 303shell is not interactive, exit immediately. 304.It Fl V Li vi 305Enable the built-in 306.Xr vi 1 307command line editor (disables 308.Fl E 309if it has been set). 310.It Fl v Li verbose 311The shell writes its input to standard error 312as it is read. 313Useful for debugging. 314.It Fl x Li xtrace 315Write each command 316(preceded by the value of the 317.Va PS4 318variable) 319to standard error before it is executed. 320Useful for debugging. 321.El 322.Pp 323The 324.Fl c 325option causes the commands to be read from the 326.Ar string 327operand instead of from the standard input. 328Keep in mind that this option only accepts a single string as its 329argument, hence multi-word strings must be quoted. 330.Pp 331The 332.Fl /+o 333option takes as its only argument the long name of an option 334to be enabled or disabled. 335For example, the following two invocations of 336.Nm 337both enable the built-in 338.Xr emacs 1 339command line editor: 340.Bd -literal -offset indent 341set -E 342set -o emacs 343.Ed 344.Pp 345If used without an argument, the 346.Fl o 347option displays the current option settings in a human-readable format. 348If 349.Cm +o 350is used without an argument, the current option settings are output 351in a format suitable for re-input into the shell. 352.Ss Lexical Structure 353The shell reads input in terms of lines from a file and breaks 354it up into words at whitespace (blanks and tabs), and at 355certain sequences of 356characters called 357.Dq operators , 358which are special to the shell. 359There are two types of operators: control operators and 360redirection operators (their meaning is discussed later). 361The following is a list of valid operators: 362.Bl -tag -width indent 363.It Control operators: 364.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact 365.It Li & Ta Li && Ta Li ( Ta Li ) Ta Li \en 366.It Li ;; Ta Li ; Ta Li | Ta Li || 367.El 368.It Redirection operators: 369.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact 370.It Li < Ta Li > Ta Li << Ta Li >> Ta Li <> 371.It Li <& Ta Li >& Ta Li <<- Ta Li >| 372.El 373.El 374.Pp 375The character 376.Ql # 377introduces a comment if used at the beginning of a word. 378The word starting with 379.Ql # 380and the rest of the line are ignored. 381.Pp 382.Tn ASCII 383.Dv NUL 384characters (character code 0) are not allowed in shell input. 385.Ss Quoting 386Quoting is used to remove the special meaning of certain characters 387or words to the shell, such as operators, whitespace, keywords, 388or alias names. 389.Pp 390There are three types of quoting: matched single quotes, 391matched double quotes, and backslash. 392.Bl -tag -width indent 393.It Single Quotes 394Enclosing characters in single quotes preserves the literal 395meaning of all the characters (except single quotes, making 396it impossible to put single-quotes in a single-quoted string). 397.It Double Quotes 398Enclosing characters within double quotes preserves the literal 399meaning of all characters except dollar sign 400.Pq Ql $ , 401backquote 402.Pq Ql ` , 403and backslash 404.Pq Ql \e . 405The backslash inside double quotes is historically weird. 406It remains literal unless it precedes the following characters, 407which it serves to quote: 408.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact 409.It Li $ Ta Li ` Ta Li \&" Ta Li \e\ Ta Li \en 410.El 411.It Backslash 412A backslash preserves the literal meaning of the following 413character, with the exception of the newline character 414.Pq Ql \en . 415A backslash preceding a newline is treated as a line continuation. 416.El 417.Ss Reserved Words 418Reserved words are words that have special meaning to the 419shell and are recognized at the beginning of a line and 420after a control operator. 421The following are reserved words: 422.Bl -column "doneXX" "elifXX" "elseXX" "untilXX" "whileX" -offset center 423.It Li \&! Ta { Ta } Ta Ic case Ta Ic do 424.It Ic done Ta Ic elif Ta Ic else Ta Ic esac Ta Ic fi 425.It Ic for Ta Ic if Ta Ic then Ta Ic until Ta Ic while 426.El 427.Ss Aliases 428An alias is a name and corresponding value set using the 429.Ic alias 430built-in command. 431Whenever a reserved word may occur (see above), 432and after checking for reserved words, the shell 433checks the word to see if it matches an alias. 434If it does, it replaces it in the input stream with its value. 435For example, if there is an alias called 436.Dq Li lf 437with the value 438.Dq Li "ls -F" , 439then the input 440.Pp 441.Dl "lf foobar" 442.Pp 443would become 444.Pp 445.Dl "ls -F foobar" 446.Pp 447Aliases provide a convenient way for naive users to 448create shorthands for commands without having to learn how 449to create functions with arguments. 450They can also be 451used to create lexically obscure code. 452This use is discouraged. 453.Pp 454An alias name may be escaped in a command line, so that it is not 455replaced by its alias value, by using quoting characters within or 456adjacent to the alias name. 457This is most often done by prefixing 458an alias name with a backslash to execute a function, built-in, or 459normal program with the same name. 460See the 461.Sx Quoting 462subsection. 463.Ss Commands 464The shell interprets the words it reads according to a 465language, the specification of which is outside the scope 466of this man page (refer to the BNF in the 467.St -p1003.2 468document). 469Essentially though, a line is read and if 470the first word of the line (or after a control operator) 471is not a reserved word, then the shell has recognized a 472simple command. 473Otherwise, a complex command or some 474other special construct may have been recognized. 475.Ss Simple Commands 476If a simple command has been recognized, the shell performs 477the following actions: 478.Bl -enum 479.It 480Leading words of the form 481.Dq Li name=value 482are stripped off and assigned to the environment of 483the simple command. 484Redirection operators and 485their arguments (as described below) are stripped 486off and saved for processing. 487.It 488The remaining words are expanded as described in 489the section called 490.Sx Word Expansions , 491and the first remaining word is considered the command 492name and the command is located. 493The remaining 494words are considered the arguments of the command. 495If no command name resulted, then the 496.Dq Li name=value 497variable assignments recognized in 1) affect the 498current shell. 499.It 500Redirections are performed as described in 501the next section. 502.El 503.Ss Redirections 504Redirections are used to change where a command reads its input 505or sends its output. 506In general, redirections open, close, or 507duplicate an existing reference to a file. 508The overall format 509used for redirection is: 510.Pp 511.D1 Oo Ar n Oc Ar redir-op file 512.Pp 513The 514.Ar redir-op 515is one of the redirection operators mentioned 516previously. 517The following gives some examples of how these 518operators can be used. 519Note that stdin and stdout are commonly used abbreviations 520for standard input and standard output respectively. 521.Bl -tag -width "1234567890XX" -offset indent 522.It Oo Ar n Oc Ns Li > Ar file 523redirect stdout (or file descriptor 524.Ar n ) 525to 526.Ar file 527.It Oo Ar n Oc Ns Li >| Ar file 528same as above, but override the 529.Fl C 530option 531.It Oo Ar n Oc Ns Li >> Ar file 532append stdout (or file descriptor 533.Ar n ) 534to 535.Ar file 536.It Oo Ar n Oc Ns Li < Ar file 537redirect stdin (or file descriptor 538.Ar n ) 539from 540.Ar file 541.It Oo Ar n Oc Ns Li <> Ar file 542redirect stdin (or file descriptor 543.Ar n ) 544to and from 545.Ar file 546.It Oo Ar n1 Oc Ns Li <& Ns Ar n2 547duplicate stdin (or file descriptor 548.Ar n1 ) 549from file descriptor 550.Ar n2 551.It Oo Ar n Oc Ns Li <&- 552close stdin (or file descriptor 553.Ar n ) 554.It Oo Ar n1 Oc Ns Li >& Ns Ar n2 555duplicate stdout (or file descriptor 556.Ar n1 ) 557to file descriptor 558.Ar n2 559.It Oo Ar n Oc Ns Li >&- 560close stdout (or file descriptor 561.Ar n ) 562.El 563.Pp 564The following redirection is often called a 565.Dq here-document . 566.Bd -unfilled -offset indent 567.Oo Ar n Oc Ns Li << Ar delimiter 568.D1 Ar here-doc-text 569.D1 ... 570.Ar delimiter 571.Ed 572.Pp 573All the text on successive lines up to the delimiter is 574saved away and made available to the command on standard 575input, or file descriptor 576.Ar n 577if it is specified. 578If the 579.Ar delimiter 580as specified on the initial line is quoted, then the 581.Ar here-doc-text 582is treated literally, otherwise the text is subjected to 583parameter expansion, command substitution, and arithmetic 584expansion (as described in the section on 585.Sx Word Expansions ) . 586If the operator is 587.Dq Li <<- 588instead of 589.Dq Li << , 590then leading tabs 591in the 592.Ar here-doc-text 593are stripped. 594.Ss Search and Execution 595There are three types of commands: shell functions, 596built-in commands, and normal programs. 597The command is searched for (by name) in that order. 598The three types of commands are all executed in a different way. 599.Pp 600When a shell function is executed, all of the shell positional 601parameters (except 602.Li $0 , 603which remains unchanged) are 604set to the arguments of the shell function. 605The variables which are explicitly placed in the environment of 606the command (by placing assignments to them before the 607function name) are made local to the function and are set 608to the values given. 609Then the command given in the function definition is executed. 610The positional parameters are restored to their original values 611when the command completes. 612This all occurs within the current shell. 613.Pp 614Shell built-in commands are executed internally to the shell, without 615spawning a new process. 616There are two kinds of built-in commands: regular and special. 617Assignments before special builtins persist after they finish 618executing and assignment errors, redirection errors and certain 619operand errors cause a script to be aborted. 620Both regular and special builtins can affect the shell in ways 621normal programs cannot. 622.Pp 623Otherwise, if the command name does not match a function 624or built-in command, the command is searched for as a normal 625program in the file system (as described in the next section). 626When a normal program is executed, the shell runs the program, 627passing the arguments and the environment to the program. 628If the program is not a normal executable file 629(i.e., if it does not begin with the 630.Dq "magic number" 631whose 632.Tn ASCII 633representation is 634.Dq Li #! , 635resulting in an 636.Er ENOEXEC 637return value from 638.Xr execve 2 ) 639the shell will interpret the program in a subshell. 640The child shell will reinitialize itself in this case, 641so that the effect will be 642as if a new shell had been invoked to handle the ad-hoc shell script, 643except that the location of hashed commands located in 644the parent shell will be remembered by the child 645(see the description of the 646.Ic hash 647built-in command below). 648.Pp 649Note that previous versions of this document 650and the source code itself misleadingly and sporadically 651refer to a shell script without a magic number 652as a 653.Dq "shell procedure" . 654.Ss Path Search 655When locating a command, the shell first looks to see if 656it has a shell function by that name. 657Then it looks for a 658built-in command by that name. 659If a built-in command is not found, 660one of two things happen: 661.Bl -enum 662.It 663Command names containing a slash are simply executed without 664performing any searches. 665.It 666The shell searches each entry in the 667.Ev PATH 668environment variable 669in turn for the command. 670The value of the 671.Ev PATH 672variable should be a series of 673entries separated by colons. 674Each entry consists of a 675directory name. 676The current directory 677may be indicated implicitly by an empty directory name, 678or explicitly by a single period. 679.El 680.Ss Command Exit Status 681Each command has an exit status that can influence the behavior 682of other shell commands. 683The paradigm is that a command exits 684with zero for normal or success, and non-zero for failure, 685error, or a false indication. 686The man page for each command 687should indicate the various exit codes and what they mean. 688Additionally, the built-in commands return exit codes, as does 689an executed shell function. 690.Pp 691If a command is terminated by a signal, its exit status is 128 plus 692the signal number. 693Signal numbers are defined in the header file 694.In sys/signal.h . 695.Ss Complex Commands 696Complex commands are combinations of simple commands 697with control operators or reserved words, together creating a larger complex 698command. 699More generally, a command is one of the following: 700.Bl -item -offset indent 701.It 702simple command 703.It 704pipeline 705.It 706list or compound-list 707.It 708compound command 709.It 710function definition 711.El 712.Pp 713Unless otherwise stated, the exit status of a command is 714that of the last simple command executed by the command. 715.Ss Pipelines 716A pipeline is a sequence of one or more commands separated 717by the control operator 718.Ql \&| . 719The standard output of all but 720the last command is connected to the standard input 721of the next command. 722The standard output of the last 723command is inherited from the shell, as usual. 724.Pp 725The format for a pipeline is: 726.Pp 727.D1 Oo Li \&! Oc Ar command1 Op Li \&| Ar command2 ... 728.Pp 729The standard output of 730.Ar command1 731is connected to the standard input of 732.Ar command2 . 733The standard input, standard output, or 734both of a command is considered to be assigned by the 735pipeline before any redirection specified by redirection 736operators that are part of the command. 737.Pp 738If the pipeline is not in the background (discussed later), 739the shell waits for all commands to complete. 740.Pp 741If the reserved word 742.Ic !\& 743does not precede the pipeline, the 744exit status is the exit status of the last command specified 745in the pipeline. 746Otherwise, the exit status is the logical 747NOT of the exit status of the last command. 748That is, if 749the last command returns zero, the exit status is 1; if 750the last command returns greater than zero, the exit status 751is zero. 752.Pp 753Because pipeline assignment of standard input or standard 754output or both takes place before redirection, it can be 755modified by redirection. 756For example: 757.Pp 758.Dl "command1 2>&1 | command2" 759.Pp 760sends both the standard output and standard error of 761.Ar command1 762to the standard input of 763.Ar command2 . 764.Pp 765A 766.Ql \&; 767or newline terminator causes the preceding 768AND-OR-list 769(described below in the section called 770.Sx Short-Circuit List Operators ) 771to be executed sequentially; 772an 773.Ql & 774causes asynchronous execution of the preceding AND-OR-list. 775.Pp 776Note that unlike some other shells, 777.Nm 778executes each process in the pipeline as a child of the 779.Nm 780process. 781Shell built-in commands are the exception to this rule. 782They are executed in the current shell, although they do not affect its 783environment when used in pipelines. 784.Ss Background Commands (&) 785If a command is terminated by the control operator ampersand 786.Pq Ql & , 787the shell executes the command asynchronously; 788the shell does not wait for the command to finish 789before executing the next command. 790.Pp 791The format for running a command in background is: 792.Pp 793.D1 Ar command1 Li & Op Ar command2 Li & Ar ... 794.Pp 795If the shell is not interactive, the standard input of an 796asynchronous command is set to 797.Pa /dev/null . 798.Ss Lists (Generally Speaking) 799A list is a sequence of zero or more commands separated by 800newlines, semicolons, or ampersands, 801and optionally terminated by one of these three characters. 802The commands in a 803list are executed in the order they are written. 804If command is followed by an ampersand, the shell starts the 805command and immediately proceeds onto the next command; 806otherwise it waits for the command to terminate before 807proceeding to the next one. 808.Ss Short-Circuit List Operators 809.Dq Li && 810and 811.Dq Li || 812are AND-OR list operators. 813.Dq Li && 814executes the first command, and then executes the second command 815if the exit status of the first command is zero. 816.Dq Li || 817is similar, but executes the second command if the exit 818status of the first command is nonzero. 819.Dq Li && 820and 821.Dq Li || 822both have the same priority. 823.Ss Flow-Control Constructs (if, while, for, case) 824The syntax of the 825.Ic if 826command is: 827.Bd -unfilled -offset indent -compact 828.Ic if Ar list 829.Ic then Ar list 830.Oo Ic elif Ar list 831.Ic then Ar list Oc Ar ... 832.Op Ic else Ar list 833.Ic fi 834.Ed 835.Pp 836The syntax of the 837.Ic while 838command is: 839.Bd -unfilled -offset indent -compact 840.Ic while Ar list 841.Ic do Ar list 842.Ic done 843.Ed 844.Pp 845The two lists are executed repeatedly while the exit status of the 846first list is zero. 847The 848.Ic until 849command is similar, but has the word 850.Ic until 851in place of 852.Ic while , 853which causes it to 854repeat until the exit status of the first list is zero. 855.Pp 856The syntax of the 857.Ic for 858command is: 859.Bd -unfilled -offset indent -compact 860.Ic for Ar variable Op Ic in Ar word ... 861.Ic do Ar list 862.Ic done 863.Ed 864.Pp 865If 866.Ic in 867and the following words are omitted, 868.Ic in Li \&"$@\&" 869is used instead. 870The words are expanded, and then the list is executed 871repeatedly with the variable set to each word in turn. 872The 873.Ic do 874and 875.Ic done 876commands may be replaced with 877.Ql { 878and 879.Ql } . 880.Pp 881The syntax of the 882.Ic break 883and 884.Ic continue 885commands is: 886.D1 Ic break Op Ar num 887.D1 Ic continue Op Ar num 888.Pp 889The 890.Ic break 891command terminates the 892.Ar num 893innermost 894.Ic for 895or 896.Ic while 897loops. 898The 899.Ic continue 900command continues with the next iteration of the innermost loop. 901These are implemented as special built-in commands. 902.Pp 903The syntax of the 904.Ic case 905command is: 906.Bd -unfilled -offset indent -compact 907.Ic case Ar word Ic in 908.Ar pattern Ns Li ) Ar list Li ;; 909.Ar ... 910.Ic esac 911.Ed 912.Pp 913The pattern can actually be one or more patterns 914(see 915.Sx Shell Patterns 916described later), 917separated by 918.Ql \&| 919characters. 920The exit code of the 921.Ic case 922command is the exit code of the last command executed in the list or 923zero if no patterns were matched. 924.Ss Grouping Commands Together 925Commands may be grouped by writing either 926.Pp 927.D1 Li \&( Ns Ar list Ns Li \%) 928.Pp 929or 930.Pp 931.D1 Li { Ar list Ns Li \&; } 932.Pp 933The first form executes the commands in a subshell. 934Note that built-in commands thus executed do not affect the current shell. 935The second form does not fork another shell, 936so it is slightly more efficient. 937Grouping commands together this way allows the user to 938redirect their output as though they were one program: 939.Bd -literal -offset indent 940{ echo -n "hello"; echo " world"; } > greeting 941.Ed 942.Ss Functions 943The syntax of a function definition is 944.Pp 945.D1 Ar name Li \&( \&) Ar command 946.Pp 947A function definition is an executable statement; when 948executed it installs a function named 949.Ar name 950and returns an 951exit status of zero. 952The 953.Ar command 954is normally a list 955enclosed between 956.Ql { 957and 958.Ql } . 959.Pp 960Variables may be declared to be local to a function by 961using the 962.Ic local 963command. 964This should appear as the first statement of a function, 965and the syntax is: 966.Pp 967.D1 Ic local Oo Ar variable ... Oc Op Fl 968.Pp 969The 970.Ic local 971command is implemented as a built-in command. 972.Pp 973When a variable is made local, it inherits the initial 974value and exported and readonly flags from the variable 975with the same name in the surrounding scope, if there is 976one. 977Otherwise, the variable is initially unset. 978The shell 979uses dynamic scoping, so that if the variable 980.Va x 981is made local to function 982.Em f , 983which then calls function 984.Em g , 985references to the variable 986.Va x 987made inside 988.Em g 989will refer to the variable 990.Va x 991declared inside 992.Em f , 993not to the global variable named 994.Va x . 995.Pp 996The only special parameter that can be made local is 997.Ql - . 998Making 999.Ql - 1000local causes any shell options that are 1001changed via the 1002.Ic set 1003command inside the function to be 1004restored to their original values when the function 1005returns. 1006.Pp 1007The syntax of the 1008.Ic return 1009command is 1010.Pp 1011.D1 Ic return Op Ar exitstatus 1012.Pp 1013It terminates the current executional scope, returning from the previous 1014nested function, sourced script, or shell instance, in that order. 1015The 1016.Ic return 1017command is implemented as a special built-in command. 1018.Ss Variables and Parameters 1019The shell maintains a set of parameters. 1020A parameter 1021denoted by a name is called a variable. 1022When starting up, 1023the shell turns all the environment variables into shell 1024variables. 1025New variables can be set using the form 1026.Pp 1027.D1 Ar name Ns = Ns Ar value 1028.Pp 1029Variables set by the user must have a name consisting solely 1030of alphabetics, numerics, and underscores. 1031The first letter of a variable name must not be numeric. 1032A parameter can also be denoted by a number 1033or a special character as explained below. 1034.Ss Positional Parameters 1035A positional parameter is a parameter denoted by a number greater than zero. 1036The shell sets these initially to the values of its command line 1037arguments that follow the name of the shell script. 1038The 1039.Ic set 1040built-in command can also be used to set or reset them. 1041.Ss Special Parameters 1042A special parameter is a parameter denoted by a special one-character 1043name. 1044The special parameters recognized by the 1045.Nm 1046shell of 1047.Fx 1048are shown in the following list, exactly as they would appear in input 1049typed by the user or in the source of a shell script. 1050.Bl -hang 1051.It Li $* 1052Expands to the positional parameters, starting from one. 1053When 1054the expansion occurs within a double-quoted string 1055it expands to a single field with the value of each parameter 1056separated by the first character of the 1057.Va IFS 1058variable, 1059or by a space if 1060.Va IFS 1061is unset. 1062.It Li $@ 1063Expands to the positional parameters, starting from one. 1064When 1065the expansion occurs within double-quotes, each positional 1066parameter expands as a separate argument. 1067If there are no positional parameters, the 1068expansion of 1069.Li @ 1070generates zero arguments, even when 1071.Li @ 1072is double-quoted. 1073What this basically means, for example, is 1074if 1075.Li $1 1076is 1077.Dq Li abc 1078and 1079.Li $2 1080is 1081.Dq Li "def ghi" , 1082then 1083.Li \&"$@\&" 1084expands to 1085the two arguments: 1086.Bd -literal -offset indent 1087"abc" "def ghi" 1088.Ed 1089.It Li $# 1090Expands to the number of positional parameters. 1091.It Li $? 1092Expands to the exit status of the most recent pipeline. 1093.It Li $- 1094(hyphen) Expands to the current option flags (the single-letter 1095option names concatenated into a string) as specified on 1096invocation, by the 1097.Ic set 1098built-in command, or implicitly 1099by the shell. 1100.It Li $$ 1101Expands to the process ID of the invoked shell. 1102A subshell 1103retains the same value of 1104.Va $ 1105as its parent. 1106.It Li $! 1107Expands to the process ID of the most recent background 1108command executed from the current shell. 1109For a 1110pipeline, the process ID is that of the last command in the 1111pipeline. 1112.It Li $0 1113(zero) Expands to the name of the shell or shell script. 1114.El 1115.Ss Word Expansions 1116This clause describes the various expansions that are 1117performed on words. 1118Not all expansions are performed on 1119every word, as explained later. 1120.Pp 1121Tilde expansions, parameter expansions, command substitutions, 1122arithmetic expansions, and quote removals that occur within 1123a single word expand to a single field. 1124It is only field 1125splitting or pathname expansion that can create multiple 1126fields from a single word. 1127The single exception to this rule is 1128the expansion of the special parameter 1129.Va @ 1130within double-quotes, 1131as was described above. 1132.Pp 1133The order of word expansion is: 1134.Bl -enum 1135.It 1136Tilde Expansion, Parameter Expansion, Command Substitution, 1137Arithmetic Expansion (these all occur at the same time). 1138.It 1139Field Splitting is performed on fields generated by step (1) 1140unless the 1141.Va IFS 1142variable is null. 1143.It 1144Pathname Expansion (unless the 1145.Fl f 1146option is in effect). 1147.It 1148Quote Removal. 1149.El 1150.Pp 1151The 1152.Ql $ 1153character is used to introduce parameter expansion, command 1154substitution, or arithmetic evaluation. 1155.Ss Tilde Expansion (substituting a user's home directory) 1156A word beginning with an unquoted tilde character 1157.Pq Ql ~ 1158is 1159subjected to tilde expansion. 1160All the characters up to a slash 1161.Pq Ql / 1162or the end of the word are treated as a username 1163and are replaced with the user's home directory. 1164If the 1165username is missing (as in 1166.Pa ~/foobar ) , 1167the tilde is replaced with the value of the 1168.Ev HOME 1169variable (the current user's home directory). 1170.Ss Parameter Expansion 1171The format for parameter expansion is as follows: 1172.Pp 1173.D1 Li ${ Ns Ar expression Ns Li } 1174.Pp 1175where 1176.Ar expression 1177consists of all characters until the matching 1178.Ql } . 1179Any 1180.Ql } 1181escaped by a backslash or within a quoted string, and characters in 1182embedded arithmetic expansions, command substitutions, and variable 1183expansions, are not examined in determining the matching 1184.Ql } . 1185.Pp 1186The simplest form for parameter expansion is: 1187.Pp 1188.D1 Li ${ Ns Ar parameter Ns Li } 1189.Pp 1190The value, if any, of 1191.Ar parameter 1192is substituted. 1193.Pp 1194The parameter name or symbol can be enclosed in braces, which are 1195optional except for positional parameters with more than one digit or 1196when parameter is followed by a character that could be interpreted as 1197part of the name. 1198If a parameter expansion occurs inside double-quotes: 1199.Bl -enum 1200.It 1201Pathname expansion is not performed on the results of the 1202expansion. 1203.It 1204Field splitting is not performed on the results of the 1205expansion, with the exception of the special parameter 1206.Va @ . 1207.El 1208.Pp 1209In addition, a parameter expansion can be modified by using one of the 1210following formats. 1211.Bl -tag -width indent 1212.It Li ${ Ns Ar parameter Ns Li :- Ns Ar word Ns Li } 1213Use Default Values. 1214If 1215.Ar parameter 1216is unset or null, the expansion of 1217.Ar word 1218is substituted; otherwise, the value of 1219.Ar parameter 1220is substituted. 1221.It Li ${ Ns Ar parameter Ns Li := Ns Ar word Ns Li } 1222Assign Default Values. 1223If 1224.Ar parameter 1225is unset or null, the expansion of 1226.Ar word 1227is assigned to 1228.Ar parameter . 1229In all cases, the 1230final value of 1231.Ar parameter 1232is substituted. 1233Quoting inside 1234.Ar word 1235does not prevent field splitting or pathname expansion. 1236Only variables, not positional 1237parameters or special parameters, can be 1238assigned in this way. 1239.It Li ${ Ns Ar parameter Ns Li :? Ns Oo Ar word Oc Ns Li } 1240Indicate Error if Null or Unset. 1241If 1242.Ar parameter 1243is unset or null, the expansion of 1244.Ar word 1245(or a message indicating it is unset if 1246.Ar word 1247is omitted) is written to standard 1248error and the shell exits with a nonzero 1249exit status. 1250Otherwise, the value of 1251.Ar parameter 1252is substituted. 1253An 1254interactive shell need not exit. 1255.It Li ${ Ns Ar parameter Ns Li :+ Ns Ar word Ns Li } 1256Use Alternate Value. 1257If 1258.Ar parameter 1259is unset or null, null is substituted; 1260otherwise, the expansion of 1261.Ar word 1262is substituted. 1263.El 1264.Pp 1265In the parameter expansions shown previously, use of the colon in the 1266format results in a test for a parameter that is unset or null; omission 1267of the colon results in a test for a parameter that is only unset. 1268.Bl -tag -width indent 1269.It Li ${# Ns Ar parameter Ns Li } 1270String Length. 1271The length in characters of 1272the value of 1273.Ar parameter . 1274.El 1275.Pp 1276The following four varieties of parameter expansion provide for substring 1277processing. 1278In each case, pattern matching notation 1279(see 1280.Sx Shell Patterns ) , 1281rather than regular expression notation, 1282is used to evaluate the patterns. 1283If parameter is one of the special parameters 1284.Va * 1285or 1286.Va @ , 1287the result of the expansion is unspecified. 1288Enclosing the full parameter expansion string in double-quotes does not 1289cause the following four varieties of pattern characters to be quoted, 1290whereas quoting characters within the braces has this effect. 1291.Bl -tag -width indent 1292.It Li ${ Ns Ar parameter Ns Li % Ns Ar word Ns Li } 1293Remove Smallest Suffix Pattern. 1294The 1295.Ar word 1296is expanded to produce a pattern. 1297The 1298parameter expansion then results in 1299.Ar parameter , 1300with the smallest portion of the 1301suffix matched by the pattern deleted. 1302.It Li ${ Ns Ar parameter Ns Li %% Ns Ar word Ns Li } 1303Remove Largest Suffix Pattern. 1304The 1305.Ar word 1306is expanded to produce a pattern. 1307The 1308parameter expansion then results in 1309.Ar parameter , 1310with the largest portion of the 1311suffix matched by the pattern deleted. 1312.It Li ${ Ns Ar parameter Ns Li # Ns Ar word Ns Li } 1313Remove Smallest Prefix Pattern. 1314The 1315.Ar word 1316is expanded to produce a pattern. 1317The 1318parameter expansion then results in 1319.Ar parameter , 1320with the smallest portion of the 1321prefix matched by the pattern deleted. 1322.It Li ${ Ns Ar parameter Ns Li ## Ns Ar word Ns Li } 1323Remove Largest Prefix Pattern. 1324The 1325.Ar word 1326is expanded to produce a pattern. 1327The 1328parameter expansion then results in 1329.Ar parameter , 1330with the largest portion of the 1331prefix matched by the pattern deleted. 1332.El 1333.Ss Command Substitution 1334Command substitution allows the output of a command to be substituted in 1335place of the command name itself. 1336Command substitution occurs when 1337the command is enclosed as follows: 1338.Pp 1339.D1 Li $( Ns Ar command Ns Li )\& 1340.Pp 1341or the backquoted version: 1342.Pp 1343.D1 Li ` Ns Ar command Ns Li ` 1344.Pp 1345The shell expands the command substitution by executing command in a 1346subshell environment and replacing the command substitution 1347with the standard output of the command, 1348removing sequences of one or more newlines at the end of the substitution. 1349Embedded newlines before the end of the output are not removed; 1350however, during field splitting, they may be translated into spaces 1351depending on the value of 1352.Va IFS 1353and the quoting that is in effect. 1354.Ss Arithmetic Expansion 1355Arithmetic expansion provides a mechanism for evaluating an arithmetic 1356expression and substituting its value. 1357The format for arithmetic expansion is as follows: 1358.Pp 1359.D1 Li $(( Ns Ar expression Ns Li )) 1360.Pp 1361The 1362.Ar expression 1363is treated as if it were in double-quotes, except 1364that a double-quote inside the expression is not treated specially. 1365The 1366shell expands all tokens in the 1367.Ar expression 1368for parameter expansion, 1369command substitution, and quote removal. 1370.Pp 1371Next, the shell treats this as an arithmetic expression and 1372substitutes the value of the expression. 1373.Ss White Space Splitting (Field Splitting) 1374After parameter expansion, command substitution, and 1375arithmetic expansion the shell scans the results of 1376expansions and substitutions that did not occur in double-quotes for 1377field splitting and multiple fields can result. 1378.Pp 1379The shell treats each character of the 1380.Va IFS 1381variable as a delimiter and uses 1382the delimiters to split the results of parameter expansion and command 1383substitution into fields. 1384.Ss Pathname Expansion (File Name Generation) 1385Unless the 1386.Fl f 1387option is set, 1388file name generation is performed 1389after word splitting is complete. 1390Each word is 1391viewed as a series of patterns, separated by slashes. 1392The 1393process of expansion replaces the word with the names of 1394all existing files whose names can be formed by replacing 1395each pattern with a string that matches the specified pattern. 1396There are two restrictions on this: first, a pattern cannot match 1397a string containing a slash, and second, 1398a pattern cannot match a string starting with a period 1399unless the first character of the pattern is a period. 1400The next section describes the patterns used for both 1401Pathname Expansion and the 1402.Ic case 1403command. 1404.Ss Shell Patterns 1405A pattern consists of normal characters, which match themselves, 1406and meta-characters. 1407The meta-characters are 1408.Ql \&! , 1409.Ql * , 1410.Ql \&? , 1411and 1412.Ql \&[ . 1413These characters lose their special meanings if they are quoted. 1414When command or variable substitution is performed and the dollar sign 1415or back quotes are not double-quoted, the value of the 1416variable or the output of the command is scanned for these 1417characters and they are turned into meta-characters. 1418.Pp 1419An asterisk 1420.Pq Ql * 1421matches any string of characters. 1422A question mark 1423.Pq Ql \&? 1424matches any single character. 1425A left bracket 1426.Pq Ql \&[ 1427introduces a character class. 1428The end of the character class is indicated by a 1429.Ql \&] ; 1430if the 1431.Ql \&] 1432is missing then the 1433.Ql \&[ 1434matches a 1435.Ql \&[ 1436rather than introducing a character class. 1437A character class matches any of the characters between the square brackets. 1438A range of characters may be specified using a minus sign. 1439The character class may be complemented by making an exclamation point 1440.Pq Ql !\& 1441the first character of the character class. 1442.Pp 1443To include a 1444.Ql \&] 1445in a character class, make it the first character listed 1446(after the 1447.Ql \&! , 1448if any). 1449To include a 1450.Ql - , 1451make it the first or last character listed. 1452.Ss Built-in Commands 1453This section lists the commands which 1454are built-in because they need to perform some operation 1455that cannot be performed by a separate process. 1456In addition to 1457these, built-in versions of essential utilities 1458are provided for efficiency. 1459.Bl -tag -width indent 1460.It Ic \&: 1461A null command that returns a 0 (true) exit value. 1462.It Ic \&. Ar file 1463The commands in the specified file are read and executed by the shell. 1464The 1465.Ic return 1466command may be used to return to the 1467.Ic \&. 1468command's caller. 1469If 1470.Ar file 1471contains any 1472.Ql / 1473characters, it is used as is. 1474Otherwise, the shell searches the 1475.Ev PATH 1476for the file. 1477If it is not found in the 1478.Ev PATH , 1479it is sought in the current working directory. 1480.It Ic \&[ 1481A built-in equivalent of 1482.Xr test 1 . 1483.It Ic alias Oo Ar name Ns Oo = Ns Ar string Oc ... Oc 1484If 1485.Ar name Ns = Ns Ar string 1486is specified, the shell defines the alias 1487.Ar name 1488with value 1489.Ar string . 1490If just 1491.Ar name 1492is specified, the value of the alias 1493.Ar name 1494is printed. 1495With no arguments, the 1496.Ic alias 1497built-in command prints the names and values of all defined aliases 1498(see 1499.Ic unalias ) . 1500Alias values are written with appropriate quoting so that they are 1501suitable for re-input to the shell. 1502Also see the 1503.Sx Aliases 1504subsection. 1505.It Ic bg Op Ar job ... 1506Continue the specified jobs 1507(or the current job if no jobs are given) 1508in the background. 1509.It Ic builtin Ar cmd Op Ar arg ... 1510Execute the specified built-in command, 1511.Ar cmd . 1512This is useful when the user wishes to override a shell function 1513with the same name as a built-in command. 1514.It Ic bind Oo Fl aeklrsv Oc Oo Ar key Oo Ar command Oc Oc 1515List or alter key bindings for the line editor. 1516This command is documented in 1517.Xr editrc 5 . 1518.It Ic cd Oo Fl L | P Oc Op Ar directory 1519Switch to the specified 1520.Ar directory , 1521or to the directory specified in the 1522.Ev HOME 1523environment variable if no 1524.Ar directory 1525is specified. 1526If 1527.Ar directory 1528does not begin with 1529.Pa / , \&. , 1530or 1531.Pa .. , 1532then the directories listed in the 1533.Ev CDPATH 1534variable will be 1535searched for the specified 1536.Ar directory . 1537If 1538.Ev CDPATH 1539is unset, the current directory is searched. 1540The format of 1541.Ev CDPATH 1542is the same as that of 1543.Ev PATH . 1544In an interactive shell, 1545the 1546.Ic cd 1547command will print out the name of the directory 1548that it actually switched to 1549if this is different from the name that the user gave. 1550These may be different either because the 1551.Ev CDPATH 1552mechanism was used or because a symbolic link was crossed. 1553.Pp 1554If the 1555.Fl P 1556option is specified, 1557.Pa .. 1558is handled physically and symbolic links are resolved before 1559.Pa .. 1560components are processed. 1561If the 1562.Fl L 1563option is specified, 1564.Pa .. 1565is handled logically. 1566This is the default. 1567.It Ic chdir 1568A synonym for the 1569.Ic cd 1570built-in command. 1571.It Ic command Oo Fl p Oc Op Ar utility Op Ar argument ... 1572.It Ic command Oo Fl v | V Oc Op Ar utility 1573The first form of invocation executes the specified 1574.Ar utility 1575as a simple command (see the 1576.Sx Simple Commands 1577section). 1578.Pp 1579If the 1580.Fl p 1581option is specified, the command search is performed using a 1582default value of 1583.Ev PATH 1584that is guaranteed to find all of the standard utilities. 1585.Pp 1586If the 1587.Fl v 1588option is specified, 1589.Ar utility 1590is not executed but a description of its interpretation by the shell is 1591printed. 1592For ordinary commands the output is the path name; for shell built-in 1593commands, shell functions and keywords only the name is written. 1594Aliases are printed as 1595.Dq Ic alias Ar name Ns = Ns Ar value . 1596.Pp 1597The 1598.Fl V 1599option is identical to 1600.Fl v 1601except for the output. 1602It prints 1603.Dq Ar utility Ic is Ar description 1604where 1605.Ar description 1606is either 1607the path name to 1608.Ar utility , 1609a special shell builtin, 1610a shell builtin, 1611a shell function, 1612a shell keyword 1613or 1614an alias for 1615.Ar value . 1616.It Ic echo Oo Fl e | n Oc Op Ar string ... 1617Print a space-separated list of the arguments to the standard output 1618and append a newline character. 1619.Bl -tag -width indent 1620.It Fl n 1621Suppress the output of the trailing newline. 1622.It Fl e 1623Process C-style backslash escape sequences. 1624The 1625.Ic echo 1626command understands the following character escapes: 1627.Bl -tag -width indent 1628.It \ea 1629Alert (ring the terminal bell) 1630.It \eb 1631Backspace 1632.It \ec 1633Suppress the trailing newline (this has the side-effect of truncating the 1634line if it is not the last character) 1635.It \ee 1636The ESC character 1637.Tn ( ASCII 16380x1b) 1639.It \ef 1640Formfeed 1641.It \en 1642Newline 1643.It \er 1644Carriage return 1645.It \et 1646Horizontal tab 1647.It \ev 1648Vertical tab 1649.It \e\e 1650Literal backslash 1651.It \e0nnn 1652(Zero) The character whose octal value is 1653.Ar nnn 1654.El 1655.Pp 1656If 1657.Ar string 1658is not enclosed in quotes then the backslash itself must be escaped 1659with a backslash to protect it from the shell. 1660For example 1661.Bd -literal -offset indent 1662$ echo -e "a\evb" 1663a 1664 b 1665$ echo -e a\e\evb 1666a 1667 b 1668$ echo -e "a\e\eb" 1669a\eb 1670$ echo -e a\e\e\e\eb 1671a\eb 1672.Ed 1673.El 1674.Pp 1675Only one of the 1676.Fl e 1677and 1678.Fl n 1679options may be specified. 1680.It Ic eval Ar string ... 1681Concatenate all the arguments with spaces. 1682Then re-parse and execute the command. 1683.It Ic exec Op Ar command Op arg ... 1684Unless 1685.Ar command 1686is omitted, 1687the shell process is replaced with the specified program 1688(which must be a real program, not a shell built-in command or function). 1689Any redirections on the 1690.Ic exec 1691command are marked as permanent, 1692so that they are not undone when the 1693.Ic exec 1694command finishes. 1695.It Ic exit Op Ar exitstatus 1696Terminate the shell process. 1697If 1698.Ar exitstatus 1699is given 1700it is used as the exit status of the shell; 1701otherwise the exit status of the preceding command is used. 1702The exit status should be an integer between 0 and 255. 1703.It Ic export Ar name ... 1704.It Ic export Op Fl p 1705The specified names are exported so that they will 1706appear in the environment of subsequent commands. 1707The only way to un-export a variable is to 1708.Ic unset 1709it. 1710The shell allows the value of a variable to be set 1711at the same time as it is exported by writing 1712.Pp 1713.D1 Ic export Ar name Ns = Ns Ar value 1714.Pp 1715With no arguments the 1716.Ic export 1717command lists the names 1718of all exported variables. 1719If the 1720.Fl p 1721option is specified, the exported variables are printed as 1722.Dq Ic export Ar name Ns = Ns Ar value 1723lines, suitable for re-input to the shell. 1724.It Ic false 1725A null command that returns a non-zero (false) exit value. 1726.It Ic fc Oo Fl e Ar editor Oc Op Ar first Op Ar last 1727.It Ic fc Fl l Oo Fl nr Oc Op Ar first Op Ar last 1728.It Ic fc Fl s Oo Ar old Ns = Ns Ar new Oc Op Ar first 1729The 1730.Ic fc 1731built-in command lists, or edits and re-executes, 1732commands previously entered to an interactive shell. 1733.Bl -tag -width indent 1734.It Fl e Ar editor 1735Use the editor named by 1736.Ar editor 1737to edit the commands. 1738The 1739.Ar editor 1740string is a command name, 1741subject to search via the 1742.Ev PATH 1743variable. 1744The value in the 1745.Ev FCEDIT 1746variable is used as a default when 1747.Fl e 1748is not specified. 1749If 1750.Ev FCEDIT 1751is null or unset, the value of the 1752.Ev EDITOR 1753variable is used. 1754If 1755.Ev EDITOR 1756is null or unset, 1757.Xr ed 1 1758is used as the editor. 1759.It Fl l No (ell) 1760List the commands rather than invoking 1761an editor on them. 1762The commands are written in the 1763sequence indicated by the 1764.Ar first 1765and 1766.Ar last 1767operands, as affected by 1768.Fl r , 1769with each command preceded by the command number. 1770.It Fl n 1771Suppress command numbers when listing with 1772.Fl l . 1773.It Fl r 1774Reverse the order of the commands listed 1775(with 1776.Fl l ) 1777or edited 1778(with neither 1779.Fl l 1780nor 1781.Fl s ) . 1782.It Fl s 1783Re-execute the command without invoking an editor. 1784.It Ar first 1785.It Ar last 1786Select the commands to list or edit. 1787The number of previous commands that can be accessed 1788are determined by the value of the 1789.Ev HISTSIZE 1790variable. 1791The value of 1792.Ar first 1793or 1794.Ar last 1795or both are one of the following: 1796.Bl -tag -width indent 1797.It Oo Cm + Oc Ns Ar num 1798A positive number representing a command number; 1799command numbers can be displayed with the 1800.Fl l 1801option. 1802.It Fl Ar num 1803A negative decimal number representing the 1804command that was executed 1805.Ar num 1806of 1807commands previously. 1808For example, \-1 is the immediately previous command. 1809.It Ar string 1810A string indicating the most recently entered command 1811that begins with that string. 1812If the 1813.Ar old Ns = Ns Ar new 1814operand is not also specified with 1815.Fl s , 1816the string form of the first operand cannot contain an embedded equal sign. 1817.El 1818.El 1819.Pp 1820The following environment variables affect the execution of 1821.Ic fc : 1822.Bl -tag -width ".Ev HISTSIZE" 1823.It Ev FCEDIT 1824Name of the editor to use for history editing. 1825.It Ev HISTSIZE 1826The number of previous commands that are accessible. 1827.El 1828.It Ic fg Op Ar job 1829Move the specified 1830.Ar job 1831or the current job to the foreground. 1832.It Ic getopts Ar optstring var 1833The 1834.Tn POSIX 1835.Ic getopts 1836command. 1837The 1838.Ic getopts 1839command deprecates the older 1840.Xr getopt 1 1841command. 1842The first argument should be a series of letters, each possibly 1843followed by a colon which indicates that the option takes an argument. 1844The specified variable is set to the parsed option. 1845The index of 1846the next argument is placed into the shell variable 1847.Va OPTIND . 1848If an option takes an argument, it is placed into the shell variable 1849.Va OPTARG . 1850If an invalid option is encountered, 1851.Ar var 1852is set to 1853.Ql \&? . 1854It returns a false value (1) when it encounters the end of the options. 1855.It Ic hash Oo Fl rv Oc Op Ar command ... 1856The shell maintains a hash table which remembers the locations of commands. 1857With no arguments whatsoever, the 1858.Ic hash 1859command prints out the contents of this table. 1860Entries which have not been looked at since the last 1861.Ic cd 1862command are marked with an asterisk; 1863it is possible for these entries to be invalid. 1864.Pp 1865With arguments, the 1866.Ic hash 1867command removes each specified 1868.Ar command 1869from the hash table (unless they are functions) and then locates it. 1870With the 1871.Fl v 1872option, 1873.Ic hash 1874prints the locations of the commands as it finds them. 1875The 1876.Fl r 1877option causes the 1878.Ic hash 1879command to delete all the entries in the hash table except for functions. 1880.It Ic jobid Op Ar job 1881Print the process IDs of the processes in the specified 1882.Ar job . 1883If the 1884.Ar job 1885argument is omitted, use the current job. 1886.It Ic jobs Oo Fl lps Oc Op Ar job ... 1887Print information about the specified jobs, or all jobs if no 1888.Ar job 1889argument is given. 1890The information printed includes job ID, status and command name. 1891.Pp 1892If the 1893.Fl l 1894option is specified, the PID of each job is also printed. 1895If the 1896.Fl p 1897option is specified, only the process IDs for the process group leaders 1898are printed, one per line. 1899If the 1900.Fl s 1901option is specified, only the PIDs of the job commands are printed, one per 1902line. 1903.It Ic local Oo Ar variable ... Oc Op Fl 1904See the 1905.Sx Functions 1906subsection. 1907.It Ic pwd Op Fl L | P 1908Print the path of the current directory. 1909The built-in command may 1910differ from the program of the same name because the 1911built-in command remembers what the current directory 1912is rather than recomputing it each time. 1913This makes 1914it faster. 1915However, if the current directory is 1916renamed, 1917the built-in version of 1918.Xr pwd 1 1919will continue to print the old name for the directory. 1920.Pp 1921If the 1922.Fl P 1923option is specified, symbolic links are resolved. 1924If the 1925.Fl L 1926option is specified, the shell's notion of the current directory 1927is printed (symbolic links are not resolved). 1928This is the default. 1929.It Ic read Oo Fl p Ar prompt Oc Oo 1930.Fl t Ar timeout Oc Oo Fl er Oc Ar variable ... 1931The 1932.Ar prompt 1933is printed if the 1934.Fl p 1935option is specified 1936and the standard input is a terminal. 1937Then a line is 1938read from the standard input. 1939The trailing newline 1940is deleted from the line and the line is split as 1941described in the section on 1942.Sx White Space Splitting (Field Splitting) 1943above, and 1944the pieces are assigned to the variables in order. 1945If there are more pieces than variables, the remaining 1946pieces (along with the characters in 1947.Va IFS 1948that separated them) 1949are assigned to the last variable. 1950If there are more variables than pieces, the remaining 1951variables are assigned the null string. 1952.Pp 1953Backslashes are treated specially, unless the 1954.Fl r 1955option is 1956specified. 1957If a backslash is followed by 1958a newline, the backslash and the newline will be 1959deleted. 1960If a backslash is followed by any other 1961character, the backslash will be deleted and the following 1962character will be treated as though it were not in 1963.Va IFS , 1964even if it is. 1965.Pp 1966If the 1967.Fl t 1968option is specified and the 1969.Ar timeout 1970elapses before a complete line of input is supplied, 1971the 1972.Ic read 1973command will return an exit status of 1 without assigning any values. 1974The 1975.Ar timeout 1976value may optionally be followed by one of 1977.Ql s , 1978.Ql m 1979or 1980.Ql h 1981to explicitly specify seconds, minutes or hours. 1982If none is supplied, 1983.Ql s 1984is assumed. 1985.Pp 1986The 1987.Fl e 1988option exists only for backward compatibility with older scripts. 1989.It Ic readonly Oo Fl p Oc Op Ar name ... 1990Each specified 1991.Ar name 1992is marked as read only, 1993so that it cannot be subsequently modified or unset. 1994The shell allows the value of a variable to be set 1995at the same time as it is marked read only 1996by using the following form: 1997.Pp 1998.D1 Ic readonly Ar name Ns = Ns Ar value 1999.Pp 2000With no arguments the 2001.Ic readonly 2002command lists the names of all read only variables. 2003If the 2004.Fl p 2005option is specified, the read-only variables are printed as 2006.Dq Ic readonly Ar name Ns = Ns Ar value 2007lines, suitable for re-input to the shell. 2008.It Ic return Op Ar exitstatus 2009See the 2010.Sx Functions 2011subsection. 2012.It Ic set Oo Fl /+abCEefIimnpTuVvx Oc Oo Fl /+o Ar longname Oc Oo 2013.Fl c Ar string Oc Op Fl - Ar arg ... 2014The 2015.Ic set 2016command performs three different functions: 2017.Bl -item 2018.It 2019With no arguments, it lists the values of all shell variables. 2020.It 2021If options are given, 2022either in short form or using the long 2023.Dq Fl /+o Ar longname 2024form, 2025it sets or clears the specified options as described in the section called 2026.Sx Argument List Processing . 2027.It 2028If the 2029.Dq Fl - 2030option is specified, 2031.Ic set 2032will replace the shell's positional parameters with the subsequent 2033arguments. 2034If no arguments follow the 2035.Dq Fl - 2036option, 2037all the positional parameters will be cleared, 2038which is equivalent to executing the command 2039.Dq Li "shift $#" . 2040The 2041.Dq Fl - 2042flag may be omitted when specifying arguments to be used 2043as positional replacement parameters. 2044This is not recommended, 2045because the first argument may begin with a dash 2046.Pq Ql - 2047or a plus 2048.Pq Ql + , 2049which the 2050.Ic set 2051command will interpret as a request to enable or disable options. 2052.El 2053.It Ic setvar Ar variable value 2054Assigns the specified 2055.Ar value 2056to the specified 2057.Ar variable . 2058The 2059.Ic setvar 2060command is intended to be used in functions that 2061assign values to variables whose names are passed as parameters. 2062In general it is better to write 2063.Dq Ar variable Ns = Ns Ar value 2064rather than using 2065.Ic setvar . 2066.It Ic shift Op Ar n 2067Shift the positional parameters 2068.Ar n 2069times, or once if 2070.Ar n 2071is not specified. 2072A shift sets the value of 2073.Li $1 2074to the value of 2075.Li $2 , 2076the value of 2077.Li $2 2078to the value of 2079.Li $3 , 2080and so on, 2081decreasing the value of 2082.Li $# 2083by one. 2084If there are zero positional parameters, shifting does not do anything. 2085.It Ic test 2086A built-in equivalent of 2087.Xr test 1 . 2088.It Ic times 2089Print the amount of time spent executing the shell and its children. 2090The first output line shows the user and system times for the shell 2091itself, the second one contains the user and system times for the 2092children. 2093.It Ic trap Oo Ar action Oc Ar signal ... 2094.It Ic trap Fl l 2095Cause the shell to parse and execute 2096.Ar action 2097when any specified 2098.Ar signal 2099is received. 2100The signals are specified by name or number. 2101In addition, the pseudo-signal 2102.Cm EXIT 2103may be used to specify an 2104.Ar action 2105that is performed when the shell terminates. 2106The 2107.Ar action 2108may be an empty string or a dash 2109.Pq Ql - ; 2110the former causes the specified signal to be ignored 2111and the latter causes the default action to be taken. 2112Omitting the 2113.Ar action 2114is another way to request the default action, for compatibility reasons this 2115usage is not recommended though. 2116When the shell forks off a subshell, 2117it resets trapped (but not ignored) signals to the default action. 2118The 2119.Ic trap 2120command has no effect on signals that were ignored on entry to the shell. 2121.Pp 2122Option 2123.Fl l 2124causes the 2125.Ic trap 2126command to display a list of valid signal names. 2127.It Ic true 2128A null command that returns a 0 (true) exit value. 2129.It Ic type Op Ar name ... 2130Interpret each 2131.Ar name 2132as a command and print the resolution of the command search. 2133Possible resolutions are: 2134shell keyword, alias, special shell builtin, shell builtin, command, 2135tracked alias 2136and not found. 2137For aliases the alias expansion is printed; 2138for commands and tracked aliases 2139the complete pathname of the command is printed. 2140.It Ic ulimit Oo Fl HSabcdflmnpstuvw Oc Op Ar limit 2141Set or display resource limits (see 2142.Xr getrlimit 2 ) . 2143If 2144.Ar limit 2145is specified, the named resource will be set; 2146otherwise the current resource value will be displayed. 2147.Pp 2148If 2149.Fl H 2150is specified, the hard limits will be set or displayed. 2151While everybody is allowed to reduce a hard limit, 2152only the superuser can increase it. 2153The 2154.Fl S 2155option 2156specifies the soft limits instead. 2157When displaying limits, 2158only one of 2159.Fl S 2160or 2161.Fl H 2162can be given. 2163The default is to display the soft limits, 2164and to set both the hard and the soft limits. 2165.Pp 2166Option 2167.Fl a 2168causes the 2169.Ic ulimit 2170command to display all resources. 2171The parameter 2172.Ar limit 2173is not acceptable in this mode. 2174.Pp 2175The remaining options specify which resource value is to be 2176displayed or modified. 2177They are mutually exclusive. 2178.Bl -tag -width indent 2179.It Fl b Ar sbsize 2180The maximum size of socket buffer usage, in bytes. 2181.It Fl c Ar coredumpsize 2182The maximal size of core dump files, in 512-byte blocks. 2183.It Fl d Ar datasize 2184The maximal size of the data segment of a process, in kilobytes. 2185.It Fl f Ar filesize 2186The maximal size of a file, in 512-byte blocks. 2187.It Fl l Ar lockedmem 2188The maximal size of memory that can be locked by a process, in 2189kilobytes. 2190.It Fl m Ar memoryuse 2191The maximal resident set size of a process, in kilobytes. 2192.It Fl n Ar nofiles 2193The maximal number of descriptors that could be opened by a process. 2194.It Fl p Ar pseudoterminals 2195The maximal number of pseudo-terminals for this user ID. 2196.It Fl s Ar stacksize 2197The maximal size of the stack segment, in kilobytes. 2198.It Fl t Ar time 2199The maximal amount of CPU time to be used by each process, in seconds. 2200.It Fl u Ar userproc 2201The maximal number of simultaneous processes for this user ID. 2202.It Fl v Ar virtualmem 2203The maximal virtual size of a process, in kilobytes. 2204.It Fl w Ar swapuse 2205The maximum amount of swap space reserved or used for this user ID, 2206in kilobytes. 2207.El 2208.It Ic umask Oo Fl S Oc Op Ar mask 2209Set the file creation mask (see 2210.Xr umask 2 ) 2211to the octal or symbolic (see 2212.Xr chmod 1 ) 2213value specified by 2214.Ar mask . 2215If the argument is omitted, the current mask value is printed. 2216If the 2217.Fl S 2218option is specified, the output is symbolic, otherwise the output is octal. 2219.It Ic unalias Oo Fl a Oc Op Ar name ... 2220The specified alias names are removed. 2221If 2222.Fl a 2223is specified, all aliases are removed. 2224.It Ic unset Oo Fl fv Oc Ar name ... 2225The specified variables or functions are unset and unexported. 2226If the 2227.Fl v 2228option is specified or no options are given, the 2229.Ar name 2230arguments are treated as variable names. 2231If the 2232.Fl f 2233option is specified, the 2234.Ar name 2235arguments are treated as function names. 2236.It Ic wait Op Ar job 2237Wait for the specified 2238.Ar job 2239to complete and return the exit status of the last process in the 2240.Ar job . 2241If the argument is omitted, wait for all jobs to complete 2242and return an exit status of zero. 2243.El 2244.Ss Commandline Editing 2245When 2246.Nm 2247is being used interactively from a terminal, the current command 2248and the command history 2249(see 2250.Ic fc 2251in 2252.Sx Built-in Commands ) 2253can be edited using 2254.Nm vi Ns -mode 2255command line editing. 2256This mode uses commands similar 2257to a subset of those described in the 2258.Xr vi 1 2259man page. 2260The command 2261.Dq Li "set -o vi" 2262(or 2263.Dq Li "set -V" ) 2264enables 2265.Nm vi Ns -mode 2266editing and places 2267.Nm 2268into 2269.Nm vi 2270insert mode. 2271With 2272.Nm vi Ns -mode 2273enabled, 2274.Nm 2275can be switched between insert mode and command mode by typing 2276.Aq ESC . 2277Hitting 2278.Aq return 2279while in command mode will pass the line to the shell. 2280.Pp 2281Similarly, the 2282.Dq Li "set -o emacs" 2283(or 2284.Dq Li "set -E" ) 2285command can be used to enable a subset of 2286.Nm emacs Ns -style 2287command line editing features. 2288.Sh ENVIRONMENT 2289The following environment variables affect the execution of 2290.Nm : 2291.Bl -tag -width ".Ev HISTSIZE" 2292.It Ev CDPATH 2293The search path used with the 2294.Ic cd 2295built-in. 2296.It Ev EDITOR 2297The fallback editor used with the 2298.Ic fc 2299built-in. 2300If not set, the default editor is 2301.Xr ed 1 . 2302.It Ev FCEDIT 2303The default editor used with the 2304.Ic fc 2305built-in. 2306.It Ev HISTSIZE 2307The number of previous commands that are accessible. 2308.It Ev HOME 2309The starting directory of 2310.Nm . 2311.It Ev IFS 2312Input Field Separators. 2313This is normally set to 2314.Aq space , 2315.Aq tab , 2316and 2317.Aq newline . 2318See the 2319.Sx White Space Splitting 2320section for more details. 2321.It Ev MAIL 2322The name of a mail file, that will be checked for the arrival of new 2323mail. 2324Overridden by 2325.Ev MAILPATH . 2326.It Ev MAILPATH 2327A colon 2328.Pq Ql \&: 2329separated list of file names, for the shell to check for incoming 2330mail. 2331This environment setting overrides the 2332.Ev MAIL 2333setting. 2334There is a maximum of 10 mailboxes that can be monitored at once. 2335.It Ev PATH 2336The default search path for executables. 2337See the 2338.Sx Path Search 2339section for details. 2340.It Ev PS1 2341The primary prompt string, which defaults to 2342.Dq Li "$ " , 2343unless you are the superuser, in which case it defaults to 2344.Dq Li "# " . 2345.It Ev PS2 2346The secondary prompt string, which defaults to 2347.Dq Li "> " . 2348.It Ev PS4 2349The prefix for the trace output (if 2350.Fl x 2351is active). 2352The default is 2353.Dq Li "+ " . 2354.It Ev TERM 2355The default terminal setting for the shell. 2356This is inherited by children of the shell, and is used in the history 2357editing modes. 2358.El 2359.Sh EXIT STATUS 2360Errors that are detected by the shell, such as a syntax error, will 2361cause the shell to exit with a non-zero exit status. 2362If the shell is not an interactive shell, the execution of the shell 2363file will be aborted. 2364Otherwise the shell will return the exit status of the last command 2365executed, or if the 2366.Ic exit 2367builtin is used with a numeric argument, it 2368will return the argument. 2369.Sh SEE ALSO 2370.Xr builtin 1 , 2371.Xr chsh 1 , 2372.Xr echo 1 , 2373.Xr ed 1 , 2374.Xr emacs 1 , 2375.Xr expr 1 , 2376.Xr getopt 1 , 2377.Xr pwd 1 , 2378.Xr test 1 , 2379.Xr vi 1 , 2380.Xr execve 2 , 2381.Xr getrlimit 2 , 2382.Xr umask 2 , 2383.Xr editrc 5 2384.Sh HISTORY 2385A 2386.Nm 2387command, the Thompson shell, appeared in 2388.At v1 . 2389It was superseded in 2390.At v7 2391by the Bourne shell, which inherited the name 2392.Nm . 2393.Pp 2394This version of 2395.Nm 2396was rewritten in 1989 under the 2397.Bx 2398license after the Bourne shell from 2399.At V.4 . 2400.Sh AUTHORS 2401This version of 2402.Nm 2403was originally written by 2404.An Kenneth Almquist . 2405.Sh BUGS 2406The 2407.Nm 2408utility does not recognize multibyte characters. 2409