1.\" $NetBSD: sh.1,v 1.229 2020/09/18 07:21:26 wiz 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 September 18, 2020 35.Dt SH 1 36.\" everything except c o and s (keep them ordered) 37.ds flags abCEeFfhIiLmnpquVvXx 38.Os 39.Sh NAME 40.Nm sh 41.Nd command interpreter (shell) 42.Sh SYNOPSIS 43.Nm 44.Bk -words 45.Op Fl \*[flags] 46.Op Cm +\*[flags] 47.Ek 48.Bk -words 49.Op Fl o Ar option_name 50.Op Cm +o Ar option_name 51.Ek 52.Bk -words 53.Op Ar command_file Op Ar argument ... 54.Ek 55.Nm 56.Fl c 57.Bk -words 58.Op Fl s 59.Op Fl \*[flags] 60.Op Cm +\*[flags] 61.Ek 62.Bk -words 63.Op Fl o Ar option_name 64.Op Cm +o Ar option_name 65.Ek 66.Bk -words 67.Ar command_string 68.Op Ar command_name Op Ar argument ... 69.Ek 70.Nm 71.Fl s 72.Bk -words 73.Op Fl \*[flags] 74.Op Cm +\*[flags] 75.Ek 76.Bk -words 77.Op Fl o Ar option_name 78.Op Cm +o Ar option_name 79.Ek 80.Bk -words 81.Op Ar argument ... 82.Ek 83.Sh DESCRIPTION 84.Nm 85is the standard command interpreter for the system. 86The current version of 87.Nm 88is in the process of being changed to conform more closely to the 89POSIX 1003.2 and 1003.2a specifications for the shell. 90This version has many 91features which make it appear similar in some respects to the Korn shell, 92but it is not a Korn shell clone (see 93.Xr ksh 1 ) . 94This man page is not intended 95to be a tutorial or a complete specification of the shell. 96.Ss Overview 97The shell is a command that reads lines from either a file or the 98terminal, interprets them, and generally executes other commands. 99A shell is the program that is running when a user logs into the system. 100(Users can select which shell is executed for them at login with the 101.Xr chsh 1 102command). 103The shell implements a language that has flow control 104constructs, a macro facility that provides a variety of features in 105addition to data storage, along with built in history and line editing 106capabilities. 107It incorporates many features to aid interactive use and 108has the advantage that the interpretative language is common to both 109interactive and non-interactive use (shell scripts). 110That is, commands 111can be typed directly to the running shell or can be put into a file and 112the file can be executed directly by the shell. 113.Ss Invocation 114If no arguments are present and if the standard input, 115and standard error output, of the shell 116are connected to a terminal (or terminals, or if the 117.Fl i 118flag is set), 119and the 120.Fl c 121option is not present, the shell is considered an interactive shell. 122An interactive shell generally prompts before each command and handles 123programming and command errors differently (as described below). 124When first starting, 125the shell inspects argument 0, and if it begins with a dash 126.Sq - , 127the shell is also considered 128a login shell. 129This is normally done automatically by the system 130when the user first logs in. 131A login shell first reads commands 132(as if by using the 133.Dq \&. 134command) 135from the files 136.Pa /etc/profile 137and 138.Pa .profile 139in the user's home directory 140.Pq \&$HOME , 141if they exist. 142If the environment variable 143.Ev ENV 144is set on entry to a shell, 145or is set in the 146.Pa .profile 147of a login shell, 148and either the shell is interactive, or the 149.Cm posix 150option is not set, 151the shell then performs parameter and arithmetic 152expansion on the value of 153.Ev ENV , 154(these are described later) 155and if no errors occurred, 156then reads commands from the file name that results. 157Note that no error messages result from these 158expansions, to verify that 159.Ev ENV 160is correct, as desired, use: 161.Dl eval printf '%s\e\en' Dq \&${ENV} 162Otherwise if 163.Ev ENV 164appears to contain a command substitution, 165which is not performed here, 166or if there were no expansions to expand, the value of 167.Ev ENV 168is used as the file name. 169.Pp 170Therefore, a user should place commands that are to be executed only at 171login time in the 172.Pa .profile 173file, and commands that are executed for every shell inside the 174.Ev ENV 175file. 176To set the 177.Ev ENV 178variable to some file, place the following line in your 179.Pa .profile 180of your home directory 181.Pp 182.Dl ENV=$HOME/.shinit; export ENV 183.Pp 184substituting for 185.Dq .shinit 186any filename you wish. 187Since the 188.Ev ENV 189file can be read for every invocation of the shell, including shell scripts 190and non-interactive shells, the following paradigm is useful for 191restricting commands in the 192.Ev ENV 193file to interactive invocations. 194Place commands within the 195.Dq Ic case 196and 197.Dq Ic esac 198below (these commands are described later): 199.Bd -literal -offset indent 200case $- in *i*) 201 # commands for interactive use only 202 ... 203esac 204.Ed 205.Pp 206If command line arguments besides the options have been specified, and 207neither 208.Fl c 209nor 210.Fl s 211was given, then the shell treats the first argument 212as the name of a file from which to read commands (a shell script). 213This also becomes 214.Li $0 215and the remaining arguments are set as the 216positional parameters of the shell 217.Li ( $1 , $2 , 218etc). 219Otherwise, if 220.Fl c 221was given, then the first argument, which must exist, 222is taken to be a string of 223.Nm 224commands to execute. 225Then if any additional arguments follow the command string, 226those arguments become 227.Li $0 , $1 , 228\&... 229Otherwise, if additional arguments were given 230(which implies that 231.Fl s 232was set) 233those arguments become 234.Li $1 , $2 , 235\&... 236If 237.Li $0 238has not been set by the preceding processing, it 239will be set to 240.Va argv\^ Ns [ 0 ] 241as passed to the shell, which will 242usually be the name of the shell itself. 243If 244.Fl s 245was given, or if neither 246.Fl c 247nor any additional (non-option) arguments were present, 248the shell reads commands from its standard input. 249.\" 250.\" 251.Ss Argument List Processing 252.\" 253Currently, all of the single letter options that can meaningfully 254be set using the 255.Ic set 256built-in, have a corresponding name 257that can be used as an argument to the 258.Fl o 259option. 260The 261.Ic set Fl o 262name is provided next to the single letter option in 263the description below. 264Some options have only a long name, they are described after 265the flag options, they are used with 266.Fl o 267or 268.Cm +o 269only, either on the command line, or with the 270.Ic set 271built-in command. 272Other options described are for the command line only. 273Specifying a dash 274.Dq Cm \- 275turns the option on, while using a plus 276.Dq Cm + 277disables the option. 278The following options can be set from the command line and, 279unless otherwise stated, with the 280.Ic set 281built-in (described later). 282.\" 283.\" strlen("quietprofile") == strlen("local_lineno"): pick the latter 284.\" to give the indent as the _ in local_lineno, and the fi ligature in 285.\" quietprofile combine to make "local_lineno' slightly wider when printed 286.\" (in italics) in a variable width font. 287.Bl -tag -width ".Fl L Em local_lineno" -offset indent 288.\" 289.It Fl a Em allexport 290Automatically export any variable to which a value is assigned 291while this flag is set, unless the variable has been marked as 292not for export. 293.It Fl b Em notify 294Enable asynchronous notification of background job completion. 295(Not implemented.) 296.It Fl C Em noclobber 297Don't overwrite existing files with 298.Dq > . 299.It Fl c 300Read commands from the 301.Ar command_string 302operand instead of, or in addition to, from the standard input. 303Special parameter 304.Dv 0 \" $0 (comments like this for searching sources only) 305will be set from the 306.Ar command_name 307operand if given, and the positional parameters 308.Dv ( 1 , 2 , 309etc.) 310set from the remaining argument operands, if any. 311.Fl c 312is only available at invocation, it cannot be 313.Ic set , 314and there is no form using 315.Dq Cm \&+ . 316.It Fl E Em emacs 317Enable the built-in emacs style 318command line editor (disables 319.Fl V 320if it has been set). 321(See the 322.Sx Command Line Editing 323section below.) 324.It Fl e Em errexit 325If not interactive, exit immediately if any untested command fails. 326If interactive, and an untested command fails, 327cease all processing of the current command and return to 328prompt for a new command. 329The exit status of a command is considered to be 330explicitly tested if the command is used to control an 331.Ic if , 332.Ic elif , 333.Ic while , 334or 335.Ic until , 336or if the command is the left hand operand of an 337.Dq && 338or 339.Dq || 340operator, 341or if it is a pipeline (or simple command) preceded by the 342.Dq \&! 343operator. 344With pipelines, only the status of the entire pipeline 345(indicated by the last command it contains) 346is tested when 347.Fl e 348is set to determine if the shell should exit. 349.It Fl F Em fork 350Cause the shell to always use 351.Xr fork 2 352instead of attempting 353.Xr vfork 2 354when it needs to create a new process. 355This should normally have no visible effect, 356but can slow execution. 357The 358.Nm 359can be compiled to always use 360.Xr fork 2 361in which case altering the 362.Fl F 363flag has no effect. 364.It Fl f Em noglob 365Disable pathname expansion. 366.It Fl h Em trackall 367Functions defined while this option is set will have paths bound to 368commands to be executed by the function at the time of the definition. 369When off when a function is defined, 370the file system is searched for commands each time the function is invoked. 371(Not implemented.) 372.It Fl I Em ignoreeof 373Ignore EOFs from input when interactive. 374(After a large number of consecutive EOFs the shell will exit anyway.) 375.It Fl i Em interactive 376Force the shell to behave interactively. 377.It Fl L Em local_lineno 378When set, before a function is defined, 379causes the variable 380.Dv LINENO 381when used within the function, 382to refer to the line number defined such that 383first line of the function is line 1. 384When reset, 385.Dv LINENO 386in a function refers to the line number within the file 387within which the definition of the function occurs. 388This option defaults to 389.Dq on 390in this shell. 391For more details see the section 392.Sx LINENO 393below. 394.It Fl m Em monitor 395Turn on job control (set automatically when interactive). 396.It Fl n Em noexec 397Read and parse commands, but do not execute them. 398This is useful for checking the syntax of shell scripts. 399If 400.Fl n 401becomes set in an interactive shell, it will automatically be 402cleared just before the next time the command line prompt 403.Pq Ev PS1 404is written. 405.It Fl p Em nopriv 406Do not attempt to reset effective UID if it does not match UID. 407This is not set by default to help avoid incorrect usage by setuid 408root programs via 409.Xr system 3 410or 411.Xr popen 3 . 412.It Fl q Em quietprofile 413If the 414.Fl v 415or 416.Fl x 417options have been set, do not apply them when reading 418initialization files, these being 419.Pa /etc/profile , 420.Pa .profile , 421and the file specified by the 422.Ev ENV 423environment variable. 424.It Fl s Em stdin 425Read commands from standard input (set automatically if 426neither 427.Fl c 428nor file arguments are present). 429If after processing a command_string with the 430.Fl c 431option, the shell has not exited, and the 432.Fl s 433option is set, it will continue reading more commands from standard input. 434This option has no effect when set or reset after the shell has 435already started reading from the command_file, or from standard input. 436Note that the 437.Fl s 438flag being set does not cause the shell to be interactive. 439.It Fl u Em nounset 440Write a message to standard error when attempting to obtain a 441value from a variable that is not set, 442and if the shell is not interactive, exit immediately. 443For interactive shells, instead return immediately to the command prompt 444and read the next command. 445Note that expansions (described later, see 446.Sx Word Expansions 447below) using the 448.Sq \&+ , 449.Sq \&\- , 450.Sq \&= , 451or 452.Sq \&? 453operators test if the variable is set, before attempting to 454obtain its value, and hence are unaffected by 455.Fl u . 456.It Fl V Em vi 457Enable the built-in 458.Xr vi 1 459command line editor (disables 460.Fl E 461if it has been set). 462(See the 463.Sx Command Line Editing 464section below.) 465.It Fl v Em verbose 466The shell writes its input to standard error as it is read. 467Useful for debugging. 468.It Fl X Em xlock 469Cause output from the 470.Ic xtrace 471.Pq Fl x 472option to be sent to standard error as it exists when the 473.Fl X 474option is enabled (regardless of its previous state.) 475For example: 476.Bd -literal -compact 477 set -X 2>/tmp/trace-file 478.Ed 479will arrange for tracing output to be sent to the file named, 480instead of wherever it was previously being sent, 481until the X option is set again, or cleared. 482.Pp 483Each change (set or clear) to 484.Fl X 485is also performed upon 486.Fl x , 487but not the converse. 488.It Fl x Em xtrace 489Write each command to standard error (preceded by the expanded value of 490.Li $PS4 ) 491before it is executed. 492Unless 493.Fl X 494is set, 495.Dq "standard error" 496means that which existed immediately before any redirections to 497be applied to the command are performed. 498Useful for debugging. 499.It "\ \ " Em cdprint 500Make an interactive shell always print the new directory name when 501changed by the 502.Ic cd 503command. 504In a non-interactive shell this option has no effect. 505.It "\ \ " Em nolog 506Prevent the entry of function definitions into the command history (see 507.Ic fc 508in the 509.Sx Built-ins 510section.) 511(Not implemented.) 512.It "\ \ " Em pipefail 513If set when a pipeline is created, 514the way the exit status of the pipeline is determined 515is altered. 516See 517.Sx Pipelines 518below for the details. 519.It "\ \ " Em posix 520Enables closer adherence to the POSIX shell standard. 521This option will default set at shell startup if the 522environment variable 523.Ev POSIXLY_CORRECT 524is present. 525That can be overridden (set or reset) by the 526.Fl o 527option on the command line. 528Currently this option controls whether (!posix) or not (posix) 529the file given by the 530.Ev ENV 531variable is read at startup by a non-interactive shell. 532It also controls whether file descriptors greater than 2 533opened using the 534.Ic exec 535built-in command are passed on to utilities executed 536.Dq ( yes 537in posix mode), 538whether a colon (:) terminates the user name in tilde (~) expansions 539other than in assignment statements 540.Dq ( no 541in posix mode), 542the format of the output of the 543.Ic kill Fl l 544command, where posix mode causes the names of the signals 545be separated by either a single space or newline, and where otherwise 546sufficient spaces are inserted to generate nice looking columns, 547and whether the shell treats 548an empty brace-list compound statement as a syntax error 549(expected by POSIX) or permits it. 550Such statements 551.Dq "{\ }" 552can be useful when defining dummy functions. 553Lastly, in posix mode, only one 554.Dq \&! 555is permitted before a pipeline. 556.It "\ \ " Em promptcmds 557Allows command substitutions (as well as parameter 558and arithmetic expansions, which are always performed) 559upon the prompt strings 560.Ev PS1 , 561.Ev PS2 , 562and 563.Ev PS4 564each time, before they are output. 565This option should not be set until after the prompts 566have been set (or verified) to avoid accidentally importing 567unwanted command substitutions from the environment. 568.It "\ \ " Em tabcomplete 569Enables filename completion in the command line editor. 570Typing a tab character will extend the current input word to match a 571filename. 572If more than one filename matches it is only extended to be the common prefix. 573Typing a second tab character will list all the matching names. 574One of the editing modes, either 575.Fl E 576or 577.Fl V , 578must be enabled for this to work. 579.El 580.Ss Lexical Structure 581The shell reads input in terms of lines from a file and breaks it up into 582words at whitespace (blanks and tabs), and at certain sequences of 583characters that are special to the shell called 584.Dq operators . 585There are two types of operators: control operators and redirection 586operators (their meaning is discussed later). 587The following is a list of operators: 588.Bl -ohang -offset indent 589.It "Control operators:" 590.Dl & && \&( \&) \&; ;; ;& \&| || <newline> 591.It "Redirection operators:" 592.Dl < > >| << >> <& >& <<- <> 593.El 594.Ss Quoting 595Quoting is used to remove the special meaning of certain characters or 596words to the shell, such as operators, whitespace, or keywords. 597There are four types of quoting: 598matched single quotes, 599matched double quotes, 600backslash, 601and 602dollar preceding matched single quotes (enhanced C style strings.) 603.Ss Backslash 604An unquoted backslash preserves the literal meaning of the following 605character, with the exception of 606.Aq newline . 607An unquoted backslash preceding a 608.Aq newline 609is treated as a line continuation, the two characters are simply removed. 610.Ss Single Quotes 611Enclosing characters in single quotes preserves the literal meaning of all 612the characters (except single quotes, making it impossible to put 613single quotes in a single-quoted string). 614.Ss Double Quotes 615Enclosing characters within double quotes preserves the literal 616meaning of all characters except dollar sign 617.Pq Li \&$ , 618backquote 619.Pq Li \&` , 620and backslash 621.Pq Li \e . 622The backslash inside double quotes is historically weird, and serves to 623quote only the following characters (and these not in all contexts): 624.Dl $ ` \*q \e <newline> , 625where a backslash newline is a line continuation as above. 626Otherwise it remains literal. 627.\" 628.\" 629.Ss Dollar Single Quotes ( Li \&$'...' ) 630.\" 631.Bd -filled -offset indent 632.Bf Em 633Note: this form of quoting is still somewhat experimental, 634and yet to be included in the POSIX standard. 635This implementation is based upon the current proposals for 636standardization, and is subject to change should the eventual 637adopted text differ. 638.Ef 639.Ed 640.Pp 641Enclosing characters in a matched pair of single quotes, with the 642first immediately preceded by an unquoted dollar sign 643.Pq Li \&$ 644provides a quoting mechanism similar to single quotes, except 645that within the sequence of characters, any backslash 646.Pq Li \e , 647is an escape character, which causes the following character to 648be treated specially. 649Only a subset of the characters that can occur in the string 650are defined after a backslash, others are reserved for future 651definition, and currently generate a syntax error if used. 652The escape sequences are modeled after the similar sequences 653in strings in the C programming language, with some extensions. 654.Pp 655The following characters are treated literally when following 656the escape character (backslash): 657.Dl \e \&' \(dq 658The sequence 659.Dq Li \e\e 660allows the escape character (backslash) to appear in the string literally. 661.Dq Li \e' 662allows a single quote character into the string, such an 663escaped single quote does not terminate the quoted string. 664.Dq Li \e\(dq 665is for compatibility with C strings, the double quote has 666no special meaning in a shell C-style string, 667and does not need to be escaped, but may be. 668.Pp 669A newline following the escape character is treated as a line continuation, 670like the same sequence in a double quoted string, 671or when not quoted \(en 672the two characters, the backslash escape and the newline, 673are removed from the input string. 674.Pp 675The following characters, when escaped, are converted in a 676manner similar to the way they would be in a string in the C language: 677.Dl a b e f n r t v 678An escaped 679.Sq a 680generates an alert (or 681.Sq BEL ) 682character, that is, control-G, or 0x07. 683In a similar way, 684.Sq b 685is backspace (0x08), 686.Sq e 687(an extension to C) is escape (0x1B), 688.Sq f 689is form feed (0x0C), 690.Sq n 691is newline (or line feed, 0x0A), 692.Sq r 693is return (0x0D), 694.Sq t 695is horizontal tab (0x09), 696and 697.Sq v 698is vertical tab (0x13). 699.Pp 700In addition to those there are 5 forms that need additional 701data, which is obtained from the subsequent characters. 702An escape 703.Pq Li \e 704followed by one, two or three, octal digits 705.Po So 0 Sc Ns \&.. Ns So 7 Sc Ns Pc 706is processed to form an 8 bit character value. 707If only one or two digits are present, the following 708character must be something other than an octal digit. 709It is safest to always use all 3 digits, with leading 710zeros if needed. 711If all three digits are present, the first must be one of 712.So 0 Sc Ns \&.. Ns So 3 Sc . 713.Pp 714An escape followed by 715.Sq x 716(lower case only) can be followed by one or two 717hexadecimal digits 718.Po So 0 Sc Ns \&.. Ns So 9 Sc , So A Sc Ns \&.. Ns So F Sc , or So a Sc Ns \&.. Ns So f Sc . Pc 719As with octal, if only one hex digit is present, the following 720character must be something other than a hex digit, 721so always giving 2 hex digits is best. 722However, unlike octal, it is unspecified in the standard 723how many hex digits can be consumed. 724This 725.Nm 726takes at most two, but other shells will continue consuming 727characters as long as they remain valid hex digits. 728Consequently, users should ensure that the character 729following the hex escape sequence is something other than 730a hex digit. 731One way to achieve this is to end the 732.Li $'...' 733string immediately 734after the final hex digit, and then, immediately start 735another, so 736.Dl \&$'\ex33'$'4...' 737always gives the character with value 0x33 738.Pq Sq 3 , 739followed by the character 740.Sq 4 , 741whereas 742.Dl \&$'\ex334' 743in some other shells would be the hex value 0x334 (10, or more, bits). 744.Pp 745There are two escape sequences beginning with 746.Sq Li \eu 747or 748.Sq Li \eU . 749The former is followed by from 1 to 4 hex digits, the latter by 750from 1 to 8 hex digits. 751Leading zeros can be used to pad the sequences to the maximum 752permitted length, to avoid any possible ambiguity problem with 753the following character, and because there are some shells that 754insist on exactly 4 (or 8) hex digits. 755These sequences are evaluated to form the value of a Unicode code 756point, which is then encoded into UTF-8 form, and entered into the 757string. 758(The code point should be converted to the appropriate 759code point value for the corresponding character in the character 760set given by the current locale, or perhaps the locale in use 761when the shell was started, but is not... currently.) 762Not all values that are possible to write are valid, values that 763specify (known) invalid Unicode code points will be rejected, or 764simply produce 765.Sq \&? . 766.Pp 767Lastly, as another addition to what is available in C, the escape 768character (backslash), followed by 769.Sq c 770(lower case only) followed by one additional character, which must 771be an alphabetic character (a letter), or one of the following: 772.Dl \&@ \&[ \&\e \&] \&^ \&_ \&? 773Other than 774.Sq Li \ec? 775the value obtained is the least significant 5 bits of the 776ASCII value of the character following the 777.Sq Li \ec 778escape sequence. 779That is what is commonly known as the 780.Dq control 781character obtained from the given character. 782The escape sequence 783.Sq Li \ec? 784yields the ASCII DEL character (0x7F). 785Note that to obtain the ASCII FS character (0x1C) this way, 786.Pq "that is control-\e" 787the trailing 788.Sq Li \e 789must be escaped itself, and so for this one case, the full 790escape sequence is 791.Dq Li \ec\e\e . 792The sequence 793.Dq Li \ec\e Ns Ar X\^ 794where 795.Sq Ar X\^ 796is some character other than 797.Sq Li \e 798is reserved for future use, its meaning is unspecified. 799In this 800.Nm 801an error is generated. 802.Pp 803If any of the preceding escape sequences generate the value 804.Sq \e0 805(a NUL character) that character, and all that follow in the same 806.Li $'...' 807string, are omitted from the resulting word. 808.Pp 809After the 810.Li $'...' 811string has had any included escape sequences 812converted, it is treated as if it had been a single quoted string. 813.\" 814.\" 815.Ss Reserved Words 816.\" 817Reserved words are words that have special meaning to the 818shell and are recognized, 819if completely unquoted, 820at the beginning of a line, 821after a control operator, 822and where the syntax of a command specifically requires a reserved word. 823The following are reserved words: 824.Bl -column while while while while -offset indent 825.It Ic \&! Ta Ic \&{ Ta Ic \&} Ta Ic case 826.It Ic do Ta Ic done Ta Ic elif Ta Ic else 827.It Ic esac Ta Ic fi Ta Ic for Ta Ic if 828.It Ic in Ta Ic then Ta Ic until Ta Ic while 829.El 830.Pp 831Their meanings are discussed later. 832.\" 833.\" 834.Ss Aliases 835.\" 836An alias is a name and corresponding value set using the 837.Ic alias 838built-in command. 839Whenever a reserved word (see above) may occur, 840and after checking for reserved words, the shell 841checks the word to see if it matches an alias. 842If it does, it replaces it in the input stream with its value. 843For example, if there is an alias called 844.Dq lf 845with the value 846.Dq "ls -F" , 847then the input: 848.Pp 849.Dl lf foobar Aq return 850.Pp 851would become 852.Pp 853.Dl ls -F foobar Aq return 854.Pp 855Aliases provide a convenient way for naive users to create shorthands for 856commands without having to learn how to create functions with arguments. 857They can also be used to create lexically obscure code. 858This use is strongly discouraged. 859.\" 860.Ss Commands 861.\" 862The shell interprets the words it reads according to a language, the 863specification of which is outside the scope of this man page (refer to the 864BNF in the POSIX 1003.2 document). 865Essentially though, a line is read and if the first 866word of the line (or after a control operator) is not a reserved word, 867then the shell has recognized a simple command. 868Otherwise, a complex 869command or some other special construct may have been recognized. 870.\" 871.\" 872.Ss Simple Commands 873.\" 874If a simple command has been recognized, the shell performs 875the following actions: 876.Bl -enum -offset indent 877.It 878Leading words of the form 879.Dq Ar name Ns Li = Ns Ar value 880are stripped off, the value is expanded, as described below, 881and the results are assigned to the environment of the simple command. 882Redirection operators and their arguments (as described below) are 883stripped off and saved for processing in step 3 below. 884.It 885The remaining words are expanded as described in the 886.Sx Word Expansions 887section below. 888The first remaining word is considered the command name and the 889command is located. 890Any remaining words are considered the arguments of the command. 891If no command name resulted, then the 892.Dq Ar name Ns Li = Ns Ar value 893variable assignments recognized in item 1 affect the current shell. 894.It 895Redirections are performed, from first to last, in the order given, 896as described in the next section. 897.El 898.\" 899.\" 900.Ss Redirections 901.\" 902Redirections are used to change where a command reads its input or sends 903its output. 904In general, redirections open, close, or duplicate an 905existing reference to a file. 906The overall format used for redirection is: 907.Pp 908.Dl Oo Ar n Oc Ns Va redir-op Ar file 909.Pp 910where 911.Va redir-op 912is one of the redirection operators mentioned previously. 913A list of the possible redirections, and their meanings, follows. 914.Pp 915The 916.Op Ar n 917is an optional number, as in 918.Sq Li 3 919(not 920.Sq Li [3] ) , 921that refers to a file descriptor. 922If present it must occur unquoted, immediately before the redirection 923operator, with no intervening white space, and becomes a 924part of that operator. 925If file descriptor 926.Ar n 927was open prior to the redirection, its previous use is closed. 928.Pp 929All redirections have a single word 930.Ar file 931argument following the operator 932(white space is allowed between the redirection operator and 933.Ar file ) , 934though it is sometimes expressed as 935.Ar n2 . 936That argument is expanded 937(see 938.Sx "Word Expansions" 939below) using tilde expansion, parameter expansion, 940arithmetic expansion, command substitution and quote removal 941to produce the path name (or file descriptor) to be used. 942No field splitting or pathname expansion takes place. 943In the list below, where the 944.Ar file 945is given as 946.Ar n2 947the result of the expansions must be a number which 948refers to a suitable open file descriptor. 949.Bl -tag -width aaabsfiles -offset indent 950.It Oo Ar n Oc Ns Ic > Ar file 951Redirect standard output (or 952.Ar n ) 953to 954.Ar file . 955.It Oo Ar n Oc Ns Ic >| Ar file 956The same, but override the 957.Fl C 958option. 959.It Oo Ar n Oc Ns Ic >> Ar file 960Append standard output (or 961.Ar n ) 962to 963.Ar file . 964.It Oo Ar n Oc Ns Ic < Ar file 965Redirect standard input (or 966.Ar n ) 967from 968.Ar file . 969.It Oo Ar n1 Oc Ns Ic <& Ar n2 970Redirect standard input (or 971.Ar n1 ) 972from a duplicate of file descriptor 973.Ar n2 . 974.It Oo Ar n Oc Ns Ic <& \(mi 975Close standard input (or 976.Ar n ) . 977Note that the 978.Sq \&\(mi 979is minus sign (or hyphen) given literally or resulting 980from the expansion of 981.Ar file 982(or 983.Ar n2 ) 984for this format. 985When given literally there is usually no space between 986the redirection operator and the 987.Sq \&\(mi , 988though that is just a convention. 989.It Oo Ar n1 Oc Ns Ic >& Ar n2 990Redirect standard output (or 991.Ar n1 ) 992to be a duplicate of 993.Ar n2 . 994.It Oo Ar n Oc Ns Ic >& \(mi 995Close standard output (or 996.Ar n ) . 997.It Oo Ar n Oc Ns Ic <> Ar file 998Open 999.Ar file 1000for reading and writing on standard input (or 1001.Ar n ) . 1002.El 1003.Pp 1004The following redirection is often called a 1005.Dq here-document . 1006.Bd -unfilled -offset indent 1007.Oo Ar n Oc Ns Ic << Ar delimiter 1008.Li \&... here-doc-text ... 1009.Ar delimiter 1010.Ed 1011.Pp 1012The 1013.Dq here-doc-text 1014starts immediately after the next unquoted newline character following 1015the here-document redirection operator. 1016If there is more than one here-document redirection on the same 1017line, then the text for the first (from left to right) is read 1018first, and subsequent here-doc-text for later here-document redirections 1019follows immediately after, until all such redirections have been 1020processed. 1021.Pp 1022All the text on successive lines up to the delimiter, 1023which must appear on a line by itself, with nothing other 1024than an immediately following newline, is 1025saved away and made available to the command on standard input, or file 1026descriptor n if it is specified. 1027If the delimiter as specified on the initial line is 1028quoted, then the here-doc-text is treated literally; otherwise, the text is 1029treated much like a double quoted string, except that 1030.Sq Li \(dq 1031characters have no special meaning, and are not escaped by 1032.Sq Li \&\e , 1033and is subjected to parameter expansion, command substitution, and arithmetic 1034expansion as described in the 1035.Sx Word Expansions 1036section below. 1037If the operator is 1038.Ic <<- 1039instead of 1040.Ic << , 1041then leading tabs in all lines in the here-doc-text, including before the 1042end delimiter, are stripped. 1043If the delimiter is not quoted, lines in here-doc-text that end with 1044an unquoted 1045.Li \e 1046are joined to the following line, the 1047.Li \e 1048and following 1049newline are simply removed while reading the here-document, 1050which thus guarantees 1051that neither of those lines can be the end delimiter. 1052.Pp 1053It is a syntax error for the end of the input file (or string) to be 1054reached before the delimiter is encountered. 1055.\" 1056.\" 1057.Ss Search and Execution 1058.\" 1059There are three types of commands: shell functions, built-in commands, and 1060normal programs \(em and the command is searched for (by name) in that order. 1061A command that contains a slash 1062.Sq \&/ 1063in its name is always a normal program. 1064They each are executed in a different way. 1065.Pp 1066When a shell function is executed, all of the shell positional parameters 1067(note: excluding 1068.Li 0 , \" $0 1069which is a special, not positional, parameter, and remains unchanged) 1070are set to the arguments of the shell function. 1071The variables which are explicitly placed in the environment of 1072the command (by placing assignments to them before the function name) are 1073made local to the function and are set to the values given, 1074and exported for the benefit of programs executed with the function. 1075Then the command given in the function definition is executed. 1076The positional parameters, and local variables, are restored to 1077their original values when the command completes. 1078This all occurs within the current shell, and the function 1079can alter variables, or other settings, of the shell, but 1080not the positional parameters nor their related special parameters. 1081.Pp 1082Shell built-ins are executed internally to the shell, without spawning a 1083new process. 1084.Pp 1085Otherwise, if the command name doesn't match a function or built-in, the 1086command is searched for as a normal program in the file system (as 1087described in the next section). 1088When a normal program is executed, the shell runs the program, 1089passing the arguments and the environment to the program. 1090If the program is not a normal executable file, and if it does 1091not begin with the 1092.Dq magic number 1093whose ASCII representation is 1094.Dq Li "#!" , 1095so 1096.Xr execve 2 1097returns 1098.Er ENOEXEC 1099then) the shell will interpret the program in a sub-shell. 1100The child shell will reinitialize itself in this case, 1101so that the effect will be as if a 1102new shell had been invoked to handle the ad-hoc shell script, except that 1103the location of hashed commands located in the parent shell will be 1104remembered by the child. 1105.Pp 1106Note that previous versions of this document and the source code itself 1107misleadingly and sporadically refer to a shell script without a magic 1108number as a 1109.Dq shell procedure . 1110.\" 1111.\" 1112.Ss Path Search 1113.\" 1114When locating a command, 1115command names containing a slash 1116.Pq Sq \&/ 1117are simply executed without performing any searches. 1118.Pp 1119If there is no slash in the name, 1120the shell first looks to see if it is a special built-in command, 1121if not it looks to see if there is a shell function by that name. 1122If that fails it looks for an ordinary built-in command. 1123If a none of these searches located the command 1124the shell searches each entry in 1125.Ev PATH 1126in turn for the command. 1127The value of the 1128.Ev PATH 1129variable should be a series of entries separated by colons. 1130Each entry consists of a directory name. 1131The current directory may be indicated 1132implicitly by an empty directory name, or explicitly by a single period. 1133If a directory searched contains an executable file with the same 1134name as the command given, 1135the search terminates, and that program is executed. 1136.Ss Command Exit Status 1137Each command has an exit status that can influence the behavior 1138of other shell commands. 1139The paradigm is that a command exits 1140with zero in normal cases, or to indicate success, 1141and non-zero for failure, 1142error, or a false indication. 1143The man page for each command 1144should indicate the various exit codes and what they mean. 1145Additionally, the built-in commands return exit codes, as does 1146an executed shell function. 1147.Pp 1148If a command consists entirely of variable assignments then the 1149exit status of the command is that of the last command substitution 1150if any, otherwise 0. 1151.Pp 1152If redirections are present, and any fail to be correctly performed, 1153any command present is not executed, and an exit status of 2 1154is returned. 1155.Ss Complex Commands 1156Complex commands are combinations of simple commands with control 1157operators or reserved words, together creating a larger complex command. 1158Overall, a shell program is a: 1159.Bl -tag -width XpipelineX 1160.It list 1161Which is a sequence of one or more AND-OR lists. 1162.It "AND-OR list" 1163is a sequence of one or more pipelines. 1164.It pipeline 1165is a sequence of one or more commands. 1166.It command 1167is one of a simple command, a compound command, or a function definition. 1168.It "simple command" 1169has been explained above, and is the basic building block. 1170.It "compound command" 1171provides mechanisms to group lists to achieve different effects. 1172.It "function definition" 1173allows new simple commands to be created as groupings of existing commands. 1174.El 1175.Pp 1176Unless otherwise stated, the exit status of a list 1177is that of the last simple command executed by the list. 1178.\" 1179.\" 1180.Ss Pipelines 1181.\" 1182A pipeline is a sequence of one or more commands separated 1183by the control operator 1184.Sq Ic \(ba , 1185and optionally preceded by the 1186.Dq Ic \&! 1187reserved word. 1188Note that 1189.Sq Ic \(ba 1190is an operator, and so is recognized anywhere it appears unquoted, 1191it does not require surrounding white space or other syntax elements. 1192On the other hand 1193.Dq Ic \&! 1194being a reserved word, must be separated from adjacent words by 1195white space (or other operators, perhaps redirects) and is only 1196recognized as the reserved word when it appears in a command word 1197position (such as at the beginning of a pipeline.) 1198.Pp 1199The standard output of all but 1200the last command in the sequence is connected to the standard input 1201of the next command. 1202The standard output of the last 1203command is inherited from the shell, as usual, 1204as is the standard input of the first command. 1205.Pp 1206The format for a pipeline is: 1207.Pp 1208.Dl [!] command1 Op Li \&| command2 No ... 1209.Pp 1210The standard output of command1 is connected to the standard input of 1211command2. 1212The standard input, standard output, or both of each command is 1213considered to be assigned by the pipeline before any redirection specified 1214by redirection operators that are part of the command are performed. 1215.Pp 1216If the pipeline is not in the background (discussed later), the shell 1217waits for all commands to complete. 1218.Pp 1219The commands in a pipeline can either be simple commands, 1220or one of the compound commands described below. 1221The simplest case of a pipeline is a single simple command. 1222.Pp 1223If the 1224.Ic pipefail 1225option was set when a pipeline was started, 1226the pipeline status is the status of 1227the last (lexically last, i.e.: rightmost) command in the 1228pipeline to exit with non-zero exit status, or zero, if, 1229and only if, all commands in the pipeline exited with a status of zero. 1230If the 1231.Ic pipefail 1232option was not set, which is the default state, 1233the pipeline status is the exit 1234status of the last (rightmost) command in the pipeline, 1235and the exit status of any other commands in the pipeline is ignored. 1236.Pp 1237If the reserved word 1238.Dq Ic \&! 1239precedes the pipeline, the exit status 1240becomes the logical NOT of the pipeline status as determined above. 1241That is, if the pipeline status is zero, the exit status is 1; 1242if the pipeline status is other than zero, the exit status is zero. 1243If there is no 1244.Dq Ic \&! 1245reserved word, the pipeline status becomes the exit status. 1246.Pp 1247Because pipeline assignment of standard input or standard output or both 1248takes place before redirection, it can be modified by redirection. 1249For example: 1250.Pp 1251.Dl $ command1 2>&1 \&| command2 1252.Pp 1253sends both the standard output and standard error of command1 1254to the standard input of command2. 1255.Pp 1256Note that unlike some other shells, each process in the pipeline is a 1257child of the invoking shell (unless it is a shell built-in, in which case 1258it executes in the current shell \(em but any effect it has on the 1259environment is wiped). 1260.Pp 1261A pipeline is a simple case of an AND-OR-list (described below.) 1262A 1263.Li \&; 1264or 1265.Aq newline 1266terminator causes the preceding pipeline, or more generally, 1267the preceding AND-OR-list to be executed sequentially; 1268that is, the shell executes the commands, and waits for them 1269to finish before proceeding to following commands. 1270An 1271.Li \&& 1272terminator causes asynchronous (background) execution 1273of the preceding AND-OR-list (see the next paragraph below). 1274The exit status of an asynchronous AND-OR-list is zero. 1275The actual status of the commands, 1276after they have completed, 1277can be obtained using the 1278.Ic wait 1279built-in command described later. 1280.\" 1281.\" 1282.Ss Background Commands \(em Ic \&& 1283.\" 1284If a command, pipeline, or AND-OR-list 1285is terminated by the control operator ampersand 1286.Pq Li \&& , 1287the 1288shell executes the command asynchronously \(em that is, the shell does not 1289wait for the command to finish before executing the next command. 1290.Pp 1291The format for running a command in background is: 1292.Pp 1293.Dl command1 & Op Li command2 & No ... 1294.Pp 1295If the shell is not interactive, the standard input of an asynchronous 1296command is set to 1297.Pa /dev/null . 1298The process identifier of the most recent command started in the 1299background can be obtained from the value of the special parameter 1300.Dq Dv \&! \" $! 1301(see 1302.Sx Special Parameters ) 1303provided it is accessed before the next asynchronous command is started. 1304.\" 1305.\" 1306.Ss Lists \(em Generally Speaking 1307.\" 1308A list is a sequence of one or more commands separated by newlines, 1309semicolons, or ampersands, and optionally terminated by one of these three 1310characters. 1311A shell program, which includes the commands given to an 1312interactive shell, is a list. 1313Each command in such a list is executed when it is fully parsed. 1314Another use of a list is as a complete-command, 1315which is parsed in its entirety, and then later the commands in 1316the list are executed only if there were no parsing errors. 1317.Pp 1318The commands in a list are executed in the order they are written. 1319If command is followed by an ampersand, the shell starts the 1320command and immediately proceeds to the next command; otherwise it waits 1321for the command to terminate before proceeding to the next one. 1322A newline is equivalent to a 1323.Sq Li \&; 1324when no other operator is present, and the command being input 1325could syntactically correctly be terminated at the point where 1326the newline is encountered, otherwise it is just whitespace. 1327.\" 1328.\" 1329.Ss AND-OR Lists (Short-Circuit List Operators) 1330.\" 1331.Dq Li \&&& 1332and 1333.Dq Li \&|| 1334are AND-OR list operators. 1335After executing the commands that precede the 1336.Dq Li \&&& 1337the subsequent command is executed 1338if and only if the exit status of the preceding command(s) is zero. 1339.Dq Li \&|| 1340is similar, but executes the subsequent command if and only if the exit status 1341of the preceding command is nonzero. 1342If a command is not executed, the exit status remains unchanged 1343and the following AND-OR list operator (if any) uses that status. 1344.Dq Li \&&& 1345and 1346.Dq Li \&|| 1347both have the same priority. 1348Note that these operators are left-associative, so 1349.Dl true || echo bar && echo baz 1350writes 1351.Dq baz 1352and nothing else. 1353This is not the way it works in C. 1354.\" 1355.\" 1356.Ss Flow-Control Constructs \(em Ic if , while , until , for , case 1357.\" 1358These commands are instances of compound commands. 1359The syntax of the 1360.Ic if 1361command is 1362.Bd -literal -offset indent 1363.Ic if Ar list 1364.Ic then Ar list 1365.Ic [ elif Ar list 1366.Ic then Ar list ] No ... 1367.Ic [ else Ar list ] 1368.Ic fi 1369.Ed 1370.Pp 1371The first list is executed, and if the exit status of that list is zero, 1372the list following the 1373.Ic then 1374is executed. 1375Otherwise the list after an 1376.Ic elif 1377(if any) is executed and the process repeats. 1378When no more 1379.Ic elif 1380reserved words, and accompanying lists, appear, 1381the list after the 1382.Ic else 1383reserved word, if any, is executed. 1384.Pp 1385The syntax of the 1386.Ic while 1387command is 1388.Bd -literal -offset indent 1389.Ic while Ar list 1390.Ic do Ar list 1391.Ic done 1392.Ed 1393.Pp 1394The two lists are executed repeatedly while the exit status of the 1395first list is zero. 1396The 1397.Ic until 1398command is similar, but has the word 1399.Ic until 1400in place of 1401.Ic while , 1402which causes it to repeat until the exit status of the first list is zero. 1403.Pp 1404The syntax of the 1405.Ic for 1406command is 1407.Bd -literal -offset indent 1408.Ic for Ar variable Op Ic in Ar word No ... 1409.Ic do Ar list 1410.Ic done 1411.Ed 1412.Pp 1413The words are expanded, or 1414.Li \*q$@\*q 1415if 1416.Ic in 1417(and the following words) is not present, 1418and then the list is executed repeatedly with the 1419variable set to each word in turn. 1420If 1421.Ic in 1422appears after the variable, but no words are 1423present, the list is not executed, and the exit status is zero. 1424.Ic do 1425and 1426.Ic done 1427may be replaced with 1428.Sq Ic \&{ 1429and 1430.Sq Ic \&} , 1431but doing so is non-standard and not recommended. 1432.Pp 1433The syntax of the 1434.Ic break 1435and 1436.Ic continue 1437commands is 1438.Bd -literal -offset indent 1439.Ic break Op Ar num 1440.Ic continue Op Ar num 1441.Ed 1442.Pp 1443.Ic break 1444terminates the 1445.Ar num 1446innermost 1447.Ic for , while , 1448or 1449.Ic until 1450loops. 1451.Ic continue 1452breaks execution of the 1453.Ar num\^ Ns -1 1454innermost 1455.Ic for , while , 1456or 1457.Ic until 1458loops, and then continues with the next iteration of the enclosing loop. 1459These are implemented as special built-in commands. 1460The parameter 1461.Ar num , 1462if given, must be an unsigned positive integer (greater than zero). 1463If not given, 1 is used. 1464.Pp 1465The syntax of the 1466.Ic case 1467command is 1468.Bd -literal -offset indent 1469.Ic case Ar word Ic in 1470.Oo Ic \&( Oc Ar pattern Ns Ic \&) Oo Ar list Oc Ic \&;& 1471.Oo Ic \&( Oc Ar pattern Ns Ic \&) Oo Ar list Oc Ic \&;; 1472.No \&... 1473.Ic esac 1474.Ed 1475.Pp 1476The pattern can actually be one or more patterns (see 1477.Sx Shell Patterns 1478described later), separated by 1479.Dq \(or 1480characters. 1481.Pp 1482.Ar word 1483is expanded and matched against each 1484.Ar pattern 1485in turn, 1486from first to last, 1487with each pattern being expanded just before the match is attempted. 1488When a match is found, pattern comparisons cease, and the associated 1489.Ar list , 1490if given, 1491is evaluated. 1492If the list is terminated with 1493.Dq Ic \&;& 1494execution then falls through to the following list, if any, 1495without evaluating its pattern, or attempting a match. 1496When a list terminated with 1497.Dq Ic \&;; 1498has been executed, or when 1499.Ic esac 1500is reached, execution of the 1501.Ic case 1502statement is complete. 1503The exit status is that of the last command executed 1504from the last list evaluated, if any, or zero otherwise. 1505.\" 1506.\" 1507.Ss Grouping Commands Together 1508.\" 1509Commands may be grouped by writing either 1510.Dl Ic \&( Ns Ar list Ns Ic \&) 1511or 1512.Dl Ic \&{ Ar list Ns Ic \&; \&} 1513These also form compound commands. 1514.Pp 1515Note that while parentheses are operators, and do not require 1516any extra syntax, braces are reserved words, so the opening brace 1517must be followed by white space (or some other operator), and the 1518closing brace must occur in a position where a new command word might 1519otherwise appear. 1520.Pp 1521The first of these executes the commands in a sub-shell. 1522Built-in commands grouped into a 1523.Li \&( Ns Ar list Ns \&) 1524will not affect the current shell. 1525The second form does not fork another shell so is slightly more efficient, 1526and allows for commands which do affect the current shell. 1527Grouping commands together this way allows you to redirect 1528their output as though they were one program: 1529.Bd -literal -offset indent 1530{ echo -n \*qhello \*q ; echo \*qworld\*q ; } > greeting 1531.Ed 1532.Pp 1533Note that 1534.Dq Ic } 1535must follow a control operator (here, 1536.Dq Ic \&; ) 1537so that it is recognized as a reserved word and not as another command argument. 1538.\" 1539.\" 1540.Ss Functions 1541.\" 1542The syntax of a function definition is 1543.Pp 1544.Dl Ar name Ns Ic \&() Ar command Op Ar redirect No ... 1545.Pp 1546A function definition is an executable statement; when executed it 1547installs a function named 1548.Ar name 1549and returns an exit status of zero. 1550To be portable, and standards compliant, the name must use the same 1551syntax as a variable name, (see 1552.Sx "Variables and Parameters" 1553below). 1554As an extension, this shell allows almost all characters in 1555.Ar name 1556.Po 1557the exception is slash 1558.Pq Sq \&/ 1559as there is no way to invoke a function with a name containing 1560a slash 1561.Pc . 1562Including quoting, whitespace, and operator characters requires 1563that the word be quoted. 1564The 1565.Ar name 1566is subject to quote removal, but no other expansions. 1567Because of implementation issues, 1568unquoted dollar signs 1569.Pq Sq \&$ 1570and backquotes 1571.Pq Sq \&` 1572are prohibited, 1573but can be included in a function name by use of quoting. 1574.Pp 1575The command is normally a list enclosed between 1576.Dq { 1577and 1578.Dq } . 1579The standard syntax also allows the command to be any of the other 1580compound commands, including a sub-shell, all of which are supported. 1581As an extension, this shell also allows a simple command 1582(or even another function definition) to be 1583used, though users should be aware this is non-standard syntax. 1584This means that 1585.Dl l() ls \*q$@\*q 1586works to make 1587.Dq l 1588an alternative name for the 1589.Ic ls 1590command. 1591.Pp 1592If the optional redirect, (see 1593.Sx Redirections ) , 1594which may be of any of the normal forms, 1595is given, it is applied each time the 1596function is called. 1597This means that a simple 1598.Dq Hello World 1599function might be written (in the extended syntax) as: 1600.Bd -literal -offset indent 1601hello() cat <<EOF 1602Hello World! 1603EOF 1604.Ed 1605.Pp 1606To be correctly standards conforming this should be re-written as: 1607.Bd -literal -offset indent 1608hello() { cat; } <<EOF 1609Hello World! 1610EOF 1611.Ed 1612.Pp 1613Note the distinction between those forms, and 1614.Bd -literal -offset indent 1615hello() { cat <<EOF 1616Hello World! 1617EOF 1618\&} 1619.Ed 1620.Pp 1621which reads and processes the here-document 1622each time the shell executes the function, and which applies 1623that input only to the cat command, not to any other commands 1624that might appear in the function. 1625.Pp 1626Variables may be declared to be local to a function by using the 1627.Ic local 1628command. 1629This should usually appear as the first statement of a function, 1630though 1631.Ic local 1632is an executable command which can be used anywhere in a function. 1633See 1634.Sx Built-ins 1635below for its definition. 1636.Pp 1637The function completes after having executed 1638.Ar command 1639with exit status set to the status returned by 1640.Ar command . 1641If 1642.Ar command 1643is a compound-command 1644it can use the 1645.Ic return 1646command (see 1647.Sx Built-ins 1648below) 1649to finish before completing all of 1650.Ar command . 1651.Ss Variables and Parameters 1652The shell maintains a set of parameters. 1653A parameter denoted by a name is called a variable. 1654When starting up, the shell turns all the environment 1655variables into shell variables, and exports them. 1656New variables can be set using the form 1657.Pp 1658.Dl Ar name Ns Li = Ns Ar value 1659.Pp 1660Variables set by the user must have a name consisting solely of 1661alphabetics, numerics, and underscores \(em the first of which must not be 1662numeric. 1663A parameter can also be denoted by a number or a special 1664character as explained below. 1665.Ss Positional Parameters 1666A positional parameter is a parameter denoted by a number (n > 0). 1667The shell sets these initially to the values of its command line arguments 1668that follow the name of the shell script. 1669The 1670.Ic set 1671built-in can also be used to set or reset them, and 1672.Ic shift 1673can be used to manipulate the list. 1674.Pp 1675To refer to the 10th (and later) positional parameters, 1676the form 1677.Li \&${ Ns Ar n Ns Li \&} 1678must be used. 1679Without the braces, a digit following 1680.Dq $ 1681can only refer to one of the first 9 positional parameters, 1682or the special parameter 1683.Dv 0 . \" $0 1684The word 1685.Dq Li $10 1686is treated identically to 1687.Dq Li ${1}0 . 1688.\" 1689.\" 1690.Ss Special Parameters 1691.\" 1692A special parameter is a parameter denoted by one of the following special 1693characters. 1694The value of the parameter is listed next to its character. 1695.Bl -tag -width thinhyphena 1696.It Dv * 1697Expands to the positional parameters, starting from one. 1698When the 1699expansion occurs within a double-quoted string it expands to a single 1700field with the value of each parameter separated by the first character of 1701the 1702.Ev IFS 1703variable, or by a 1704.Aq space 1705if 1706.Ev IFS 1707is unset. 1708.It Dv @ \" $@ 1709Expands to the positional parameters, starting from one. 1710When the expansion occurs within double quotes, each positional 1711parameter expands as a separate argument. 1712If there are no positional parameters, the 1713expansion of @ generates zero arguments, even when 1714.Dv $@ 1715is double-quoted. 1716What this basically means, for example, is 1717if 1718.Li $1 1719is 1720.Dq abc 1721and 1722.Li $2 1723is 1724.Dq def\ ghi , 1725then 1726.Li \*q$@\*q 1727expands to 1728the two arguments: 1729.Pp 1730.Sm off 1731.Dl \*q abc \*q \ \*q def\ ghi \*q 1732.Sm on 1733.It Dv # 1734Expands to the number of positional parameters. 1735.It Dv \&? 1736Expands to the exit status of the most recent pipeline. 1737.It Dv \- No (hyphen, or minus) 1738Expands to the current option flags (the single-letter 1739option names concatenated into a string) as specified on 1740invocation, by the set built-in command, or implicitly 1741by the shell. 1742.It Dv $ 1743Expands to the process ID of the invoked shell. 1744A sub-shell retains the same value of 1745.Dv $ 1746as its parent. 1747.It Dv \&! 1748Expands to the process ID of the most recent background 1749command executed from the current shell. 1750For a pipeline, the process ID is that of the last command in the pipeline. 1751If no background commands have yet been started by the shell, then 1752.Dq Dv \&! 1753will be unset. 1754Once set, the value of 1755.Dq Dv \&! 1756will be retained until another background command is started. 1757.It Dv 0 No (zero) \" $0 1758Expands to the name of the shell or shell script. 1759.El 1760.\" 1761.\" 1762.Ss Word Expansions 1763.\" 1764This section describes the various expansions that are performed on words. 1765Not all expansions are performed on every word, as explained later. 1766.Pp 1767Tilde expansions, parameter expansions, command substitutions, arithmetic 1768expansions, and quote removals that occur within a single word expand to a 1769single field. 1770It is only field splitting or pathname expansion that can 1771create multiple fields from a single word. 1772The single exception to this 1773rule is the expansion of the special parameter 1774.Dv @ \" $@ 1775within double quotes, as was described above. 1776.Pp 1777The order of word expansion is: 1778.Bl -enum 1779.It 1780Tilde Expansion, Parameter Expansion, Command Substitution, 1781Arithmetic Expansion (these all occur at the same time, and the 1782result of any of these expansions is not rescanned for further 1783instances of the expansion, or any of the others). 1784.It 1785Unless the 1786.Ev IFS 1787variable has an empty value, 1788Field Splitting is performed on fields 1789generated by step (1) except for Tilde Expansion. 1790.It 1791Pathname Expansion (unless set 1792.Fl f 1793is in effect). 1794.It 1795Quote Removal. 1796.El 1797.Pp 1798The $ character is used to introduce parameter expansion, command 1799substitution, or arithmetic evaluation. 1800.Ss Tilde Expansion (substituting a user's home directory) 1801A word beginning with an unquoted tilde character (~) is 1802subjected to tilde expansion. 1803Provided all of the subsequent characters in the word are unquoted 1804up to an unquoted slash (/) 1805or when in an assignment or not in posix mode, an unquoted colon (:), 1806or if neither of those appear, the end of the word, 1807they are treated as a user name 1808and are replaced with the pathname of the named user's home directory. 1809If the user name is missing (as in 1810.Pa ~/foobar ) , 1811the tilde is replaced with the value of the 1812.Dv HOME 1813variable (the current user's home directory). 1814.Pp 1815In variable assignments, 1816an unquoted tilde immediately after the assignment operator (=), and 1817each unquoted tilde immediately after an unquoted colon in the value 1818to be assigned is also subject to tilde expansion as just stated. 1819.\" 1820.\" 1821.Ss Parameter Expansion 1822.\" 1823The format for parameter expansion is as follows: 1824.Pp 1825.Dl ${ Ns Ar expression Ns Li } 1826.Pp 1827where 1828.Ar expression 1829consists of all characters until the matching 1830.Sq Li } . 1831Any 1832.Sq Li } 1833escaped by a backslash or within a quoted string, and characters in 1834embedded arithmetic expansions, command substitutions, and variable 1835expansions, are not examined in determining the matching 1836.Sq Li } . 1837.Pp 1838The simplest form for parameter expansion is: 1839.Pp 1840.Dl ${ Ns Ar parameter Ns Li } 1841.Pp 1842The value, if any, of 1843.Ar parameter 1844is substituted. 1845.Pp 1846The parameter name or symbol can be enclosed in braces, 1847which are optional in this simple case, 1848except for positional parameters with more than one digit or 1849when parameter is followed by a character that could be interpreted as 1850part of the name. 1851If a parameter expansion occurs inside double quotes: 1852.Bl -enum 1853.It 1854pathname expansion is not performed on the results of the expansion; 1855.It 1856field splitting is not performed on the results of the 1857expansion. 1858Note that the special rules for 1859.Dv @ \" $@ 1860can result in multiple fields being produced, but this is 1861not because of field-splitting. 1862If unquoted, each 1863field produced by 1864.Dv $@ \" $@ 1865is subject to field splitting. 1866.El 1867.Pp 1868In addition, a parameter expansion where braces are used, 1869can be modified by using one of the following formats. 1870If the 1871.Sq Ic \&: 1872is omitted in the following modifiers, then the test in the expansion 1873applies only to unset parameters, not null ones. 1874.Bl -tag -width aaparameterwordaaaaa 1875.It Li ${ Ns Ar parameter Ns Ic :- Ns Ar word Ns Li } 1876.Sy Use Default Values. 1877If 1878.Ar parameter 1879is unset or null, the expansion of 1880.Ar word 1881is substituted; otherwise, the value of 1882.Ar parameter 1883is substituted. 1884.It Li ${ Ns Ar parameter Ns Ic := Ns Ar word Ns Li } 1885.Sy Assign Default Values. 1886If 1887.Ar parameter 1888is unset or null, the expansion of 1889.Ar word 1890is assigned to 1891.Ar parameter . 1892In all cases, the final value of 1893.Ar parameter 1894is substituted. 1895Only variables, not positional parameters or special 1896parameters, can be assigned in this way. 1897.It Li ${ Ns Ar parameter Ns Ic :? Ns Oo Ar word\^ Oc Ns Li } 1898.Sy Indicate Error if Null or Unset. 1899If 1900.Ar parameter 1901is unset or null, the expansion of 1902.Ar word 1903(or a message indicating it is unset if 1904.Ar word 1905is omitted) 1906is written to standard error and a non-interactive shell exits with 1907a nonzero exit status. 1908An interactive shell will not exit, but any associated command(s) will 1909not be executed. 1910If the 1911.Ar parameter 1912is set, its value is substituted. 1913.It Li ${ Ns Ar parameter Ns Ic :+ Ns Ar word Ns Li } 1914.Sy Use Alternative Value. 1915If 1916.Ar parameter 1917is unset or null, null is substituted; 1918otherwise, the expansion of 1919.Ar word 1920is substituted. 1921The value of 1922.Ar parameter 1923.Em is not used 1924in this expansion. 1925.It Li ${ Ns Ic # Ns Ar parameter Ns Li } 1926.Sy String Length. 1927The length in characters of the value of 1928.Ar parameter . 1929.El 1930.Pp 1931The following four varieties of parameter expansion provide for substring 1932processing. 1933In each case, pattern matching notation (see 1934.Sx Shell Patterns ) , 1935rather than regular expression notation, is used to evaluate the patterns. 1936If parameter is 1937.Dv * 1938or 1939.Dv @ , \" $@ 1940the result of the expansion is unspecified. 1941Enclosing the full parameter expansion string in double quotes does not 1942cause the following four varieties of pattern characters to be quoted, 1943whereas quoting characters within the braces has this effect. 1944.Bl -tag -width aaparameterwordaaaaa 1945.It Li ${ Ns Ar parameter Ns Ic % Ns Ar word Ns Li } 1946.Sy Remove Smallest Suffix Pattern. 1947The 1948.Ar word 1949is expanded to produce a pattern. 1950The parameter expansion then results in 1951.Ar parameter , 1952with the 1953smallest portion of the suffix matched by the pattern deleted. 1954If the 1955.Ar word 1956is to start with a 1957.Sq Li \&% 1958character, it must be quoted. 1959.It Li ${ Ns Ar parameter Ns Ic %% Ns Ar word Ns Li } 1960.Sy Remove Largest Suffix Pattern. 1961The 1962.Ar word 1963is expanded to produce a pattern. 1964The parameter expansion then results in 1965.Ar parameter , 1966with the largest 1967portion of the suffix matched by the pattern deleted. 1968The 1969.Dq Ic %% 1970pattern operator only produces different results from the 1971.Dq Ic \&% 1972operator when the pattern contains at least one unquoted 1973.Sq Li \&* . 1974.It Li ${ Ns Ar parameter Ns Ic \&# Ns Ar word Ns Li } 1975.Sy Remove Smallest Prefix Pattern. 1976The 1977.Ar word 1978is expanded to produce a pattern. 1979The parameter expansion then results in 1980.Ar parameter , 1981with the 1982smallest portion of the prefix matched by the pattern deleted. 1983If the 1984.Ar word 1985is to start with a 1986.Sq Li \&# 1987character, it must be quoted. 1988.It Li ${ Ns Ar parameter Ns Ic \&## Ns Ar word Ns Li } 1989.Sy Remove Largest Prefix Pattern. 1990The 1991.Ar word 1992is expanded to produce a pattern. 1993The parameter expansion then results in 1994.Ar parameter , 1995with the largest 1996portion of the prefix matched by the pattern deleted. 1997This has the same relationship with the 1998.Dq Ic \&# 1999pattern operator as 2000.Dq Ic %% 2001has with 2002.Dq Ic \&% . 2003.El 2004.\" 2005.\" 2006.Ss Command Substitution 2007.\" 2008Command substitution allows the output of a command to be substituted in 2009place of the command (and surrounding syntax). 2010Command substitution occurs when a word contains 2011a command list enclosed as follows: 2012.Pp 2013.Dl Ic $( Ns Ar list Ns Ic \&) 2014.Pp 2015or the older 2016.Pq Dq backquoted 2017version, which is best avoided: 2018.Pp 2019.Dl Ic \&` Ns Ar list Ns Ns Ic \&` 2020.Pp 2021See the section 2022.Sx Complex Commands 2023above for the definition of 2024.Ic list . 2025.Pp 2026The shell expands the command substitution by executing the 2027.Ar list 2028in a sub-shell environment and replacing the command substitution with the 2029standard output of the 2030.Ar list 2031after removing any sequence of one or more 2032.Ao newline Ac Ns s 2033from the end of the substitution. 2034(Embedded 2035.Ao newline Ac Ns s 2036before 2037the end of the output are not removed; however, during field splitting, 2038they may be used to separate fields 2039.Pq "as spaces usually are" 2040depending on the value of 2041.Ev IFS 2042and any quoting that is in effect.) 2043.Pp 2044Note that if a command substitution includes commands 2045to be run in the background, 2046the sub-shell running those commands 2047will only wait for them to complete if an appropriate 2048.Ic wait 2049command is included in the command list. 2050However, the shell in which the result of the command substitution 2051will be used will wait for both the sub-shell to exit and for the 2052file descriptor that was initially standard output for the command 2053substitution sub-shell to be closed. 2054In some circumstances this might not happen until all processes 2055started by the command substitution have finished. 2056.\" While the exit of the sub-shell closes its standard output, 2057.\" any background process left running may still 2058.\" have that file descriptor open. 2059.\" This includes yet another sub-shell which might have been 2060.\" (almost invisibly) created to wait for some other command to complete, 2061.\" and even where the background command has had its 2062.\" standard output redirected or closed, 2063.\" the waiting sub-shell may still have it open. 2064.\" Thus there is no guarantee that the result of a command substitution 2065.\" will be available in the shell which is to use it before all of 2066.\" the commands started by the command substitution have completed, 2067.\" though careful coding can often avoid delays beyond the termination 2068.\" of the command substitution sub-shell. 2069.\" .Pp 2070.\" For example, assuming a script were to contain the following 2071.\" code (which could be done better other ways, attempting 2072.\" this kind of trickery is not recommended): 2073.\" .Bd -literal -offset indent 2074.\" if [ "$( printf "Ready? " >&2; read ans; printf "${ans}"; 2075.\" { sleep 120 >/dev/null && kill $$ >/dev/null 2>&1; }& )" = go ] 2076.\" then 2077.\" printf Working... 2078.\" # more code 2079.\" fi 2080.\" .Ed 2081.\" .Pp 2082.\" the 2083.\" .Dq Working... 2084.\" output will not be printed, and code that follows will never be executed. 2085.\" Nor will anything later in the script 2086.\" .Po 2087.\" unless 2088.\" .Dv SIGTERM 2089.\" is trapped or ignored 2090.\" .Pc . 2091.\" .Pp 2092.\" The intent is to prompt the user, wait for the user to 2093.\" answer, then if the answer was 2094.\" .Dq go 2095.\" do the appropriate work, but set a 2 minute time limit 2096.\" on completing that work. 2097.\" If the work is not done by then, kill the shell doing it. 2098.\" .Pp 2099.\" It will usually not work as written, as while the 2 minute 2100.\" .Sq sleep 2101.\" has its standard output redirected, as does the 2102.\" .Sq kill 2103.\" that follows (which also redirects standard error, 2104.\" so the user would not see an error if the work were 2105.\" completed and there was no parent process left to kill); 2106.\" the sub-shell waiting for the 2107.\" .Sq sleep 2108.\" to finish successfully, so it can start the 2109.\" .Sq kill , 2110.\" does not. 2111.\" It waits, with standard output still open, 2112.\" for the 2 minutes until the sleep is done, 2113.\" even though the kill is not going to need that file descriptor, 2114.\" and there is nothing else which could. 2115.\" The command substitution does not complete until after 2116.\" the kill has executed and the background sub-shell 2117.\" finishes \(en at which time the shell running it is 2118.\" presumably dead. 2119.\" .Pp 2120.\" Rewriting the background sub-shell part of that as 2121.\" .Dl "{ sleep 120 && kill $$ 2>&1; } >/dev/null &" 2122.\" would allow this 2123.\" .Nm 2124.\" to perform as expected, but that is not guaranteed, 2125.\" not all shells would behave as planned. 2126.\" It is advised to avoid starting background processes 2127.\" from within a command substitution. 2128.\" 2129.\" 2130.Ss Arithmetic Expansion 2131.\" 2132Arithmetic expansion provides a mechanism for evaluating an arithmetic 2133expression and substituting its value. 2134The format for arithmetic expansion is as follows: 2135.Pp 2136.Dl Ic $(( Ns Ar expression Ns Ic \&)) 2137.Pp 2138The expression in an arithmetic expansion is treated as if it were in 2139double quotes, except that a double quote character inside the expression 2140is just a normal character (it quotes nothing.) 2141The shell expands all tokens in the expression for parameter expansion, 2142command substitution, and quote removal (the only quoting character is 2143the backslash 2144.Sq \&\e , 2145and only when followed by another 2146.Sq \&\e , 2147a dollar sign 2148.Sq \&$ , 2149a backquote 2150.Sq \&` 2151or a newline.) 2152.Pp 2153Next, the shell evaluates the expanded result as an arithmetic expression 2154and substitutes the calculated value of that expression. 2155.Pp 2156Arithmetic expressions use a syntax similar to that 2157of the C language, and are evaluated using the 2158.Ql intmax_t 2159data type (this is an extension to POSIX, which requires only 2160.Ql long 2161arithmetic.) 2162Shell variables may be referenced by name inside an arithmetic 2163expression, without needing a 2164.Dq \&$ 2165sign. 2166Variables that are not set, or which have an empty (null string) value, 2167used this way evaluate as zero (that is, 2168.Dq x 2169in arithmetic, as an R-Value, is evaluated as 2170.Dq ${x:-0} ) 2171unless the 2172.Nm 2173.Fl u 2174flag is set, in which case a reference to an unset variable is an error. 2175Note that unset variables used in the ${var} form expand to a null 2176string, which might result in syntax errors. 2177Referencing the value of a variable which is not numeric is an error. 2178.Pp 2179All of the C expression operators applicable to integers are supported, 2180and operate as they would in a C expression. 2181Use white space, or parentheses, to disambiguate confusing syntax, 2182otherwise, as in C, the longest sequence of consecutive characters 2183which make a valid token (operator, variable name, or number) is taken 2184to be that token, even if the token designated cannot be used 2185and a different interpretation could produce a successful parse. 2186This means, as an example, that 2187.Dq a+++++b 2188is parsed as the gibberish sequence 2189.Dq "a ++ ++ + b" , 2190rather than as the valid alternative 2191.Dq "a ++ + ++ b" . 2192Similarly, separate the 2193.Sq \&, 2194operator from numbers with white space to avoid the possibility 2195of confusion with the decimal indicator in some locales (though 2196fractional, or floating-point, numbers are not supported in this 2197implementation.) 2198.Pp 2199It should not be necessary to state that the C operators which 2200operate on, or produce, pointer types, are not supported. 2201Those include unary 2202.Dq \&* 2203and 2204.Dq \&& 2205and the struct and array referencing binary operators: 2206.Dq \&. , 2207.Dq \&-> 2208and 2209.Dq \&[ . 2210.\" 2211.\" 2212.Ss White Space Splitting (Field Splitting) 2213.\" 2214After parameter expansion, command substitution, and 2215arithmetic expansion the shell scans the results of 2216expansions and substitutions that did not occur in double quotes, 2217and 2218.Dq Li $@ 2219even if it did, 2220for field splitting and multiple fields can result. 2221.Pp 2222The shell treats each character of the 2223.Ev IFS 2224as a delimiter and uses the delimiters to split the results of parameter 2225expansion and command substitution into fields. 2226.Pp 2227Non-whitespace characters in 2228.Ev IFS 2229are treated strictly as parameter separators. 2230So adjacent non-whitespace 2231.Ev IFS 2232characters will produce empty parameters. 2233On the other hand, any sequence of whitespace 2234characters that occur in 2235.Ev IFS 2236(known as 2237.Ev IFS 2238whitespace) 2239can occur, leading and trailing 2240.Ev IFS 2241whitespace, and any 2242.Ev IFS 2243whitespace surrounding a non whitespace 2244.Ev IFS 2245delimiter, is removed. 2246Any sequence of 2247.Ev IFS 2248whitespace characters without a non-whitespace 2249.Ev IFS 2250delimiter acts as a single field separator. 2251.Pp 2252If 2253.Ev IFS 2254is unset it is assumed to contain space, tab, and newline, 2255all of which are 2256.Ev IFS 2257whitespace characters. 2258If 2259.Ev IFS 2260is set to a null string, there are no delimiters, 2261and no field splitting occurs. 2262.Ss Pathname Expansion (File Name Generation) 2263Unless the 2264.Fl f 2265flag is set, file name generation is performed after word splitting is 2266complete. 2267Each word is viewed as a series of patterns, separated by slashes. 2268The process of expansion replaces the word with the names of all 2269existing files whose names can be formed by replacing each pattern with a 2270string that matches the specified pattern. 2271There are two restrictions on 2272this: first, a pattern cannot match a string containing a slash, and 2273second, a pattern cannot match a string starting with a period unless the 2274first character of the pattern is a period. 2275The next section describes the 2276patterns used for both Pathname Expansion and the 2277.Ic case 2278command. 2279.Ss Shell Patterns 2280A pattern consists of normal characters, which match themselves, 2281and meta-characters. 2282The meta-characters are 2283.Dq \&! , 2284.Dq * , 2285.Dq \&? , 2286and 2287.Dq \&[ . 2288These characters lose their special meanings if they are quoted. 2289When command or variable substitution is performed 2290and the dollar sign or backquotes are not double-quoted, 2291the value of the variable or the output of 2292the command is scanned for these characters and they are turned into 2293meta-characters. 2294.Pp 2295An asterisk 2296.Pq Dq * 2297matches any string of characters. 2298A question mark 2299.Pq Dq \&? 2300matches any single character. 2301A left bracket 2302.Pq Dq \&[ 2303introduces a character class. 2304The end of the character class is indicated by a right bracket 2305.Pq Dq \&] ; 2306if this 2307.Dq \&] 2308is missing then the 2309.Dq \&[ 2310matches a 2311.Dq \&[ 2312rather than introducing a character class. 2313A character class matches any of the characters between the square brackets. 2314A named class of characters (see 2315.Xr wctype 3 ) 2316may be specified by surrounding the name with 2317.Pq Dq [: 2318and 2319.Pq Dq :] . 2320For example, 2321.Pq Dq [[:alpha:]] 2322is a shell pattern that matches a single letter. 2323A range of characters may be specified using a minus sign 2324.Pq Dq \(mi . 2325The character class may be complemented 2326by making an exclamation mark 2327.Pq Dq \&! 2328the first character of the character class. 2329.Pp 2330To include a 2331.Dq \&] 2332in a character class, make it the first character listed (after the 2333.Dq \&! , 2334if any). 2335To include a 2336.Dq \(mi , 2337make it the first (after !) or last character listed. 2338If both 2339.Dq \&] 2340and 2341.Dq \(mi 2342are to be included, the 2343.Dq \&] 2344must be first (after !) 2345and the 2346.Dq \(mi 2347last, in the character class. 2348.\" 2349.\" 2350.Ss Built-ins 2351.\" 2352This section lists the built-in commands which are built-in because they 2353need to perform some operation that can't be performed by a separate 2354process. 2355Or just because they traditionally are. 2356In addition to these, there are several other commands that may 2357be built in for efficiency (e.g. 2358.Xr printf 1 , 2359.Xr echo 1 , 2360.Xr test 1 , 2361etc). 2362.Bl -tag -width 5n 2363.It Ic \&: Op Ar arg ... 2364A null command that returns a 0 (true) exit value. 2365Any arguments or redirects are evaluated, then ignored. 2366.\" 2367.It Ic \&. Ar file 2368The dot command reads and executes the commands from the specified 2369.Ar file 2370in the current shell environment. 2371The file does not need to be executable and is looked up from the directories 2372listed in the 2373.Ev PATH 2374variable if its name does not contain a directory separator 2375.Pq Sq / . 2376The 2377.Ic return 2378command (see below) 2379can be used for a premature return from the sourced file. 2380.Pp 2381The POSIX standard has been unclear on how loop control keywords (break 2382and continue) behave across a dot command boundary. 2383This implementation allows them to control loops surrounding the dot command, 2384but obviously such behavior should not be relied on. 2385It is now permitted by the standard, but not required. 2386.\" 2387.It Ic alias Op Ar name Ns Op Li = Ns Ar string ... 2388If 2389.Ar name Ns Li = Ns Ar string 2390is specified, the shell defines the alias 2391.Ar name 2392with value 2393.Ar string . 2394If just 2395.Ar name 2396is specified, the value of the alias 2397.Ar name 2398is printed. 2399With no arguments, the 2400.Ic alias 2401built-in prints the 2402names and values of all defined aliases (see 2403.Ic unalias ) . 2404.\" 2405.It Ic bg Op Ar job ... 2406Continue the specified jobs (or the current job if no 2407jobs are given) in the background. 2408.\" 2409.It Ic command Oo Fl pVv Oc Ar command Op Ar arg ... 2410Execute the specified command but ignore shell functions when searching 2411for it. 2412(This is useful when you 2413have a shell function with the same name as a command.) 2414.Bl -tag -width 5n 2415.It Fl p 2416search for command using a 2417.Ev PATH 2418that guarantees to find all the standard utilities. 2419.It Fl V 2420Do not execute the command but 2421search for the command and print the resolution of the 2422command search. 2423This is the same as the 2424.Ic type 2425built-in. 2426.It Fl v 2427Do not execute the command but 2428search for the command and print the absolute pathname 2429of utilities, the name for built-ins or the expansion of aliases. 2430.El 2431.\" 2432.It Ic cd Oo Fl P Oc Op Ar directory Op Ar replace 2433Switch to the specified directory (default 2434.Ev $HOME ) . 2435If 2436.Ar replace 2437is specified, then the new directory name is generated by replacing 2438the first occurrence of the string 2439.Ar directory 2440in the current directory name with 2441.Ar replace . 2442Otherwise if 2443.Ar directory 2444is 2445.Sq Li - , 2446then the current working directory is changed to the previous current 2447working directory as set in 2448.Ev OLDPWD . 2449Otherwise if an entry for 2450.Ev CDPATH 2451appears in the environment of the 2452.Ic cd 2453command or the shell variable 2454.Ev CDPATH 2455is set and the directory name does not begin with a slash, 2456and its first (or only) component isn't dot or dot dot, 2457then the directories listed in 2458.Ev CDPATH 2459will be searched for the specified directory. 2460The format of 2461.Ev CDPATH 2462is the same as that of 2463.Ev PATH . 2464.Pp 2465The 2466.Fl P 2467option instructs the shell to update 2468.Ev PWD 2469with the specified physical directory path and change to that directory. 2470This is the default. 2471.Pp 2472When the directory changes, the variable 2473.Ev OLDPWD 2474is set to the working directory before the change. 2475.Pp 2476Some shells also support a 2477.Fl L 2478option, which instructs the shell to update 2479.Ev PWD 2480with the logical path and to change the current directory 2481accordingly. 2482This is not supported. 2483.Pp 2484In an interactive shell, the 2485.Ic cd 2486command will print out the name of the 2487directory that it actually switched to if this is different from the name 2488that the user gave, 2489or always if the 2490.Cm cdprint 2491option is set. 2492The destination may be different either because the 2493.Ev CDPATH 2494mechanism was used 2495.\" or because a symbolic link was crossed. 2496or if the 2497.Ar replace 2498argument was used. 2499.\" 2500.It Ic eval Ar string ... 2501Concatenate all the arguments with spaces. 2502Then re-parse and execute the command. 2503.\" 2504.It Ic exec Op Ar command Op Ar arg ... 2505Unless 2506.Ar command 2507is omitted, the shell process is replaced with the 2508specified program (which must be a real program, not a shell built-in or 2509function). 2510Any redirections on the 2511.Ic exec 2512command are marked as permanent, so that they are not undone when the 2513.Ic exec 2514command finishes. 2515When the 2516.Cm posix 2517option is not set, 2518file descriptors created via such redirections are marked close-on-exec 2519(see 2520.Xr open 2 2521.Dv O_CLOEXEC 2522or 2523.Xr fcntl 2 2524.Dv F_SETFD / 2525.Dv FD_CLOEXEC ) , 2526unless the descriptors refer to the standard input, 2527output, or error (file descriptors 0, 1, 2). 2528Traditionally Bourne-like shells 2529(except 2530.Xr ksh 1 ) , 2531made those file descriptors available to exec'ed processes. 2532To be assured the close-on-exec setting is off, 2533redirect the descriptor to (or from) itself, 2534either when invoking a command for which the descriptor is wanted open, 2535or by using 2536.Ic exec 2537(perhaps the same 2538.Ic exec 2539as opened it, after the open) 2540to leave the descriptor open in the shell 2541and pass it to all commands invoked subsequently. 2542Alternatively, see the 2543.Ic fdflags 2544command below, which can set, or clear, this, and other, 2545file descriptor flags. 2546.\" 2547.It Ic exit Op Ar exitstatus 2548Terminate the shell process. 2549If 2550.Ar exitstatus 2551is given it is used as the exit status of the shell; otherwise the 2552exit status of the preceding command (the current value of $?) is used. 2553.\" 2554.It Ic export Oo Fl nx Oc Ar name Ns Oo =value Oc ... 2555.It Ic export Oo Fl x Oc Oo Fl p Oo Ar name ... Oc Oc 2556.It Ic export Fl q Oo Fl x Oc Ar name ... 2557With no options, 2558but one or more names, 2559the specified names are exported so that they will appear in the 2560environment of subsequent commands. 2561With 2562.Fl n 2563the specified names are un-exported. 2564Variables can also be un-exported using the 2565.Ic unset 2566built in command. 2567With 2568.Fl x 2569(exclude) the specified names are marked not to be exported, 2570and any that had been exported, will be un-exported. 2571Later attempts to export the variable will be refused. 2572Note this does not prevent explicitly exporting a variable 2573to a single command, script or function by preceding that 2574command invocation by a variable assignment to that variable, 2575provided the variable is not also read-only. 2576That is 2577.Bd -literal -offset indent 2578export -x FOO # FOO will now not be exported by default 2579export FOO # this command will fail (non-fatally) 2580FOO=some_value my_command 2581.Ed 2582.Pp 2583still passes the value 2584.Pq Li FOO=some_value 2585to 2586.Li my_command 2587through the environment. 2588.Pp 2589The shell allows the value of a variable to be set at the 2590same time it is exported (or unexported, etc) by writing 2591.Pp 2592.Dl export [-nx] name=value 2593.Pp 2594Note that in such a usage, the 2595.Dq name=value 2596argument often needs to be quoted, 2597more often than is required of an assignment statement, 2598as, like with any other command, the command name and 2599arguments are all subject to the various expansions, 2600including filename expansion and field splitting, 2601before the 2602.Ic export 2603command is invoked. 2604With the default value for 2605.Dv IFS : 2606.Bd -unfilled -compact -offset indent 2607X='a b c' 2608export Y=$X 2609.Ed 2610the command invoked would be 2611.Dl "export Y=a b c" 2612exporting 2613.Dv Y , 2614with the value 2615.Dq a 2616and also exporting the variables named 2617.Dq b 2618and 2619.Dq c , 2620which is probably not as intended. 2621.Pp 2622With no arguments the export command lists the names of all 2623set exported variables, 2624or if 2625.Fl x 2626was given, all set variables marked not for export. 2627With the 2628.Fl p 2629option specified, the output will be formatted suitably for 2630non-interactive use, and unset variables are included. 2631When 2632.Fl p 2633is given, variable names, but not values, may also be 2634given, in which case output is limited to the variables named. 2635.Pp 2636With 2637.Fl q 2638and a list of variable names, the 2639.Ic export 2640command will exit with status 0 if all the named 2641variables have been marked for export, or 1 if 2642any are not so marked. 2643If 2644.Fl x 2645is also given, 2646the test is instead for variables marked not to be exported. 2647.Pp 2648Other than with 2649.Fl q , 2650the 2651.Ic export 2652built-in exits with status 0, 2653unless an attempt is made to export a variable which has 2654been marked as unavailable for export, 2655in which cases it exits with status 1. 2656In all cases if 2657an invalid option, or option combination, is given, 2658or an invalid variable name is present, 2659.Ic export 2660will write a message to the standard error output, 2661and exit with a non-zero status. 2662A non-interactive shell will terminate. 2663.Pp 2664Note that there is no restriction upon exporting, 2665or un-exporting, read-only variables. 2666The no-export flag can be reset by unsetting the variable 2667and creating it again \(en provided the variable is not also read-only. 2668.\" 2669.It Ic fc Oo Fl e Ar editor Oc Op Ar first Op Ar last 2670.It Ic fc Fl l Oo Fl nr Oc Op Ar first Op Ar last 2671.It Ic fc Fl s Oo Ar old=new Oc Op Ar first 2672The 2673.Ic fc 2674built-in lists, or edits and re-executes, commands previously entered 2675to an interactive shell. 2676.Bl -tag -width 5n 2677.It Fl e Ar editor 2678Use the editor named by 2679.Ar editor 2680to edit the commands. 2681The 2682.Ar editor 2683string is a command name, subject to search via the 2684.Ev PATH 2685variable. 2686The value in the 2687.Ev FCEDIT 2688variable is used as a default when 2689.Fl e 2690is not specified. 2691If 2692.Ev FCEDIT 2693is null or unset, the value of the 2694.Ev EDITOR 2695variable is used. 2696If 2697.Ev EDITOR 2698is null or unset, 2699.Xr ed 1 2700is used as the editor. 2701.It Fl l No (ell) 2702List the commands rather than invoking an editor on them. 2703The commands are written in the sequence indicated by 2704the first and last operands, as affected by 2705.Fl r , 2706with each command preceded by the command number. 2707.It Fl n 2708Suppress command numbers when listing with 2709.Fl l . 2710.It Fl r 2711Reverse the order of the commands listed (with 2712.Fl l ) 2713or edited (with neither 2714.Fl l 2715nor 2716.Fl s ) . 2717.It Fl s 2718Re-execute the command without invoking an editor. 2719.It Ar first 2720.It Ar last 2721Select the commands to list or edit. 2722The number of previous commands that 2723can be accessed are determined by the value of the 2724.Ev HISTSIZE 2725variable. 2726The value of 2727.Ar first 2728or 2729.Ar last 2730or both are one of the following: 2731.Bl -tag -width 5n 2732.It Oo Cm + Oc Ns Ar number 2733A positive number representing a command number; command numbers can be 2734displayed with the 2735.Fl l 2736option. 2737.It Cm \- Ns Ar number 2738A negative decimal number representing the command that was executed 2739number of commands previously. 2740For example, \-1 is the immediately previous command. 2741.El 2742.It Ar string 2743A string indicating the most recently entered command that begins with 2744that string. 2745If the 2746.Ar old Ns Li = Ns Ar new 2747operand is not also specified with 2748.Fl s , 2749the string form of the first operand cannot contain an embedded equal sign. 2750.El 2751.Pp 2752The following environment variables affect the execution of 2753.Ic fc : 2754.Bl -tag -width HISTSIZE 2755.It Ev FCEDIT 2756Name of the editor to use. 2757.It Ev HISTSIZE 2758The number of previous commands that are accessible. 2759.El 2760.\" 2761.It Ic fg Op Ar job 2762Move the specified job or the current job to the foreground. 2763A foreground job can interact with the user via standard input, 2764and receive signals from the terminal. 2765.\" 2766.It Ic fdflags Oo Fl v Oc Op Ar fd ... 2767.It Ic fdflags Oo Fl v Oc Fl s Ar flags fd Op ... 2768Get or set file descriptor flags. 2769The 2770.Fl v 2771argument enables verbose printing, printing flags that are also off, and 2772the flags of the file descriptor being set after setting. 2773The 2774.Fl s 2775flag interprets the 2776.Ar flags 2777argument as a comma separated list of file descriptor flags, each preceded 2778with a 2779.Dq \(pl 2780or a 2781.Dq \(mi 2782indicating to set or clear the respective flag. 2783Valid flags are: 2784.Cm append , 2785.Cm async , 2786.Cm sync , 2787.Cm nonblock , 2788.Cm fsync , 2789.Cm dsync , 2790.Cm rsync , 2791.Cm direct , 2792.Cm nosigpipe , 2793and 2794.Cm cloexec . 2795Unique abbreviations of these names, of at least 2 characters, 2796may be used on input. 2797See 2798.Xr fcntl 2 2799and 2800.Xr open 2 2801for more information. 2802.\" 2803.It Ic getopts Ar optstring var 2804The POSIX 2805.Ic getopts 2806command, not to be confused with the 2807Bell Labs\[en]derived 2808.Xr getopt 1 . 2809.Pp 2810The first argument should be a series of letters, each of which may be 2811optionally followed by a colon to indicate that the option requires an 2812argument. 2813The variable specified is set to the parsed option. 2814.Pp 2815The 2816.Ic getopts 2817command deprecates the older 2818.Xr getopt 1 2819utility due to its handling of arguments containing whitespace. 2820.Pp 2821The 2822.Ic getopts 2823built-in may be used to obtain options and their arguments 2824from a list of parameters. 2825When invoked, 2826.Ic getopts 2827places the value of the next option from the option string in the list in 2828the shell variable specified by 2829.Ar var 2830and its index in the shell variable 2831.Ev OPTIND . 2832When the shell is invoked, 2833.Ev OPTIND 2834is initialized to 1. 2835For each option that requires an argument, the 2836.Ic getopts 2837built-in will place it in the shell variable 2838.Ev OPTARG . 2839If an option is not allowed for in the 2840.Ar optstring , 2841then 2842.Ev OPTARG 2843will be unset. 2844.Pp 2845.Ar optstring 2846is a string of recognized option letters (see 2847.Xr getopt 3 ) . 2848If a letter is followed by a colon, the option is expected to have an 2849argument which may or may not be separated from it by whitespace. 2850If an option character is not found where expected, 2851.Ic getopts 2852will set the variable 2853.Ar var 2854to a 2855.Sq Li \&? ; 2856.Ic getopts 2857will then unset 2858.Ev OPTARG 2859and write output to standard error. 2860By specifying a colon as the first character of 2861.Ar optstring 2862all errors will be ignored. 2863.Pp 2864A nonzero value is returned when the last option is reached. 2865If there are no remaining arguments, 2866.Ic getopts 2867will set 2868.Ar var 2869to the special option, 2870.Dq Li \-\- , 2871otherwise, it will set 2872.Ar var 2873to 2874.Sq Li \&? . 2875.Pp 2876The following code fragment shows how one might process the arguments 2877for a command that can take the options 2878.Fl a 2879and 2880.Fl b , 2881and the option 2882.Fl c , 2883which requires an argument. 2884.Bd -literal -offset indent 2885while getopts abc: f 2886do 2887 case $f in 2888 a | b) flag=$f;; 2889 c) carg=$OPTARG;; 2890 \e?) echo $USAGE; exit 1;; 2891 esac 2892done 2893shift $((OPTIND - 1)) 2894.Ed 2895.Pp 2896This code will accept any of the following as equivalent: 2897.Bd -literal -offset indent 2898cmd \-acarg file file 2899cmd \-a \-c arg file file 2900cmd \-carg -a file file 2901cmd \-a \-carg \-\- file file 2902.Ed 2903.\" 2904.It Ic hash Oo Fl rv Oc Op Ar command ... 2905The shell maintains a hash table which remembers the 2906locations of commands. 2907With no arguments whatsoever, 2908the 2909.Ic hash 2910command prints out the contents of this table. 2911Entries which have not been looked at since the last 2912.Ic cd 2913command are marked with an asterisk; it is possible for these entries 2914to be invalid. 2915.Pp 2916With arguments, the 2917.Ic hash 2918command removes the specified commands from the hash table (unless 2919they are functions) and then locates them. 2920With the 2921.Fl v 2922option, hash prints the locations of the commands as it finds them. 2923The 2924.Fl r 2925option causes the hash command to delete all the entries in the hash table 2926except for functions. 2927.\" 2928.It Ic inputrc Ar file 2929Read the 2930.Ar file 2931to set key bindings as defined by 2932.Xr editrc 5 . 2933.\" 2934.It Ic jobid Oo Fl g Ns \&| Ns Fl j Ns \&| Ns Fl p Oc Op Ar job 2935With no flags, print the process identifiers of the processes in the job. 2936If the 2937.Ar job 2938argument is omitted, the current job is used. 2939Any of the ways to select a job may be used for 2940.Ar job , 2941including the 2942.Sq Li \&% 2943forms, or the process id of the job leader 2944.Po 2945.Dq Li \&$! 2946if the job was created in the background. 2947.Pc 2948.Pp 2949If one of the flags is given, then instead of the list of 2950process identifiers, the 2951.Ic jobid 2952command prints: 2953.Bl -tag -width ".Fl g" 2954.It Fl g 2955the process group, if one was created for this job, 2956or nothing otherwise (the job is in the same process 2957group as the shell.) 2958.It Fl j 2959the job identifier (using 2960.Dq Li \&% Ns Ar n 2961notation, where 2962.Ar n 2963is a number) is printed. 2964.It Fl p 2965only the process id of the process group leader is printed. 2966.El 2967.Pp 2968These flags are mutually exclusive. 2969.Pp 2970.Ic jobid 2971exits with status 2 if there is an argument error, 2972status 1, if with 2973.Fl g 2974the job had no separate process group, 2975or with 2976.Fl p 2977there is no process group leader (should not happen), 2978and otherwise exits with status 0. 2979.\" 2980.It Ic jobs Oo Fl l Ns \&| Ns Fl p Oc Op Ar job ... 2981Without 2982.Ar job 2983arguments, 2984this command lists out all the background processes 2985which are children of the current shell process. 2986With 2987.Ar job 2988arguments, the listed jobs are shown instead. 2989Without flags, the output contains the job 2990identifier (see 2991.Sx Job Control 2992below), an indicator character if the job is the current or previous job, 2993the current status of the job (running, suspended, or terminated successfully, 2994unsuccessfully, or by a signal) 2995and a (usually abbreviated) command string. 2996.Pp 2997With the 2998.Fl l 2999flag the output is in a longer form, with the process identifiers 3000of each process (run from the top level, as in a pipeline), and the 3001status of each process, rather than the job status. 3002.Pp 3003With the 3004.Fl p 3005flag, the output contains only the process identifier of the lead 3006process. 3007.Pp 3008In an interactive shell, each job shown as completed in the output 3009from the jobs command is implicitly waited for, and is removed from 3010the jobs table, never to be seen again. 3011In an interactive shell, when a background job terminates, the 3012.Ic jobs 3013command (with that job as an argument) is implicitly run just 3014before outputting the next PS1 command prompt, after the job 3015terminated. 3016This indicates that the job finished, shows its status, 3017and cleans up the job table entry for that job. 3018Non-interactive shells need to execute 3019.Ic wait 3020commands to clean up terminated background jobs. 3021.\" 3022.It Ic local Oo Fl INx Oc Oo Ar variable | \- Oc ... 3023Define local variables for a function. 3024Local variables have their attributes, and values, 3025as they were before the 3026.Ic local 3027declaration, restored when the function terminates. 3028.Pp 3029With the 3030.Fl N 3031flag, variables made local, are unset initially inside 3032the function. 3033Unless the 3034.Fl x 3035flag is also given, such variables are also unexported. 3036The 3037.Fl I 3038flag, which is the default in this shell, causes 3039the initial value and exported attribute 3040of local variables 3041to be inherited from the variable 3042with the same name in the surrounding 3043scope, if there is one. 3044If there is not, the variable is initially unset, 3045and not exported. 3046The 3047.Fl N 3048and 3049.Fl I 3050flags are mutually exclusive, if both are given, the last specified applies. 3051The read-only and unexportable attributes are always 3052inherited, if a variable with the same name already exists. 3053.Pp 3054The 3055.Fl x 3056flag (lower case) causes the local variable to be exported, 3057while the function runs, unless it has the unexportable attribute. 3058This can also be accomplished by using the 3059.Ic export 3060command, giving the same 3061.Ar variable 3062names, after the 3063.Ic local 3064command. 3065.Pp 3066Making an existing read-only variable local is possible, 3067but pointless. 3068If an attempt is made to assign an initial value to such 3069a variable, the 3070.Ic local 3071command fails, as does any later attempted assignment. 3072If the 3073.Ic readonly 3074command is applied to a variable that has been declared local, 3075the variable cannot be (further) modified within the function, 3076or any other functions it calls, however when the function returns, 3077the previous status (and value) of the variable is returned. 3078.Pp 3079Values may be given to local variables on the 3080.Ic local 3081command line in a similar fashion as used for 3082.Ic export 3083and 3084.Ic readonly . 3085These values are assigned immediately after the initialization 3086described above. 3087Note that any variable references on the command line will have 3088been expanded before 3089.Ic local 3090is executed, so expressions like 3091.Bd -unfilled -offset indent 3092local -N X="${X}" 3093.Ed 3094.Pp 3095are well defined, first $X is expanded, and then the command run is 3096.Dl "local -N X='old-value-of-X'" 3097See the description of the 3098.Ic export 3099built-in command for notes on why quoting the value is required. 3100.Pp 3101After arranging to preserve the old value and attributes, of 3102.Dv X 3103.Dq ( old-value-of X ) 3104.Ic local 3105unsets 3106.Dv X , 3107unexports it, and then assigns the 3108.Dq old-value-of-X 3109to 3110.Ev X . 3111.Pp 3112The shell uses dynamic scoping, so that if you make the variable 3113.Dv x 3114local to 3115function 3116.Dv f , 3117which then calls function 3118.Dv g , 3119references to the variable 3120.Dv x 3121made inside 3122.Dv g 3123will refer to the variable 3124.Dv x 3125declared inside 3126.Dv f , 3127not to the global variable named 3128.Dv x . 3129.Pp 3130Another way to view this, is as if the shell just has one flat, global, 3131namespace, in which all variables exist. 3132The 3133.Ic local 3134command conceptually copies the variable(s) named to unnamed temporary 3135variables, and when the function ends, copies them back again. 3136All references to the variables reference the same global variables, 3137but while the function is active, after the 3138.Ic local 3139command has run, the values and attributes of the variables might 3140be altered, and later, when the function completes, be restored. 3141.Pp 3142Note that the positional parameters 3143.Dv 1 , \" $1 3144.Dv 2 , \" $2 3145\&... (see 3146.Sx Positional Parameters ) , 3147and the special parameters 3148.Dv \&# , \" $# 3149.Dv \&* \" $* 3150and 3151.Dv \&@ \" $@ 3152(see 3153.Sx Special Parameters ) , 3154are always made local in all functions, and are reset inside the 3155function to represent the options and arguments passed to the function. 3156Note that 3157.Li $0 3158however retains the value it had outside the function, 3159as do all the other special parameters. 3160.Pp 3161The only special parameter that can optionally be made local is 3162.Dq Li \- . 3163Making 3164.Dq Li \- 3165local causes any shell options that are changed via the set command inside the 3166function to be restored to their original values when the function 3167returns. 3168If 3169.Fl X 3170option is altered after 3171.Dq Li \- 3172has been made local, then when the function returns, the previous 3173destination for 3174.Cm xtrace 3175output (as of the time of the 3176.Ic local 3177command) will also be restored. 3178If any of the shell's magic variables 3179(those which return a value which may vary without 3180the variable being explicitly altered, 3181e.g.: 3182.Dv SECONDS 3183or 3184.Dv HOSTNAME ) 3185are made local in a function, 3186they will lose their special properties when set 3187within the function, including by the 3188.Ic local 3189command itself 3190(if not to be set in the function, there is little point 3191in making a variable local) 3192but those properties will be restored when the function returns. 3193.Pp 3194It is an error to use 3195.Ic local 3196outside the scope of a function definition. 3197When used inside a function, it exits with status 0, 3198unless an undefined option is used, or an attempt is made to 3199assign a value to a read-only variable. 3200.Pp 3201Note that either 3202.Fl I 3203or 3204.Fl N 3205should always be used, or variables made local should always 3206be given a value, or explicitly unset, as the default behavior 3207(inheriting the earlier value, or starting unset after 3208.Ic local ) 3209differs amongst shell implementations. 3210Using 3211.Dq Li local \&\- 3212is an extension not implemented by most shells. 3213.Pp 3214See the section 3215.Sx LINENO 3216below for details of the effects of making the variable 3217.Dv LINENO 3218local. 3219.\" 3220.It Ic pwd Op Fl \&LP 3221Print the current directory. 3222If 3223.Fl L 3224is specified the cached value (initially set from 3225.Ev PWD ) 3226is checked to see if it refers to the current directory; if it does 3227the value is printed. 3228Otherwise the current directory name is found using 3229.Xr getcwd 3 . 3230The environment variable 3231.Ev PWD 3232is set to the printed value. 3233.Pp 3234The default is 3235.Ic pwd 3236.Fl L , 3237but note that the built-in 3238.Ic cd 3239command doesn't support the 3240.Fl L 3241option and will cache (almost) the absolute path. 3242If 3243.Ic cd 3244is changed (as unlikely as that is), 3245.Ic pwd 3246may be changed to default to 3247.Ic pwd 3248.Fl P . 3249.Pp 3250If the current directory is renamed and replaced by a symlink to the 3251same directory, or the initial 3252.Ev PWD 3253value followed a symbolic link, then the cached value may not 3254be the absolute path. 3255.Pp 3256The built-in command may differ from the program of the same name because 3257the program will use 3258.Ev PWD 3259and the built-in uses a separately cached value. 3260.\" 3261.It Ic read Oo Fl p Ar prompt Oc Oo Fl r Oc Ar variable Op Ar ... 3262The 3263.Ar prompt 3264is printed if the 3265.Fl p 3266option is specified and the standard input is a terminal. 3267Then a line is read from the standard input. 3268The trailing newline is deleted from the 3269line and the line is split as described in the field splitting section of the 3270.Sx Word Expansions 3271section above, and the pieces are assigned to the variables in order. 3272If there are more pieces than variables, the remaining pieces 3273(along with the characters in 3274.Ev IFS 3275that separated them) are assigned to the last variable. 3276If there are more variables than pieces, 3277the remaining variables are assigned the null string. 3278The 3279.Ic read 3280built-in will indicate success unless EOF is encountered on input, in 3281which case failure is returned. 3282.Pp 3283By default, unless the 3284.Fl r 3285option is specified, the backslash 3286.Dq \e 3287acts as an escape character, causing the following character to be treated 3288literally. 3289If a backslash is followed by a newline, the backslash and the 3290newline will be deleted. 3291.\" 3292.It Ic readonly Ar name Ns Oo =value Oc ... 3293.It Ic readonly Oo Fl p Oo Ar name ... Oc Oc 3294.It Ic readonly Fl q Ar name ... 3295With no options, 3296the specified names are marked as read only, so that they cannot be 3297subsequently modified or unset. 3298The shell allows the value of a variable 3299to be set at the same time it is marked read only by writing 3300.Pp 3301.Dl readonly name=value 3302.Pp 3303where the value often needs to be quoted, as explained for the 3304.Ic export 3305command. 3306.Pp 3307With no arguments the 3308.Ic readonly 3309command lists the names of all set read only variables. 3310With the 3311.Fl p 3312option specified, 3313the output will be formatted suitably for non-interactive use, 3314and unset variables are included. 3315When the 3316.Fl p 3317option is given, 3318a list of variable names (without values) may also be specified, 3319in which case output is limited to the named variables. 3320.Pp 3321With the 3322.Fl q 3323option, the 3324.Ic readonly 3325command tests the read-only status of the variables listed 3326and exits with status 0 if all named variables are read-only, 3327or with status 1 if any are not read-only. 3328.Pp 3329Other than as specified for 3330.Fl q 3331the 3332.Ic readonly 3333command normally exits with status 0. 3334In all cases, if an unknown option, or an invalid option combination, 3335or an invalid variable name, is given; 3336or a variable which was already read-only is attempted to be set; 3337the exit status will not be zero, a diagnostic 3338message will be written to the standard error output, 3339and a non-interactive shell will terminate. 3340.\" 3341.It Ic return Op Ar n 3342Stop executing the current function or a dot command with return value of 3343.Ar n 3344or the value of the last executed command, if not specified. 3345For portability, 3346.Ar n 3347should be in the range from 0 to 255. 3348.Pp 3349The POSIX standard says that the results of 3350.Ic return 3351outside a function or a dot command are unspecified. 3352This implementation treats such a return as a no-op with a return value of 0 3353(success, true). 3354Use the 3355.Ic exit 3356command instead, if you want to return from a script or exit 3357your shell. 3358.\" 3359.It Ic set Oo { Fl options | Cm +options | Cm \-- } Oc Ar arg ... 3360The 3361.Ic set 3362command performs four different functions. 3363.Pp 3364With no arguments, it lists the values of all shell variables. 3365.Pp 3366With a single option of either 3367.Dq Fl o 3368or 3369.Dq Cm +o 3370.Ic set 3371outputs the current values of the options. 3372In the 3373.Fl o 3374form, all options are listed, with their current values. 3375In the 3376.Cm +o 3377form, the shell outputs a string that can later be used 3378as a command to reset all options to their current values. 3379.Pp 3380If options are given, it sets the specified option 3381flags, or clears them as described in the 3382.Sx Argument List Processing 3383section. 3384In addition to the options listed there, 3385when the 3386.Dq "option name" 3387given to 3388.Ic set Fl o 3389is 3390.Cm default 3391all of the options are reset to the values they had immediately 3392after 3393.Nm 3394initialization, before any startup scripts, or other input, had been processed. 3395While this may be of use to users or scripts, its primary purpose 3396is for use in the output of 3397.Dq Ic set Cm +o , 3398to avoid that command needing to list every available option. 3399There is no 3400.Cm +o default . 3401.Pp 3402The fourth use of the 3403.Ic set 3404command is to set the values of the shell's 3405positional parameters to the specified arguments. 3406To change the positional 3407parameters without changing any options, use 3408.Dq -\|- 3409as the first argument to 3410.Ic set . 3411If no following arguments are present, the 3412.Ic set 3413command 3414will clear all the positional parameters (equivalent to executing 3415.Dq Li shift $# . ) 3416Otherwise the following arguments become 3417.Li \&$1 , 3418.Li \&$2 , 3419\&..., 3420and 3421.Li \&$# 3422is set to the number of arguments present. 3423.\" 3424.It Ic setvar Ar variable Ar value 3425Assigns 3426.Ar value 3427to 3428.Ar variable . 3429(In general it is better to write 3430.Li variable=value 3431rather than using 3432.Ic setvar . 3433.Ic setvar 3434is intended to be used in 3435functions that assign values to variables whose names are passed as 3436parameters.) 3437.\" 3438.It Ic shift Op Ar n 3439Shift the positional parameters 3440.Ar n 3441times. 3442If 3443.Ar n 3444is omitted, 1 is assumed. 3445Each 3446.Ic shift 3447sets the value of 3448.Li $1 3449to the previous value of 3450.Li $2 , 3451the value of 3452.Li $2 3453to the previous value of 3454.Li $3 , 3455and so on, decreasing 3456the value of 3457.Li $# 3458by one. 3459The shift count must be less than or equal to the number of 3460positional parameters ( 3461.Dq Li $# ) 3462before the shift. 3463.\" 3464.It Ic specialvar Ar variable ... 3465For each 3466.Ar variable 3467name given, 3468if the variable named is one which, 3469in this 3470.Nm , 3471could be treated as a special variable, 3472then cause that 3473.Ar variable 3474to be made special, undoing any effects of an earlier 3475.Ic unset 3476or assignment to the variable. 3477If all 3478.Ar variable Ns s 3479given are recognized special variables in this 3480.Nm 3481the 3482.Ic specialvar 3483command will exit with status 0, otherwise 1. 3484Invalid usage will result in an exit status of 2. 3485.Pp 3486Note that all variables capable of being special are created 3487that way, this command is not required to cause that to happen. 3488However should such a variable be imported from the environment, 3489that will cause (for those special variables so designated) 3490the special effects for that variable to be lost. 3491Consequently, as the contents of the environment cannot be controlled, 3492any script which desires to make use of the properties 3493of most of the special variables should use this command, 3494naming the variables required, 3495to ensure that their special properties are available. 3496.\" 3497.It Ic times 3498Prints two lines to standard output. 3499Each line contains two accumulated time values, expressed 3500in minutes and seconds (including fractions of a second.) 3501The first value gives the user time consumed, the second the system time. 3502.Pp 3503The first output line gives the CPU and system times consumed by the 3504shell itself. 3505The second line gives the accumulated times for children of this 3506shell (and their descendants) which have exited, and then been 3507successfully waited for by the relevant parent. 3508See 3509.Xr times 3 3510for more information. 3511.Pp 3512.Ic times 3513has no parameters, and exits with an exit status of 0 unless 3514an attempt is made to give it an option. 3515.\" 3516.It Ic trap Ar action signal ... 3517.It Ic trap \- 3518.It Ic trap Op Fl l 3519.It Ic trap Fl p Oo Ar signal ... Oc 3520.It Ic trap Fl P Ar signal ... 3521.It Ic trap Ar N signal ... 3522.Pp 3523Cause the shell to parse and execute action when any of the specified 3524signals are received. 3525The signals are specified by signal number or as the name of the signal. 3526If 3527.Ar signal 3528is 3529.Li 0 \" $0 3530or its equivalent, 3531.Li EXIT , 3532the action is executed when the shell exits. 3533The 3534.Ar action 3535may be a null (empty) string, 3536which causes the specified signals to be ignored. 3537With 3538.Ar action 3539set to 3540.Sq Li - 3541the specified signals are set to their default actions. 3542If the first 3543.Ar signal 3544is specified in its numeric form, then 3545.Ar action 3546can be omitted to achieve the same effect. 3547This archaic, 3548but still standard, 3549form should not be relied upon, use the explicit 3550.Sq Li - 3551action. 3552If no signals are specified with an action of 3553.Sq Li - , 3554all signals are reset. 3555.Pp 3556When the shell forks off a sub-shell, it resets trapped (but not ignored) 3557signals to the default action. 3558On non-interactive shells, the 3559.Ic trap 3560command has no effect on signals that were 3561ignored on entry to the shell. 3562On interactive shells, the 3563.Ic trap 3564command will catch or reset signals ignored on entry. 3565.Pp 3566Issuing 3567.Ic trap 3568with option 3569.Fl l 3570will print a list of valid signal names. 3571.Ic trap 3572without any arguments causes it to write a list of signals and their 3573associated non-default actions to the standard output in a format 3574that is suitable as an input to the shell that achieves the same 3575trapping results. 3576With the 3577.Fl p 3578flag, trap prints the same information for the signals specified, 3579or if none are given, for all signals, including those where the 3580action is the default. 3581The 3582.Fl P 3583flag is similar, but prints only the action(s) associated with the 3584named signals, at least (and usually only) one of which must be given. 3585Nothing is printed if the action is the default, 3586an empty line is printed for ignored signals. 3587These variants of the trap command may be executed in a sub-shell 3588.Pq "such as in a command substitution" , 3589provided they appear as the initial sequence of commands in that sub-shell, 3590in which case the state of traps from the parent of that 3591sub-shell is reported. 3592.Pp 3593Examples: 3594.Pp 3595.Dl trap 3596.Pp 3597List trapped signals and their corresponding actions. 3598.Pp 3599.Dl trap -l 3600.Pp 3601Print a list of valid signals. 3602.Pp 3603.Dl trap '' INT QUIT tstp 30 3604.Pp 3605Ignore signals INT QUIT TSTP USR1. 3606.Pp 3607.Dl trap date INT 3608.Pp 3609Run the 3610.Dq date 3611command (print the date) upon receiving signal INT. 3612.Pp 3613.Dl trap HUP INT 3614.Pp 3615Run the 3616.Dq HUP 3617command, or function, upon receiving signal INT. 3618.Pp 3619.Dl Ic eval Qo \&$( trap -P QUIT \&) Qc 3620.Pp 3621Parse and execute the action that would be invoked were a 3622.Dv SIGQUIT 3623received. 3624.Pp 3625.Dl trap 1 2 3626.Pp 3627Reset the actions for signals 1 (HUP) and 2 (INT) to their defaults. 3628.Bd -literal -offset indent 3629traps=$(trap -p) 3630 # more commands ... 3631trap 'action' SIG 3632 # more commands ... 3633eval "$traps" 3634.Ed 3635.Pp 3636Save the trap status, execute commands, changing some traps, 3637and then reset all traps to their values at the start of the sequence. 3638The 3639.Fl p 3640option is required in the first command here, 3641or any signals that were previously 3642untrapped (in their default states) 3643and which were altered during the intermediate code, 3644would not be reset by the final 3645.Ic eval . 3646.\" 3647.It Ic type Op Ar name ... 3648Interpret each 3649.Ar name 3650as a command and print the resolution of the command search. 3651Possible resolutions are: 3652shell keyword, alias, shell built-in, 3653command, tracked alias and not found. 3654For aliases the alias expansion is 3655printed; for commands and tracked aliases the complete pathname of the 3656command is printed. 3657.\" 3658.It Ic ulimit Oo Fl H Ns \*(Ba Ns Fl S Oc Op Fl a \*(Ba Fl btfdscmlrpnv Op Ar value 3659Inquire about or set the hard or soft limits on processes or set new 3660limits. 3661The choice between hard limit (which no process is allowed to 3662violate, and which may not be raised once it has been lowered) and soft 3663limit (which causes processes to be signaled but not necessarily killed, 3664and which may be raised) is made with these flags: 3665.Bl -tag -width Fl 3666.It Fl H 3667set or inquire about hard limits 3668.It Fl S 3669set or inquire about soft limits. 3670.El 3671.Pp 3672If neither 3673.Fl H 3674nor 3675.Fl S 3676is specified, the soft limit is displayed or both limits are set. 3677If both are specified, the last one wins. 3678.Pp 3679The limit to be interrogated or set, then, is chosen by specifying 3680any one of these flags: 3681.Bl -tag -width Fl 3682.It Fl a 3683show all the current limits 3684.It Fl b 3685the socket buffer size of a process (bytes) 3686.It Fl c 3687the largest core dump size that can be produced 3688(512-byte blocks) 3689.It Fl d 3690the data segment size of a process (kilobytes) 3691.It Fl f 3692the largest file that can be created 3693(512-byte blocks) 3694.It Fl l 3695how much memory a process can lock with 3696.Xr mlock 2 3697(kilobytes) 3698.It Fl m 3699the total physical memory that can be 3700in use by a process (kilobytes) 3701.It Fl n 3702the number of files a process can have open at once 3703.It Fl p 3704the number of processes this user can 3705have at one time 3706.It Fl r 3707the number of threads this user can 3708have at one time 3709.It Fl s 3710the stack size of a process (kilobytes) 3711.It Fl t 3712CPU time (seconds) 3713.It Fl v 3714how large a process address space can be 3715.El 3716.Pp 3717If none of these is specified, it is the limit on file size that is shown 3718or set. 3719If value is specified, the limit is set to that number; otherwise 3720the current limit is displayed. 3721.Pp 3722Limits of an arbitrary process can be displayed or set using the 3723.Xr sysctl 8 3724utility. 3725.It Ic umask Oo Fl S Oc Op Ar mask 3726Set the value of umask (see 3727.Xr umask 2 ) 3728to the specified octal value. 3729If the argument is omitted, the umask value is printed. 3730With 3731.Fl S 3732a symbolic form is used instead of an octal number. 3733.It Ic unalias Oo Fl a Oc Op Ar name 3734If 3735.Ar name 3736is specified, the shell removes that alias. 3737If 3738.Fl a 3739is specified, all aliases are removed. 3740.It Ic unset Oo Fl efvx Oc Ar name ... 3741If 3742.Fl v 3743is specified, the specified variables are unset and unexported. 3744Readonly variables cannot be unset. 3745If 3746.Fl f 3747is specified, the specified functions are undefined. 3748If 3749.Fl e 3750is given, the specified variables are unexported, but otherwise unchanged, 3751alternatively, if 3752.Fl x 3753is given, the exported status of the variable will be retained, 3754even after it is unset. 3755.Pp 3756If no flags are provided 3757.Fl v 3758is assumed. 3759If 3760.Fl f 3761is given with one of the other flags, 3762then the named variables will be unset, or unexported, and functions 3763of the same names will be undefined. 3764The 3765.Fl e 3766and 3767.Fl x 3768flags both imply 3769.Fl v . 3770If 3771.Fl e 3772is given, the 3773.Fl x 3774flag is ignored. 3775.Pp 3776The exit status is 0, unless an attempt was made to unset 3777a readonly variable, in which case the exit status is 1. 3778It is not an error to unset (or undefine) a variable (or function) 3779that is not currently set (or defined.) 3780.\" 3781.It Ic wait Oo Fl n Oc Oo Fl p Ar var Oc Op Ar job ... 3782Wait for the specified jobs to complete 3783and return the exit status of the last job in the parameter list, 3784or 127 if that job is not a current child of the shell. 3785.Pp 3786If no 3787.Ar job 3788arguments are given, wait for all jobs to 3789complete and then return an exit status of zero 3790(including when there were no jobs, and so nothing exited.) 3791.Pp 3792With the 3793.Fl n 3794option, wait instead for any one of the given 3795.Ar job Ns s, 3796or if none are given, any job, to complete, and 3797return the exit status of that job. 3798If none of the given 3799.Ar job 3800arguments is a current child of the shell, 3801or if no 3802.Ar job 3803arguments are given and the shell has no unwaited for children, 3804then the exit status will be 127. 3805.Pp 3806The 3807.Fl p Ar var 3808option allows the process (or job) identifier of the 3809job for which the exit status is returned to be obtained. 3810The variable named (which must not be readonly) will be 3811unset initially, then if a job has exited and its status is 3812being returned, set to the identifier from the 3813arg list (if given) of that job, 3814or the lead process identifier of the job to exit when used with 3815.Fl n 3816and no job arguments. 3817Note that 3818.Fl p 3819with neither 3820.Fl n 3821nor 3822.Ar job 3823arguments is useless, as in that case no job status is 3824returned, the variable named is simply unset. 3825.Pp 3826If the wait is interrupted by a signal, 3827its exit status will be greater than 128, 3828and 3829.Ar var , 3830if given, will remain unset. 3831.Pp 3832Once waited upon, by specific process number or job-id, 3833or by a 3834.Ic wait 3835with no arguments, 3836knowledge of the child is removed from the system, 3837and it cannot be waited upon again. 3838.Pp 3839Note than when a list of jobs are given, more that 3840one argument might refer to the same job. 3841In that case, if the final argument represents a job 3842that is also given earlier in the list, it is not 3843defined whether the status returned will be the 3844exit status of the job, or 127 indicating that 3845the child no longer existed when the wait command 3846reached the later argument in the list. 3847In this 3848.Nm 3849the exit status will be that from the job. 3850.Nm 3851waits for each job exactly once, regardless of 3852how many times (or how many different ways) it 3853is listed in the arguments to 3854.Ic wait . 3855That is 3856.Bd -literal -offset indent -compact 3857wait 100 100 100 3858.Ed 3859is identical to 3860.Bd -literal -offset indent -compact 3861wait 100 3862.Ed 3863.El 3864.\" 3865.\" 3866.Ss Job Control 3867.\" 3868Each process (or set of processes) started by 3869.Nm 3870is created as a 3871.Dq job 3872and added to the jobs table. 3873When enabled by the 3874.Fl m 3875option 3876.Pq aka Fl o Cm monitor 3877when the job is created, 3878.Nm 3879places each job (if run from the top level shell) 3880into a process group of its own, which allows control 3881of the process(es), and its/their descendants, as a unit. 3882When the 3883.Fl m 3884option is off, or when started from a sub-shell environment, 3885jobs share the same process group as the parent shell. 3886The 3887.Fl m 3888option is enabled by default in interactive shells with 3889a terminal as standard input and standard error. 3890.Pp 3891Jobs with separate process groups may be stopped, and then later 3892resumed in the foreground (with access to the terminal) 3893or in the background (where attempting to read from the 3894terminal will result in the job stopping.) 3895A list of current jobs can be obtained using the 3896.Ic jobs 3897built-in command. 3898Jobs are identified using either the process identifier 3899of the lead process of the job (the value available in 3900the special parameter 3901.Dq Dv \&! 3902if the job is started in the background), or using percent 3903notation. 3904Each job is given a 3905.Dq job number 3906which is a small integer, starting from 1, and can be 3907referenced as 3908.Dq Li \&% Ns Ar n 3909where 3910.Ar n 3911is that number. 3912Note that this applies to jobs both with and without their own process groups. 3913Job numbers are shown in the output from the 3914.Ic jobs 3915command enclosed in brackets 3916.Po 3917.Sq Li \&[ 3918and 3919.Sq Li \&] 3920.Pc . 3921Whenever the job table becomes empty, the numbers begin at one again. 3922In addition, there is the concept of a current, and a previous job, 3923identified by 3924.Dq Li \&%+ 3925.Po 3926or 3927.Dq Li \&%% 3928or even just 3929.Dq Li \&% 3930.Pc , 3931and a previous job, identified by 3932.Dq Li \&%\- . 3933Whenever a background job is started, 3934or a job is resumed in the background, 3935it becomes the current job. 3936The job that was the current job 3937(prepare for a big surprise here, drum roll..., wait for it...\&) 3938becomes the previous job. 3939When the current job terminates, the previous job is 3940promoted to be the current job. 3941In addition the form 3942.Dq Li \&% Ns Ar string\^ 3943finds the job for which the command starts with 3944.Ar string 3945and the form 3946.Dq Li \&%? Ns Ar string\^ 3947finds the job which contains the 3948.Ar string 3949in its command somewhere. 3950Both forms require the result to be unambiguous. 3951For this purpose the 3952.Dq command 3953is that shown in the output from the 3954.Ic jobs 3955command, not the original command line. 3956.Pp 3957The 3958.Ic bg , 3959.Ic fg , 3960.Ic jobid , 3961.Ic jobs , 3962.Ic kill , 3963and 3964.Ic wait 3965commands all accept job identifiers as arguments, in addition to 3966process identifiers (larger integers). 3967See the 3968.Sx Built-ins 3969section above, and 3970.Xr kill 1 , 3971for more details of those commands. 3972In addition, a job identifier 3973(using one of the 3974.Dq \&% forms ) 3975issued as a command, without arguments, is interpreted as 3976if it had been given as the argument to the 3977.Ic fg 3978command. 3979.Pp 3980To cause a foreground process to stop, enter the terminal's 3981.Ic stop 3982character (usually control-Z). 3983To cause a background process to stop, send it a 3984.Dv STOP 3985signal, using the kill command. 3986A useful function to define is 3987.Bd -literal -offset indent 3988stop() { kill -s STOP "${@:-%%}"; } 3989.Ed 3990.Pp 3991The 3992.Ic fg 3993command resumes a stopped job, placing it in the foreground, 3994and 3995.Ic bg 3996resumes a stopped job in the background. 3997The 3998.Ic jobid 3999command provides information about process identifiers, job identifiers, 4000and the process group identifier, for a job. 4001.Pp 4002Whenever a sub-shell is created, the jobs table becomes invalid 4003(the sub-shell has no children.) 4004However, to enable uses like 4005.Bd -literal -offset indent 4006PID=$(jobid -p %1) 4007.Ed 4008.Pp 4009the table is only actually cleared in a sub-shell when needed to 4010create the first job there (built-in commands run in the foreground 4011do not create jobs.) 4012Note that in this environment, there is no useful current job 4013.Dq ( Li \&%% 4014actually refers to the sub-shell itself, but is not accessible) 4015but the job which is the current job in the parent can be accessed as 4016.Dq Li \&%\- . 4017.\" 4018.\" 4019.Ss Command Line Editing 4020.\" 4021When 4022.Nm 4023is being used interactively from a terminal, the current command 4024and the command history (see 4025.Ic fc 4026in the 4027.Sx Built-ins 4028section) 4029can be edited using emacs-mode or vi-mode command-line editing. 4030The command 4031.Ql set -o emacs 4032(or 4033.Fl E 4034option) 4035enables emacs-mode editing. 4036The command 4037.Ql set -o vi 4038(or 4039.Fl V 4040option) 4041enables vi-mode editing and places the current shell process into 4042vi insert mode. 4043(See the 4044.Sx Argument List Processing 4045section above.) 4046.Pp 4047The vi-mode uses commands similar to a subset of those described in the 4048.Xr vi 1 4049man page. 4050With vi-mode 4051enabled, 4052.Nm sh 4053can be switched between insert mode and command mode. 4054It's similar to 4055.Ic vi : 4056pressing the 4057.Aq ESC 4058key will throw you into vi command mode. 4059Pressing the 4060.Aq return 4061key while in command mode will pass the line to the shell. 4062.Pp 4063The emacs-mode uses commands similar to a subset available in the 4064.Ic emacs 4065editor. 4066With emacs-mode enabled, special keys can be used to modify the text 4067in the buffer using the control key. 4068.Pp 4069.Nm 4070uses the 4071.Xr editline 3 4072library. 4073See 4074.Xr editline 7 4075for a list of the possible command bindings, 4076and the default settings in emacs and vi modes. 4077Also see 4078.Xr editrc 5 4079for the commands that can be given to configure 4080.Xr editline 7 4081in the file named by the 4082.Ev EDITRC 4083parameter, 4084or a file used with the 4085.Ic inputrc 4086built-in command, 4087or using 4088.Xr editline 7 Ap s 4089configuration command line. 4090.Pp 4091When command line editing is enabled, the 4092.Xr editline 7 4093functions control printing of the 4094.Ev PS1 4095and 4096.Ev PS2 4097prompts when required. 4098As, in this mode, the command line editor needs to 4099keep track of what characters are in what position on 4100the command line, care needs to be taken when setting 4101the prompts. 4102Normal printing characters are handled automatically, 4103however mode setting sequences, which do not actually display 4104on the terminal, need to be identified to 4105.Xr editline 7 . 4106This is done, when needed, by choosing a character that 4107is not needed anywhere in the prompt, including in the mode 4108setting sequences, any single character is acceptable, 4109and assigning it to the shell parameter 4110.Dv PSlit . 4111Then that character should be used, in pairs, in the 4112prompt string. 4113Between each pair of 4114.Dv PSlit 4115characters are mode setting sequences, which affect the printing 4116attributes of the following (normal) characters of the prompt, 4117but do not themselves appear visibly, nor change the terminal's 4118cursor position. 4119.Pp 4120Each such sequence, that is 4121.Dv PSlit 4122character, mode setting character sequence, and another 4123.Dv PSlit 4124character, must currently be followed by at least one following 4125normal prompt character, or it will be ignored. 4126That is, a 4127.Dv PSlit 4128character cannot be the final character of 4129.Ev PS1 4130or 4131.Ev PS2 , 4132nor may two 4133.Dv PSlit 4134delimited sequences appear adjacent to each other. 4135Each sequence can contain as many mode altering sequences as are 4136required however. 4137Only the first character from 4138.Dv PSlit 4139will be used. 4140When set 4141.Dv PSlit 4142should usually be set to a string containing just one 4143character, then it can simply be embedded in 4144.Ev PS1 4145(or 4146.Ev PS2 ) 4147as in 4148.Pp 4149.D1 Li PS1=\*q${PSlit} Ns Ar mset\^ Ns Li ${PSlit}XYZ${PSlit} Ns Ar mclr\^ Ns Li ${PSlit}ABC\*q 4150.Pp 4151The prompt visible will be 4152.Dq XYZABC 4153with the 4154.Dq XYZ 4155part shown according as defined by the mode setting characters 4156.Ar mset , 4157and then cleared again by 4158.Ar mclr . 4159See 4160.Xr tput 1 4161for one method to generate appropriate mode sequences. 4162Note that both parts, XYZ and ABC, must each contain at least one 4163character. 4164.Pp 4165If 4166.Dv PSlit 4167is unset, which is its initial state, or set to a null string, 4168no literal character will be defined, 4169and all characters of the prompt strings will be assumed 4170to be visible characters (which includes spaces etc.) 4171To allow smooth use of prompts, without needing redefinition, when 4172.Xr editline 7 4173is disabled, the character chosen should be one which will be 4174ignored by the terminal if received, as when 4175.Xr editline 7 4176is not in use, the prompt strings are simply written to the terminal. 4177For example, setting: 4178.\" XXX: PS1 line is too long for -offset indent 4179.Bd -literal -offset left 4180 PSlit="$(printf\ '\e1')" 4181 PS1="${PSlit}$(tput\ bold\ blink)${PSlit}\e$${PSlit}$(tput\ sgr0)${PSlit}\ " 4182.Ed 4183.Pp 4184will arrange for the primary prompt to be a bold blinking dollar sign, 4185if supported by the current terminal, followed by an (ordinary) space, 4186and, as the SOH (control-A) character 4187.Pq Sq \e1 4188will not normally affect 4189a terminal, this same prompt will usually work with 4190.Xr editline 7 4191enabled or disabled. 4192.Sh ENVIRONMENT 4193.Bl -tag -width MAILCHECK 4194.It Ev CDPATH 4195The search path used with the 4196.Ic cd 4197built-in. 4198.It Ev EDITRC 4199Gives the name of the file containing commands for 4200.Xr editline 7 . 4201See 4202.Xr editrc 5 4203for possible content and format. 4204The file is processed, when in interactive mode with 4205command line editing enabled, whenever 4206.Ev EDITRC 4207is set (even with no actual value change,) 4208and if command line editing changes from disabled to enabled, 4209or the editor style used is changed. 4210(See the 4211.Fl E 4212and 4213.Fl V 4214options of the 4215.Ic set 4216built-in command, described in 4217.Sx Built-ins 4218above, which are documented further above in 4219.Sx Argument List Processing . ) 4220If unset 4221.Dq $HOME/.editrc 4222is used. 4223.It Ev ENV 4224Names the file sourced at startup by the shell. 4225Unused by this shell after initialization, 4226but is usually passed through the environment to 4227descendant shells. 4228See the 4229.Sx Invocation 4230section above for details of how 4231.Ev ENV 4232is processed and used. 4233.It Ev EUSER 4234Set to the login name of the effective user id running the shell, 4235as returned by 4236.Bd -literal -offset indent -compact 4237getpwuid(geteuid())->pw_name 4238.Ed 4239.Po 4240See 4241.Xr getpwuid 3 4242and 4243.Xr geteuid 2 4244for more details. 4245.Pc 4246This is obtained each time 4247.Ev EUSER 4248is expanded, so changes to the shell's execution identity 4249cause updates without further action. 4250If unset, it returns nothing. 4251If set it loses its special properties, and is simply a variable. 4252See the 4253.Ic specialvar 4254built-in command for remedial action. 4255.It Ev HISTSIZE 4256The number of lines in the history buffer for the shell. 4257.It Ev HOME 4258Set automatically by 4259.Xr login 1 4260from the user's login directory in the password file 4261.Pq Xr passwd 5 . 4262This environment variable also functions as the default argument for the 4263.Ic cd 4264built-in. 4265.It Ev HOSTNAME 4266Set to the current hostname of the system, as returned by 4267.Xr gethostname 3 . 4268This is obtained each time 4269.Ev HOSTNAME 4270is expanded, so changes to the system's name are reflected 4271without further action. 4272If unset, it returns nothing. 4273If set it loses its special properties, and is simply a variable. 4274See the 4275.Ic specialvar 4276built-in command for remedial action. 4277.It Ev IFS 4278Input Field Separators. 4279This is normally set to 4280.Aq space , 4281.Aq tab , 4282and 4283.Aq newline . 4284.Sx White Space Splitting 4285section for more details. 4286.It Ev LANG 4287The string used to specify localization information that allows users 4288to work with different culture-specific and language conventions. 4289See 4290.Xr nls 7 . 4291.It Dv LINENO 4292The current line number in the script or function. 4293See the section 4294.Sx LINENO 4295below for more details. 4296.It Ev MAIL 4297The name of a mail file, that will be checked for the arrival of new mail. 4298Overridden by 4299.Ev MAILPATH . 4300The check occurs just before 4301.Ev PS1 4302is written, immediately after reporting jobs which have changed status, 4303in interactive shells only. 4304New mail is considered to have arrived if the monitored file 4305has increased in size since the last check. 4306.\" .It Ev MAILCHECK 4307.\" The frequency in seconds that the shell checks for the arrival of mail 4308.\" in the files specified by the 4309.\" .Ev MAILPATH 4310.\" or the 4311.\" .Ev MAIL 4312.\" file. 4313.\" If set to 0, the check will occur at each prompt. 4314.It Ev MAILPATH 4315A colon 4316.Dq \&: 4317separated list of file names, for the shell to check for incoming mail. 4318This environment setting overrides the 4319.Ev MAIL 4320setting. 4321There is a maximum of 10 mailboxes that can be monitored at once. 4322.It Ev PATH 4323The default search path for executables. 4324See the 4325.Sx Path Search 4326section above. 4327.It Ev POSIXLY_CORRECT 4328If set in the environment upon initialization of the shell, 4329then the shell option 4330.Ic posix 4331will be set. 4332.Po 4333See the description of the 4334.Ic set 4335command in the 4336.Sx Built-ins 4337section. 4338.Pc 4339After initialization it is unused by the shell, 4340but is usually passed through the environment to 4341descendant processes, including other instances of the shell, 4342which may interpret it in a similar way. 4343.It Ev PPID 4344The process identified of the parent process of the 4345current shell. 4346This value is set at shell startup, ignoring 4347any value in the environment, and then made readonly. 4348.It Ev PS1 4349The primary prompt string, which defaults to 4350.Dq Li "$ " , 4351unless you are the superuser, in which case it defaults to 4352.Dq Li "# " . 4353This string is subject to parameter, arithmetic, and if 4354enabled by setting the 4355.Ic promptcmds 4356option, command substitution before being output. 4357During execution of commands used by command substitution, 4358execution tracing, the 4359.Ic xtrace 4360.Ic ( set Fl x ) 4361option is temporarily disabled. 4362If 4363.Ic promptcmds 4364is not set and the prompt string uses command substitution, 4365the prompt used will be an appropriate error string. 4366For other expansion errors, the prompt will become an 4367empty string, without an error message. 4368To verify parsing of 4369.Ev PS1 , 4370the method suggested for 4371.Ev ENV 4372can be used. 4373.It Ev PS2 4374The secondary prompt string, which defaults to 4375.Dq Li "> " . 4376After expansion (as for 4377.Ev PS1 ) 4378it is written whenever more input is required to complete the 4379current command. 4380.It Ev PS4 4381is Output, after expansion like 4382.Ev PS1 , 4383as a prefix for each line when execution trace 4384.Ic ( set Fl x ) 4385is enabled. 4386.Ev PS4 4387defaults to 4388.Dq Li "+ " . 4389.It Ev PSc 4390Initialized by the shell, ignoring any value from the environment, 4391to a single character string, either 4392.Sq \&# 4393or 4394.Sq \&$ , 4395depending upon whether the current user is the superuser or not. 4396This is intended for use when building a custom 4397.Ev PS1 . 4398.It Ev PSlit 4399Defines the character which may be embedded in pairs, in 4400.Ev PS1 4401or 4402.Ev PS2 4403to indicate to 4404.Xr editline 7 4405that the characters between each pair of occurrences of the 4406.Dv PSlit 4407character will not appear in the visible prompt, and will not 4408cause the terminal's cursor to change position, but rather set terminal 4409attributes for the following prompt character(s) at least one of 4410which must be present. 4411See 4412.Sx Command Line Editing 4413above for more information. 4414.It Ev RANDOM 4415Returns a different pseudo-random integer, 4416in the range [0,32767] each time it is accessed. 4417.Ev RANDOM 4418can be assigned an integer value to seed the PRNG. 4419If the value assigned is a constant, then the 4420sequence of values produces on subsequent references of 4421.Ev RANDOM 4422will repeat after the next time the same constant is assigned. 4423Note, this is not guaranteed to remain constant from one version 4424of the shell to another \(en the PRNG algorithm, or seeding 4425method is subject to change. 4426If 4427.Ev RANDOM 4428is assigned an empty value (null string) then the next time 4429.Ev RANDOM 4430is accessed, it will be seeded from a more genuinely random source. 4431The sequence of pseudo-random numbers generated will not be able to 4432be generated again (except by luck, whether good or bad, depends!) 4433This is also how the initial seed is generated, if none has been 4434assigned before 4435.Ev RANDOM 4436is first accessed after shell initialization. 4437Should the error message 4438.Dq "RANDOM initialisation failed" 4439appear on standard error, it indicates that the source 4440of good random numbers was not available, and 4441.Ev RANDOM 4442has instead been seeded with a more predictable value. 4443The following sequence of random numbers will 4444not be as unpredictable as they otherwise would be. 4445.It Ev SECONDS 4446Returns the number of seconds since the current shell was started. 4447If unset, it remains unset, and returns nothing, unless set again. 4448If set, it loses its special properties, and becomes a normal variable. 4449See the 4450.Ic specialvar 4451built-in command for remedial action. 4452.It Ev START_TIME 4453Initialized by the shell to the number of seconds since the Epoch 4454(see 4455.Xr localtime 3 ) 4456when the shell was started. 4457The value of 4458.Dl $(( Ns Ev START_TIME + Ev SECONDS Ns )) 4459represents the current time, if 4460.Ev START_TIME 4461has not been modified, and 4462.Ev SECONDS 4463has not been set or unset. 4464.It Ev TERM 4465The default terminal setting for the shell. 4466This is inherited by 4467children of the shell, and is used in the history editing modes. 4468.\" This is explicitly last, not in sort order - please leave! 4469.It Ev ToD 4470When referenced, uses the value of 4471.Ev ToD_FORMAT 4472(or 4473.Dq \&%T 4474if 4475.Ev ToD_FORMAT 4476is unset) as the format argument to 4477.Xr strftime 3 4478to encode the current time of day, in the time zone 4479defined by 4480.Ev TZ 4481if set, or current local time if not, and returns the result. 4482If unset 4483.Ev ToD 4484returns nothing. 4485If set, it loses its special properties, and becomes a normal variable. 4486See the 4487.Ic specialvar 4488built-in command for remedial action. 4489.It Ev ToD_FORMAT 4490Can be set to the 4491.Xr strftime 3 4492format string to be used when expanding 4493.Ev ToD . 4494Initially unset. 4495.It Ev TZ 4496If set, gives the time zone 4497(see 4498.Xr localtime 3 , 4499.Xr environ 7 ) 4500to use when formatting 4501.Ev ToD 4502and if exported, other utilities that deal with times. 4503If unset, the system's local wall clock time zone is used. 4504.It Ev NETBSD_SHELL 4505Unlike the variables previously mentioned, 4506this variable is somewhat strange, 4507in that it cannot be set, 4508inherited from the environment, 4509modified, or exported from the shell. 4510If set, by the shell, it indicates that the shell is the 4511.Ic sh 4512defined by this manual page, and gives its version information. 4513It can also give information in additional space separated words, 4514after the version string. 4515If the shell was built as part of a reproducible build, 4516the relevant date that was used for that build will be included. 4517Finally, any non-standard compilation options, 4518which may affect features available, 4519that were used when building the shell will be listed. 4520.Ev NETBSD_SHELL 4521behaves like any other variable that has the read-only 4522and un-exportable attributes set. 4523.El 4524.Ss Dv LINENO 4525.Dv LINENO 4526is in many respects a normal shell variable, containing an 4527integer value, and can be expanded using any of the forms 4528mentioned above which can be used for any other variable. 4529.Pp 4530.Dv LINENO 4531can be exported, made readonly, or unset, as with any other 4532variable, with similar effects. 4533Note that while being readonly prevents later attempts to 4534set, or unset, 4535.Dv LINENO , 4536it does not prevent its value changing. 4537References to 4538.Dv LINENO 4539.Pq "when not unset" 4540always obtain the current line number. 4541However, 4542.Dv LINENO 4543should normally not ever be set or unset. 4544In this shell setting 4545.Dv LINENO 4546reverses the effect of an earlier 4547.Ic unset , 4548but does not otherwise affect the value obtained. 4549If unset, 4550.Dv LINENO 4551should not normally be set again, doing so is not portable. 4552If 4553.Dv LINENO 4554is set or unset, different shells act differently. 4555The value of 4556.Dv LINENO 4557is never imported from the environment when the shell is 4558started, though if present there, as with any other variable, 4559.Dv LINENO 4560will be exported by this shell. 4561.Pp 4562.Dv LINENO 4563is set automatically by the shell to be the number of the source 4564line on which it occurs. 4565When exported, 4566.Dv LINENO 4567is exported with its value set to the line number it would have 4568had had it been referenced on the command line of the command to 4569which it is exported. 4570Line numbers are counted from 1, which is the first line the shell 4571reads from any particular file. 4572For this shell, standard input, including in an interactive shell, 4573the user's terminal, is just another file and lines are counted 4574there as well. 4575However note that not all shells count interactive 4576lines this way, it is not wise to rely upon 4577.Dv LINENO 4578having a useful value, except in a script, or a function. 4579.Pp 4580The role of 4581.Dv LINENO 4582in functions is less clear. 4583In some shells, 4584.Dv LINENO 4585continues to refer to the line number in the script which defines 4586the function, 4587in others lines count from one within the function, always (and 4588resume counting normally once the function definition is complete) 4589and others count in functions from one if the function is defined 4590interactively, but otherwise just reference the line number in the 4591script in which the function is defined. 4592This shell gives the user the option to choose. 4593If the 4594.Fl L 4595flag (the 4596.Ic local_lineno 4597option, see 4598.Sx Argument List Processing ) 4599is set, when the function is defined, then the function 4600defaults to counting lines with one being the first line of the 4601function. 4602When the 4603.Fl L 4604flag is not set, the shell counts lines in a function definition 4605in the same continuous sequence as the lines that surround the 4606function definition. 4607Further, if 4608.Dv LINENO 4609is made local 4610(see 4611.Sx Built-ins 4612above) 4613inside the function, the function can decide which 4614behavior it prefers. 4615If 4616.Dv LINENO 4617is made local and inherited, and not given a value, as in 4618.Dl local Fl I Dv LINENO 4619then from that point in the function, 4620.Dv LINENO 4621will give the line number as if lines are counted in sequence 4622with the lines that surround the function definition (and 4623any other function definitions in which this is nested.) 4624If 4625.Dv LINENO 4626is made local, and in that same command, given a value, as 4627.Dl local Oo Fl I Ns | Ns Fl N Oc Dv LINENO Ns = Ns Ar value 4628then 4629.Dv LINENO 4630will give the line number as if lines are counted from one 4631from the beginning of the function. 4632The value nominally assigned in this case is irrelevant, and ignored. 4633For completeness, if lineno is made local and unset, as in 4634.Dl local Fl N Dv LINENO 4635then 4636.Dv LINENO 4637is simply unset inside the function, and gives no value at all. 4638.Pp 4639Now for some technical details. 4640The line on which 4641.Dv LINENO 4642occurs in a parameter expansion, is the line that contains the 4643.Sq \&$ 4644that begins the expansion of 4645.Dv LINENO . 4646In the case of nested expansions, that 4647.Sq \&$ 4648is the one that actually has 4649.Dv LINENO 4650as its parameter. 4651In an arithmetic expansion, where no 4652.Sq \&$ 4653is used to evaluate 4654.Dv LINENO 4655but 4656.Dv LINENO 4657is simply referenced as a variable, then the value is the 4658line number of the line that contains the 4659.Sq L 4660of 4661.Dv LINENO . 4662For functions line one of the function definition (when relevant) 4663is the line that contains the first character of the 4664function name in the definition. 4665When exported, the line number of the command is the line number 4666where the first character of the word which becomes the command name occurs. 4667.Pp 4668When the shell opens a new file, for any reason, 4669it counts lines from one in that file, 4670and then resumes its original counting once it resumes reading the 4671previous input stream. 4672When handling a string passed to 4673.Ic eval 4674the line number starts at the line on which the string starts, 4675and then if the string contains internal newline characters, 4676those characters increase the line number. 4677This means that references to 4678.Dv LINENO 4679in such a case can produce values larger than would be 4680produced by a reference on the line after the 4681.Ic eval . 4682.Sh FILES 4683.Bl -item 4684.It 4685.Pa $HOME/.profile 4686.It 4687.Pa /etc/profile 4688.El 4689.Sh EXIT STATUS 4690Errors that are detected by the shell, such as a syntax error, will cause the 4691shell to exit with a non-zero exit status. 4692If the shell is not an 4693interactive shell, the execution of the shell file will be aborted. 4694Otherwise 4695the shell will return the exit status of the last command executed, or 4696if the exit built-in is used with a numeric argument, it will return the 4697argument. 4698.Sh SEE ALSO 4699.Xr csh 1 , 4700.Xr echo 1 , 4701.Xr getopt 1 , 4702.Xr ksh 1 , 4703.Xr login 1 , 4704.Xr printf 1 , 4705.Xr test 1 , 4706.Xr editline 3 , 4707.Xr getopt 3 , 4708.\" .Xr profile 4 , 4709.Xr editrc 5 , 4710.Xr passwd 5 , 4711.Xr editline 7 , 4712.Xr environ 7 , 4713.Xr nls 7 , 4714.Xr sysctl 8 4715.Sh HISTORY 4716A 4717.Nm 4718command appeared in 4719.At v1 . 4720It was replaced in 4721.At v7 4722with a version that introduced the basis of the current syntax. 4723That was, however, unmaintainable so we wrote this one. 4724.Sh BUGS 4725Setuid shell scripts should be avoided at all costs, as they are a 4726significant security risk. 4727.Pp 4728The characters generated by filename completion should probably be quoted 4729to ensure that the filename is still valid after the input line has been 4730processed. 4731.Pp 4732Job control of compound statements (loops, etc) is a complete mess. 4733.Pp 4734Many, many, more. 4735(But less than there were...) 4736