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 February 8, 2011 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. 459Using aliases in scripts is discouraged 460because the command that defines them must be executed 461before the code that uses them is parsed. 462This is fragile and not portable. 463.Pp 464An alias name may be escaped in a command line, so that it is not 465replaced by its alias value, by using quoting characters within or 466adjacent to the alias name. 467This is most often done by prefixing 468an alias name with a backslash to execute a function, built-in, or 469normal program with the same name. 470See the 471.Sx Quoting 472subsection. 473.Ss Commands 474The shell interprets the words it reads according to a 475language, the specification of which is outside the scope 476of this man page (refer to the BNF in the 477.St -p1003.2 478document). 479Essentially though, a line is read and if 480the first word of the line (or after a control operator) 481is not a keyword, then the shell has recognized a 482simple command. 483Otherwise, a complex command or some 484other special construct may have been recognized. 485.Ss Simple Commands 486If a simple command has been recognized, the shell performs 487the following actions: 488.Bl -enum 489.It 490Leading words of the form 491.Dq Li name=value 492are stripped off and assigned to the environment of 493the simple command. 494Redirection operators and 495their arguments (as described below) are stripped 496off and saved for processing. 497.It 498The remaining words are expanded as described in 499the section called 500.Sx Word Expansions , 501and the first remaining word is considered the command 502name and the command is located. 503The remaining 504words are considered the arguments of the command. 505If no command name resulted, then the 506.Dq Li name=value 507variable assignments recognized in 1) affect the 508current shell. 509.It 510Redirections are performed as described in 511the next section. 512.El 513.Ss Redirections 514Redirections are used to change where a command reads its input 515or sends its output. 516In general, redirections open, close, or 517duplicate an existing reference to a file. 518The overall format 519used for redirection is: 520.Pp 521.D1 Oo Ar n Oc Ar redir-op file 522.Pp 523The 524.Ar redir-op 525is one of the redirection operators mentioned 526previously. 527The following gives some examples of how these 528operators can be used. 529Note that stdin and stdout are commonly used abbreviations 530for standard input and standard output respectively. 531.Bl -tag -width "1234567890XX" -offset indent 532.It Oo Ar n Oc Ns Li > Ar file 533redirect stdout (or file descriptor 534.Ar n ) 535to 536.Ar file 537.It Oo Ar n Oc Ns Li >| Ar file 538same as above, but override the 539.Fl C 540option 541.It Oo Ar n Oc Ns Li >> Ar file 542append stdout (or file descriptor 543.Ar n ) 544to 545.Ar file 546.It Oo Ar n Oc Ns Li < Ar file 547redirect stdin (or file descriptor 548.Ar n ) 549from 550.Ar file 551.It Oo Ar n Oc Ns Li <> Ar file 552redirect stdin (or file descriptor 553.Ar n ) 554to and from 555.Ar file 556.It Oo Ar n1 Oc Ns Li <& Ns Ar n2 557duplicate stdin (or file descriptor 558.Ar n1 ) 559from file descriptor 560.Ar n2 561.It Oo Ar n Oc Ns Li <&- 562close stdin (or file descriptor 563.Ar n ) 564.It Oo Ar n1 Oc Ns Li >& Ns Ar n2 565duplicate stdout (or file descriptor 566.Ar n1 ) 567to file descriptor 568.Ar n2 569.It Oo Ar n Oc Ns Li >&- 570close stdout (or file descriptor 571.Ar n ) 572.El 573.Pp 574The following redirection is often called a 575.Dq here-document . 576.Bd -unfilled -offset indent 577.Oo Ar n Oc Ns Li << Ar delimiter 578.D1 Ar here-doc-text 579.D1 ... 580.Ar delimiter 581.Ed 582.Pp 583All the text on successive lines up to the delimiter is 584saved away and made available to the command on standard 585input, or file descriptor 586.Ar n 587if it is specified. 588If the 589.Ar delimiter 590as specified on the initial line is quoted, then the 591.Ar here-doc-text 592is treated literally, otherwise the text is subjected to 593parameter expansion, command substitution, and arithmetic 594expansion (as described in the section on 595.Sx Word Expansions ) . 596If the operator is 597.Dq Li <<- 598instead of 599.Dq Li << , 600then leading tabs 601in the 602.Ar here-doc-text 603are stripped. 604.Ss Search and Execution 605There are three types of commands: shell functions, 606built-in commands, and normal programs. 607The command is searched for (by name) in that order. 608The three types of commands are all executed in a different way. 609.Pp 610When a shell function is executed, all of the shell positional 611parameters (except 612.Li $0 , 613which remains unchanged) are 614set to the arguments of the shell function. 615The variables which are explicitly placed in the environment of 616the command (by placing assignments to them before the 617function name) are made local to the function and are set 618to the values given. 619Then the command given in the function definition is executed. 620The positional parameters are restored to their original values 621when the command completes. 622This all occurs within the current shell. 623.Pp 624Shell built-in commands are executed internally to the shell, without 625spawning a new process. 626There are two kinds of built-in commands: regular and special. 627Assignments before special builtins persist after they finish 628executing and assignment errors, redirection errors and certain 629operand errors cause a script to be aborted. 630Special builtins cannot be overridden with a function. 631Both regular and special builtins can affect the shell in ways 632normal programs cannot. 633.Pp 634Otherwise, if the command name does not match a function 635or built-in command, the command is searched for as a normal 636program in the file system (as described in the next section). 637When a normal program is executed, the shell runs the program, 638passing the arguments and the environment to the program. 639If the program is not a normal executable file 640(i.e., if it does not begin with the 641.Dq "magic number" 642whose 643.Tn ASCII 644representation is 645.Dq Li #! , 646resulting in an 647.Er ENOEXEC 648return value from 649.Xr execve 2 ) 650but appears to be a text file, 651the shell will run a new instance of 652.Nm 653to interpret it. 654.Pp 655Note that previous versions of this document 656and the source code itself misleadingly and sporadically 657refer to a shell script without a magic number 658as a 659.Dq "shell procedure" . 660.Ss Path Search 661When locating a command, the shell first looks to see if 662it has a shell function by that name. 663Then it looks for a 664built-in command by that name. 665If a built-in command is not found, 666one of two things happen: 667.Bl -enum 668.It 669Command names containing a slash are simply executed without 670performing any searches. 671.It 672The shell searches each entry in the 673.Va PATH 674variable 675in turn for the command. 676The value of the 677.Va PATH 678variable should be a series of 679entries separated by colons. 680Each entry consists of a 681directory name. 682The current directory 683may be indicated implicitly by an empty directory name, 684or explicitly by a single period. 685.El 686.Ss Command Exit Status 687Each command has an exit status that can influence the behavior 688of other shell commands. 689The paradigm is that a command exits 690with zero for normal or success, and non-zero for failure, 691error, or a false indication. 692The man page for each command 693should indicate the various exit codes and what they mean. 694Additionally, the built-in commands return exit codes, as does 695an executed shell function. 696.Pp 697If a command is terminated by a signal, its exit status is 128 plus 698the signal number. 699Signal numbers are defined in the header file 700.In sys/signal.h . 701.Ss Complex Commands 702Complex commands are combinations of simple commands 703with control operators or keywords, together creating a larger complex 704command. 705More generally, a command is one of the following: 706.Bl -item -offset indent 707.It 708simple command 709.It 710pipeline 711.It 712list or compound-list 713.It 714compound command 715.It 716function definition 717.El 718.Pp 719Unless otherwise stated, the exit status of a command is 720that of the last simple command executed by the command. 721.Ss Pipelines 722A pipeline is a sequence of one or more commands separated 723by the control operator 724.Ql \&| . 725The standard output of all but 726the last command is connected to the standard input 727of the next command. 728The standard output of the last 729command is inherited from the shell, as usual. 730.Pp 731The format for a pipeline is: 732.Pp 733.D1 Oo Li \&! Oc Ar command1 Op Li \&| Ar command2 ... 734.Pp 735The standard output of 736.Ar command1 737is connected to the standard input of 738.Ar command2 . 739The standard input, standard output, or 740both of a command is considered to be assigned by the 741pipeline before any redirection specified by redirection 742operators that are part of the command. 743.Pp 744Note that unlike some other shells, 745.Nm 746executes each process in a pipeline with more than one command 747in a subshell environment and as a child of the 748.Nm 749process. 750.Pp 751If the pipeline is not in the background (discussed later), 752the shell waits for all commands to complete. 753.Pp 754If the keyword 755.Ic !\& 756does not precede the pipeline, the 757exit status is the exit status of the last command specified 758in the pipeline. 759Otherwise, the exit status is the logical 760NOT of the exit status of the last command. 761That is, if 762the last command returns zero, the exit status is 1; if 763the last command returns greater than zero, the exit status 764is zero. 765.Pp 766Because pipeline assignment of standard input or standard 767output or both takes place before redirection, it can be 768modified by redirection. 769For example: 770.Pp 771.Dl "command1 2>&1 | command2" 772.Pp 773sends both the standard output and standard error of 774.Ar command1 775to the standard input of 776.Ar command2 . 777.Pp 778A 779.Ql \&; 780or newline terminator causes the preceding 781AND-OR-list 782(described below in the section called 783.Sx Short-Circuit List Operators ) 784to be executed sequentially; 785an 786.Ql & 787causes asynchronous execution of the preceding AND-OR-list. 788.Ss Background Commands (&) 789If a command is terminated by the control operator ampersand 790.Pq Ql & , 791the shell executes the command asynchronously; 792the shell does not wait for the command to finish 793before executing the next command. 794.Pp 795The format for running a command in background is: 796.Pp 797.D1 Ar command1 Li & Op Ar command2 Li & Ar ... 798.Pp 799If the shell is not interactive, the standard input of an 800asynchronous command is set to 801.Pa /dev/null . 802.Ss Lists (Generally Speaking) 803A list is a sequence of zero or more commands separated by 804newlines, semicolons, or ampersands, 805and optionally terminated by one of these three characters. 806The commands in a 807list are executed in the order they are written. 808If command is followed by an ampersand, the shell starts the 809command and immediately proceeds onto the next command; 810otherwise it waits for the command to terminate before 811proceeding to the next one. 812.Ss Short-Circuit List Operators 813.Dq Li && 814and 815.Dq Li || 816are AND-OR list operators. 817.Dq Li && 818executes the first command, and then executes the second command 819if the exit status of the first command is zero. 820.Dq Li || 821is similar, but executes the second command if the exit 822status of the first command is nonzero. 823.Dq Li && 824and 825.Dq Li || 826both have the same priority. 827.Ss Flow-Control Constructs (if, while, for, case) 828The syntax of the 829.Ic if 830command is: 831.Bd -unfilled -offset indent -compact 832.Ic if Ar list 833.Ic then Ar list 834.Oo Ic elif Ar list 835.Ic then Ar list Oc Ar ... 836.Op Ic else Ar list 837.Ic fi 838.Ed 839.Pp 840The syntax of the 841.Ic while 842command is: 843.Bd -unfilled -offset indent -compact 844.Ic while Ar list 845.Ic do Ar list 846.Ic done 847.Ed 848.Pp 849The two lists are executed repeatedly while the exit status of the 850first list is zero. 851The 852.Ic until 853command is similar, but has the word 854.Ic until 855in place of 856.Ic while , 857which causes it to 858repeat until the exit status of the first list is zero. 859.Pp 860The syntax of the 861.Ic for 862command is: 863.Bd -unfilled -offset indent -compact 864.Ic for Ar variable Op Ic in Ar word ... 865.Ic do Ar list 866.Ic done 867.Ed 868.Pp 869If 870.Ic in 871and the following words are omitted, 872.Ic in Li \&"$@\&" 873is used instead. 874The words are expanded, and then the list is executed 875repeatedly with the variable set to each word in turn. 876The 877.Ic do 878and 879.Ic done 880commands may be replaced with 881.Ql { 882and 883.Ql } . 884.Pp 885The syntax of the 886.Ic break 887and 888.Ic continue 889commands is: 890.D1 Ic break Op Ar num 891.D1 Ic continue Op Ar num 892.Pp 893The 894.Ic break 895command terminates the 896.Ar num 897innermost 898.Ic for 899or 900.Ic while 901loops. 902The 903.Ic continue 904command continues with the next iteration of the innermost loop. 905These are implemented as special built-in commands. 906.Pp 907The syntax of the 908.Ic case 909command is: 910.Bd -unfilled -offset indent -compact 911.Ic case Ar word Ic in 912.Ar pattern Ns Li ) Ar list Li ;; 913.Ar ... 914.Ic esac 915.Ed 916.Pp 917The pattern can actually be one or more patterns 918(see 919.Sx Shell Patterns 920described later), 921separated by 922.Ql \&| 923characters. 924The exit code of the 925.Ic case 926command is the exit code of the last command executed in the list or 927zero if no patterns were matched. 928.Ss Grouping Commands Together 929Commands may be grouped by writing either 930.Pp 931.D1 Li \&( Ns Ar list Ns Li \%) 932.Pp 933or 934.Pp 935.D1 Li { Ar list Ns Li \&; } 936.Pp 937The first form executes the commands in a subshell. 938Note that built-in commands thus executed do not affect the current shell. 939The second form does not fork another shell, 940so it is slightly more efficient. 941Grouping commands together this way allows the user to 942redirect their output as though they were one program: 943.Bd -literal -offset indent 944{ echo -n "hello"; echo " world"; } > greeting 945.Ed 946.Ss Functions 947The syntax of a function definition is 948.Pp 949.D1 Ar name Li \&( \&) Ar command 950.Pp 951A function definition is an executable statement; when 952executed it installs a function named 953.Ar name 954and returns an 955exit status of zero. 956The 957.Ar command 958is normally a list 959enclosed between 960.Ql { 961and 962.Ql } . 963.Pp 964Variables may be declared to be local to a function by 965using the 966.Ic local 967command. 968This should appear as the first statement of a function, 969and the syntax is: 970.Pp 971.D1 Ic local Oo Ar variable ... Oc Op Fl 972.Pp 973The 974.Ic local 975command is implemented as a built-in command. 976.Pp 977When a variable is made local, it inherits the initial 978value and exported and readonly flags from the variable 979with the same name in the surrounding scope, if there is 980one. 981Otherwise, the variable is initially unset. 982The shell 983uses dynamic scoping, so that if the variable 984.Va x 985is made local to function 986.Em f , 987which then calls function 988.Em g , 989references to the variable 990.Va x 991made inside 992.Em g 993will refer to the variable 994.Va x 995declared inside 996.Em f , 997not to the global variable named 998.Va x . 999.Pp 1000The only special parameter that can be made local is 1001.Ql - . 1002Making 1003.Ql - 1004local causes any shell options that are 1005changed via the 1006.Ic set 1007command inside the function to be 1008restored to their original values when the function 1009returns. 1010.Pp 1011The syntax of the 1012.Ic return 1013command is 1014.Pp 1015.D1 Ic return Op Ar exitstatus 1016.Pp 1017It terminates the current executional scope, returning from the previous 1018nested function, sourced script, or shell instance, in that order. 1019The 1020.Ic return 1021command is implemented as a special built-in command. 1022.Ss Variables and Parameters 1023The shell maintains a set of parameters. 1024A parameter 1025denoted by a name is called a variable. 1026When starting up, 1027the shell turns all the environment variables into shell 1028variables. 1029New variables can be set using the form 1030.Pp 1031.D1 Ar name Ns = Ns Ar value 1032.Pp 1033Variables set by the user must have a name consisting solely 1034of alphabetics, numerics, and underscores. 1035The first letter of a variable name must not be numeric. 1036A parameter can also be denoted by a number 1037or a special character as explained below. 1038.Ss Positional Parameters 1039A positional parameter is a parameter denoted by a number greater than zero. 1040The shell sets these initially to the values of its command line 1041arguments that follow the name of the shell script. 1042The 1043.Ic set 1044built-in command can also be used to set or reset them. 1045.Ss Special Parameters 1046Special parameters are parameters denoted by a single special character 1047or the digit zero. 1048They are shown in the following list, exactly as they would appear in input 1049typed by the user or in the source of a shell script. 1050.Bl -hang 1051.It Li $* 1052Expands to the positional parameters, starting from one. 1053When 1054the expansion occurs within a double-quoted string 1055it expands to a single field with the value of each parameter 1056separated by the first character of the 1057.Va IFS 1058variable, 1059or by a space if 1060.Va IFS 1061is unset. 1062.It Li $@ 1063Expands to the positional parameters, starting from one. 1064When 1065the expansion occurs within double-quotes, each positional 1066parameter expands as a separate argument. 1067If there are no positional parameters, the 1068expansion of 1069.Li @ 1070generates zero arguments, even when 1071.Li @ 1072is double-quoted. 1073What this basically means, for example, is 1074if 1075.Li $1 1076is 1077.Dq Li abc 1078and 1079.Li $2 1080is 1081.Dq Li "def ghi" , 1082then 1083.Li \&"$@\&" 1084expands to 1085the two arguments: 1086.Bd -literal -offset indent 1087"abc" "def ghi" 1088.Ed 1089.It Li $# 1090Expands to the number of positional parameters. 1091.It Li $? 1092Expands to the exit status of the most recent pipeline. 1093.It Li $- 1094(hyphen) Expands to the current option flags (the single-letter 1095option names concatenated into a string) as specified on 1096invocation, by the 1097.Ic set 1098built-in command, or implicitly 1099by the shell. 1100.It Li $$ 1101Expands to the process ID of the invoked shell. 1102A subshell 1103retains the same value of 1104.Va $ 1105as its parent. 1106.It Li $! 1107Expands to the process ID of the most recent background 1108command executed from the current shell. 1109For a 1110pipeline, the process ID is that of the last command in the 1111pipeline. 1112If this parameter is referenced, the shell will remember 1113the process ID and its exit status until the 1114.Ic wait 1115built-in command reports completion of the process. 1116.It Li $0 1117(zero) Expands to the name of the shell script if passed on the command line, 1118the 1119.Ar name 1120operand if given (with 1121.Fl c ) 1122or otherwise argument 0 passed to the shell. 1123.El 1124.Ss Special Variables 1125The following variables are set by the shell or 1126have special meaning to it: 1127.Bl -tag -width ".Va HISTSIZE" 1128.It Va CDPATH 1129The search path used with the 1130.Ic cd 1131built-in. 1132.It Va EDITOR 1133The fallback editor used with the 1134.Ic fc 1135built-in. 1136If not set, the default editor is 1137.Xr ed 1 . 1138.It Va FCEDIT 1139The default editor used with the 1140.Ic fc 1141built-in. 1142.It Va HISTSIZE 1143The number of previous commands that are accessible. 1144.It Va HOME 1145The user's home directory, 1146used in tilde expansion and as a default directory for the 1147.Ic cd 1148built-in. 1149.It Va IFS 1150Input Field Separators. 1151This is normally set to 1152.Aq space , 1153.Aq tab , 1154and 1155.Aq newline . 1156See the 1157.Sx White Space Splitting 1158section for more details. 1159.It Va LINENO 1160The current line number in the script or function. 1161.It Va MAIL 1162The name of a mail file, that will be checked for the arrival of new 1163mail. 1164Overridden by 1165.Va MAILPATH . 1166.It Va MAILPATH 1167A colon 1168.Pq Ql \&: 1169separated list of file names, for the shell to check for incoming 1170mail. 1171This variable overrides the 1172.Va MAIL 1173setting. 1174There is a maximum of 10 mailboxes that can be monitored at once. 1175.It Va PATH 1176The default search path for executables. 1177See the 1178.Sx Path Search 1179section for details. 1180.It Va PPID 1181The parent process ID of the invoked shell. 1182This is set at startup 1183unless this variable is in the environment. 1184A later change of parent process ID is not reflected. 1185A subshell retains the same value of 1186.Va PPID . 1187.It Va PS1 1188The primary prompt string, which defaults to 1189.Dq Li "$ " , 1190unless you are the superuser, in which case it defaults to 1191.Dq Li "# " . 1192.It Va PS2 1193The secondary prompt string, which defaults to 1194.Dq Li "> " . 1195.It Va PS4 1196The prefix for the trace output (if 1197.Fl x 1198is active). 1199The default is 1200.Dq Li "+ " . 1201.El 1202.Ss Word Expansions 1203This clause describes the various expansions that are 1204performed on words. 1205Not all expansions are performed on 1206every word, as explained later. 1207.Pp 1208Tilde expansions, parameter expansions, command substitutions, 1209arithmetic expansions, and quote removals that occur within 1210a single word expand to a single field. 1211It is only field 1212splitting or pathname expansion that can create multiple 1213fields from a single word. 1214The single exception to this rule is 1215the expansion of the special parameter 1216.Va @ 1217within double-quotes, 1218as was described above. 1219.Pp 1220The order of word expansion is: 1221.Bl -enum 1222.It 1223Tilde Expansion, Parameter Expansion, Command Substitution, 1224Arithmetic Expansion (these all occur at the same time). 1225.It 1226Field Splitting is performed on fields generated by step (1) 1227unless the 1228.Va IFS 1229variable is null. 1230.It 1231Pathname Expansion (unless the 1232.Fl f 1233option is in effect). 1234.It 1235Quote Removal. 1236.El 1237.Pp 1238The 1239.Ql $ 1240character is used to introduce parameter expansion, command 1241substitution, or arithmetic expansion. 1242.Ss Tilde Expansion (substituting a user's home directory) 1243A word beginning with an unquoted tilde character 1244.Pq Ql ~ 1245is 1246subjected to tilde expansion. 1247All the characters up to a slash 1248.Pq Ql / 1249or the end of the word are treated as a username 1250and are replaced with the user's home directory. 1251If the 1252username is missing (as in 1253.Pa ~/foobar ) , 1254the tilde is replaced with the value of the 1255.Va HOME 1256variable (the current user's home directory). 1257.Ss Parameter Expansion 1258The format for parameter expansion is as follows: 1259.Pp 1260.D1 Li ${ Ns Ar expression Ns Li } 1261.Pp 1262where 1263.Ar expression 1264consists of all characters until the matching 1265.Ql } . 1266Any 1267.Ql } 1268escaped by a backslash or within a single-quoted or double-quoted 1269string, and characters in 1270embedded arithmetic expansions, command substitutions, and variable 1271expansions, are not examined in determining the matching 1272.Ql } . 1273If the variants with 1274.Ql + , 1275.Ql - , 1276.Ql = 1277or 1278.Ql ?\& 1279occur within a double-quoted string, 1280as an extension there may be unquoted parts 1281(via double-quotes inside the expansion); 1282.Ql } 1283within such parts are also not examined in determining the matching 1284.Ql } . 1285.Pp 1286The simplest form for parameter expansion is: 1287.Pp 1288.D1 Li ${ Ns Ar parameter Ns Li } 1289.Pp 1290The value, if any, of 1291.Ar parameter 1292is substituted. 1293.Pp 1294The parameter name or symbol can be enclosed in braces, which are 1295optional except for positional parameters with more than one digit or 1296when parameter is followed by a character that could be interpreted as 1297part of the name. 1298If a parameter expansion occurs inside double-quotes: 1299.Bl -enum 1300.It 1301Pathname expansion is not performed on the results of the 1302expansion. 1303.It 1304Field splitting is not performed on the results of the 1305expansion, with the exception of the special parameter 1306.Va @ . 1307.El 1308.Pp 1309In addition, a parameter expansion can be modified by using one of the 1310following formats. 1311.Bl -tag -width indent 1312.It Li ${ Ns Ar parameter Ns Li :- Ns Ar word Ns Li } 1313Use Default Values. 1314If 1315.Ar parameter 1316is unset or null, the expansion of 1317.Ar word 1318is substituted; otherwise, the value of 1319.Ar parameter 1320is substituted. 1321.It Li ${ Ns Ar parameter Ns Li := Ns Ar word Ns Li } 1322Assign Default Values. 1323If 1324.Ar parameter 1325is unset or null, the expansion of 1326.Ar word 1327is assigned to 1328.Ar parameter . 1329In all cases, the 1330final value of 1331.Ar parameter 1332is substituted. 1333Quoting inside 1334.Ar word 1335does not prevent field splitting or pathname expansion. 1336Only variables, not positional 1337parameters or special parameters, can be 1338assigned in this way. 1339.It Li ${ Ns Ar parameter Ns Li :? Ns Oo Ar word Oc Ns Li } 1340Indicate Error if Null or Unset. 1341If 1342.Ar parameter 1343is unset or null, the expansion of 1344.Ar word 1345(or a message indicating it is unset if 1346.Ar word 1347is omitted) is written to standard 1348error and the shell exits with a nonzero 1349exit status. 1350Otherwise, the value of 1351.Ar parameter 1352is substituted. 1353An 1354interactive shell need not exit. 1355.It Li ${ Ns Ar parameter Ns Li :+ Ns Ar word Ns Li } 1356Use Alternate Value. 1357If 1358.Ar parameter 1359is unset or null, null is substituted; 1360otherwise, the expansion of 1361.Ar word 1362is substituted. 1363.El 1364.Pp 1365In the parameter expansions shown previously, use of the colon in the 1366format results in a test for a parameter that is unset or null; omission 1367of the colon results in a test for a parameter that is only unset. 1368.Pp 1369The 1370.Ar word 1371inherits the type of quoting 1372(unquoted, double-quoted or here-document) 1373from the surroundings, 1374with the exception that a backslash that quotes a closing brace is removed 1375during quote removal. 1376.Bl -tag -width indent 1377.It Li ${# Ns Ar parameter Ns Li } 1378String Length. 1379The length in characters of 1380the value of 1381.Ar parameter . 1382.El 1383.Pp 1384The following four varieties of parameter expansion provide for substring 1385processing. 1386In each case, pattern matching notation 1387(see 1388.Sx Shell Patterns ) , 1389rather than regular expression notation, 1390is used to evaluate the patterns. 1391If parameter is one of the special parameters 1392.Va * 1393or 1394.Va @ , 1395the result of the expansion is unspecified. 1396Enclosing the full parameter expansion string in double-quotes does not 1397cause the following four varieties of pattern characters to be quoted, 1398whereas quoting characters within the braces has this effect. 1399.Bl -tag -width indent 1400.It Li ${ Ns Ar parameter Ns Li % Ns Ar word Ns Li } 1401Remove Smallest Suffix Pattern. 1402The 1403.Ar word 1404is expanded to produce a pattern. 1405The 1406parameter expansion then results in 1407.Ar parameter , 1408with the smallest portion of the 1409suffix matched by the pattern deleted. 1410.It Li ${ Ns Ar parameter Ns Li %% Ns Ar word Ns Li } 1411Remove Largest Suffix Pattern. 1412The 1413.Ar word 1414is expanded to produce a pattern. 1415The 1416parameter expansion then results in 1417.Ar parameter , 1418with the largest portion of the 1419suffix matched by the pattern deleted. 1420.It Li ${ Ns Ar parameter Ns Li # Ns Ar word Ns Li } 1421Remove Smallest Prefix Pattern. 1422The 1423.Ar word 1424is expanded to produce a pattern. 1425The 1426parameter expansion then results in 1427.Ar parameter , 1428with the smallest portion of the 1429prefix matched by the pattern deleted. 1430.It Li ${ Ns Ar parameter Ns Li ## Ns Ar word Ns Li } 1431Remove Largest Prefix Pattern. 1432The 1433.Ar word 1434is expanded to produce a pattern. 1435The 1436parameter expansion then results in 1437.Ar parameter , 1438with the largest portion of the 1439prefix matched by the pattern deleted. 1440.El 1441.Ss Command Substitution 1442Command substitution allows the output of a command to be substituted in 1443place of the command name itself. 1444Command substitution occurs when 1445the command is enclosed as follows: 1446.Pp 1447.D1 Li $( Ns Ar command Ns Li )\& 1448.Pp 1449or the backquoted version: 1450.Pp 1451.D1 Li ` Ns Ar command Ns Li ` 1452.Pp 1453The shell expands the command substitution by executing command in a 1454subshell environment and replacing the command substitution 1455with the standard output of the command, 1456removing sequences of one or more newlines at the end of the substitution. 1457Embedded newlines before the end of the output are not removed; 1458however, during field splitting, they may be translated into spaces 1459depending on the value of 1460.Va IFS 1461and the quoting that is in effect. 1462.Ss Arithmetic Expansion 1463Arithmetic expansion provides a mechanism for evaluating an arithmetic 1464expression and substituting its value. 1465The format for arithmetic expansion is as follows: 1466.Pp 1467.D1 Li $(( Ns Ar expression Ns Li )) 1468.Pp 1469The 1470.Ar expression 1471is treated as if it were in double-quotes, except 1472that a double-quote inside the expression is not treated specially. 1473The 1474shell expands all tokens in the 1475.Ar expression 1476for parameter expansion, 1477command substitution, 1478arithmetic expansion 1479and quote removal. 1480.Pp 1481The allowed expressions are a subset of C expressions, 1482summarized below. 1483.Bl -tag -width "Variables" -offset indent 1484.It Values 1485All values are of type 1486.Ft intmax_t . 1487.It Constants 1488Decimal, octal (starting with 1489.Li 0 ) 1490and hexadecimal (starting with 1491.Li 0x ) 1492integer constants. 1493.It Variables 1494Shell variables can be read and written 1495and contain integer constants. 1496.It Unary operators 1497.Li "! ~ + -" 1498.It Binary operators 1499.Li "* / % + - << >> < <= > >= == != & ^ | && ||" 1500.It Assignment operators 1501.Li "= += -= *= /= %= <<= >>= &= ^= |=" 1502.It Conditional operator 1503.Li "? :" 1504.El 1505.Pp 1506The result of the expression is substituted in decimal. 1507.Ss White Space Splitting (Field Splitting) 1508After parameter expansion, command substitution, and 1509arithmetic expansion the shell scans the results of 1510expansions and substitutions that did not occur in double-quotes for 1511field splitting and multiple fields can result. 1512.Pp 1513The shell treats each character of the 1514.Va IFS 1515variable as a delimiter and uses 1516the delimiters to split the results of parameter expansion and command 1517substitution into fields. 1518.Ss Pathname Expansion (File Name Generation) 1519Unless the 1520.Fl f 1521option is set, 1522file name generation is performed 1523after word splitting is complete. 1524Each word is 1525viewed as a series of patterns, separated by slashes. 1526The 1527process of expansion replaces the word with the names of 1528all existing files whose names can be formed by replacing 1529each pattern with a string that matches the specified pattern. 1530There are two restrictions on this: first, a pattern cannot match 1531a string containing a slash, and second, 1532a pattern cannot match a string starting with a period 1533unless the first character of the pattern is a period. 1534The next section describes the patterns used for both 1535Pathname Expansion and the 1536.Ic case 1537command. 1538.Ss Shell Patterns 1539A pattern consists of normal characters, which match themselves, 1540and meta-characters. 1541The meta-characters are 1542.Ql \&! , 1543.Ql * , 1544.Ql \&? , 1545and 1546.Ql \&[ . 1547These characters lose their special meanings if they are quoted. 1548When command or variable substitution is performed and the dollar sign 1549or back quotes are not double-quoted, the value of the 1550variable or the output of the command is scanned for these 1551characters and they are turned into meta-characters. 1552.Pp 1553An asterisk 1554.Pq Ql * 1555matches any string of characters. 1556A question mark 1557.Pq Ql \&? 1558matches any single character. 1559A left bracket 1560.Pq Ql \&[ 1561introduces a character class. 1562The end of the character class is indicated by a 1563.Ql \&] ; 1564if the 1565.Ql \&] 1566is missing then the 1567.Ql \&[ 1568matches a 1569.Ql \&[ 1570rather than introducing a character class. 1571A character class matches any of the characters between the square brackets. 1572A range of characters may be specified using a minus sign. 1573The character class may be complemented by making an exclamation point 1574.Pq Ql !\& 1575the first character of the character class. 1576.Pp 1577To include a 1578.Ql \&] 1579in a character class, make it the first character listed 1580(after the 1581.Ql \&! , 1582if any). 1583To include a 1584.Ql - , 1585make it the first or last character listed. 1586.Ss Built-in Commands 1587This section lists the built-in commands. 1588.Bl -tag -width indent 1589.It Ic \&: 1590A null command that returns a 0 (true) exit value. 1591.It Ic \&. Ar file 1592The commands in the specified file are read and executed by the shell. 1593The 1594.Ic return 1595command may be used to return to the 1596.Ic \&. 1597command's caller. 1598If 1599.Ar file 1600contains any 1601.Ql / 1602characters, it is used as is. 1603Otherwise, the shell searches the 1604.Va PATH 1605for the file. 1606If it is not found in the 1607.Va PATH , 1608it is sought in the current working directory. 1609.It Ic \&[ 1610A built-in equivalent of 1611.Xr test 1 . 1612.It Ic alias Oo Ar name Ns Oo = Ns Ar string Oc ... Oc 1613If 1614.Ar name Ns = Ns Ar string 1615is specified, the shell defines the alias 1616.Ar name 1617with value 1618.Ar string . 1619If just 1620.Ar name 1621is specified, the value of the alias 1622.Ar name 1623is printed. 1624With no arguments, the 1625.Ic alias 1626built-in command prints the names and values of all defined aliases 1627(see 1628.Ic unalias ) . 1629Alias values are written with appropriate quoting so that they are 1630suitable for re-input to the shell. 1631Also see the 1632.Sx Aliases 1633subsection. 1634.It Ic bg Op Ar job ... 1635Continue the specified jobs 1636(or the current job if no jobs are given) 1637in the background. 1638.It Ic bind Oo Fl aeklrsv Oc Oo Ar key Oo Ar command Oc Oc 1639List or alter key bindings for the line editor. 1640This command is documented in 1641.Xr editrc 5 . 1642.It Ic break Op Ar num 1643See the 1644.Sx Flow-Control Constructs 1645subsection. 1646.It Ic builtin Ar cmd Op Ar arg ... 1647Execute the specified built-in command, 1648.Ar cmd . 1649This is useful when the user wishes to override a shell function 1650with the same name as a built-in command. 1651.It Ic cd Oo Fl L | P Oc Op Ar directory 1652Switch to the specified 1653.Ar directory , 1654or to the directory specified in the 1655.Va HOME 1656environment variable if no 1657.Ar directory 1658is specified. 1659If 1660.Ar directory 1661does not begin with 1662.Pa / , \&. , 1663or 1664.Pa .. , 1665then the directories listed in the 1666.Va CDPATH 1667variable will be 1668searched for the specified 1669.Ar directory . 1670If 1671.Va CDPATH 1672is unset, the current directory is searched. 1673The format of 1674.Va CDPATH 1675is the same as that of 1676.Va PATH . 1677In an interactive shell, 1678the 1679.Ic cd 1680command will print out the name of the directory 1681that it actually switched to 1682if this is different from the name that the user gave. 1683These may be different either because the 1684.Va CDPATH 1685mechanism was used or because a symbolic link was crossed. 1686.Pp 1687If the 1688.Fl P 1689option is specified, 1690.Pa .. 1691is handled physically and symbolic links are resolved before 1692.Pa .. 1693components are processed. 1694If the 1695.Fl L 1696option is specified, 1697.Pa .. 1698is handled logically. 1699This is the default. 1700.It Ic chdir 1701A synonym for the 1702.Ic cd 1703built-in command. 1704.It Ic command Oo Fl p Oc Op Ar utility Op Ar argument ... 1705.It Ic command Oo Fl p Oc Fl v Ar utility 1706.It Ic command Oo Fl p Oc Fl V Ar utility 1707The first form of invocation executes the specified 1708.Ar utility , 1709ignoring shell functions in the search. 1710If 1711.Ar utility 1712is a special builtin, 1713it is executed as if it were a regular builtin. 1714.Pp 1715If the 1716.Fl p 1717option is specified, the command search is performed using a 1718default value of 1719.Va PATH 1720that is guaranteed to find all of the standard utilities. 1721.Pp 1722If the 1723.Fl v 1724option is specified, 1725.Ar utility 1726is not executed but a description of its interpretation by the shell is 1727printed. 1728For ordinary commands the output is the path name; for shell built-in 1729commands, shell functions and keywords only the name is written. 1730Aliases are printed as 1731.Dq Ic alias Ar name Ns = Ns Ar value . 1732.Pp 1733The 1734.Fl V 1735option is identical to 1736.Fl v 1737except for the output. 1738It prints 1739.Dq Ar utility Ic is Ar description 1740where 1741.Ar description 1742is either 1743the path name to 1744.Ar utility , 1745a special shell builtin, 1746a shell builtin, 1747a shell function, 1748a shell keyword 1749or 1750an alias for 1751.Ar value . 1752.It Ic continue Op Ar num 1753See the 1754.Sx Flow-Control Constructs 1755subsection. 1756.It Ic echo Oo Fl e | n Oc Op Ar string ... 1757Print a space-separated list of the arguments to the standard output 1758and append a newline character. 1759.Bl -tag -width indent 1760.It Fl n 1761Suppress the output of the trailing newline. 1762.It Fl e 1763Process C-style backslash escape sequences. 1764The 1765.Ic echo 1766command understands the following character escapes: 1767.Bl -tag -width indent 1768.It \ea 1769Alert (ring the terminal bell) 1770.It \eb 1771Backspace 1772.It \ec 1773Suppress the trailing newline (this has the side-effect of truncating the 1774line if it is not the last character) 1775.It \ee 1776The ESC character 1777.Tn ( ASCII 17780x1b) 1779.It \ef 1780Formfeed 1781.It \en 1782Newline 1783.It \er 1784Carriage return 1785.It \et 1786Horizontal tab 1787.It \ev 1788Vertical tab 1789.It \e\e 1790Literal backslash 1791.It \e0nnn 1792(Zero) The character whose octal value is 1793.Ar nnn 1794.El 1795.Pp 1796If 1797.Ar string 1798is not enclosed in quotes then the backslash itself must be escaped 1799with a backslash to protect it from the shell. 1800For example 1801.Bd -literal -offset indent 1802$ echo -e "a\evb" 1803a 1804 b 1805$ echo -e a\e\evb 1806a 1807 b 1808$ echo -e "a\e\eb" 1809a\eb 1810$ echo -e a\e\e\e\eb 1811a\eb 1812.Ed 1813.El 1814.Pp 1815Only one of the 1816.Fl e 1817and 1818.Fl n 1819options may be specified. 1820.It Ic eval Ar string ... 1821Concatenate all the arguments with spaces. 1822Then re-parse and execute the command. 1823.It Ic exec Op Ar command Op arg ... 1824Unless 1825.Ar command 1826is omitted, 1827the shell process is replaced with the specified program 1828(which must be a real program, not a shell built-in command or function). 1829Any redirections on the 1830.Ic exec 1831command are marked as permanent, 1832so that they are not undone when the 1833.Ic exec 1834command finishes. 1835.It Ic exit Op Ar exitstatus 1836Terminate the shell process. 1837If 1838.Ar exitstatus 1839is given 1840it is used as the exit status of the shell; 1841otherwise, if the shell is executing an 1842.Cm EXIT 1843trap, the exit status of the last command before the trap is used; 1844if the shell is executing a trap for a signal, 1845the shell exits by resending the signal to itself; 1846otherwise, the exit status of the preceding command is used. 1847The exit status should be an integer between 0 and 255. 1848.It Ic export Ar name ... 1849.It Ic export Op Fl p 1850The specified names are exported so that they will 1851appear in the environment of subsequent commands. 1852The only way to un-export a variable is to 1853.Ic unset 1854it. 1855The shell allows the value of a variable to be set 1856at the same time as it is exported by writing 1857.Pp 1858.D1 Ic export Ar name Ns = Ns Ar value 1859.Pp 1860With no arguments the 1861.Ic export 1862command lists the names 1863of all exported variables. 1864If the 1865.Fl p 1866option is specified, the exported variables are printed as 1867.Dq Ic export Ar name Ns = Ns Ar value 1868lines, suitable for re-input to the shell. 1869.It Ic false 1870A null command that returns a non-zero (false) exit value. 1871.It Ic fc Oo Fl e Ar editor Oc Op Ar first Op Ar last 1872.It Ic fc Fl l Oo Fl nr Oc Op Ar first Op Ar last 1873.It Ic fc Fl s Oo Ar old Ns = Ns Ar new Oc Op Ar first 1874The 1875.Ic fc 1876built-in command lists, or edits and re-executes, 1877commands previously entered to an interactive shell. 1878.Bl -tag -width indent 1879.It Fl e Ar editor 1880Use the editor named by 1881.Ar editor 1882to edit the commands. 1883The 1884.Ar editor 1885string is a command name, 1886subject to search via the 1887.Va PATH 1888variable. 1889The value in the 1890.Va FCEDIT 1891variable is used as a default when 1892.Fl e 1893is not specified. 1894If 1895.Va FCEDIT 1896is null or unset, the value of the 1897.Va EDITOR 1898variable is used. 1899If 1900.Va EDITOR 1901is null or unset, 1902.Xr ed 1 1903is used as the editor. 1904.It Fl l No (ell) 1905List the commands rather than invoking 1906an editor on them. 1907The commands are written in the 1908sequence indicated by the 1909.Ar first 1910and 1911.Ar last 1912operands, as affected by 1913.Fl r , 1914with each command preceded by the command number. 1915.It Fl n 1916Suppress command numbers when listing with 1917.Fl l . 1918.It Fl r 1919Reverse the order of the commands listed 1920(with 1921.Fl l ) 1922or edited 1923(with neither 1924.Fl l 1925nor 1926.Fl s ) . 1927.It Fl s 1928Re-execute the command without invoking an editor. 1929.It Ar first 1930.It Ar last 1931Select the commands to list or edit. 1932The number of previous commands that can be accessed 1933are determined by the value of the 1934.Va HISTSIZE 1935variable. 1936The value of 1937.Ar first 1938or 1939.Ar last 1940or both are one of the following: 1941.Bl -tag -width indent 1942.It Oo Cm + Oc Ns Ar num 1943A positive number representing a command number; 1944command numbers can be displayed with the 1945.Fl l 1946option. 1947.It Fl Ar num 1948A negative decimal number representing the 1949command that was executed 1950.Ar num 1951of 1952commands previously. 1953For example, \-1 is the immediately previous command. 1954.It Ar string 1955A string indicating the most recently entered command 1956that begins with that string. 1957If the 1958.Ar old Ns = Ns Ar new 1959operand is not also specified with 1960.Fl s , 1961the string form of the first operand cannot contain an embedded equal sign. 1962.El 1963.El 1964.Pp 1965The following variables affect the execution of 1966.Ic fc : 1967.Bl -tag -width ".Va HISTSIZE" 1968.It Va FCEDIT 1969Name of the editor to use for history editing. 1970.It Va HISTSIZE 1971The number of previous commands that are accessible. 1972.El 1973.It Ic fg Op Ar job 1974Move the specified 1975.Ar job 1976or the current job to the foreground. 1977.It Ic getopts Ar optstring var 1978The 1979.Tn POSIX 1980.Ic getopts 1981command. 1982The 1983.Ic getopts 1984command deprecates the older 1985.Xr getopt 1 1986command. 1987The first argument should be a series of letters, each possibly 1988followed by a colon which indicates that the option takes an argument. 1989The specified variable is set to the parsed option. 1990The index of 1991the next argument is placed into the shell variable 1992.Va OPTIND . 1993If an option takes an argument, it is placed into the shell variable 1994.Va OPTARG . 1995If an invalid option is encountered, 1996.Ar var 1997is set to 1998.Ql \&? . 1999It returns a false value (1) when it encounters the end of the options. 2000.It Ic hash Oo Fl rv Oc Op Ar command ... 2001The shell maintains a hash table which remembers the locations of commands. 2002With no arguments whatsoever, the 2003.Ic hash 2004command prints out the contents of this table. 2005Entries which have not been looked at since the last 2006.Ic cd 2007command are marked with an asterisk; 2008it is possible for these entries to be invalid. 2009.Pp 2010With arguments, the 2011.Ic hash 2012command removes each specified 2013.Ar command 2014from the hash table (unless they are functions) and then locates it. 2015With the 2016.Fl v 2017option, 2018.Ic hash 2019prints the locations of the commands as it finds them. 2020The 2021.Fl r 2022option causes the 2023.Ic hash 2024command to delete all the entries in the hash table except for functions. 2025.It Ic jobid Op Ar job 2026Print the process IDs of the processes in the specified 2027.Ar job . 2028If the 2029.Ar job 2030argument is omitted, use the current job. 2031.It Ic jobs Oo Fl lps Oc Op Ar job ... 2032Print information about the specified jobs, or all jobs if no 2033.Ar job 2034argument is given. 2035The information printed includes job ID, status and command name. 2036.Pp 2037If the 2038.Fl l 2039option is specified, the PID of each job is also printed. 2040If the 2041.Fl p 2042option is specified, only the process IDs for the process group leaders 2043are printed, one per line. 2044If the 2045.Fl s 2046option is specified, only the PIDs of the job commands are printed, one per 2047line. 2048.It Ic kill 2049A built-in equivalent of 2050.Xr kill 1 2051that additionally supports sending signals to jobs. 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 kill 1 , 2481.Xr printf 1 , 2482.Xr pwd 1 , 2483.Xr test 1 , 2484.Xr vi 1 , 2485.Xr execve 2 , 2486.Xr getrlimit 2 , 2487.Xr umask 2 , 2488.Xr editrc 5 2489.Sh HISTORY 2490A 2491.Nm 2492command, the Thompson shell, appeared in 2493.At v1 . 2494It was superseded in 2495.At v7 2496by the Bourne shell, which inherited the name 2497.Nm . 2498.Pp 2499This version of 2500.Nm 2501was rewritten in 1989 under the 2502.Bx 2503license after the Bourne shell from 2504.At V.4 . 2505.Sh AUTHORS 2506This version of 2507.Nm 2508was originally written by 2509.An Kenneth Almquist . 2510.Sh BUGS 2511The 2512.Nm 2513utility does not recognize multibyte characters. 2514