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