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