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