1.\"- 2.\" Copyright (c) 1991, 1993 3.\" The Regents of the University of California. All rights reserved. 4.\" 5.\" This code is derived from software contributed to Berkeley by 6.\" Kenneth Almquist. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 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.\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95 33.\" $FreeBSD$ 34.\" 35.Dd February 24, 2019 36.Dt SH 1 37.Os 38.Sh NAME 39.Nm sh 40.Nd command interpreter (shell) 41.Sh SYNOPSIS 42.Nm 43.Op Fl /+abCEefhIimnPpTuVvx 44.Op Fl /+o Ar longname 45.Oo 46.Ar script 47.Op Ar arg ... 48.Oc 49.Nm 50.Op Fl /+abCEefhIimnPpTuVvx 51.Op Fl /+o Ar longname 52.Fl c Ar string 53.Oo 54.Ar name 55.Op Ar arg ... 56.Oc 57.Nm 58.Op Fl /+abCEefhIimnPpTuVvx 59.Op Fl /+o Ar longname 60.Fl s 61.Op Ar arg ... 62.Sh DESCRIPTION 63The 64.Nm 65utility is the standard command interpreter for the system. 66The current version of 67.Nm 68is close to the 69.St -p1003.1 70specification for the shell. 71It only supports features 72designated by POSIX, 73plus a few Berkeley extensions. 74This man page is not intended to be a tutorial nor a complete 75specification of the shell. 76.Ss Overview 77The shell is a command that reads lines from 78either a file or the terminal, interprets them, and 79generally executes other commands. 80It is the program that is started when a user logs into the system, 81although a user can select a different shell with the 82.Xr chsh 1 83command. 84The shell 85implements a language that has flow control constructs, 86a macro facility that provides a variety of features in 87addition to data storage, along with built-in history and line 88editing capabilities. 89It incorporates many features to 90aid interactive use and has the advantage that the interpretative 91language is common to both interactive and non-interactive 92use (shell scripts). 93That is, commands can be typed directly 94to the running shell or can be put into a file, 95which can be executed directly by the shell. 96.Ss Invocation 97.\" 98.\" XXX This next sentence is incredibly confusing. 99.\" 100If no arguments are present and if the standard input of the shell 101is connected to a terminal 102(or if the 103.Fl i 104option is set), 105the shell is considered an interactive shell. 106An interactive shell 107generally prompts before each command and handles programming 108and command errors differently (as described below). 109When first starting, the shell inspects argument 0, and 110if it begins with a dash 111.Pq Ql - , 112the shell is also considered a login shell. 113This is normally done automatically by the system 114when the user first logs in. 115A login shell first reads commands 116from the files 117.Pa /etc/profile 118and then 119.Pa .profile 120in a user's home directory, 121if they exist. 122If the environment variable 123.Ev ENV 124is set on entry to a shell, or is set in the 125.Pa .profile 126of a login shell, the shell then subjects its value to parameter expansion 127and arithmetic expansion and reads commands from the named file. 128Therefore, a user should place commands that are to be executed only 129at login time in the 130.Pa .profile 131file, and commands that are executed for every shell inside the 132.Ev ENV 133file. 134The user can set the 135.Ev ENV 136variable to some file by placing the following line in the file 137.Pa .profile 138in the home directory, 139substituting for 140.Pa .shrc 141the filename desired: 142.Pp 143.Dl "ENV=$HOME/.shrc; export ENV" 144.Pp 145The first non-option argument specified on the command line 146will be treated as the 147name of a file from which to read commands (a shell script), and 148the remaining arguments are set as the positional parameters 149of the shell 150.Li ( $1 , $2 , 151etc.). 152Otherwise, the shell reads commands 153from its standard input. 154.Pp 155Unlike older versions of 156.Nm 157the 158.Ev ENV 159script is only sourced on invocation of interactive shells. 160This 161closes a well-known, and sometimes easily exploitable security 162hole related to poorly thought out 163.Ev ENV 164scripts. 165.Ss Argument List Processing 166All of the single letter options to 167.Nm 168have a corresponding long name, 169with the exception of 170.Fl c 171and 172.Fl /+o . 173These long names are provided next to the single letter options 174in the descriptions below. 175The long name for an option may be specified as an argument to the 176.Fl /+o 177option of 178.Nm . 179Once the shell is running, 180the long name for an option may be specified as an argument to the 181.Fl /+o 182option of the 183.Ic set 184built-in command 185(described later in the section called 186.Sx Built-in Commands ) . 187Introducing an option with a dash 188.Pq Ql - 189enables the option, 190while using a plus 191.Pq Ql + 192disables the option. 193A 194.Dq Li -- 195or plain 196.Ql - 197will stop option processing and will force the remaining 198words on the command line to be treated as arguments. 199The 200.Fl /+o 201and 202.Fl c 203options do not have long names. 204They take arguments and are described after the single letter options. 205.Bl -tag -width indent 206.It Fl a Li allexport 207Flag variables for export when assignments are made to them. 208.It Fl b Li notify 209Enable asynchronous notification of background job 210completion. 211(UNIMPLEMENTED) 212.It Fl C Li noclobber 213Do not overwrite existing files with 214.Ql > . 215.It Fl E Li emacs 216Enable the built-in 217.Xr emacs 1 218command line editor (disables the 219.Fl V 220option if it has been set; 221set automatically when interactive on terminals). 222.It Fl e Li errexit 223Exit immediately if any untested command fails in non-interactive mode. 224The exit status of a command is considered to be 225explicitly tested if the command is part of the list used to control 226an 227.Ic if , elif , while , 228or 229.Ic until ; 230if the command is the left 231hand operand of an 232.Dq Li && 233or 234.Dq Li || 235operator; or if the command is a pipeline preceded by the 236.Ic !\& 237keyword. 238If a shell function is executed and its exit status is explicitly 239tested, all commands of the function are considered to be tested as 240well. 241.Pp 242It is recommended to check for failures explicitly 243instead of relying on 244.Fl e 245because it tends to behave in unexpected ways, 246particularly in larger scripts. 247.It Fl f Li noglob 248Disable pathname expansion. 249.It Fl h Li trackall 250A do-nothing option for POSIX compliance. 251.It Fl I Li ignoreeof 252Ignore 253.Dv EOF Ap s 254from input when in interactive mode. 255.It Fl i Li interactive 256Force the shell to behave interactively. 257.It Fl m Li monitor 258Turn on job control (set automatically when interactive). 259A new process group is created for each pipeline (called a job). 260It is possible to suspend jobs or to have them run in the foreground or 261in the background. 262In a non-interactive shell, 263this option can be set even if no terminal is available 264and is useful to place processes in separate process groups. 265.It Fl n Li noexec 266If not interactive, read commands but do not 267execute them. 268This is useful for checking the 269syntax of shell scripts. 270.It Fl P Li physical 271Change the default for the 272.Ic cd 273and 274.Ic pwd 275commands from 276.Fl L 277(logical directory layout) 278to 279.Fl P 280(physical directory layout). 281.It Fl p Li privileged 282Turn on privileged mode. 283This mode is enabled on startup 284if either the effective user or group ID is not equal to the 285real user or group ID. 286Turning this mode off sets the 287effective user and group IDs to the real user and group IDs. 288When this mode is enabled for interactive shells, the file 289.Pa /etc/suid_profile 290is sourced instead of 291.Pa ~/.profile 292after 293.Pa /etc/profile 294is sourced, and the contents of the 295.Ev ENV 296variable are ignored. 297.It Fl s Li stdin 298Read commands from standard input (set automatically 299if no file arguments are present). 300This option has 301no effect when set after the shell has already started 302running (i.e., when set with the 303.Ic set 304command). 305.It Fl T Li trapsasync 306When waiting for a child, execute traps immediately. 307If this option is not set, 308traps are executed after the child exits, 309as specified in 310.St -p1003.2 . 311This nonstandard option is useful for putting guarding shells around 312children that block signals. 313The surrounding shell may kill the child 314or it may just return control to the tty and leave the child alone, 315like this: 316.Bd -literal -offset indent 317sh -T -c "trap 'exit 1' 2 ; some-blocking-program" 318.Ed 319.It Fl u Li nounset 320Write a message to standard error when attempting 321to expand a variable, a positional parameter or 322the special parameter 323.Va \&! 324that is not set, and if the 325shell is not interactive, exit immediately. 326.It Fl V Li vi 327Enable the built-in 328.Xr vi 1 329command line editor (disables 330.Fl E 331if it has been set). 332.It Fl v Li verbose 333The shell writes its input to standard error 334as it is read. 335Useful for debugging. 336.It Fl x Li xtrace 337Write each command 338(preceded by the value of the 339.Va PS4 340variable subjected to parameter expansion and arithmetic expansion) 341to standard error before it is executed. 342Useful for debugging. 343.It Li nolog 344Another do-nothing option for POSIX compliance. 345It only has a long name. 346.It Li pipefail 347Change the exit status of a pipeline to the last non-zero exit status of 348any command in the pipeline, if any. 349Since an exit due to 350.Dv SIGPIPE 351counts as a non-zero exit status, 352this option may cause non-zero exit status for successful pipelines 353if a command such as 354.Xr head 1 355in the pipeline terminates with status 0 without reading its 356input completely. 357This option only has a long name. 358.El 359.Pp 360The 361.Fl c 362option causes the commands to be read from the 363.Ar string 364operand instead of from the standard input. 365Keep in mind that this option only accepts a single string as its 366argument, hence multi-word strings must be quoted. 367.Pp 368The 369.Fl /+o 370option takes as its only argument the long name of an option 371to be enabled or disabled. 372For example, the following two invocations of 373.Nm 374both enable the built-in 375.Xr emacs 1 376command line editor: 377.Bd -literal -offset indent 378set -E 379set -o emacs 380.Ed 381.Pp 382If used without an argument, the 383.Fl o 384option displays the current option settings in a human-readable format. 385If 386.Cm +o 387is used without an argument, the current option settings are output 388in a format suitable for re-input into the shell. 389.Ss Lexical Structure 390The shell reads input in terms of lines from a file and breaks 391it up into words at whitespace (blanks and tabs), and at 392certain sequences of 393characters called 394.Dq operators , 395which are special to the shell. 396There are two types of operators: control operators and 397redirection operators (their meaning is discussed later). 398The following is a list of valid operators: 399.Bl -tag -width indent 400.It Control operators: 401.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact 402.It Li & Ta Li && Ta Li \&( Ta Li \&) Ta Li \en 403.It Li ;; Ta Li ;& Ta Li \&; Ta Li \&| Ta Li || 404.El 405.It Redirection operators: 406.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact 407.It Li < Ta Li > Ta Li << Ta Li >> Ta Li <> 408.It Li <& Ta Li >& Ta Li <<- Ta Li >| Ta \& 409.El 410.El 411.Pp 412The character 413.Ql # 414introduces a comment if used at the beginning of a word. 415The word starting with 416.Ql # 417and the rest of the line are ignored. 418.Pp 419ASCII 420.Dv NUL 421characters (character code 0) are not allowed in shell input. 422.Ss Quoting 423Quoting is used to remove the special meaning of certain characters 424or words to the shell, such as operators, whitespace, keywords, 425or alias names. 426.Pp 427There are four types of quoting: matched single quotes, 428dollar-single quotes, 429matched double quotes, and backslash. 430.Bl -tag -width indent 431.It Single Quotes 432Enclosing characters in single quotes preserves the literal 433meaning of all the characters (except single quotes, making 434it impossible to put single-quotes in a single-quoted string). 435.It Dollar-Single Quotes 436Enclosing characters between 437.Li $' 438and 439.Li ' 440preserves the literal meaning of all characters 441except backslashes and single quotes. 442A backslash introduces a C-style escape sequence: 443.Bl -tag -width xUnnnnnnnn 444.It \ea 445Alert (ring the terminal bell) 446.It \eb 447Backspace 448.It \ec Ns Ar c 449The control character denoted by 450.Li ^ Ns Ar c 451in 452.Xr stty 1 . 453If 454.Ar c 455is a backslash, it must be doubled. 456.It \ee 457The ESC character (ASCII 0x1b) 458.It \ef 459Formfeed 460.It \en 461Newline 462.It \er 463Carriage return 464.It \et 465Horizontal tab 466.It \ev 467Vertical tab 468.It \e\e 469Literal backslash 470.It \e\&' 471Literal single-quote 472.It \e\&" 473Literal double-quote 474.It \e Ns Ar nnn 475The byte whose octal value is 476.Ar nnn 477(one to three digits) 478.It \ex Ns Ar nn 479The byte whose hexadecimal value is 480.Ar nn 481(one or more digits only the last two of which are used) 482.It \eu Ns Ar nnnn 483The Unicode code point 484.Ar nnnn 485(four hexadecimal digits) 486.It \eU Ns Ar nnnnnnnn 487The Unicode code point 488.Ar nnnnnnnn 489(eight hexadecimal digits) 490.El 491.Pp 492The sequences for Unicode code points are currently only useful with 493UTF-8 locales. 494They reject code point 0 and UTF-16 surrogates. 495.Pp 496If an escape sequence would produce a byte with value 0, 497that byte and the rest of the string until the matching single-quote 498are ignored. 499.Pp 500Any other string starting with a backslash is an error. 501.It Double Quotes 502Enclosing characters within double quotes preserves the literal 503meaning of all characters except dollar sign 504.Pq Ql $ , 505backquote 506.Pq Ql ` , 507and backslash 508.Pq Ql \e . 509The backslash inside double quotes is historically weird. 510It remains literal unless it precedes the following characters, 511which it serves to quote: 512.Pp 513.Bl -column "XXX" "XXX" "XXX" "XXX" "XXX" -offset center -compact 514.It Li $ Ta Li ` Ta Li \&" Ta Li \e Ta Li \en 515.El 516.It Backslash 517A backslash preserves the literal meaning of the following 518character, with the exception of the newline character 519.Pq Ql \en . 520A backslash preceding a newline is treated as a line continuation. 521.El 522.Ss Keywords 523Keywords or reserved words are words that have special meaning to the 524shell and are recognized at the beginning of a line and 525after a control operator. 526The following are keywords: 527.Bl -column "doneXX" "elifXX" "elseXX" "untilXX" "whileX" -offset center 528.It Li \&! Ta { Ta } Ta Ic case Ta Ic do 529.It Ic done Ta Ic elif Ta Ic else Ta Ic esac Ta Ic fi 530.It Ic for Ta Ic if Ta Ic then Ta Ic until Ta Ic while 531.El 532.Ss Aliases 533An alias is a name and corresponding value set using the 534.Ic alias 535built-in command. 536Wherever the command word of a simple command may occur, 537and after checking for keywords if a keyword may occur, the shell 538checks the word to see if it matches an alias. 539If it does, it replaces it in the input stream with its value. 540For example, if there is an alias called 541.Dq Li lf 542with the value 543.Dq Li "ls -F" , 544then the input 545.Pp 546.Dl "lf foobar" 547.Pp 548would become 549.Pp 550.Dl "ls -F foobar" 551.Pp 552Aliases are also recognized after an alias 553whose value ends with a space or tab. 554For example, if there is also an alias called 555.Dq Li nohup 556with the value 557.Dq Li "nohup " , 558then the input 559.Pp 560.Dl "nohup lf foobar" 561.Pp 562would become 563.Pp 564.Dl "nohup ls -F foobar" 565.Pp 566Aliases provide a convenient way for naive users to 567create shorthands for commands without having to learn how 568to create functions with arguments. 569Using aliases in scripts is discouraged 570because the command that defines them must be executed 571before the code that uses them is parsed. 572This is fragile and not portable. 573.Pp 574An alias name may be escaped in a command line, so that it is not 575replaced by its alias value, by using quoting characters within or 576adjacent to the alias name. 577This is most often done by prefixing 578an alias name with a backslash to execute a function, built-in, or 579normal program with the same name. 580See the 581.Sx Quoting 582subsection. 583.Ss Commands 584The shell interprets the words it reads according to a 585language, the specification of which is outside the scope 586of this man page (refer to the BNF in the 587.St -p1003.2 588document). 589Essentially though, a line is read and if 590the first word of the line (or after a control operator) 591is not a keyword, then the shell has recognized a 592simple command. 593Otherwise, a complex command or some 594other special construct may have been recognized. 595.Ss Simple Commands 596If a simple command has been recognized, the shell performs 597the following actions: 598.Bl -enum 599.It 600Leading words of the form 601.Dq Li name=value 602are stripped off and assigned to the environment of 603the simple command 604(they do not affect expansions). 605Redirection operators and 606their arguments (as described below) are stripped 607off and saved for processing. 608.It 609The remaining words are expanded as described in 610the section called 611.Sx Word Expansions , 612and the first remaining word is considered the command 613name and the command is located. 614The remaining 615words are considered the arguments of the command. 616If no command name resulted, then the 617.Dq Li name=value 618variable assignments recognized in 1) affect the 619current shell. 620.It 621Redirections are performed as described in 622the next section. 623.El 624.Ss Redirections 625Redirections are used to change where a command reads its input 626or sends its output. 627In general, redirections open, close, or 628duplicate an existing reference to a file. 629The overall format 630used for redirection is: 631.Pp 632.D1 Oo Ar n Oc Ar redir-op file 633.Pp 634The 635.Ar redir-op 636is one of the redirection operators mentioned 637previously. 638The following gives some examples of how these 639operators can be used. 640Note that stdin and stdout are commonly used abbreviations 641for standard input and standard output respectively. 642.Bl -tag -width "1234567890XX" -offset indent 643.It Oo Ar n Oc Ns Li > Ar file 644redirect stdout (or file descriptor 645.Ar n ) 646to 647.Ar file 648.It Oo Ar n Oc Ns Li >| Ar file 649same as above, but override the 650.Fl C 651option 652.It Oo Ar n Oc Ns Li >> Ar file 653append stdout (or file descriptor 654.Ar n ) 655to 656.Ar file 657.It Oo Ar n Oc Ns Li < Ar file 658redirect stdin (or file descriptor 659.Ar n ) 660from 661.Ar file 662.It Oo Ar n Oc Ns Li <> Ar file 663redirect stdin (or file descriptor 664.Ar n ) 665to and from 666.Ar file 667.It Oo Ar n1 Oc Ns Li <& Ns Ar n2 668duplicate stdin (or file descriptor 669.Ar n1 ) 670from file descriptor 671.Ar n2 672.It Oo Ar n Oc Ns Li <&- 673close stdin (or file descriptor 674.Ar n ) 675.It Oo Ar n1 Oc Ns Li >& Ns Ar n2 676duplicate stdout (or file descriptor 677.Ar n1 ) 678to file descriptor 679.Ar n2 680.It Oo Ar n Oc Ns Li >&- 681close stdout (or file descriptor 682.Ar n ) 683.El 684.Pp 685The following redirection is often called a 686.Dq here-document . 687.Bd -unfilled -offset indent 688.Oo Ar n Oc Ns Li << Ar delimiter 689.Ar here-doc-text 690.Ar ... 691.Ar delimiter 692.Ed 693.Pp 694All the text on successive lines up to the delimiter is 695saved away and made available to the command on standard 696input, or file descriptor 697.Ar n 698if it is specified. 699If the 700.Ar delimiter 701as specified on the initial line is quoted, then the 702.Ar here-doc-text 703is treated literally, otherwise the text is subjected to 704parameter expansion, command substitution, and arithmetic 705expansion (as described in the section on 706.Sx Word Expansions ) . 707If the operator is 708.Dq Li <<- 709instead of 710.Dq Li << , 711then leading tabs 712in the 713.Ar here-doc-text 714are stripped. 715.Ss Search and Execution 716There are three types of commands: shell functions, 717built-in commands, and normal programs. 718The command is searched for (by name) in that order. 719The three types of commands are all executed in a different way. 720.Pp 721When a shell function is executed, all of the shell positional 722parameters (except 723.Li $0 , 724which remains unchanged) are 725set to the arguments of the shell function. 726The variables which are explicitly placed in the environment of 727the command (by placing assignments to them before the 728function name) are made local to the function and are set 729to the values given. 730Then the command given in the function definition is executed. 731The positional parameters are restored to their original values 732when the command completes. 733This all occurs within the current shell. 734.Pp 735Shell built-in commands are executed internally to the shell, without 736spawning a new process. 737There are two kinds of built-in commands: regular and special. 738Assignments before special builtins persist after they finish 739executing and assignment errors, redirection errors and certain 740operand errors cause a script to be aborted. 741Special builtins cannot be overridden with a function. 742Both regular and special builtins can affect the shell in ways 743normal programs cannot. 744.Pp 745Otherwise, if the command name does not match a function 746or built-in command, the command is searched for as a normal 747program in the file system (as described in the next section). 748When a normal program is executed, the shell runs the program, 749passing the arguments and the environment to the program. 750If the program is not a normal executable file 751(i.e., if it does not begin with the 752.Dq "magic number" 753whose ASCII representation is 754.Dq Li #! , 755resulting in an 756.Er ENOEXEC 757return value from 758.Xr execve 2 ) 759but appears to be a text file, 760the shell will run a new instance of 761.Nm 762to interpret it. 763.Pp 764Note that previous versions of this document 765and the source code itself misleadingly and sporadically 766refer to a shell script without a magic number 767as a 768.Dq "shell procedure" . 769.Ss Path Search 770When locating a command, the shell first looks to see if 771it has a shell function by that name. 772Then it looks for a 773built-in command by that name. 774If a built-in command is not found, 775one of two things happen: 776.Bl -enum 777.It 778Command names containing a slash are simply executed without 779performing any searches. 780.It 781The shell searches each entry in the 782.Va PATH 783variable 784in turn for the command. 785The value of the 786.Va PATH 787variable should be a series of 788entries separated by colons. 789Each entry consists of a 790directory name. 791The current directory 792may be indicated implicitly by an empty directory name, 793or explicitly by a single period. 794.El 795.Ss Command Exit Status 796Each command has an exit status that can influence the behavior 797of other shell commands. 798The paradigm is that a command exits 799with zero for normal or success, and non-zero for failure, 800error, or a false indication. 801The man page for each command 802should indicate the various exit codes and what they mean. 803Additionally, the built-in commands return exit codes, as does 804an executed shell function. 805.Pp 806If a command is terminated by a signal, its exit status is greater than 128. 807The signal name can be found by passing the exit status to 808.Li kill -l . 809.Pp 810If there is no command word, 811the exit status is the exit status of the last command substitution executed, 812or zero if the command does not contain any command substitutions. 813.Ss Complex Commands 814Complex commands are combinations of simple commands 815with control operators or keywords, together creating a larger complex 816command. 817More generally, a command is one of the following: 818.Bl -item -offset indent 819.It 820simple command 821.It 822pipeline 823.It 824list or compound-list 825.It 826compound command 827.It 828function definition 829.El 830.Pp 831Unless otherwise stated, the exit status of a command is 832that of the last simple command executed by the command, 833or zero if no simple command was executed. 834.Ss Pipelines 835A pipeline is a sequence of one or more commands separated 836by the control operator 837.Ql \&| . 838The standard output of all but 839the last command is connected to the standard input 840of the next command. 841The standard output of the last 842command is inherited from the shell, as usual. 843.Pp 844The format for a pipeline is: 845.Pp 846.D1 Oo Li \&! Oc Ar command1 Op Li \&| Ar command2 ... 847.Pp 848The standard output of 849.Ar command1 850is connected to the standard input of 851.Ar command2 . 852The standard input, standard output, or 853both of a command is considered to be assigned by the 854pipeline before any redirection specified by redirection 855operators that are part of the command. 856.Pp 857Note that unlike some other shells, 858.Nm 859executes each process in a pipeline with more than one command 860in a subshell environment and as a child of the 861.Nm 862process. 863.Pp 864If the pipeline is not in the background (discussed later), 865the shell waits for all commands to complete. 866.Pp 867If the keyword 868.Ic !\& 869does not precede the pipeline, the 870exit status is the exit status of the last command specified 871in the pipeline if the 872.Cm pipefail 873option is not set or all commands returned zero, 874or the last non-zero exit status of any command in the pipeline otherwise. 875Otherwise, the exit status is the logical 876NOT of that exit status. 877That is, if 878that status is zero, the exit status is 1; if 879that status is greater than zero, the exit status 880is zero. 881.Pp 882Because pipeline assignment of standard input or standard 883output or both takes place before redirection, it can be 884modified by redirection. 885For example: 886.Pp 887.Dl "command1 2>&1 | command2" 888.Pp 889sends both the standard output and standard error of 890.Ar command1 891to the standard input of 892.Ar command2 . 893.Pp 894A 895.Ql \&; 896or newline terminator causes the preceding 897AND-OR-list 898(described below in the section called 899.Sx Short-Circuit List Operators ) 900to be executed sequentially; 901an 902.Ql & 903causes asynchronous execution of the preceding AND-OR-list. 904.Ss Background Commands (&) 905If a command is terminated by the control operator ampersand 906.Pq Ql & , 907the shell executes the command in a subshell environment (see 908.Sx Grouping Commands Together 909below) and asynchronously; 910the shell does not wait for the command to finish 911before executing the next command. 912.Pp 913The format for running a command in background is: 914.Pp 915.D1 Ar command1 Li & Op Ar command2 Li & Ar ... 916.Pp 917If the shell is not interactive, the standard input of an 918asynchronous command is set to 919.Pa /dev/null . 920.Pp 921The exit status is zero. 922.Ss Lists (Generally Speaking) 923A list is a sequence of zero or more commands separated by 924newlines, semicolons, or ampersands, 925and optionally terminated by one of these three characters. 926The commands in a 927list are executed in the order they are written. 928If command is followed by an ampersand, the shell starts the 929command and immediately proceeds onto the next command; 930otherwise it waits for the command to terminate before 931proceeding to the next one. 932.Ss Short-Circuit List Operators 933.Dq Li && 934and 935.Dq Li || 936are AND-OR list operators. 937.Dq Li && 938executes the first command, and then executes the second command 939if the exit status of the first command is zero. 940.Dq Li || 941is similar, but executes the second command if the exit 942status of the first command is nonzero. 943.Dq Li && 944and 945.Dq Li || 946both have the same priority. 947.Ss Flow-Control Constructs (if, while, for, case) 948The syntax of the 949.Ic if 950command is: 951.Bd -unfilled -offset indent -compact 952.Ic if Ar list 953.Ic then Ar list 954.Oo Ic elif Ar list 955.Ic then Ar list Oc Ar ... 956.Op Ic else Ar list 957.Ic fi 958.Ed 959.Pp 960The exit status is that of selected 961.Ic then 962or 963.Ic else 964list, 965or zero if no list was selected. 966.Pp 967The syntax of the 968.Ic while 969command is: 970.Bd -unfilled -offset indent -compact 971.Ic while Ar list 972.Ic do Ar list 973.Ic done 974.Ed 975.Pp 976The two lists are executed repeatedly while the exit status of the 977first list is zero. 978The 979.Ic until 980command is similar, but has the word 981.Ic until 982in place of 983.Ic while , 984which causes it to 985repeat until the exit status of the first list is zero. 986.Pp 987The exit status is that of the last execution of the second list, 988or zero if it was never executed. 989.Pp 990The syntax of the 991.Ic for 992command is: 993.Bd -unfilled -offset indent -compact 994.Ic for Ar variable Op Ic in Ar word ... 995.Ic do Ar list 996.Ic done 997.Ed 998.Pp 999If 1000.Ic in 1001and the following words are omitted, 1002.Ic in Li \&"$@\&" 1003is used instead. 1004The words are expanded, and then the list is executed 1005repeatedly with the variable set to each word in turn. 1006The 1007.Ic do 1008and 1009.Ic done 1010commands may be replaced with 1011.Ql { 1012and 1013.Ql } . 1014.Pp 1015The syntax of the 1016.Ic break 1017and 1018.Ic continue 1019commands is: 1020.D1 Ic break Op Ar num 1021.D1 Ic continue Op Ar num 1022.Pp 1023The 1024.Ic break 1025command terminates the 1026.Ar num 1027innermost 1028.Ic for 1029or 1030.Ic while 1031loops. 1032The 1033.Ic continue 1034command continues with the next iteration of the innermost loop. 1035These are implemented as special built-in commands. 1036.Pp 1037The syntax of the 1038.Ic case 1039command is: 1040.Bd -unfilled -offset indent -compact 1041.Ic case Ar word Ic in 1042.Ar pattern ) Ar list Li ;; 1043.Ar ... 1044.Ic esac 1045.Ed 1046.Pp 1047The pattern can actually be one or more patterns 1048(see 1049.Sx Shell Patterns 1050described later), 1051separated by 1052.Ql \&| 1053characters. 1054Tilde expansion, parameter expansion, command substitution, 1055arithmetic expansion and quote removal are applied to the word. 1056Then, each pattern is expanded in turn using tilde expansion, 1057parameter expansion, command substitution and arithmetic expansion and 1058the expanded form of the word is checked against it. 1059If a match is found, the corresponding list is executed. 1060If the selected list is terminated by the control operator 1061.Ql ;& 1062instead of 1063.Ql ;; , 1064execution continues with the next list, 1065continuing until a list terminated with 1066.Ql ;; 1067or the end of the 1068.Ic case 1069command. 1070.Ss Grouping Commands Together 1071Commands may be grouped by writing either 1072.Pp 1073.Sm off 1074.Bd -literal -offset -ident 1075.Po Ar list Pc 1076.Ed 1077.Sm on 1078.Pp 1079or 1080.Bd -literal -offset -ident 1081.No { Ar list ; } 1082.Ed 1083.Pp 1084The first form executes the commands in a subshell environment. 1085A subshell environment has its own copy of: 1086.Bl -enum 1087.It 1088The current working directory as set by 1089.Ic cd . 1090.It 1091The file creation mask as set by 1092.Ic umask . 1093.It 1094Resource limits as set by 1095.Ic ulimit . 1096.It 1097References to open files. 1098.It 1099Traps as set by 1100.Ic trap . 1101.It 1102Known jobs. 1103.It 1104Positional parameters and variables. 1105.It 1106Shell options. 1107.It 1108Shell functions. 1109.It 1110Shell aliases. 1111.El 1112.Pp 1113These are copied from the parent shell environment, 1114except that trapped (but not ignored) signals are reset to the default action 1115and known jobs are cleared. 1116Any changes do not affect the parent shell environment. 1117.Pp 1118A subshell environment may be implemented as a child process or differently. 1119If job control is enabled in an interactive shell, 1120commands grouped in parentheses can be suspended and continued as a unit. 1121.Pp 1122For compatibility with other shells, 1123two open parentheses in sequence should be separated by whitespace. 1124.Pp 1125The second form never forks another shell, 1126so it is slightly more efficient. 1127Grouping commands together this way allows the user to 1128redirect their output as though they were one program: 1129.Bd -literal -offset indent 1130{ echo -n "hello"; echo " world"; } > greeting 1131.Ed 1132.Ss Functions 1133The syntax of a function definition is 1134.Pp 1135.D1 Ar name Li \&( \&) Ar command 1136.Pp 1137A function definition is an executable statement; when 1138executed it installs a function named 1139.Ar name 1140and returns an 1141exit status of zero. 1142The 1143.Ar command 1144is normally a list 1145enclosed between 1146.Ql { 1147and 1148.Ql } . 1149.Pp 1150Variables may be declared to be local to a function by 1151using the 1152.Ic local 1153command. 1154This should appear as the first statement of a function, 1155and the syntax is: 1156.Pp 1157.D1 Ic local Oo Ar variable ... Oc Op Fl 1158.Pp 1159The 1160.Ic local 1161command is implemented as a built-in command. 1162The exit status is zero 1163unless the command is not in a function or a variable name is invalid. 1164.Pp 1165When a variable is made local, it inherits the initial 1166value and exported and readonly flags from the variable 1167with the same name in the surrounding scope, if there is 1168one. 1169Otherwise, the variable is initially unset. 1170The shell 1171uses dynamic scoping, so that if the variable 1172.Va x 1173is made local to function 1174.Em f , 1175which then calls function 1176.Em g , 1177references to the variable 1178.Va x 1179made inside 1180.Em g 1181will refer to the variable 1182.Va x 1183declared inside 1184.Em f , 1185not to the global variable named 1186.Va x . 1187.Pp 1188The only special parameter that can be made local is 1189.Ql - . 1190Making 1191.Ql - 1192local causes any shell options 1193(including those that only have long names) 1194that are 1195changed via the 1196.Ic set 1197command inside the function to be 1198restored to their original values when the function 1199returns. 1200.Pp 1201The syntax of the 1202.Ic return 1203command is 1204.Pp 1205.D1 Ic return Op Ar exitstatus 1206.Pp 1207It terminates the current executional scope, returning from the closest 1208nested function or sourced script; 1209if no function or sourced script is being executed, 1210it exits the shell instance. 1211The 1212.Ic return 1213command is implemented as a special built-in command. 1214.Ss Variables and Parameters 1215The shell maintains a set of parameters. 1216A parameter 1217denoted by a name 1218(consisting solely 1219of alphabetics, numerics, and underscores, 1220and starting with an alphabetic or an underscore) 1221is called a variable. 1222When starting up, 1223the shell turns all environment variables with valid names into shell 1224variables. 1225New variables can be set using the form 1226.Pp 1227.D1 Ar name Ns = Ns Ar value 1228.Pp 1229A parameter can also be denoted by a number 1230or a special character as explained below. 1231.Pp 1232Assignments are expanded differently from other words: 1233tilde expansion is also performed after the equals sign and after any colon 1234and usernames are also terminated by colons, 1235and field splitting and pathname expansion are not performed. 1236.Pp 1237This special expansion applies not only to assignments that form a simple 1238command by themselves or precede a command word, 1239but also to words passed to the 1240.Ic export , 1241.Ic local 1242or 1243.Ic readonly 1244built-in commands that have this form. 1245For this, the builtin's name must be literal 1246(not the result of an expansion) 1247and may optionally be preceded by one or more literal instances of 1248.Ic command 1249without options. 1250.Ss Positional Parameters 1251A positional parameter is a parameter denoted by a number greater than zero. 1252The shell sets these initially to the values of its command line 1253arguments that follow the name of the shell script. 1254The 1255.Ic set 1256built-in command can also be used to set or reset them. 1257.Ss Special Parameters 1258Special parameters are parameters denoted by a single special character 1259or the digit zero. 1260They are shown in the following list, exactly as they would appear in input 1261typed by the user or in the source of a shell script. 1262.Bl -hang 1263.It Li $* 1264Expands to the positional parameters, starting from one. 1265When 1266the expansion occurs within a double-quoted string 1267it expands to a single field with the value of each parameter 1268separated by the first character of the 1269.Va IFS 1270variable, 1271or by a space if 1272.Va IFS 1273is unset. 1274.It Li $@ 1275Expands to the positional parameters, starting from one. 1276When 1277the expansion occurs within double-quotes, each positional 1278parameter expands as a separate argument. 1279If there are no positional parameters, the 1280expansion of 1281.Li @ 1282generates zero arguments, even when 1283.Li @ 1284is double-quoted. 1285What this basically means, for example, is 1286if 1287.Li $1 1288is 1289.Dq Li abc 1290and 1291.Li $2 1292is 1293.Dq Li "def ghi" , 1294then 1295.Li \&"$@\&" 1296expands to 1297the two arguments: 1298.Bd -literal -offset indent 1299"abc" "def ghi" 1300.Ed 1301.It Li $# 1302Expands to the number of positional parameters. 1303.It Li $? 1304Expands to the exit status of the most recent pipeline. 1305.It Li $- 1306(hyphen) Expands to the current option flags (the single-letter 1307option names concatenated into a string) as specified on 1308invocation, by the 1309.Ic set 1310built-in command, or implicitly 1311by the shell. 1312.It Li $$ 1313Expands to the process ID of the invoked shell. 1314A subshell 1315retains the same value of 1316.Va $ 1317as its parent. 1318.It Li $! 1319Expands to the process ID of the most recent background 1320command executed from the current shell. 1321For a 1322pipeline, the process ID is that of the last command in the 1323pipeline. 1324If this parameter is referenced, the shell will remember 1325the process ID and its exit status until the 1326.Ic wait 1327built-in command reports completion of the process. 1328.It Li $0 1329(zero) Expands to the name of the shell script if passed on the command line, 1330the 1331.Ar name 1332operand if given (with 1333.Fl c ) 1334or otherwise argument 0 passed to the shell. 1335.El 1336.Ss Special Variables 1337The following variables are set by the shell or 1338have special meaning to it: 1339.Bl -tag -width ".Va HISTSIZE" 1340.It Va CDPATH 1341The search path used with the 1342.Ic cd 1343built-in. 1344.It Va EDITOR 1345The fallback editor used with the 1346.Ic fc 1347built-in. 1348If not set, the default editor is 1349.Xr ed 1 . 1350.It Va FCEDIT 1351The default editor used with the 1352.Ic fc 1353built-in. 1354.It Va HISTSIZE 1355The number of previous commands that are accessible. 1356.It Va HOME 1357The user's home directory, 1358used in tilde expansion and as a default directory for the 1359.Ic cd 1360built-in. 1361.It Va IFS 1362Input Field Separators. 1363This is initialized at startup to 1364.Aq space , 1365.Aq tab , 1366and 1367.Aq newline 1368in that order. 1369This value also applies if 1370.Va IFS 1371is unset, but not if it is set to the empty string. 1372See the 1373.Sx White Space Splitting 1374section for more details. 1375.It Va LINENO 1376The current line number in the script or function. 1377.It Va MAIL 1378The name of a mail file, that will be checked for the arrival of new 1379mail. 1380Overridden by 1381.Va MAILPATH . 1382.It Va MAILPATH 1383A colon 1384.Pq Ql \&: 1385separated list of file names, for the shell to check for incoming 1386mail. 1387This variable overrides the 1388.Va MAIL 1389setting. 1390There is a maximum of 10 mailboxes that can be monitored at once. 1391.It Va OPTIND 1392The index of the next argument to be processed by 1393.Ic getopts . 1394This is initialized to 1 at startup. 1395.It Va PATH 1396The default search path for executables. 1397See the 1398.Sx Path Search 1399section for details. 1400.It Va PPID 1401The parent process ID of the invoked shell. 1402This is set at startup 1403unless this variable is in the environment. 1404A later change of parent process ID is not reflected. 1405A subshell retains the same value of 1406.Va PPID . 1407.It Va PS1 1408The primary prompt string, which defaults to 1409.Dq Li "$ " , 1410unless you are the superuser, in which case it defaults to 1411.Dq Li "# " . 1412.Va PS1 1413may include any of the following formatting sequences, 1414which are replaced by the given information: 1415.Bl -tag -width indent 1416.It Li \eH 1417This system's fully-qualified hostname (FQDN). 1418.It Li \eh 1419This system's hostname. 1420.It Li \eu 1421User name. 1422.It Li \eW 1423The final component of the current working directory. 1424.It Li \ew 1425The entire path of the current working directory. 1426.It Li \e$ 1427Superuser status. 1428.Dq Li "$ " 1429for normal users and 1430.Dq Li "# " 1431for superusers. 1432.It Li \e\e 1433A literal backslash. 1434.El 1435.It Va PS2 1436The secondary prompt string, which defaults to 1437.Dq Li "> " . 1438.Va PS2 1439may include any of the formatting sequences from 1440.Va PS1 . 1441.It Va PS4 1442The prefix for the trace output (if 1443.Fl x 1444is active). 1445The default is 1446.Dq Li "+ " . 1447.El 1448.Ss Word Expansions 1449This clause describes the various expansions that are 1450performed on words. 1451Not all expansions are performed on 1452every word, as explained later. 1453.Pp 1454Tilde expansions, parameter expansions, command substitutions, 1455arithmetic expansions, and quote removals that occur within 1456a single word expand to a single field. 1457It is only field 1458splitting or pathname expansion that can create multiple 1459fields from a single word. 1460The single exception to this rule is 1461the expansion of the special parameter 1462.Va @ 1463within double-quotes, 1464as was described above. 1465.Pp 1466The order of word expansion is: 1467.Bl -enum 1468.It 1469Tilde Expansion, Parameter Expansion, Command Substitution, 1470Arithmetic Expansion (these all occur at the same time). 1471.It 1472Field Splitting is performed on fields generated by step (1) 1473unless the 1474.Va IFS 1475variable is null. 1476.It 1477Pathname Expansion (unless the 1478.Fl f 1479option is in effect). 1480.It 1481Quote Removal. 1482.El 1483.Pp 1484The 1485.Ql $ 1486character is used to introduce parameter expansion, command 1487substitution, or arithmetic expansion. 1488.Ss Tilde Expansion (substituting a user's home directory) 1489A word beginning with an unquoted tilde character 1490.Pq Ql ~ 1491is 1492subjected to tilde expansion. 1493All the characters up to a slash 1494.Pq Ql / 1495or the end of the word are treated as a username 1496and are replaced with the user's home directory. 1497If the 1498username is missing (as in 1499.Pa ~/foobar ) , 1500the tilde is replaced with the value of the 1501.Va HOME 1502variable (the current user's home directory). 1503.Ss Parameter Expansion 1504The format for parameter expansion is as follows: 1505.Pp 1506.D1 Li ${ Ns Ar expression Ns Li } 1507.Pp 1508where 1509.Ar expression 1510consists of all characters until the matching 1511.Ql } . 1512Any 1513.Ql } 1514escaped by a backslash or within a single-quoted or double-quoted 1515string, and characters in 1516embedded arithmetic expansions, command substitutions, and variable 1517expansions, are not examined in determining the matching 1518.Ql } . 1519If the variants with 1520.Ql + , 1521.Ql - , 1522.Ql = 1523or 1524.Ql ?\& 1525occur within a double-quoted string, 1526as an extension there may be unquoted parts 1527(via double-quotes inside the expansion); 1528.Ql } 1529within such parts are also not examined in determining the matching 1530.Ql } . 1531.Pp 1532The simplest form for parameter expansion is: 1533.Pp 1534.D1 Li ${ Ns Ar parameter Ns Li } 1535.Pp 1536The value, if any, of 1537.Ar parameter 1538is substituted. 1539.Pp 1540The parameter name or symbol can be enclosed in braces, which are 1541optional except for positional parameters with more than one digit or 1542when parameter is followed by a character that could be interpreted as 1543part of the name. 1544If a parameter expansion occurs inside double-quotes: 1545.Bl -enum 1546.It 1547Field splitting is not performed on the results of the 1548expansion, with the exception of the special parameter 1549.Va @ . 1550.It 1551Pathname expansion is not performed on the results of the 1552expansion. 1553.El 1554.Pp 1555In addition, a parameter expansion can be modified by using one of the 1556following formats. 1557.Bl -tag -width indent 1558.It Li ${ Ns Ar parameter Ns Li :- Ns Ar word Ns Li } 1559Use Default Values. 1560If 1561.Ar parameter 1562is unset or null, the expansion of 1563.Ar word 1564is substituted; otherwise, the value of 1565.Ar parameter 1566is substituted. 1567.It Li ${ Ns Ar parameter Ns Li := Ns Ar word Ns Li } 1568Assign Default Values. 1569If 1570.Ar parameter 1571is unset or null, the expansion of 1572.Ar word 1573is assigned to 1574.Ar parameter . 1575In all cases, the 1576final value of 1577.Ar parameter 1578is substituted. 1579Quoting inside 1580.Ar word 1581does not prevent field splitting or pathname expansion. 1582Only variables, not positional 1583parameters or special parameters, can be 1584assigned in this way. 1585.It Li ${ Ns Ar parameter Ns Li :? Ns Oo Ar word Oc Ns Li } 1586Indicate Error if Null or Unset. 1587If 1588.Ar parameter 1589is unset or null, the expansion of 1590.Ar word 1591(or a message indicating it is unset if 1592.Ar word 1593is omitted) is written to standard 1594error and the shell exits with a nonzero 1595exit status. 1596Otherwise, the value of 1597.Ar parameter 1598is substituted. 1599An 1600interactive shell need not exit. 1601.It Li ${ Ns Ar parameter Ns Li :+ Ns Ar word Ns Li } 1602Use Alternate Value. 1603If 1604.Ar parameter 1605is unset or null, null is substituted; 1606otherwise, the expansion of 1607.Ar word 1608is substituted. 1609.El 1610.Pp 1611In the parameter expansions shown previously, use of the colon in the 1612format results in a test for a parameter that is unset or null; omission 1613of the colon results in a test for a parameter that is only unset. 1614.Pp 1615The 1616.Ar word 1617inherits the type of quoting 1618(unquoted, double-quoted or here-document) 1619from the surroundings, 1620with the exception that a backslash that quotes a closing brace is removed 1621during quote removal. 1622.Bl -tag -width indent 1623.It Li ${# Ns Ar parameter Ns Li } 1624String Length. 1625The length in characters of 1626the value of 1627.Ar parameter . 1628.El 1629.Pp 1630The following four varieties of parameter expansion provide for substring 1631processing. 1632In each case, pattern matching notation 1633(see 1634.Sx Shell Patterns ) , 1635rather than regular expression notation, 1636is used to evaluate the patterns. 1637If parameter is one of the special parameters 1638.Va * 1639or 1640.Va @ , 1641the result of the expansion is unspecified. 1642Enclosing the full parameter expansion string in double-quotes does not 1643cause the following four varieties of pattern characters to be quoted, 1644whereas quoting characters within the braces has this effect. 1645.Bl -tag -width indent 1646.It Li ${ Ns Ar parameter Ns Li % Ns Ar word Ns Li } 1647Remove Smallest Suffix Pattern. 1648The 1649.Ar word 1650is expanded to produce a pattern. 1651The 1652parameter expansion then results in 1653.Ar parameter , 1654with the smallest portion of the 1655suffix matched by the pattern deleted. 1656.It Li ${ Ns Ar parameter Ns Li %% Ns Ar word Ns Li } 1657Remove Largest Suffix Pattern. 1658The 1659.Ar word 1660is expanded to produce a pattern. 1661The 1662parameter expansion then results in 1663.Ar parameter , 1664with the largest portion of the 1665suffix matched by the pattern deleted. 1666.It Li ${ Ns Ar parameter Ns Li # Ns Ar word Ns Li } 1667Remove Smallest Prefix Pattern. 1668The 1669.Ar word 1670is expanded to produce a pattern. 1671The 1672parameter expansion then results in 1673.Ar parameter , 1674with the smallest portion of the 1675prefix matched by the pattern deleted. 1676.It Li ${ Ns Ar parameter Ns Li ## Ns Ar word Ns Li } 1677Remove Largest Prefix Pattern. 1678The 1679.Ar word 1680is expanded to produce a pattern. 1681The 1682parameter expansion then results in 1683.Ar parameter , 1684with the largest portion of the 1685prefix matched by the pattern deleted. 1686.El 1687.Ss Command Substitution 1688Command substitution allows the output of a command to be substituted in 1689place of the command name itself. 1690Command substitution occurs when 1691the command is enclosed as follows: 1692.Pp 1693.D1 Li $( Ns Ar command Ns Li )\& 1694.Pp 1695or the backquoted version: 1696.Pp 1697.D1 Li ` Ns Ar command Ns Li ` 1698.Pp 1699The shell expands the command substitution by executing command 1700and replacing the command substitution 1701with the standard output of the command, 1702removing sequences of one or more newlines at the end of the substitution. 1703Embedded newlines before the end of the output are not removed; 1704however, during field splitting, they may be translated into spaces 1705depending on the value of 1706.Va IFS 1707and the quoting that is in effect. 1708The command is executed in a subshell environment, 1709except that the built-in commands 1710.Ic jobid , 1711.Ic jobs , 1712and 1713.Ic trap 1714return information about the parent shell environment 1715and 1716.Ic times 1717returns information about the same process 1718if they are the only command in a command substitution. 1719.Pp 1720If a command substitution of the 1721.Li $( 1722form begins with a subshell, 1723the 1724.Li $( 1725and 1726.Li (\& 1727must be separated by whitespace 1728to avoid ambiguity with arithmetic expansion. 1729.Ss Arithmetic Expansion 1730Arithmetic expansion provides a mechanism for evaluating an arithmetic 1731expression and substituting its value. 1732The format for arithmetic expansion is as follows: 1733.Pp 1734.D1 Li $(( Ns Ar expression Ns Li )) 1735.Pp 1736The 1737.Ar expression 1738is treated as if it were in double-quotes, except 1739that a double-quote inside the expression is not treated specially. 1740The 1741shell expands all tokens in the 1742.Ar expression 1743for parameter expansion, 1744command substitution, 1745arithmetic expansion 1746and quote removal. 1747.Pp 1748The allowed expressions are a subset of C expressions, 1749summarized below. 1750.Bl -tag -width "Variables" -offset indent 1751.It Values 1752All values are of type 1753.Ft intmax_t . 1754.It Constants 1755Decimal, octal (starting with 1756.Li 0 ) 1757and hexadecimal (starting with 1758.Li 0x ) 1759integer constants. 1760.It Variables 1761Shell variables can be read and written 1762and contain integer constants. 1763.It Unary operators 1764.Li "! ~ + -" 1765.It Binary operators 1766.Li "* / % + - << >> < <= > >= == != & ^ | && ||"\& 1767.It Assignment operators 1768.Li "= += -= *= /= %= <<= >>= &= ^= |=" 1769.It Conditional operator 1770.Li "? :"\& 1771.El 1772.Pp 1773The result of the expression is substituted in decimal. 1774.Ss White Space Splitting (Field Splitting) 1775In certain contexts, 1776after parameter expansion, command substitution, and 1777arithmetic expansion the shell scans the results of 1778expansions and substitutions that did not occur in double-quotes for 1779field splitting and multiple fields can result. 1780.Pp 1781Characters in 1782.Va IFS 1783that are whitespace 1784.Po 1785.Aq space , 1786.Aq tab , 1787and 1788.Aq newline 1789.Pc 1790are treated differently from other characters in 1791.Va IFS . 1792.Pp 1793Whitespace in 1794.Va IFS 1795at the beginning or end of a word is discarded. 1796.Pp 1797Subsequently, a field is delimited by either 1798.Bl -enum 1799.It 1800a non-whitespace character in 1801.Va IFS 1802with any whitespace in 1803.Va IFS 1804surrounding it, or 1805.It 1806one or more whitespace characters in 1807.Va IFS . 1808.El 1809.Pp 1810If a word ends with a non-whitespace character in 1811.Va IFS , 1812there is no empty field after this character. 1813.Pp 1814If no field is delimited, the word is discarded. 1815In particular, if a word consists solely of an unquoted substitution 1816and the result of the substitution is null, 1817it is removed by field splitting even if 1818.Va IFS 1819is null. 1820.Ss Pathname Expansion (File Name Generation) 1821Unless the 1822.Fl f 1823option is set, 1824file name generation is performed 1825after word splitting is complete. 1826Each word is 1827viewed as a series of patterns, separated by slashes. 1828The 1829process of expansion replaces the word with the names of 1830all existing files whose names can be formed by replacing 1831each pattern with a string that matches the specified pattern. 1832There are two restrictions on this: first, a pattern cannot match 1833a string containing a slash, and second, 1834a pattern cannot match a string starting with a period 1835unless the first character of the pattern is a period. 1836The next section describes the patterns used for 1837Pathname Expansion, 1838the four varieties of parameter expansion for substring processing and the 1839.Ic case 1840command. 1841.Ss Shell Patterns 1842A pattern consists of normal characters, which match themselves, 1843and meta-characters. 1844The meta-characters are 1845.Ql * , 1846.Ql \&? , 1847and 1848.Ql \&[ . 1849These characters lose their special meanings if they are quoted. 1850When command or variable substitution is performed and the dollar sign 1851or back quotes are not double-quoted, the value of the 1852variable or the output of the command is scanned for these 1853characters and they are turned into meta-characters. 1854.Pp 1855An asterisk 1856.Pq Ql * 1857matches any string of characters. 1858A question mark 1859.Pq Ql \&? 1860matches any single character. 1861A left bracket 1862.Pq Ql \&[ 1863introduces a character class. 1864The end of the character class is indicated by a 1865.Ql \&] ; 1866if the 1867.Ql \&] 1868is missing then the 1869.Ql \&[ 1870matches a 1871.Ql \&[ 1872rather than introducing a character class. 1873A character class matches any of the characters between the square brackets. 1874A locale-dependent range of characters may be specified using a minus sign. 1875A named class of characters (see 1876.Xr wctype 3 ) 1877may be specified by surrounding the name with 1878.Ql \&[:\& 1879and 1880.Ql :\&] . 1881For example, 1882.Ql \&[\&[:alpha:\&]\&] 1883is a shell pattern that matches a single letter. 1884The character class may be complemented by making an exclamation point 1885.Pq Ql !\& 1886the first character of the character class. 1887A caret 1888.Pq Ql ^ 1889has the same effect but is non-standard. 1890.Pp 1891To include a 1892.Ql \&] 1893in a character class, make it the first character listed 1894(after the 1895.Ql \&! 1896or 1897.Ql ^ , 1898if any). 1899To include a 1900.Ql - , 1901make it the first or last character listed. 1902.Ss Built-in Commands 1903This section lists the built-in commands. 1904.Bl -tag -width indent 1905.It Ic \&: 1906A null command that returns a 0 (true) exit value. 1907.It Ic \&. Ar file 1908The commands in the specified file are read and executed by the shell. 1909The 1910.Ic return 1911command may be used to return to the 1912.Ic \&. 1913command's caller. 1914If 1915.Ar file 1916contains any 1917.Ql / 1918characters, it is used as is. 1919Otherwise, the shell searches the 1920.Va PATH 1921for the file. 1922If it is not found in the 1923.Va PATH , 1924it is sought in the current working directory. 1925.It Ic \&[ 1926A built-in equivalent of 1927.Xr test 1 . 1928.It Ic alias Oo Ar name Ns Oo = Ns Ar string Oc ... Oc 1929If 1930.Ar name Ns = Ns Ar string 1931is specified, the shell defines the alias 1932.Ar name 1933with value 1934.Ar string . 1935If just 1936.Ar name 1937is specified, the value of the alias 1938.Ar name 1939is printed. 1940With no arguments, the 1941.Ic alias 1942built-in command prints the names and values of all defined aliases 1943(see 1944.Ic unalias ) . 1945Alias values are written with appropriate quoting so that they are 1946suitable for re-input to the shell. 1947Also see the 1948.Sx Aliases 1949subsection. 1950.It Ic bg Op Ar job ... 1951Continue the specified jobs 1952(or the current job if no jobs are given) 1953in the background. 1954.It Ic bind Oo Fl aeklrsv Oc Oo Ar key Oo Ar command Oc Oc 1955List or alter key bindings for the line editor. 1956This command is documented in 1957.Xr editrc 5 . 1958.It Ic break Op Ar num 1959See the 1960.Sx Flow-Control Constructs 1961subsection. 1962.It Ic builtin Ar cmd Op Ar arg ... 1963Execute the specified built-in command, 1964.Ar cmd . 1965This is useful when the user wishes to override a shell function 1966with the same name as a built-in command. 1967.It Ic cd Oo Fl L | P Oc Oo Fl e Oc Op Ar directory 1968.It Ic cd Fl 1969Switch to the specified 1970.Ar directory , 1971to the directory specified in the 1972.Va HOME 1973environment variable if no 1974.Ar directory 1975is specified or 1976to the directory specified in the 1977.Va OLDPWD 1978environment variable if 1979.Ar directory 1980is 1981.Fl . 1982If 1983.Ar directory 1984does not begin with 1985.Pa / , \&. , 1986or 1987.Pa .. , 1988then the directories listed in the 1989.Va CDPATH 1990variable will be 1991searched for the specified 1992.Ar directory . 1993If 1994.Va CDPATH 1995is unset, the current directory is searched. 1996The format of 1997.Va CDPATH 1998is the same as that of 1999.Va PATH . 2000In an interactive shell, 2001the 2002.Ic cd 2003command will print out the name of the directory 2004that it actually switched to 2005if the 2006.Va CDPATH 2007mechanism was used or if 2008.Ar directory 2009was 2010.Fl . 2011.Pp 2012If the 2013.Fl P 2014option is specified, 2015.Pa .. 2016is handled physically and symbolic links are resolved before 2017.Pa .. 2018components are processed. 2019If the 2020.Fl L 2021option is specified, 2022.Pa .. 2023is handled logically. 2024This is the default. 2025.Pp 2026The 2027.Fl e 2028option causes 2029.Ic cd 2030to return exit status 1 if the full pathname of the new directory 2031cannot be determined reliably or at all. 2032Normally this is not considered an error, 2033although a warning is printed. 2034.Pp 2035If changing the directory fails, the exit status is greater than 1. 2036If the directory is changed, the exit status is 0, or also 1 if 2037.Fl e 2038was given. 2039.It Ic chdir 2040A synonym for the 2041.Ic cd 2042built-in command. 2043.It Ic command Oo Fl p Oc Op Ar utility Op Ar argument ... 2044.It Ic command Oo Fl p Oc Fl v Ar utility 2045.It Ic command Oo Fl p Oc Fl V Ar utility 2046The first form of invocation executes the specified 2047.Ar utility , 2048ignoring shell functions in the search. 2049If 2050.Ar utility 2051is a special builtin, 2052it is executed as if it were a regular builtin. 2053.Pp 2054If the 2055.Fl p 2056option is specified, the command search is performed using a 2057default value of 2058.Va PATH 2059that is guaranteed to find all of the standard utilities. 2060.Pp 2061If the 2062.Fl v 2063option is specified, 2064.Ar utility 2065is not executed but a description of its interpretation by the shell is 2066printed. 2067For ordinary commands the output is the path name; for shell built-in 2068commands, shell functions and keywords only the name is written. 2069Aliases are printed as 2070.Dq Ic alias Ar name Ns = Ns Ar value . 2071.Pp 2072The 2073.Fl V 2074option is identical to 2075.Fl v 2076except for the output. 2077It prints 2078.Dq Ar utility Ic is Ar description 2079where 2080.Ar description 2081is either 2082the path name to 2083.Ar utility , 2084a special shell builtin, 2085a shell builtin, 2086a shell function, 2087a shell keyword 2088or 2089an alias for 2090.Ar value . 2091.It Ic continue Op Ar num 2092See the 2093.Sx Flow-Control Constructs 2094subsection. 2095.It Ic echo Oo Fl e | n Oc Op Ar string ... 2096Print a space-separated list of the arguments to the standard output 2097and append a newline character. 2098.Bl -tag -width indent 2099.It Fl n 2100Suppress the output of the trailing newline. 2101.It Fl e 2102Process C-style backslash escape sequences. 2103The 2104.Ic echo 2105command understands the following character escapes: 2106.Bl -tag -width indent 2107.It \ea 2108Alert (ring the terminal bell) 2109.It \eb 2110Backspace 2111.It \ec 2112Suppress the trailing newline (this has the side-effect of truncating the 2113line if it is not the last character) 2114.It \ee 2115The ESC character (ASCII 0x1b) 2116.It \ef 2117Formfeed 2118.It \en 2119Newline 2120.It \er 2121Carriage return 2122.It \et 2123Horizontal tab 2124.It \ev 2125Vertical tab 2126.It \e\e 2127Literal backslash 2128.It \e0nnn 2129(Zero) The character whose octal value is 2130.Ar nnn 2131.El 2132.Pp 2133If 2134.Ar string 2135is not enclosed in quotes then the backslash itself must be escaped 2136with a backslash to protect it from the shell. 2137For example 2138.Bd -literal -offset indent 2139$ echo -e "a\evb" 2140a 2141 b 2142$ echo -e a\e\evb 2143a 2144 b 2145$ echo -e "a\e\eb" 2146a\eb 2147$ echo -e a\e\e\e\eb 2148a\eb 2149.Ed 2150.El 2151.Pp 2152Only one of the 2153.Fl e 2154and 2155.Fl n 2156options may be specified. 2157.It Ic eval Ar string ... 2158Concatenate all the arguments with spaces. 2159Then re-parse and execute the command. 2160.It Ic exec Op Ar command Op arg ... 2161Unless 2162.Ar command 2163is omitted, 2164the shell process is replaced with the specified program 2165(which must be a real program, not a shell built-in command or function). 2166Any redirections on the 2167.Ic exec 2168command are marked as permanent, 2169so that they are not undone when the 2170.Ic exec 2171command finishes. 2172.It Ic exit Op Ar exitstatus 2173Terminate the shell process. 2174If 2175.Ar exitstatus 2176is given 2177it is used as the exit status of the shell. 2178Otherwise, if the shell is executing an 2179.Cm EXIT 2180trap, the exit status of the last command before the trap is used; 2181if the shell is executing a trap for a signal, 2182the shell exits by resending the signal to itself. 2183Otherwise, the exit status of the preceding command is used. 2184The exit status should be an integer between 0 and 255. 2185.It Ic export Ar name ... 2186.It Ic export Op Fl p 2187The specified names are exported so that they will 2188appear in the environment of subsequent commands. 2189The only way to un-export a variable is to 2190.Ic unset 2191it. 2192The shell allows the value of a variable to be set 2193at the same time as it is exported by writing 2194.Pp 2195.D1 Ic export Ar name Ns = Ns Ar value 2196.Pp 2197With no arguments the 2198.Ic export 2199command lists the names 2200of all exported variables. 2201If the 2202.Fl p 2203option is specified, the exported variables are printed as 2204.Dq Ic export Ar name Ns = Ns Ar value 2205lines, suitable for re-input to the shell. 2206.It Ic false 2207A null command that returns a non-zero (false) exit value. 2208.It Ic fc Oo Fl e Ar editor Oc Op Ar first Op Ar last 2209.It Ic fc Fl l Oo Fl nr Oc Op Ar first Op Ar last 2210.It Ic fc Fl s Oo Ar old Ns = Ns Ar new Oc Op Ar first 2211The 2212.Ic fc 2213built-in command lists, or edits and re-executes, 2214commands previously entered to an interactive shell. 2215.Bl -tag -width indent 2216.It Fl e Ar editor 2217Use the editor named by 2218.Ar editor 2219to edit the commands. 2220The 2221.Ar editor 2222string is a command name, 2223subject to search via the 2224.Va PATH 2225variable. 2226The value in the 2227.Va FCEDIT 2228variable is used as a default when 2229.Fl e 2230is not specified. 2231If 2232.Va FCEDIT 2233is null or unset, the value of the 2234.Va EDITOR 2235variable is used. 2236If 2237.Va EDITOR 2238is null or unset, 2239.Xr ed 1 2240is used as the editor. 2241.It Fl l No (ell) 2242List the commands rather than invoking 2243an editor on them. 2244The commands are written in the 2245sequence indicated by the 2246.Ar first 2247and 2248.Ar last 2249operands, as affected by 2250.Fl r , 2251with each command preceded by the command number. 2252.It Fl n 2253Suppress command numbers when listing with 2254.Fl l . 2255.It Fl r 2256Reverse the order of the commands listed 2257(with 2258.Fl l ) 2259or edited 2260(with neither 2261.Fl l 2262nor 2263.Fl s ) . 2264.It Fl s 2265Re-execute the command without invoking an editor. 2266.It Ar first 2267.It Ar last 2268Select the commands to list or edit. 2269The number of previous commands that can be accessed 2270are determined by the value of the 2271.Va HISTSIZE 2272variable. 2273The value of 2274.Ar first 2275or 2276.Ar last 2277or both are one of the following: 2278.Bl -tag -width indent 2279.It Oo Cm + Oc Ns Ar num 2280A positive number representing a command number; 2281command numbers can be displayed with the 2282.Fl l 2283option. 2284.It Fl Ar num 2285A negative decimal number representing the 2286command that was executed 2287.Ar num 2288of 2289commands previously. 2290For example, \-1 is the immediately previous command. 2291.It Ar string 2292A string indicating the most recently entered command 2293that begins with that string. 2294If the 2295.Ar old Ns = Ns Ar new 2296operand is not also specified with 2297.Fl s , 2298the string form of the first operand cannot contain an embedded equal sign. 2299.El 2300.El 2301.Pp 2302The following variables affect the execution of 2303.Ic fc : 2304.Bl -tag -width ".Va HISTSIZE" 2305.It Va FCEDIT 2306Name of the editor to use for history editing. 2307.It Va HISTSIZE 2308The number of previous commands that are accessible. 2309.El 2310.It Ic fg Op Ar job 2311Move the specified 2312.Ar job 2313or the current job to the foreground. 2314.It Ic getopts Ar optstring var 2315The POSIX 2316.Ic getopts 2317command. 2318The 2319.Ic getopts 2320command deprecates the older 2321.Xr getopt 1 2322command. 2323The first argument should be a series of letters, each possibly 2324followed by a colon which indicates that the option takes an argument. 2325The specified variable is set to the parsed option. 2326The index of 2327the next argument is placed into the shell variable 2328.Va OPTIND . 2329If an option takes an argument, it is placed into the shell variable 2330.Va OPTARG . 2331If an invalid option is encountered, 2332.Ar var 2333is set to 2334.Ql \&? . 2335It returns a false value (1) when it encounters the end of the options. 2336A new set of arguments may be parsed by assigning 2337.Li OPTIND=1 . 2338.It Ic hash Oo Fl rv Oc Op Ar command ... 2339The shell maintains a hash table which remembers the locations of commands. 2340With no arguments whatsoever, the 2341.Ic hash 2342command prints out the contents of this table. 2343.Pp 2344With arguments, the 2345.Ic hash 2346command removes each specified 2347.Ar command 2348from the hash table (unless they are functions) and then locates it. 2349With the 2350.Fl v 2351option, 2352.Ic hash 2353prints the locations of the commands as it finds them. 2354The 2355.Fl r 2356option causes the 2357.Ic hash 2358command to delete all the entries in the hash table except for functions. 2359.It Ic jobid Op Ar job 2360Print the process IDs of the processes in the specified 2361.Ar job . 2362If the 2363.Ar job 2364argument is omitted, use the current job. 2365.It Ic jobs Oo Fl lps Oc Op Ar job ... 2366Print information about the specified jobs, or all jobs if no 2367.Ar job 2368argument is given. 2369The information printed includes job ID, status and command name. 2370.Pp 2371If the 2372.Fl l 2373option is specified, the PID of each job is also printed. 2374If the 2375.Fl p 2376option is specified, only the process IDs for the process group leaders 2377are printed, one per line. 2378If the 2379.Fl s 2380option is specified, only the PIDs of the job commands are printed, one per 2381line. 2382.It Ic kill 2383A built-in equivalent of 2384.Xr kill 1 2385that additionally supports sending signals to jobs. 2386.It Ic local Oo Ar variable ... Oc Op Fl 2387See the 2388.Sx Functions 2389subsection. 2390.It Ic printf 2391A built-in equivalent of 2392.Xr printf 1 . 2393.It Ic pwd Op Fl L | P 2394Print the path of the current directory. 2395The built-in command may 2396differ from the program of the same name because the 2397built-in command remembers what the current directory 2398is rather than recomputing it each time. 2399This makes 2400it faster. 2401However, if the current directory is 2402renamed, 2403the built-in version of 2404.Xr pwd 1 2405will continue to print the old name for the directory. 2406.Pp 2407If the 2408.Fl P 2409option is specified, symbolic links are resolved. 2410If the 2411.Fl L 2412option is specified, the shell's notion of the current directory 2413is printed (symbolic links are not resolved). 2414This is the default. 2415.It Ic read Oo Fl p Ar prompt Oc Oo 2416.Fl t Ar timeout Oc Oo Fl er Oc Ar variable ... 2417The 2418.Ar prompt 2419is printed if the 2420.Fl p 2421option is specified 2422and the standard input is a terminal. 2423Then a line is 2424read from the standard input. 2425The trailing newline 2426is deleted from the line and the line is split as 2427described in the section on 2428.Sx White Space Splitting (Field Splitting)\& 2429above, and 2430the pieces are assigned to the variables in order. 2431If there are more pieces than variables, the remaining 2432pieces (along with the characters in 2433.Va IFS 2434that separated them) 2435are assigned to the last variable. 2436If there are more variables than pieces, the remaining 2437variables are assigned the null string. 2438.Pp 2439Backslashes are treated specially, unless the 2440.Fl r 2441option is 2442specified. 2443If a backslash is followed by 2444a newline, the backslash and the newline will be 2445deleted. 2446If a backslash is followed by any other 2447character, the backslash will be deleted and the following 2448character will be treated as though it were not in 2449.Va IFS , 2450even if it is. 2451.Pp 2452If the 2453.Fl t 2454option is specified and the 2455.Ar timeout 2456elapses before a complete line of input is supplied, 2457the 2458.Ic read 2459command will return an exit status as if terminated by 2460.Dv SIGALRM 2461without assigning any values. 2462The 2463.Ar timeout 2464value may optionally be followed by one of 2465.Ql s , 2466.Ql m 2467or 2468.Ql h 2469to explicitly specify seconds, minutes or hours. 2470If none is supplied, 2471.Ql s 2472is assumed. 2473.Pp 2474The 2475.Fl e 2476option exists only for backward compatibility with older scripts. 2477.Pp 2478The exit status is 0 on success, 1 on end of file, 2479between 2 and 128 if an error occurs 2480and greater than 128 if a trapped signal interrupts 2481.Ic read . 2482.It Ic readonly Oo Fl p Oc Op Ar name ... 2483Each specified 2484.Ar name 2485is marked as read only, 2486so that it cannot be subsequently modified or unset. 2487The shell allows the value of a variable to be set 2488at the same time as it is marked read only 2489by using the following form: 2490.Pp 2491.D1 Ic readonly Ar name Ns = Ns Ar value 2492.Pp 2493With no arguments the 2494.Ic readonly 2495command lists the names of all read only variables. 2496If the 2497.Fl p 2498option is specified, the read-only variables are printed as 2499.Dq Ic readonly Ar name Ns = Ns Ar value 2500lines, suitable for re-input to the shell. 2501.It Ic return Op Ar exitstatus 2502See the 2503.Sx Functions 2504subsection. 2505.It Ic set Oo Fl /+abCEefIimnpTuVvx Oc Oo Fl /+o Ar longname 2506.Oc Op Fl - Ar arg ... 2507The 2508.Ic set 2509command performs three different functions: 2510.Bl -item 2511.It 2512With no arguments, it lists the values of all shell variables. 2513.It 2514If options are given, 2515either in short form or using the long 2516.Dq Fl /+o Ar longname 2517form, 2518it sets or clears the specified options as described in the section called 2519.Sx Argument List Processing . 2520.It 2521If the 2522.Dq Fl - 2523option is specified, 2524.Ic set 2525will replace the shell's positional parameters with the subsequent 2526arguments. 2527If no arguments follow the 2528.Dq Fl - 2529option, 2530all the positional parameters will be cleared, 2531which is equivalent to executing the command 2532.Dq Li "shift $#" . 2533The 2534.Dq Fl - 2535flag may be omitted when specifying arguments to be used 2536as positional replacement parameters. 2537This is not recommended, 2538because the first argument may begin with a dash 2539.Pq Ql - 2540or a plus 2541.Pq Ql + , 2542which the 2543.Ic set 2544command will interpret as a request to enable or disable options. 2545.El 2546.It Ic setvar Ar variable value 2547Assigns the specified 2548.Ar value 2549to the specified 2550.Ar variable . 2551The 2552.Ic setvar 2553command is intended to be used in functions that 2554assign values to variables whose names are passed as parameters. 2555In general it is better to write 2556.Dq Ar variable Ns = Ns Ar value 2557rather than using 2558.Ic setvar . 2559.It Ic shift Op Ar n 2560Shift the positional parameters 2561.Ar n 2562times, or once if 2563.Ar n 2564is not specified. 2565A shift sets the value of 2566.Li $1 2567to the value of 2568.Li $2 , 2569the value of 2570.Li $2 2571to the value of 2572.Li $3 , 2573and so on, 2574decreasing the value of 2575.Li $# 2576by one. 2577For portability, shifting if there are zero positional parameters 2578should be avoided, since the shell may abort. 2579.It Ic test 2580A built-in equivalent of 2581.Xr test 1 . 2582.It Ic times 2583Print the amount of time spent executing the shell process and its children. 2584The first output line shows the user and system times for the shell process 2585itself, the second one contains the user and system times for the 2586children. 2587.It Ic trap Oo Ar action Oc Ar signal ... 2588.It Ic trap Fl l 2589Cause the shell to parse and execute 2590.Ar action 2591when any specified 2592.Ar signal 2593is received. 2594The signals are specified by name or number. 2595In addition, the pseudo-signal 2596.Cm EXIT 2597may be used to specify an 2598.Ar action 2599that is performed when the shell terminates. 2600The 2601.Ar action 2602may be an empty string or a dash 2603.Pq Ql - ; 2604the former causes the specified signal to be ignored 2605and the latter causes the default action to be taken. 2606Omitting the 2607.Ar action 2608and using only signal numbers is another way to request the default action. 2609In a subshell or utility environment, 2610the shell resets trapped (but not ignored) signals to the default action. 2611The 2612.Ic trap 2613command has no effect on signals that were ignored on entry to the shell. 2614.Pp 2615Option 2616.Fl l 2617causes the 2618.Ic trap 2619command to display a list of valid signal names. 2620.It Ic true 2621A null command that returns a 0 (true) exit value. 2622.It Ic type Op Ar name ... 2623Interpret each 2624.Ar name 2625as a command and print the resolution of the command search. 2626Possible resolutions are: 2627shell keyword, alias, special shell builtin, shell builtin, command, 2628tracked alias 2629and not found. 2630For aliases the alias expansion is printed; 2631for commands and tracked aliases 2632the complete pathname of the command is printed. 2633.It Ic ulimit Oo Fl HSabcdfklmnopstuvw Oc Op Ar limit 2634Set or display resource limits (see 2635.Xr getrlimit 2 ) . 2636If 2637.Ar limit 2638is specified, the named resource will be set; 2639otherwise the current resource value will be displayed. 2640.Pp 2641If 2642.Fl H 2643is specified, the hard limits will be set or displayed. 2644While everybody is allowed to reduce a hard limit, 2645only the superuser can increase it. 2646The 2647.Fl S 2648option 2649specifies the soft limits instead. 2650When displaying limits, 2651only one of 2652.Fl S 2653or 2654.Fl H 2655can be given. 2656The default is to display the soft limits, 2657and to set both the hard and the soft limits. 2658.Pp 2659Option 2660.Fl a 2661causes the 2662.Ic ulimit 2663command to display all resources. 2664The parameter 2665.Ar limit 2666is not acceptable in this mode. 2667.Pp 2668The remaining options specify which resource value is to be 2669displayed or modified. 2670They are mutually exclusive. 2671.Bl -tag -width indent 2672.It Fl b Ar sbsize 2673The maximum size of socket buffer usage, in bytes. 2674.It Fl c Ar coredumpsize 2675The maximal size of core dump files, in 512-byte blocks. 2676Setting 2677.Ar coredumpsize 2678to 0 prevents core dump files from being created. 2679.It Fl d Ar datasize 2680The maximal size of the data segment of a process, in kilobytes. 2681.It Fl f Ar filesize 2682The maximal size of a file, in 512-byte blocks. 2683.It Fl k Ar kqueues 2684The maximal number of kqueues 2685(see 2686.Xr kqueue 2 ) 2687for this user ID. 2688.It Fl l Ar lockedmem 2689The maximal size of memory that can be locked by a process, in 2690kilobytes. 2691.It Fl m Ar memoryuse 2692The maximal resident set size of a process, in kilobytes. 2693.It Fl n Ar nofiles 2694The maximal number of descriptors that could be opened by a process. 2695.It Fl o Ar umtxp 2696The maximal number of process-shared locks 2697(see 2698.Xr pthread 3 ) 2699for this user ID. 2700.It Fl p Ar pseudoterminals 2701The maximal number of pseudo-terminals for this user ID. 2702.It Fl s Ar stacksize 2703The maximal size of the stack segment, in kilobytes. 2704.It Fl t Ar time 2705The maximal amount of CPU time to be used by each process, in seconds. 2706.It Fl u Ar userproc 2707The maximal number of simultaneous processes for this user ID. 2708.It Fl v Ar virtualmem 2709The maximal virtual size of a process, in kilobytes. 2710.It Fl w Ar swapuse 2711The maximum amount of swap space reserved or used for this user ID, 2712in kilobytes. 2713.El 2714.It Ic umask Oo Fl S Oc Op Ar mask 2715Set the file creation mask (see 2716.Xr umask 2 ) 2717to the octal or symbolic (see 2718.Xr chmod 1 ) 2719value specified by 2720.Ar mask . 2721If the argument is omitted, the current mask value is printed. 2722If the 2723.Fl S 2724option is specified, the output is symbolic, otherwise the output is octal. 2725.It Ic unalias Oo Fl a Oc Op Ar name ... 2726The specified alias names are removed. 2727If 2728.Fl a 2729is specified, all aliases are removed. 2730.It Ic unset Oo Fl fv Oc Ar name ... 2731The specified variables or functions are unset and unexported. 2732If the 2733.Fl v 2734option is specified or no options are given, the 2735.Ar name 2736arguments are treated as variable names. 2737If the 2738.Fl f 2739option is specified, the 2740.Ar name 2741arguments are treated as function names. 2742.It Ic wait Op Ar job ... 2743Wait for each specified 2744.Ar job 2745to complete and return the exit status of the last process in the 2746last specified 2747.Ar job . 2748If any 2749.Ar job 2750specified is unknown to the shell, it is treated as if it 2751were a known job that exited with exit status 127. 2752If no operands are given, wait for all jobs to complete 2753and return an exit status of zero. 2754.El 2755.Ss Command Line Editing 2756When 2757.Nm 2758is being used interactively from a terminal, the current command 2759and the command history 2760(see 2761.Ic fc 2762in 2763.Sx Built-in Commands ) 2764can be edited using 2765.Nm vi Ns -mode 2766command line editing. 2767This mode uses commands similar 2768to a subset of those described in the 2769.Xr vi 1 2770man page. 2771The command 2772.Dq Li "set -o vi" 2773(or 2774.Dq Li "set -V" ) 2775enables 2776.Nm vi Ns -mode 2777editing and places 2778.Nm 2779into 2780.Nm vi 2781insert mode. 2782With 2783.Nm vi Ns -mode 2784enabled, 2785.Nm 2786can be switched between insert mode and command mode by typing 2787.Aq ESC . 2788Hitting 2789.Aq return 2790while in command mode will pass the line to the shell. 2791.Pp 2792Similarly, the 2793.Dq Li "set -o emacs" 2794(or 2795.Dq Li "set -E" ) 2796command can be used to enable a subset of 2797.Nm emacs Ns -style 2798command line editing features. 2799.Sh ENVIRONMENT 2800The following environment variables affect the execution of 2801.Nm : 2802.Bl -tag -width ".Ev LANGXXXXXX" 2803.It Ev ENV 2804Initialization file for interactive shells. 2805.It Ev LANG , Ev LC_* 2806Locale settings. 2807These are inherited by children of the shell, 2808and is used in a limited manner by the shell itself. 2809.It Ev OLDPWD 2810The previous current directory. 2811This is used and updated by 2812.Ic cd . 2813.It Ev PWD 2814An absolute pathname for the current directory, 2815possibly containing symbolic links. 2816This is used and updated by the shell. 2817.It Ev TERM 2818The default terminal setting for the shell. 2819This is inherited by children of the shell, and is used in the history 2820editing modes. 2821.El 2822.Pp 2823Additionally, environment variables are turned into shell variables 2824at startup, 2825which may affect the shell as described under 2826.Sx Special Variables . 2827.Sh FILES 2828.Bl -tag -width "/etc/suid_profileXX" -compact 2829.It Pa ~/.profile 2830User's login profile. 2831.It Pa /etc/profile 2832System login profile. 2833.It Pa /etc/shells 2834Shell database. 2835.It Pa /etc/suid_profile 2836Privileged shell profile. 2837.El 2838.Sh EXIT STATUS 2839If the 2840.Ar script 2841cannot be found, the exit status will be 127; 2842if it cannot be opened for another reason, the exit status will be 126. 2843Other errors that are detected by the shell, such as a syntax error, will 2844cause the shell to exit with a non-zero exit status. 2845If the shell is not an interactive shell, the execution of the shell 2846file will be aborted. 2847Otherwise the shell will return the exit status of the last command 2848executed, or if the 2849.Ic exit 2850builtin is used with a numeric argument, it 2851will return the argument. 2852.Sh SEE ALSO 2853.Xr builtin 1 , 2854.Xr chsh 1 , 2855.Xr echo 1 , 2856.Xr ed 1 , 2857.Xr emacs 1 , 2858.Xr kill 1 , 2859.Xr printf 1 , 2860.Xr pwd 1 , 2861.Xr test 1 , 2862.Xr vi 1 , 2863.Xr execve 2 , 2864.Xr getrlimit 2 , 2865.Xr umask 2 , 2866.Xr wctype 3 , 2867.Xr editrc 5 , 2868.Xr shells 5 2869.Sh HISTORY 2870A 2871.Nm 2872command, the Thompson shell, appeared in 2873.At v1 . 2874It was superseded in 2875.At v7 2876by the Bourne shell, which inherited the name 2877.Nm . 2878.Pp 2879This version of 2880.Nm 2881was rewritten in 1989 under the 2882.Bx 2883license after the Bourne shell from 2884.At V.4 . 2885.Sh AUTHORS 2886This version of 2887.Nm 2888was originally written by 2889.An Kenneth Almquist . 2890.Sh BUGS 2891The 2892.Nm 2893utility does not recognize multibyte characters other than UTF-8. 2894Splitting using 2895.Va IFS 2896does not recognize multibyte characters. 2897