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