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 one of the following 951special characters. 952The value of the parameter is listed 953next to its character. 954.Bl -hang 955.It Li * 956Expands to the positional parameters, starting from one. 957When 958the expansion occurs within a double-quoted string 959it expands to a single field with the value of each parameter 960separated by the first character of the 961.Ev IFS 962variable, 963or by a 964.Aq space 965if 966.Ev IFS 967is unset. 968.It Li @ 969Expands to the positional parameters, starting from one. 970When 971the expansion occurs within double-quotes, each positional 972parameter expands as a separate argument. 973If there are no positional parameters, the 974expansion of 975.Li @ 976generates zero arguments, even when 977.Li @ 978is double-quoted. 979What this basically means, for example, is 980if $1 is 981.Dq abc 982and $2 is 983.Dq def ghi , 984then 985.Qq Li $@ 986expands to 987the two arguments: 988.Bd -literal -offset indent 989"abc" "def ghi" 990.Ed 991.It Li # 992Expands to the number of positional parameters. 993.It Li \&? 994Expands to the exit status of the most recent pipeline. 995.It Li - 996(hyphen) Expands to the current option flags (the single-letter 997option names concatenated into a string) as specified on 998invocation, by the set built-in command, or implicitly 999by the shell. 1000.It Li $ 1001Expands to the process ID of the invoked shell. 1002A subshell 1003retains the same value of $ as its parent. 1004.It Li \&! 1005Expands to the process ID of the most recent background 1006command executed from the current shell. 1007For a 1008pipeline, the process ID is that of the last command in the 1009pipeline. 1010.It Li 0 1011(zero) Expands to the name of the shell or shell script. 1012.El 1013.Ss Word Expansions 1014This clause describes the various expansions that are 1015performed on words. 1016Not all expansions are performed on 1017every word, as explained later. 1018.Pp 1019Tilde expansions, parameter expansions, command substitutions, 1020arithmetic expansions, and quote removals that occur within 1021a single word expand to a single field. 1022It is only field 1023splitting or pathname expansion that can create multiple 1024fields from a single word. 1025The single exception to this rule is 1026the expansion of the special parameter 1027.Li @ 1028within double-quotes, 1029as was described above. 1030.Pp 1031The order of word expansion is: 1032.Bl -enum 1033.It 1034Tilde Expansion, Parameter Expansion, Command Substitution, 1035Arithmetic Expansion (these all occur at the same time). 1036.It 1037Field Splitting is performed on fields generated by step (1) 1038unless the 1039.Ev IFS 1040variable is null. 1041.It 1042Pathname Expansion (unless the 1043.Fl f 1044option is in effect). 1045.It 1046Quote Removal. 1047.El 1048.Pp 1049The 1050.Dq Li $ 1051character is used to introduce parameter expansion, command 1052substitution, or arithmetic evaluation. 1053.Ss Tilde Expansion (substituting a user's home directory) 1054A word beginning with an unquoted tilde character 1055.Pq Li ~ 1056is 1057subjected to tilde expansion. 1058All the characters up to a slash 1059.Pq Li / 1060or the end of the word are treated as a username 1061and are replaced with the user's home directory. 1062If the 1063username is missing (as in ~/foobar), the tilde is replaced 1064with the value of the HOME variable (the current user's 1065home directory). 1066.Ss Parameter Expansion 1067The format for parameter expansion is as follows: 1068.Bd -literal -offset indent 1069${expression} 1070.Ed 1071.Pp 1072where expression consists of all characters until the matching 1073.Dq Li } . 1074Any 1075.Dq Li } 1076escaped by a backslash or within a quoted string, and characters in 1077embedded arithmetic expansions, command substitutions, and variable 1078expansions, are not examined in determining the matching 1079.Dq Li } . 1080.Pp 1081The simplest form for parameter expansion is: 1082.Bd -literal -offset indent 1083${parameter} 1084.Ed 1085.Pp 1086The value, if any, of parameter is substituted. 1087.Pp 1088The parameter name or symbol can be enclosed in braces, which are 1089optional except for positional parameters with more than one digit or 1090when parameter is followed by a character that could be interpreted as 1091part of the name. 1092If a parameter expansion occurs inside double-quotes: 1093.Bl -enum 1094.It 1095Pathname expansion is not performed on the results of the 1096expansion. 1097.It 1098Field splitting is not performed on the results of the 1099expansion, with the exception of the special parameter 1100.Li @ . 1101.El 1102.Pp 1103In addition, a parameter expansion can be modified by using one of the 1104following formats. 1105.Bl -tag -width indent 1106.It Li ${parameter:-word} 1107Use Default Values. 1108If parameter is unset or 1109null, the expansion of word is 1110substituted; otherwise, the value of 1111parameter is substituted. 1112.It Li ${parameter:=word} 1113Assign Default Values. 1114If parameter is unset 1115or null, the expansion of word is 1116assigned to parameter. 1117In all cases, the 1118final value of parameter is 1119substituted. 1120Only variables, not positional 1121parameters or special parameters, can be 1122assigned in this way. 1123.It Li ${parameter:?[word]} 1124Indicate Error if Null or Unset. 1125If 1126parameter is unset or null, the expansion of 1127word (or a message indicating it is unset if 1128word is omitted) is written to standard 1129error and the shell exits with a nonzero 1130exit status. 1131Otherwise, the value of 1132parameter is substituted. 1133An 1134interactive shell need not exit. 1135.It Li ${parameter:+word} 1136Use Alternate Value. 1137If parameter is unset 1138or null, null is substituted; 1139otherwise, the expansion of word is 1140substituted. 1141.El 1142.Pp 1143In the parameter expansions shown previously, use of the colon in the 1144format results in a test for a parameter that is unset or null; omission 1145of the colon results in a test for a parameter that is only unset. 1146.Bl -tag -width indent 1147.It Li ${#parameter} 1148String Length. 1149The length in characters of 1150the value of parameter. 1151.El 1152.Pp 1153The following four varieties of parameter expansion provide for substring 1154processing. 1155In each case, pattern matching notation 1156(see 1157.Sx Shell Patterns ) , 1158rather than regular expression notation, 1159is used to evaluate the patterns. 1160If parameter is one of the special parameters 1161.Li * 1162or 1163.Li @ , 1164the result of the expansion is unspecified. 1165Enclosing the full parameter expansion string in double-quotes does not 1166cause the following four varieties of pattern characters to be quoted, 1167whereas quoting characters within the braces has this effect. 1168.Bl -tag -width indent 1169.It Li ${parameter%word} 1170Remove Smallest Suffix Pattern. 1171The word 1172is expanded to produce a pattern. 1173The 1174parameter expansion then results in 1175parameter, with the smallest portion of the 1176suffix matched by the pattern deleted. 1177.It Li ${parameter%%word} 1178Remove Largest Suffix Pattern. 1179The word 1180is expanded to produce a pattern. 1181The 1182parameter expansion then results in 1183parameter, with the largest portion of the 1184suffix matched by the pattern deleted. 1185.It Li ${parameter#word} 1186Remove Smallest Prefix Pattern. 1187The word 1188is expanded to produce a pattern. 1189The 1190parameter expansion then results in 1191parameter, with the smallest portion of the 1192prefix matched by the pattern deleted. 1193.It Li ${parameter##word} 1194Remove Largest Prefix Pattern. 1195The word 1196is expanded to produce a pattern. 1197The 1198parameter expansion then results in 1199parameter, with the largest portion of the 1200prefix matched by the pattern deleted. 1201.El 1202.Ss Command Substitution 1203Command substitution allows the output of a command to be substituted in 1204place of the command name itself. 1205Command substitution occurs when 1206the command is enclosed as follows: 1207.Bd -literal -offset indent 1208$(command) 1209.Ed 1210.Pp 1211or the backquoted version: 1212.Bd -literal -offset indent 1213`command` 1214.Ed 1215.Pp 1216The shell expands the command substitution by executing command in a 1217subshell environment and replacing the command substitution 1218with the standard output of the command, 1219removing sequences of one or more newlines at the end of the substitution. 1220Embedded newlines before the end of the output are not removed; 1221however, during field splitting, they may be translated into spaces 1222depending on the value of 1223.Ev IFS 1224and the quoting that is in effect. 1225.Ss Arithmetic Expansion 1226Arithmetic expansion provides a mechanism for evaluating an arithmetic 1227expression and substituting its value. 1228The format for arithmetic expansion is as follows: 1229.Bd -literal -offset indent 1230$((expression)) 1231.Ed 1232.Pp 1233The expression is treated as if it were in double-quotes, except 1234that a double-quote inside the expression is not treated specially. 1235The 1236shell expands all tokens in the expression for parameter expansion, 1237command substitution, and quote removal. 1238.Pp 1239Next, the shell treats this as an arithmetic expression and 1240substitutes the value of the expression. 1241.Ss White Space Splitting (Field Splitting) 1242After parameter expansion, command substitution, and 1243arithmetic expansion the shell scans the results of 1244expansions and substitutions that did not occur in double-quotes for 1245field splitting and multiple fields can result. 1246.Pp 1247The shell treats each character of the 1248.Ev IFS 1249as a delimiter and uses 1250the delimiters to split the results of parameter expansion and command 1251substitution into fields. 1252.Ss Pathname Expansion (File Name Generation) 1253Unless the 1254.Fl f 1255option is set, 1256file name generation is performed 1257after word splitting is complete. 1258Each word is 1259viewed as a series of patterns, separated by slashes. 1260The 1261process of expansion replaces the word with the names of 1262all existing files whose names can be formed by replacing 1263each pattern with a string that matches the specified pattern. 1264There are two restrictions on this: first, a pattern cannot match 1265a string containing a slash, and second, 1266a pattern cannot match a string starting with a period 1267unless the first character of the pattern is a period. 1268The next section describes the patterns used for both 1269Pathname Expansion and the 1270.Ic case 1271command. 1272.Ss Shell Patterns 1273A pattern consists of normal characters, which match themselves, 1274and meta-characters. 1275The meta-characters are 1276.Dq Li \&! , 1277.Dq Li * , 1278.Dq Li \&? , 1279and 1280.Dq Li [ . 1281These characters lose their special meanings if they are quoted. 1282When command or variable substitution is performed and the dollar sign 1283or back quotes are not double-quoted, the value of the 1284variable or the output of the command is scanned for these 1285characters and they are turned into meta-characters. 1286.Pp 1287An asterisk 1288.Pq Li * 1289matches any string of characters. 1290A question mark 1291.Pq Li \&? 1292matches any single character. 1293A left bracket 1294.Pq Li [ 1295introduces a character class. 1296The end of the character class is indicated by a 1297.Dq Li \&] ; 1298if the 1299.Dq Li \&] 1300is missing then the 1301.Dq Li [ 1302matches a 1303.Dq Li [ 1304rather than introducing a character class. 1305A character class matches any of the characters between the square brackets. 1306A range of characters may be specified using a minus sign. 1307The character class may be complemented by making an exclamation point 1308.Pq Li !\& 1309the first character of the character class. 1310.Pp 1311To include a 1312.Dq Li \&] 1313in a character class, make it the first character listed 1314(after the 1315.Dq Li \&! , 1316if any). 1317To include a 1318.Dq Li - , 1319make it the first or last character listed. 1320.Ss Built-in Commands 1321This section lists the commands which 1322are built-in because they need to perform some operation 1323that cannot be performed by a separate process. 1324In addition to 1325these, a built-in version of the 1326.Xr test 1 1327command is provided for efficiency. 1328.Bl -tag -width indent 1329.It Ic \&: 1330A null command that returns a 0 (true) exit value. 1331.It Ic \&. Ar file 1332The commands in the specified file are read and executed by the shell. 1333The 1334.Ic return 1335command may be used to return to the 1336.Ic \&. 1337command's caller. 1338If 1339.Ar file 1340contains any 1341.Dq / 1342characters, it is used as is. 1343Otherwise, the shell searches the 1344.Ev PATH 1345for the file. 1346If it is not found in the 1347.Ev PATH , 1348it is sought in the current working directory. 1349.It Ic alias Op Ar name ... 1350.It Ic alias Op Ar name Ns = Ns Ar string ... 1351If 1352.Ar name Ns = Ns Ar string 1353is specified, the shell defines the alias 1354.Ar name 1355with value 1356.Ar string . 1357If just 1358.Ar name 1359is specified, the value of the alias 1360.Ar name 1361is printed. 1362With no arguments, the 1363.Ic alias 1364built-in command prints the names and values of all defined aliases 1365(see 1366.Ic unalias ) . 1367Alias values are written with appropriate quoting so that they are 1368suitable for re-input to the shell. 1369.It Ic bg Op Ar job ... 1370Continue the specified jobs 1371(or the current job if no jobs are given) 1372in the background. 1373.It Ic builtin Ar cmd Op Ar arg ... 1374Execute the specified built-in command, 1375.Ar cmd . 1376This is useful when the user wishes to override a shell function 1377with the same name as a built-in command. 1378.It Ic bind Oo Fl aeklrsv Oc Oo Ar key Oo Ar command Oc Oc 1379List or alter key bindings for the line editor. 1380This command is documented in 1381.Xr editrc 5 . 1382.It Ic cd Oo Fl L | P Oc Op Ar directory 1383Switch to the specified 1384.Ar directory , 1385or to the directory specified in the 1386.Ev HOME 1387environment variable if no 1388.Ar directory 1389is specified. 1390If 1391.Ar directory 1392does not begin with 1393.Pa / , \&. , 1394or 1395.Pa .. , 1396then the directories listed in the 1397.Ev CDPATH 1398variable will be 1399searched for the specified 1400.Ar directory . 1401If 1402.Ev CDPATH 1403is unset, the current directory is searched. 1404The format of 1405.Ar CDPATH 1406is the same as that of 1407.Ev PATH . 1408In an interactive shell, 1409the 1410.Ic cd 1411command will print out the name of the directory 1412that it actually switched to 1413if this is different from the name that the user gave. 1414These may be different either because the 1415.Ev CDPATH 1416mechanism was used or because a symbolic link was crossed. 1417.Pp 1418If the 1419.Fl P 1420option is specified, 1421.Pa .. 1422is handled physically and symbolic links are resolved before 1423.Pa .. 1424components are processed. 1425If the 1426.Fl L 1427option is specified, 1428.Pa .. 1429is handled logically. 1430This is the default. 1431.It Ic chdir 1432A synonym for the 1433.Ic cd 1434built-in command. 1435.It Ic command Oo Fl p Oc Op Ar utility Op Ar argument ... 1436Execute the specified 1437.Ar utility 1438as a simple command (see the 1439.Sx Simple Commands 1440section). 1441.Pp 1442If the 1443.Fl p 1444option is specified, the command search is performed using a 1445default value of 1446.Ev PATH 1447that is guaranteed to find all of the standard utilities. 1448.It Ic echo Oo Fl e | n Oc Op Ar string 1449Print 1450.Ar string 1451to the standard output with a newline appended. 1452.Bl -tag -width indent 1453.It Fl n 1454Suppress the output of the trailing newline. 1455.It Fl e 1456Process C-style backslash escape sequences. 1457.Ic echo 1458understands the following character escapes: 1459.Bl -tag -width indent 1460.It \ea 1461Alert (ring the terminal bell) 1462.It \eb 1463Backspace 1464.It \ec 1465Suppress the trailing newline (this has the side-effect of truncating the 1466line if it is not the last character) 1467.It \ee 1468The ESC character (ASCII 0x1b) 1469.It \ef 1470Formfeed 1471.It \en 1472Newline 1473.It \er 1474Carriage return 1475.It \et 1476Horizontal tab 1477.It \ev 1478Vertical tab 1479.It \e\e 1480Literal backslash 1481.It \e0nnn 1482(Zero) The character whose octal value is nnn 1483.El 1484.Pp 1485If 1486.Ar string 1487is not enclosed in quotes then the backslash itself must be escaped 1488with a backslash to protect it from the shell. 1489For example 1490.Bd -literal -offset indent 1491$ echo -e "a\evb" 1492a 1493 b 1494$ echo -e a\e\evb 1495a 1496 b 1497$ echo -e "a\e\eb" 1498a\eb 1499$ echo -e a\e\e\e\eb 1500a\eb 1501.Ed 1502.El 1503.Pp 1504Only one of the 1505.Fl e 1506and 1507.Fl n 1508options may be specified. 1509.It Ic eval Ar string ... 1510Concatenate all the arguments with spaces. 1511Then re-parse and execute the command. 1512.It Ic exec Op Ar command Op arg ... 1513Unless 1514.Ar command 1515is omitted, 1516the shell process is replaced with the specified program 1517(which must be a real program, not a shell built-in command or function). 1518Any redirections on the 1519.Ic exec 1520command are marked as permanent, 1521so that they are not undone when the 1522.Ic exec 1523command finishes. 1524.It Ic exit Op Ar exitstatus 1525Terminate the shell process. 1526If 1527.Ar exitstatus 1528is given 1529it is used as the exit status of the shell; 1530otherwise the exit status of the preceding command is used. 1531.It Ic export Oo Fl p Oc Op Ar name ... 1532The specified names are exported so that they will 1533appear in the environment of subsequent commands. 1534The only way to un-export a variable is to 1535.Ic unset 1536it. 1537The shell allows the value of a variable to be set 1538at the same time as it is exported by writing 1539.Bd -literal -offset indent 1540export name=value 1541.Ed 1542.Pp 1543With no arguments the export command lists the names 1544of all exported variables. 1545If the 1546.Fl p 1547option is specified, the exported variables are printed as 1548.Dq Ic export Ar name Ns = Ns Ar value 1549lines, suitable for re-input to the shell. 1550.It Ic fc Oo Fl e Ar editor Oc Op Ar first Op Ar last 1551.It Ic fc Fl l Oo Fl nr Oc Op Ar first Op Ar last 1552.It Ic fc Fl s Oo Ar old Ns = Ns Ar new Oc Op Ar first 1553The 1554.Ic fc 1555built-in command lists, or edits and re-executes, 1556commands previously entered to an interactive shell. 1557.Bl -tag -width indent 1558.It Fl e Ar editor 1559Use the editor named by 1560.Ar editor 1561to edit the commands. 1562The editor string is a command name, 1563subject to search via the 1564.Ev PATH 1565variable. 1566The value in the 1567.Ev FCEDIT 1568variable is used as a default when 1569.Fl e 1570is not specified. 1571If 1572.Ev FCEDIT 1573is null or unset, the value of the 1574.Ev EDITOR 1575variable is used. 1576If 1577.Ev EDITOR 1578is null or unset, 1579.Xr ed 1 1580is used as the editor. 1581.It Fl l No (ell) 1582List the commands rather than invoking 1583an editor on them. 1584The commands are written in the 1585sequence indicated by the first and last operands, as 1586affected by 1587.Fl r , 1588with each command preceded by the command number. 1589.It Fl n 1590Suppress command numbers when listing with 1591.Fl l . 1592.It Fl r 1593Reverse the order of the commands listed 1594(with 1595.Fl l ) 1596or edited 1597(with neither 1598.Fl l 1599nor 1600.Fl s ) . 1601.It Fl s 1602Re-execute the command without invoking an editor. 1603.It Ar first 1604.It Ar last 1605Select the commands to list or edit. 1606The number of previous commands that can be accessed 1607are determined by the value of the 1608.Ev HISTSIZE 1609variable. 1610The value of 1611.Ar first 1612or 1613.Ar last 1614or both are one of the following: 1615.Bl -tag -width indent 1616.It Ar [+]num 1617A positive number representing a command number; 1618command numbers can be displayed with the 1619.Fl l 1620option. 1621.It Ar -num 1622A negative decimal number representing the 1623command that was executed 1624.Ar num 1625of 1626commands previously. 1627For example, -1 is the immediately previous command. 1628.It Ar string 1629A string indicating the most recently entered command 1630that begins with that string. 1631If the 1632.Ar old=new 1633operand is not also specified with 1634.Fl s , 1635the string form of the first operand cannot contain an embedded equal sign. 1636.El 1637.El 1638.Pp 1639The following environment variables affect the execution of 1640.Ic fc : 1641.Bl -tag -width indent 1642.It Ev FCEDIT 1643Name of the editor to use. 1644.It Ev HISTSIZE 1645The number of previous commands that are accessible. 1646.El 1647.It Ic fg Op Ar job 1648Move the specified 1649.Ar job 1650or the current job to the foreground. 1651.It Ic getopts Ar optstring Ar var 1652The POSIX 1653.Ic getopts 1654command. 1655The 1656.Ic getopts 1657command deprecates the older 1658.Xr getopt 1 1659command. 1660The first argument should be a series of letters, each possibly 1661followed by a colon which indicates that the option takes an argument. 1662The specified variable is set to the parsed option. 1663The index of 1664the next argument is placed into the shell variable 1665.Ev OPTIND . 1666If an option takes an argument, it is placed into the shell variable 1667.Ev OPTARG . 1668If an invalid option is encountered, 1669.Ev var 1670is set to 1671.Dq Li \&? . 1672It returns a false value (1) when it encounters the end of the options. 1673.It Ic hash Oo Fl rv Oc Op Ar command ... 1674The shell maintains a hash table which remembers the locations of commands. 1675With no arguments whatsoever, the 1676.Ic hash 1677command prints out the contents of this table. 1678Entries which have not been looked at since the last 1679.Ic cd 1680command are marked with an asterisk; 1681it is possible for these entries to be invalid. 1682.Pp 1683With arguments, the 1684.Ic hash 1685command removes each specified 1686.Ar command 1687from the hash table (unless they are functions) and then locates it. 1688With the 1689.Fl v 1690option, 1691.Ic hash 1692prints the locations of the commands as it finds them. 1693The 1694.Fl r 1695option causes the 1696.Ic hash 1697command to delete all the entries in the hash table except for functions. 1698.It Ic jobid Op Ar job 1699Print the process id's of the processes in the specified 1700.Ar job . 1701If the 1702.Ar job 1703argument is omitted, use the current job. 1704.It Ic jobs Oo Fl ls Oc Op Ar job ... 1705Print information about the specified jobs, or all jobs if no 1706.Ar job 1707argument is given. 1708The information printed includes job ID, status and command name. 1709.Pp 1710If the 1711.Fl l 1712option is specified, the PID of each job is also printed. 1713If the 1714.Fl s 1715option is specified, only the PIDs of the jobs are printed, one per line. 1716.It Ic pwd Op Fl L | P 1717Print the path of the current directory. 1718The built-in command may 1719differ from the program of the same name because the 1720built-in command remembers what the current directory 1721is rather than recomputing it each time. 1722This makes 1723it faster. 1724However, if the current directory is 1725renamed, 1726the built-in version of 1727.Xr pwd 1 1728will continue to print the old name for the directory. 1729.Pp 1730If the 1731.Fl P 1732option is specified, symbolic links are resolved. 1733If the 1734.Fl L 1735option is specified, the shell's notion of the current directory 1736is printed (symbolic links are not resolved). 1737This is the default. 1738.It Ic read Oo Fl p Ar prompt Oc Oo Fl t Ar timeout Oc Oo Fl er Oc Ar variable ... 1739The 1740.Ar prompt 1741is printed if the 1742.Fl p 1743option is specified 1744and the standard input is a terminal. 1745Then a line is 1746read from the standard input. 1747The trailing newline 1748is deleted from the line and the line is split as 1749described in the section on 1750.Sx White Space Splitting (Field Splitting) 1751above, and 1752the pieces are assigned to the variables in order. 1753If there are more pieces than variables, the remaining 1754pieces (along with the characters in 1755.Ev IFS 1756that separated them) 1757are assigned to the last variable. 1758If there are more variables than pieces, the remaining 1759variables are assigned the null string. 1760.Pp 1761Backslashes are treated specially, unless the 1762.Fl r 1763option is 1764specified. 1765If a backslash is followed by 1766a newline, the backslash and the newline will be 1767deleted. 1768If a backslash is followed by any other 1769character, the backslash will be deleted and the following 1770character will be treated as though it were not in 1771.Ev IFS , 1772even if it is. 1773.Pp 1774If the 1775.Fl t 1776option is specified and the 1777.Ar timeout 1778elapses before any input is supplied, 1779the 1780.Ic read 1781command will return without assigning any values. 1782The 1783.Ar timeout 1784value may optionally be followed by one of 1785.Dq s , 1786.Dq m 1787or 1788.Dq h 1789to explicitly specify seconds, minutes or hours. 1790If none is supplied, 1791.Dq s 1792is assumed. 1793.Pp 1794The 1795.Fl e 1796option exists only for backward compatibility with older scripts. 1797.It Ic readonly Oo Fl p Oc Op Ar name ... 1798Each specified 1799.Ar name 1800is marked as read only, 1801so that it cannot be subsequently modified or unset. 1802The shell allows the value of a variable to be set 1803at the same time as it is marked read only 1804by using the following form: 1805.Bd -literal -offset indent 1806readonly name=value 1807.Ed 1808.Pp 1809With no arguments the 1810.Ic readonly 1811command lists the names of all read only variables. 1812If the 1813.Fl p 1814option is specified, the read-only variables are printed as 1815.Dq Ic readonly Ar name Ns = Ns Ar value 1816lines, suitable for re-input to the shell. 1817.It Ic set Oo Fl /+abCEefIimnpTuVvx Oc Oo Fl /+o Ar longname Oc Oo 1818.Fl c Ar string Oc Op Fl - Ar arg ... 1819The 1820.Ic set 1821command performs three different functions: 1822.Bl -item 1823.It 1824With no arguments, it lists the values of all shell variables. 1825.It 1826If options are given, 1827either in short form or using the long 1828.Dq Fl /+o Ar longname 1829form, 1830it sets or clears the specified options as described in the section called 1831.Sx Argument List Processing . 1832.It 1833If the 1834.Dq Fl - 1835option is specified, 1836.Ic set 1837will replace the shell's positional parameters with the subsequent 1838arguments. 1839If no arguments follow the 1840.Dq Fl - 1841option, 1842all the positional parameters will be cleared, 1843which is equivalent to executing the command 1844.Dq Li shift $# . 1845The 1846.Dq Fl - 1847flag may be omitted when specifying arguments to be used 1848as positional replacement parameters. 1849This is not recommended, 1850because the first argument may begin with a dash 1851.Pq Li - 1852or a plus 1853.Pq Li + , 1854which the 1855.Ic set 1856command will interpret as a request to enable or disable options. 1857.El 1858.It Ic setvar Ar variable Ar value 1859Assigns the specified 1860.Ar value 1861to the specified 1862.Ar variable . 1863.Ic Setvar 1864is intended to be used in functions that 1865assign values to variables whose names are passed as parameters. 1866In general it is better to write 1867.Bd -literal -offset indent 1868variable=value 1869.Ed 1870rather than using 1871.Ic setvar . 1872.It Ic shift Op Ar n 1873Shift the positional parameters 1874.Ar n 1875times, or once if 1876.Ar n 1877is not specified. 1878A shift sets the value of $1 to the value of $2, 1879the value of $2 to the value of $3, and so on, 1880decreasing the value of $# by one. 1881If there are zero positional parameters, shifting does not do anything. 1882.It Ic trap Oo Ar action Oc Ar signal ... 1883Cause the shell to parse and execute 1884.Ar action 1885when any specified 1886.Ar signal 1887is received. 1888The signals are specified by name or number. 1889In addition, the pseudo-signal 1890.Cm EXIT 1891may be used to specify an action that is performed when the shell terminates. 1892The 1893.Ar action 1894may be null or omitted; 1895the former causes the specified signal to be ignored 1896and the latter causes the default action to be taken. 1897When the shell forks off a subshell, 1898it resets trapped (but not ignored) signals to the default action. 1899The 1900.Ic trap 1901command has no effect on signals that were ignored on entry to the shell. 1902.It Ic type Op Ar name ... 1903Interpret each 1904.Ar name 1905as a command and print the resolution of the command search. 1906Possible resolutions are: 1907shell keyword, alias, shell built-in command, command, tracked alias 1908and not found. 1909For aliases the alias expansion is printed; 1910for commands and tracked aliases 1911the complete pathname of the command is printed. 1912.It Ic ulimit Oo Fl HSabcdflmnstuv Oc Op Ar limit 1913Set or display resource limits (see 1914.Xr getrlimit 2 ) . 1915If 1916.Ar limit 1917is specified, the named resource will be set; 1918otherwise the current resource value will be displayed. 1919.Pp 1920If 1921.Fl H 1922is specified, the hard limits will be set or displayed. 1923While everybody is allowed to reduce a hard limit, 1924only the superuser can increase it. 1925The 1926.Fl S 1927option 1928specifies the soft limits instead. 1929When displaying limits, 1930only one of 1931.Fl S 1932or 1933.Fl H 1934can be given. 1935The default is to display the soft limits, 1936and to set both the hard and the soft limits. 1937.Pp 1938Option 1939.Fl a 1940causes the 1941.Ic ulimit 1942command to display all resources. 1943The parameter 1944.Ar limit 1945is not acceptable in this mode. 1946.Pp 1947The remaining options specify which resource value is to be 1948displayed or modified. 1949They are mutually exclusive. 1950.Bl -tag -width indent 1951.It Fl b Ar sbsize 1952The maximum size of socket buffer usage, in bytes. 1953.It Fl c Ar coredumpsize 1954The maximal size of core dump files, in 512-byte blocks. 1955.It Fl d Ar datasize 1956The maximal size of the data segment of a process, in kilobytes. 1957.It Fl f Ar filesize 1958The maximal size of a file, in 512-byte blocks. 1959.It Fl l Ar lockedmem 1960The maximal size of memory that can be locked by a process, in 1961kilobytes. 1962.It Fl m Ar memoryuse 1963The maximal resident set size of a process, in kilobytes. 1964.It Fl n Ar nofiles 1965The maximal number of descriptors that could be opened by a process. 1966.It Fl s Ar stacksize 1967The maximal size of the stack segment, in kilobytes. 1968.It Fl t Ar time 1969The maximal amount of CPU time to be used by each process, in seconds. 1970.It Fl u Ar userproc 1971The maximal number of simultaneous processes for this user ID. 1972.It Fl v Ar virtualmem 1973The maximal virtual size of a process, in kilobytes. 1974.El 1975.It Ic umask Op Ar mask 1976Set the file creation mask (see 1977.Xr umask 2 ) 1978to the octal value specified by 1979.Ar mask . 1980If the argument is omitted, the current mask value is printed. 1981.It Ic unalias Oo Fl a Oc Op Ar name 1982If 1983.Ar name 1984is specified, the shell removes that alias. 1985If 1986.Fl a 1987is specified, all aliases are removed. 1988.It Ic unset Oo Fl fv Oc Ar name ... 1989The specified variables or functions are unset and unexported. 1990If the 1991.Fl v 1992option is specified or no options are given, the 1993.Ar name 1994arguments are treated as variable names. 1995If the 1996.Fl f 1997option is specified, the 1998.Ar name 1999arguments are treated as function names. 2000.It Ic wait Op Ar job 2001Wait for the specified 2002.Ar job 2003to complete and return the exit status of the last process in the 2004.Ar job . 2005If the argument is omitted, wait for all jobs to complete 2006and return an exit status of zero. 2007.El 2008.Ss Commandline Editing 2009When 2010.Nm 2011is being used interactively from a terminal, the current command 2012and the command history 2013(see 2014.Ic fc 2015in 2016.Sx Built-in Commands ) 2017can be edited using vi-mode command line editing. 2018This mode uses commands similar 2019to a subset of those described in the vi man page. 2020The command 2021.Dq Li set -o vi 2022(or 2023.Dq Li set -V ) 2024enables vi-mode editing and places 2025.Nm 2026into vi insert mode. 2027With vi-mode enabled, 2028.Nm 2029can be switched between insert mode and command mode by typing 2030.Aq ESC . 2031Hitting 2032.Aq return 2033while in command mode will pass the line to the shell. 2034.Pp 2035Similarly, the 2036.Dq Li set -o emacs 2037(or 2038.Dq Li set -E ) 2039command can be used to enable a subset of 2040emacs-style command line editing features. 2041.Sh SEE ALSO 2042.Xr builtin 1 , 2043.Xr echo 1 , 2044.Xr expr 1 , 2045.Xr pwd 1 , 2046.Xr test 1 2047.Sh HISTORY 2048A 2049.Nm 2050command appeared in 2051.At v1 . 2052.Sh BUGS 2053The 2054.Nm 2055utility does not recognize multibyte characters. 2056