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: head/bin/sh/sh.1 362957 2020-07-06 10:05:35Z 0mp $ 34.\" 35.Dd September 27, 2020 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.Sm off 1073.Bd -literal -offset -ident 1074.Po Ar list Pc 1075.Ed 1076.Sm on 1077.Pp 1078or 1079.Bd -literal -offset -ident 1080.No { Ar list ; } 1081.Ed 1082.Pp 1083The first form executes the commands in a subshell environment. 1084A subshell environment has its own copy of: 1085.Bl -enum 1086.It 1087The current working directory as set by 1088.Ic cd . 1089.It 1090The file creation mask as set by 1091.Ic umask . 1092.It 1093Resource limits as set by 1094.Ic ulimit . 1095.It 1096References to open files. 1097.It 1098Traps as set by 1099.Ic trap . 1100.It 1101Known jobs. 1102.It 1103Positional parameters and variables. 1104.It 1105Shell options. 1106.It 1107Shell functions. 1108.It 1109Shell aliases. 1110.El 1111.Pp 1112These are copied from the parent shell environment, 1113except that trapped (but not ignored) signals are reset to the default action 1114and known jobs are cleared. 1115Any changes do not affect the parent shell environment. 1116.Pp 1117A subshell environment may be implemented as a child process or differently. 1118If job control is enabled in an interactive shell, 1119commands grouped in parentheses can be suspended and continued as a unit. 1120.Pp 1121For compatibility with other shells, 1122two open parentheses in sequence should be separated by whitespace. 1123.Pp 1124The second form never forks another shell, 1125so it is slightly more efficient. 1126Grouping commands together this way allows the user to 1127redirect their output as though they were one program: 1128.Bd -literal -offset indent 1129{ echo -n "hello"; echo " world"; } > greeting 1130.Ed 1131.Ss Functions 1132The syntax of a function definition is 1133.Pp 1134.D1 Ar name Li \&( \&) Ar command 1135.Pp 1136A function definition is an executable statement; when 1137executed it installs a function named 1138.Ar name 1139and returns an 1140exit status of zero. 1141The 1142.Ar command 1143is normally a list 1144enclosed between 1145.Ql { 1146and 1147.Ql } . 1148.Pp 1149Variables may be declared to be local to a function by 1150using the 1151.Ic local 1152command. 1153This should appear as the first statement of a function, 1154and the syntax is: 1155.Pp 1156.D1 Ic local Oo Ar variable ... Oc Op Fl 1157.Pp 1158The 1159.Ic local 1160command is implemented as a built-in command. 1161The exit status is zero 1162unless the command is not in a function or a variable name is invalid. 1163.Pp 1164When a variable is made local, it inherits the initial 1165value and exported and readonly flags from the variable 1166with the same name in the surrounding scope, if there is 1167one. 1168Otherwise, the variable is initially unset. 1169The shell 1170uses dynamic scoping, so that if the variable 1171.Va x 1172is made local to function 1173.Em f , 1174which then calls function 1175.Em g , 1176references to the variable 1177.Va x 1178made inside 1179.Em g 1180will refer to the variable 1181.Va x 1182declared inside 1183.Em f , 1184not to the global variable named 1185.Va x . 1186.Pp 1187The only special parameter that can be made local is 1188.Ql - . 1189Making 1190.Ql - 1191local causes any shell options 1192(including those that only have long names) 1193that are 1194changed via the 1195.Ic set 1196command inside the function to be 1197restored to their original values when the function 1198returns. 1199.Pp 1200The syntax of the 1201.Ic return 1202command is 1203.Pp 1204.D1 Ic return Op Ar exitstatus 1205.Pp 1206It terminates the current executional scope, returning from the closest 1207nested function or sourced script; 1208if no function or sourced script is being executed, 1209it exits the shell instance. 1210The 1211.Ic return 1212command is implemented as a special built-in command. 1213.Ss Variables and Parameters 1214The shell maintains a set of parameters. 1215A parameter 1216denoted by a name 1217(consisting solely 1218of alphabetics, numerics, and underscores, 1219and starting with an alphabetic or an underscore) 1220is called a variable. 1221When starting up, 1222the shell turns all environment variables with valid names into shell 1223variables. 1224New variables can be set using the form 1225.Pp 1226.D1 Ar name Ns = Ns Ar value 1227.Pp 1228A parameter can also be denoted by a number 1229or a special character as explained below. 1230.Pp 1231Assignments are expanded differently from other words: 1232tilde expansion is also performed after the equals sign and after any colon 1233and usernames are also terminated by colons, 1234and field splitting and pathname expansion are not performed. 1235.Pp 1236This special expansion applies not only to assignments that form a simple 1237command by themselves or precede a command word, 1238but also to words passed to the 1239.Ic export , 1240.Ic local 1241or 1242.Ic readonly 1243built-in commands that have this form. 1244For this, the builtin's name must be literal 1245(not the result of an expansion) 1246and may optionally be preceded by one or more literal instances of 1247.Ic command 1248without options. 1249.Ss Positional Parameters 1250A positional parameter is a parameter denoted by a number greater than zero. 1251The shell sets these initially to the values of its command line 1252arguments that follow the name of the shell script. 1253The 1254.Ic set 1255built-in command can also be used to set or reset them. 1256.Ss Special Parameters 1257Special parameters are parameters denoted by a single special character 1258or the digit zero. 1259They are shown in the following list, exactly as they would appear in input 1260typed by the user or in the source of a shell script. 1261.Bl -hang 1262.It Li $* 1263Expands to the positional parameters, starting from one. 1264When 1265the expansion occurs within a double-quoted string 1266it expands to a single field with the value of each parameter 1267separated by the first character of the 1268.Va IFS 1269variable, 1270or by a space if 1271.Va IFS 1272is unset. 1273.It Li $@ 1274Expands to the positional parameters, starting from one. 1275When 1276the expansion occurs within double-quotes, each positional 1277parameter expands as a separate argument. 1278If there are no positional parameters, the 1279expansion of 1280.Li @ 1281generates zero arguments, even when 1282.Li @ 1283is double-quoted. 1284What this basically means, for example, is 1285if 1286.Li $1 1287is 1288.Dq Li abc 1289and 1290.Li $2 1291is 1292.Dq Li "def ghi" , 1293then 1294.Li \&"$@\&" 1295expands to 1296the two arguments: 1297.Bd -literal -offset indent 1298"abc" "def ghi" 1299.Ed 1300.It Li $# 1301Expands to the number of positional parameters. 1302.It Li $? 1303Expands to the exit status of the most recent pipeline. 1304.It Li $- 1305(hyphen) Expands to the current option flags (the single-letter 1306option names concatenated into a string) as specified on 1307invocation, by the 1308.Ic set 1309built-in command, or implicitly 1310by the shell. 1311.It Li $$ 1312Expands to the process ID of the invoked shell. 1313A subshell 1314retains the same value of 1315.Va $ 1316as its parent. 1317.It Li $! 1318Expands to the process ID of the most recent background 1319command executed from the current shell. 1320For a 1321pipeline, the process ID is that of the last command in the 1322pipeline. 1323If this parameter is referenced, the shell will remember 1324the process ID and its exit status until the 1325.Ic wait 1326built-in command reports completion of the process. 1327.It Li $0 1328(zero) Expands to the name of the shell script if passed on the command line, 1329the 1330.Ar name 1331operand if given (with 1332.Fl c ) 1333or otherwise argument 0 passed to the shell. 1334.El 1335.Ss Special Variables 1336The following variables are set by the shell or 1337have special meaning to it: 1338.Bl -tag -width ".Va HISTSIZE" 1339.It Va CDPATH 1340The search path used with the 1341.Ic cd 1342built-in. 1343.It Va EDITOR 1344The fallback editor used with the 1345.Ic fc 1346built-in. 1347If not set, the default editor is 1348.Xr ed 1 . 1349.It Va FCEDIT 1350The default editor used with the 1351.Ic fc 1352built-in. 1353.It Va HISTSIZE 1354The number of previous commands that are accessible. 1355.It Va HOME 1356The user's home directory, 1357used in tilde expansion and as a default directory for the 1358.Ic cd 1359built-in. 1360.It Va IFS 1361Input Field Separators. 1362This is initialized at startup to 1363.Aq space , 1364.Aq tab , 1365and 1366.Aq newline 1367in that order. 1368This value also applies if 1369.Va IFS 1370is unset, but not if it is set to the empty string. 1371See the 1372.Sx White Space Splitting 1373section for more details. 1374.It Va LINENO 1375The current line number in the script or function. 1376.It Va MAIL 1377The name of a mail file, that will be checked for the arrival of new 1378mail. 1379Overridden by 1380.Va MAILPATH . 1381.It Va MAILPATH 1382A colon 1383.Pq Ql \&: 1384separated list of file names, for the shell to check for incoming 1385mail. 1386This variable overrides the 1387.Va MAIL 1388setting. 1389There is a maximum of 10 mailboxes that can be monitored at once. 1390.It Va OPTIND 1391The index of the next argument to be processed by 1392.Ic getopts . 1393This is initialized to 1 at startup. 1394.It Va PATH 1395The default search path for executables. 1396See the 1397.Sx Path Search 1398section for details. 1399.It Va PPID 1400The parent process ID of the invoked shell. 1401This is set at startup 1402unless this variable is in the environment. 1403A later change of parent process ID is not reflected. 1404A subshell retains the same value of 1405.Va PPID . 1406.It Va PS1 1407The primary prompt string, which defaults to 1408.Dq Li "$ " , 1409unless you are the superuser, in which case it defaults to 1410.Dq Li "# " . 1411.Va PS1 1412may include any of the following formatting sequences, 1413which are replaced by the given information: 1414.Bl -tag -width indent 1415.It Li \eH 1416This system's fully-qualified hostname (FQDN). 1417.It Li \eh 1418This system's hostname. 1419.It Li \eu 1420User name. 1421.It Li \eW 1422The final component of the current working directory. 1423.It Li \ew 1424The entire path of the current working directory. 1425.It Li \e$ 1426Superuser status. 1427.Dq Li "$" 1428for normal users and 1429.Dq Li "#" 1430for superusers. 1431.It Li \e\e 1432A literal backslash. 1433.El 1434.It Va PS2 1435The secondary prompt string, which defaults to 1436.Dq Li "> " . 1437.Va PS2 1438may include any of the formatting sequences from 1439.Va PS1 . 1440.It Va PS4 1441The prefix for the trace output (if 1442.Fl x 1443is active). 1444The default is 1445.Dq Li "+ " . 1446.El 1447.Ss Word Expansions 1448This clause describes the various expansions that are 1449performed on words. 1450Not all expansions are performed on 1451every word, as explained later. 1452.Pp 1453Tilde expansions, parameter expansions, command substitutions, 1454arithmetic expansions, and quote removals that occur within 1455a single word expand to a single field. 1456It is only field 1457splitting or pathname expansion that can create multiple 1458fields from a single word. 1459The single exception to this rule is 1460the expansion of the special parameter 1461.Va @ 1462within double-quotes, 1463as was described above. 1464.Pp 1465The order of word expansion is: 1466.Bl -enum 1467.It 1468Tilde Expansion, Parameter Expansion, Command Substitution, 1469Arithmetic Expansion (these all occur at the same time). 1470.It 1471Field Splitting is performed on fields generated by step (1) 1472unless the 1473.Va IFS 1474variable is null. 1475.It 1476Pathname Expansion (unless the 1477.Fl f 1478option is in effect). 1479.It 1480Quote Removal. 1481.El 1482.Pp 1483The 1484.Ql $ 1485character is used to introduce parameter expansion, command 1486substitution, or arithmetic expansion. 1487.Ss Tilde Expansion (substituting a user's home directory) 1488A word beginning with an unquoted tilde character 1489.Pq Ql ~ 1490is 1491subjected to tilde expansion. 1492All the characters up to a slash 1493.Pq Ql / 1494or the end of the word are treated as a username 1495and are replaced with the user's home directory. 1496If the 1497username is missing (as in 1498.Pa ~/foobar ) , 1499the tilde is replaced with the value of the 1500.Va HOME 1501variable (the current user's home directory). 1502.Ss Parameter Expansion 1503The format for parameter expansion is as follows: 1504.Pp 1505.D1 Li ${ Ns Ar expression Ns Li } 1506.Pp 1507where 1508.Ar expression 1509consists of all characters until the matching 1510.Ql } . 1511Any 1512.Ql } 1513escaped by a backslash or within a single-quoted or double-quoted 1514string, and characters in 1515embedded arithmetic expansions, command substitutions, and variable 1516expansions, are not examined in determining the matching 1517.Ql } . 1518If the variants with 1519.Ql + , 1520.Ql - , 1521.Ql = 1522or 1523.Ql ?\& 1524occur within a double-quoted string, 1525as an extension there may be unquoted parts 1526(via double-quotes inside the expansion); 1527.Ql } 1528within such parts are also not examined in determining the matching 1529.Ql } . 1530.Pp 1531The simplest form for parameter expansion is: 1532.Pp 1533.D1 Li ${ Ns Ar parameter Ns Li } 1534.Pp 1535The value, if any, of 1536.Ar parameter 1537is substituted. 1538.Pp 1539The parameter name or symbol can be enclosed in braces, which are 1540optional except for positional parameters with more than one digit or 1541when parameter is followed by a character that could be interpreted as 1542part of the name. 1543If a parameter expansion occurs inside double-quotes: 1544.Bl -enum 1545.It 1546Field splitting is not performed on the results of the 1547expansion, with the exception of the special parameter 1548.Va @ . 1549.It 1550Pathname expansion is not performed on the results of the 1551expansion. 1552.El 1553.Pp 1554In addition, a parameter expansion can be modified by using one of the 1555following formats. 1556.Bl -tag -width indent 1557.It Li ${ Ns Ar parameter Ns Li :- Ns Ar word Ns Li } 1558Use Default Values. 1559If 1560.Ar parameter 1561is unset or null, the expansion of 1562.Ar word 1563is substituted; otherwise, the value of 1564.Ar parameter 1565is substituted. 1566.It Li ${ Ns Ar parameter Ns Li := Ns Ar word Ns Li } 1567Assign Default Values. 1568If 1569.Ar parameter 1570is unset or null, the expansion of 1571.Ar word 1572is assigned to 1573.Ar parameter . 1574In all cases, the 1575final value of 1576.Ar parameter 1577is substituted. 1578Quoting inside 1579.Ar word 1580does not prevent field splitting or pathname expansion. 1581Only variables, not positional 1582parameters or special parameters, can be 1583assigned in this way. 1584.It Li ${ Ns Ar parameter Ns Li :? Ns Oo Ar word Oc Ns Li } 1585Indicate Error if Null or Unset. 1586If 1587.Ar parameter 1588is unset or null, the expansion of 1589.Ar word 1590(or a message indicating it is unset if 1591.Ar word 1592is omitted) is written to standard 1593error and the shell exits with a nonzero 1594exit status. 1595Otherwise, the value of 1596.Ar parameter 1597is substituted. 1598An 1599interactive shell need not exit. 1600.It Li ${ Ns Ar parameter Ns Li :+ Ns Ar word Ns Li } 1601Use Alternate Value. 1602If 1603.Ar parameter 1604is unset or null, null is substituted; 1605otherwise, the expansion of 1606.Ar word 1607is substituted. 1608.El 1609.Pp 1610In the parameter expansions shown previously, use of the colon in the 1611format results in a test for a parameter that is unset or null; omission 1612of the colon results in a test for a parameter that is only unset. 1613.Pp 1614The 1615.Ar word 1616inherits the type of quoting 1617(unquoted, double-quoted or here-document) 1618from the surroundings, 1619with the exception that a backslash that quotes a closing brace is removed 1620during quote removal. 1621.Bl -tag -width indent 1622.It Li ${# Ns Ar parameter Ns Li } 1623String Length. 1624The length in characters of 1625the value of 1626.Ar parameter . 1627.El 1628.Pp 1629The following four varieties of parameter expansion provide for substring 1630processing. 1631In each case, pattern matching notation 1632(see 1633.Sx Shell Patterns ) , 1634rather than regular expression notation, 1635is used to evaluate the patterns. 1636If parameter is one of the special parameters 1637.Va * 1638or 1639.Va @ , 1640the result of the expansion is unspecified. 1641Enclosing the full parameter expansion string in double-quotes does not 1642cause the following four varieties of pattern characters to be quoted, 1643whereas quoting characters within the braces has this effect. 1644.Bl -tag -width indent 1645.It Li ${ Ns Ar parameter Ns Li % Ns Ar word Ns Li } 1646Remove Smallest Suffix Pattern. 1647The 1648.Ar word 1649is expanded to produce a pattern. 1650The 1651parameter expansion then results in 1652.Ar parameter , 1653with the smallest portion of the 1654suffix matched by the pattern deleted. 1655.It Li ${ Ns Ar parameter Ns Li %% Ns Ar word Ns Li } 1656Remove Largest Suffix Pattern. 1657The 1658.Ar word 1659is expanded to produce a pattern. 1660The 1661parameter expansion then results in 1662.Ar parameter , 1663with the largest portion of the 1664suffix matched by the pattern deleted. 1665.It Li ${ Ns Ar parameter Ns Li # Ns Ar word Ns Li } 1666Remove Smallest Prefix Pattern. 1667The 1668.Ar word 1669is expanded to produce a pattern. 1670The 1671parameter expansion then results in 1672.Ar parameter , 1673with the smallest portion of the 1674prefix matched by the pattern deleted. 1675.It Li ${ Ns Ar parameter Ns Li ## Ns Ar word Ns Li } 1676Remove Largest Prefix Pattern. 1677The 1678.Ar word 1679is expanded to produce a pattern. 1680The 1681parameter expansion then results in 1682.Ar parameter , 1683with the largest portion of the 1684prefix matched by the pattern deleted. 1685.El 1686.Ss Command Substitution 1687Command substitution allows the output of a command to be substituted in 1688place of the command name itself. 1689Command substitution occurs when 1690the command is enclosed as follows: 1691.Pp 1692.D1 Li $( Ns Ar command Ns Li )\& 1693.Pp 1694or the backquoted version: 1695.Pp 1696.D1 Li ` Ns Ar command Ns Li ` 1697.Pp 1698The shell expands the command substitution by executing command 1699and replacing the command substitution 1700with the standard output of the command, 1701removing sequences of one or more newlines at the end of the substitution. 1702Embedded newlines before the end of the output are not removed; 1703however, during field splitting, they may be translated into spaces 1704depending on the value of 1705.Va IFS 1706and the quoting that is in effect. 1707The command is executed in a subshell environment, 1708except that the built-in commands 1709.Ic jobid , 1710.Ic jobs , 1711and 1712.Ic trap 1713return information about the parent shell environment 1714and 1715.Ic times 1716returns information about the same process 1717if they are the only command in a command substitution. 1718.Pp 1719If a command substitution of the 1720.Li $( 1721form begins with a subshell, 1722the 1723.Li $( 1724and 1725.Li (\& 1726must be separated by whitespace 1727to avoid ambiguity with arithmetic expansion. 1728.Ss Arithmetic Expansion 1729Arithmetic expansion provides a mechanism for evaluating an arithmetic 1730expression and substituting its value. 1731The format for arithmetic expansion is as follows: 1732.Pp 1733.D1 Li $(( Ns Ar expression Ns Li )) 1734.Pp 1735The 1736.Ar expression 1737is treated as if it were in double-quotes, except 1738that a double-quote inside the expression is not treated specially. 1739The 1740shell expands all tokens in the 1741.Ar expression 1742for parameter expansion, 1743command substitution, 1744arithmetic expansion 1745and quote removal. 1746.Pp 1747The allowed expressions are a subset of C expressions, 1748summarized below. 1749.Bl -tag -width "Variables" -offset indent 1750.It Values 1751All values are of type 1752.Ft intmax_t . 1753.It Constants 1754Decimal, octal (starting with 1755.Li 0 ) 1756and hexadecimal (starting with 1757.Li 0x ) 1758integer constants. 1759.It Variables 1760Shell variables can be read and written 1761and contain integer constants. 1762.It Unary operators 1763.Li "! ~ + -" 1764.It Binary operators 1765.Li "* / % + - << >> < <= > >= == != & ^ | && ||"\& 1766.It Assignment operators 1767.Li "= += -= *= /= %= <<= >>= &= ^= |=" 1768.It Conditional operator 1769.Li "? :"\& 1770.El 1771.Pp 1772The result of the expression is substituted in decimal. 1773.Ss White Space Splitting (Field Splitting) 1774In certain contexts, 1775after parameter expansion, command substitution, and 1776arithmetic expansion the shell scans the results of 1777expansions and substitutions that did not occur in double-quotes for 1778field splitting and multiple fields can result. 1779.Pp 1780Characters in 1781.Va IFS 1782that are whitespace 1783.Po 1784.Aq space , 1785.Aq tab , 1786and 1787.Aq newline 1788.Pc 1789are treated differently from other characters in 1790.Va IFS . 1791.Pp 1792Whitespace in 1793.Va IFS 1794at the beginning or end of a word is discarded. 1795.Pp 1796Subsequently, a field is delimited by either 1797.Bl -enum 1798.It 1799a non-whitespace character in 1800.Va IFS 1801with any whitespace in 1802.Va IFS 1803surrounding it, or 1804.It 1805one or more whitespace characters in 1806.Va IFS . 1807.El 1808.Pp 1809If a word ends with a non-whitespace character in 1810.Va IFS , 1811there is no empty field after this character. 1812.Pp 1813If no field is delimited, the word is discarded. 1814In particular, if a word consists solely of an unquoted substitution 1815and the result of the substitution is null, 1816it is removed by field splitting even if 1817.Va IFS 1818is null. 1819.Ss Pathname Expansion (File Name Generation) 1820Unless the 1821.Fl f 1822option is set, 1823file name generation is performed 1824after word splitting is complete. 1825Each word is 1826viewed as a series of patterns, separated by slashes. 1827The 1828process of expansion replaces the word with the names of 1829all existing files whose names can be formed by replacing 1830each pattern with a string that matches the specified pattern. 1831There are two restrictions on this: first, a pattern cannot match 1832a string containing a slash, and second, 1833a pattern cannot match a string starting with a period 1834unless the first character of the pattern is a period. 1835The next section describes the patterns used for 1836Pathname Expansion, 1837the four varieties of parameter expansion for substring processing and the 1838.Ic case 1839command. 1840.Ss Shell Patterns 1841A pattern consists of normal characters, which match themselves, 1842and meta-characters. 1843The meta-characters are 1844.Ql * , 1845.Ql \&? , 1846and 1847.Ql \&[ . 1848These characters lose their special meanings if they are quoted. 1849When command or variable substitution is performed and the dollar sign 1850or back quotes are not double-quoted, the value of the 1851variable or the output of the command is scanned for these 1852characters and they are turned into meta-characters. 1853.Pp 1854An asterisk 1855.Pq Ql * 1856matches any string of characters. 1857A question mark 1858.Pq Ql \&? 1859matches any single character. 1860A left bracket 1861.Pq Ql \&[ 1862introduces a character class. 1863The end of the character class is indicated by a 1864.Ql \&] ; 1865if the 1866.Ql \&] 1867is missing then the 1868.Ql \&[ 1869matches a 1870.Ql \&[ 1871rather than introducing a character class. 1872A character class matches any of the characters between the square brackets. 1873A locale-dependent range of characters may be specified using a minus sign. 1874A named class of characters (see 1875.Xr wctype 3 ) 1876may be specified by surrounding the name with 1877.Ql \&[:\& 1878and 1879.Ql :\&] . 1880For example, 1881.Ql \&[\&[:alpha:\&]\&] 1882is a shell pattern that matches a single letter. 1883The character class may be complemented by making an exclamation point 1884.Pq Ql !\& 1885the first character of the character class. 1886A caret 1887.Pq Ql ^ 1888has the same effect but is non-standard. 1889.Pp 1890To include a 1891.Ql \&] 1892in a character class, make it the first character listed 1893(after the 1894.Ql \&! 1895or 1896.Ql ^ , 1897if any). 1898To include a 1899.Ql - , 1900make it the first or last character listed. 1901.Ss Built-in Commands 1902This section lists the built-in commands. 1903.Bl -tag -width indent 1904.It Ic \&: 1905A null command that returns a 0 (true) exit value. 1906.It Ic \&. Ar file 1907The commands in the specified file are read and executed by the shell. 1908The 1909.Ic return 1910command may be used to return to the 1911.Ic \&. 1912command's caller. 1913If 1914.Ar file 1915contains any 1916.Ql / 1917characters, it is used as is. 1918Otherwise, the shell searches the 1919.Va PATH 1920for the file. 1921If it is not found in the 1922.Va PATH , 1923it is sought in the current working directory. 1924.It Ic \&[ 1925A built-in equivalent of 1926.Xr test 1 . 1927.It Ic alias Oo Ar name Ns Oo = Ns Ar string Oc ... Oc 1928If 1929.Ar name Ns = Ns Ar string 1930is specified, the shell defines the alias 1931.Ar name 1932with value 1933.Ar string . 1934If just 1935.Ar name 1936is specified, the value of the alias 1937.Ar name 1938is printed. 1939With no arguments, the 1940.Ic alias 1941built-in command prints the names and values of all defined aliases 1942(see 1943.Ic unalias ) . 1944Alias values are written with appropriate quoting so that they are 1945suitable for re-input to the shell. 1946Also see the 1947.Sx Aliases 1948subsection. 1949.It Ic bg Op Ar job ... 1950Continue the specified jobs 1951(or the current job if no jobs are given) 1952in the background. 1953.It Ic bind Oo Fl aeklrsv Oc Oo Ar key Oo Ar command Oc Oc 1954List or alter key bindings for the line editor. 1955This command is documented in 1956.Xr editrc 5 . 1957.It Ic break Op Ar num 1958See the 1959.Sx Flow-Control Constructs 1960subsection. 1961.It Ic builtin Ar cmd Op Ar arg ... 1962Execute the specified built-in command, 1963.Ar cmd . 1964This is useful when the user wishes to override a shell function 1965with the same name as a built-in command. 1966.It Ic cd Oo Fl L | P Oc Oo Fl e Oc Op Ar directory 1967.It Ic cd Fl 1968Switch to the specified 1969.Ar directory , 1970to the directory specified in the 1971.Va HOME 1972environment variable if no 1973.Ar directory 1974is specified or 1975to the directory specified in the 1976.Va OLDPWD 1977environment variable if 1978.Ar directory 1979is 1980.Fl . 1981If 1982.Ar directory 1983does not begin with 1984.Pa / , \&. , 1985or 1986.Pa .. , 1987then the directories listed in the 1988.Va CDPATH 1989variable will be 1990searched for the specified 1991.Ar directory . 1992If 1993.Va CDPATH 1994is unset, the current directory is searched. 1995The format of 1996.Va CDPATH 1997is the same as that of 1998.Va PATH . 1999In an interactive shell, 2000the 2001.Ic cd 2002command will print out the name of the directory 2003that it actually switched to 2004if the 2005.Va CDPATH 2006mechanism was used or if 2007.Ar directory 2008was 2009.Fl . 2010.Pp 2011If the 2012.Fl P 2013option is specified, 2014.Pa .. 2015is handled physically and symbolic links are resolved before 2016.Pa .. 2017components are processed. 2018If the 2019.Fl L 2020option is specified, 2021.Pa .. 2022is handled logically. 2023This is the default. 2024.Pp 2025The 2026.Fl e 2027option causes 2028.Ic cd 2029to return exit status 1 if the full pathname of the new directory 2030cannot be determined reliably or at all. 2031Normally this is not considered an error, 2032although a warning is printed. 2033.Pp 2034If changing the directory fails, the exit status is greater than 1. 2035If the directory is changed, the exit status is 0, or also 1 if 2036.Fl e 2037was given. 2038.It Ic chdir 2039A synonym for the 2040.Ic cd 2041built-in command. 2042.It Ic command Oo Fl p Oc Op Ar utility Op Ar argument ... 2043.It Ic command Oo Fl p Oc Fl v Ar utility 2044.It Ic command Oo Fl p Oc Fl V Ar utility 2045The first form of invocation executes the specified 2046.Ar utility , 2047ignoring shell functions in the search. 2048If 2049.Ar utility 2050is a special builtin, 2051it is executed as if it were a regular builtin. 2052.Pp 2053If the 2054.Fl p 2055option is specified, the command search is performed using a 2056default value of 2057.Va PATH 2058that is guaranteed to find all of the standard utilities. 2059.Pp 2060If the 2061.Fl v 2062option is specified, 2063.Ar utility 2064is not executed but a description of its interpretation by the shell is 2065printed. 2066For ordinary commands the output is the path name; for shell built-in 2067commands, shell functions and keywords only the name is written. 2068Aliases are printed as 2069.Dq Ic alias Ar name Ns = Ns Ar value . 2070.Pp 2071The 2072.Fl V 2073option is identical to 2074.Fl v 2075except for the output. 2076It prints 2077.Dq Ar utility Ic is Ar description 2078where 2079.Ar description 2080is either 2081the path name to 2082.Ar utility , 2083a special shell builtin, 2084a shell builtin, 2085a shell function, 2086a shell keyword 2087or 2088an alias for 2089.Ar value . 2090.It Ic continue Op Ar num 2091See the 2092.Sx Flow-Control Constructs 2093subsection. 2094.It Ic echo Oo Fl e | n Oc Op Ar string ... 2095Print a space-separated list of the arguments to the standard output 2096and append a newline character. 2097.Bl -tag -width indent 2098.It Fl n 2099Suppress the output of the trailing newline. 2100.It Fl e 2101Process C-style backslash escape sequences. 2102The 2103.Ic echo 2104command understands the following character escapes: 2105.Bl -tag -width indent 2106.It \ea 2107Alert (ring the terminal bell) 2108.It \eb 2109Backspace 2110.It \ec 2111Suppress the trailing newline (this has the side-effect of truncating the 2112line if it is not the last character) 2113.It \ee 2114The ESC character (ASCII 0x1b) 2115.It \ef 2116Formfeed 2117.It \en 2118Newline 2119.It \er 2120Carriage return 2121.It \et 2122Horizontal tab 2123.It \ev 2124Vertical tab 2125.It \e\e 2126Literal backslash 2127.It \e0nnn 2128(Zero) The character whose octal value is 2129.Ar nnn 2130.El 2131.Pp 2132If 2133.Ar string 2134is not enclosed in quotes then the backslash itself must be escaped 2135with a backslash to protect it from the shell. 2136For example 2137.Bd -literal -offset indent 2138$ echo -e "a\evb" 2139a 2140 b 2141$ echo -e a\e\evb 2142a 2143 b 2144$ echo -e "a\e\eb" 2145a\eb 2146$ echo -e a\e\e\e\eb 2147a\eb 2148.Ed 2149.El 2150.Pp 2151Only one of the 2152.Fl e 2153and 2154.Fl n 2155options may be specified. 2156.It Ic eval Ar string ... 2157Concatenate all the arguments with spaces. 2158Then re-parse and execute the command. 2159.It Ic exec Op Ar command Op arg ... 2160Unless 2161.Ar command 2162is omitted, 2163the shell process is replaced with the specified program 2164(which must be a real program, not a shell built-in command or function). 2165Any redirections on the 2166.Ic exec 2167command are marked as permanent, 2168so that they are not undone when the 2169.Ic exec 2170command finishes. 2171.It Ic exit Op Ar exitstatus 2172Terminate the shell process. 2173If 2174.Ar exitstatus 2175is given 2176it is used as the exit status of the shell. 2177Otherwise, if the shell is executing an 2178.Cm EXIT 2179trap, the exit status of the last command before the trap is used; 2180if the shell is executing a trap for a signal, 2181the shell exits by resending the signal to itself. 2182Otherwise, the exit status of the preceding command is used. 2183The exit status should be an integer between 0 and 255. 2184.It Ic export Ar name ... 2185.It Ic export Op Fl p 2186The specified names are exported so that they will 2187appear in the environment of subsequent commands. 2188The only way to un-export a variable is to 2189.Ic unset 2190it. 2191The shell allows the value of a variable to be set 2192at the same time as it is exported by writing 2193.Pp 2194.D1 Ic export Ar name Ns = Ns Ar value 2195.Pp 2196With no arguments the 2197.Ic export 2198command lists the names 2199of all exported variables. 2200If the 2201.Fl p 2202option is specified, the exported variables are printed as 2203.Dq Ic export Ar name Ns = Ns Ar value 2204lines, suitable for re-input to the shell. 2205.It Ic false 2206A null command that returns a non-zero (false) exit value. 2207.It Ic fc Oo Fl e Ar editor Oc Op Ar first Op Ar last 2208.It Ic fc Fl l Oo Fl nr Oc Op Ar first Op Ar last 2209.It Ic fc Fl s Oo Ar old Ns = Ns Ar new Oc Op Ar first 2210The 2211.Ic fc 2212built-in command lists, or edits and re-executes, 2213commands previously entered to an interactive shell. 2214.Bl -tag -width indent 2215.It Fl e Ar editor 2216Use the editor named by 2217.Ar editor 2218to edit the commands. 2219The 2220.Ar editor 2221string is a command name, 2222subject to search via the 2223.Va PATH 2224variable. 2225The value in the 2226.Va FCEDIT 2227variable is used as a default when 2228.Fl e 2229is not specified. 2230If 2231.Va FCEDIT 2232is null or unset, the value of the 2233.Va EDITOR 2234variable is used. 2235If 2236.Va EDITOR 2237is null or unset, 2238.Xr ed 1 2239is used as the editor. 2240.It Fl l No (ell) 2241List the commands rather than invoking 2242an editor on them. 2243The commands are written in the 2244sequence indicated by the 2245.Ar first 2246and 2247.Ar last 2248operands, as affected by 2249.Fl r , 2250with each command preceded by the command number. 2251.It Fl n 2252Suppress command numbers when listing with 2253.Fl l . 2254.It Fl r 2255Reverse the order of the commands listed 2256(with 2257.Fl l ) 2258or edited 2259(with neither 2260.Fl l 2261nor 2262.Fl s ) . 2263.It Fl s 2264Re-execute the command without invoking an editor. 2265.It Ar first 2266.It Ar last 2267Select the commands to list or edit. 2268The number of previous commands that can be accessed 2269are determined by the value of the 2270.Va HISTSIZE 2271variable. 2272The value of 2273.Ar first 2274or 2275.Ar last 2276or both are one of the following: 2277.Bl -tag -width indent 2278.It Oo Cm + Oc Ns Ar num 2279A positive number representing a command number; 2280command numbers can be displayed with the 2281.Fl l 2282option. 2283.It Fl Ar num 2284A negative decimal number representing the 2285command that was executed 2286.Ar num 2287of 2288commands previously. 2289For example, \-1 is the immediately previous command. 2290.It Ar string 2291A string indicating the most recently entered command 2292that begins with that string. 2293If the 2294.Ar old Ns = Ns Ar new 2295operand is not also specified with 2296.Fl s , 2297the string form of the first operand cannot contain an embedded equal sign. 2298.El 2299.El 2300.Pp 2301The following variables affect the execution of 2302.Ic fc : 2303.Bl -tag -width ".Va HISTSIZE" 2304.It Va FCEDIT 2305Name of the editor to use for history editing. 2306.It Va HISTSIZE 2307The number of previous commands that are accessible. 2308.El 2309.It Ic fg Op Ar job 2310Move the specified 2311.Ar job 2312or the current job to the foreground. 2313.It Ic getopts Ar optstring var 2314The POSIX 2315.Ic getopts 2316command. 2317The 2318.Ic getopts 2319command deprecates the older 2320.Xr getopt 1 2321command. 2322The first argument should be a series of letters, each possibly 2323followed by a colon which indicates that the option takes an argument. 2324The specified variable is set to the parsed option. 2325The index of 2326the next argument is placed into the shell variable 2327.Va OPTIND . 2328If an option takes an argument, it is placed into the shell variable 2329.Va OPTARG . 2330If an invalid option is encountered, 2331.Ar var 2332is set to 2333.Ql \&? . 2334It returns a false value (1) when it encounters the end of the options. 2335A new set of arguments may be parsed by assigning 2336.Li OPTIND=1 . 2337.It Ic hash Oo Fl rv Oc Op Ar command ... 2338The shell maintains a hash table which remembers the locations of commands. 2339With no arguments whatsoever, the 2340.Ic hash 2341command prints out the contents of this table. 2342.Pp 2343With arguments, the 2344.Ic hash 2345command removes each specified 2346.Ar command 2347from the hash table (unless they are functions) and then locates it. 2348With the 2349.Fl v 2350option, 2351.Ic hash 2352prints the locations of the commands as it finds them. 2353The 2354.Fl r 2355option causes the 2356.Ic hash 2357command to delete all the entries in the hash table except for functions. 2358.It Ic jobid Op Ar job 2359Print the process IDs of the processes in the specified 2360.Ar job . 2361If the 2362.Ar job 2363argument is omitted, use the current job. 2364.It Ic jobs Oo Fl lps Oc Op Ar job ... 2365Print information about the specified jobs, or all jobs if no 2366.Ar job 2367argument is given. 2368The information printed includes job ID, status and command name. 2369.Pp 2370If the 2371.Fl l 2372option is specified, the PID of each job is also printed. 2373If the 2374.Fl p 2375option is specified, only the process IDs for the process group leaders 2376are printed, one per line. 2377If the 2378.Fl s 2379option is specified, only the PIDs of the job commands are printed, one per 2380line. 2381.It Ic kill 2382A built-in equivalent of 2383.Xr kill 1 2384that additionally supports sending signals to jobs. 2385.It Ic local Oo Ar variable ... Oc Op Fl 2386See the 2387.Sx Functions 2388subsection. 2389.It Ic printf 2390A built-in equivalent of 2391.Xr printf 1 . 2392.It Ic pwd Op Fl L | P 2393Print the path of the current directory. 2394The built-in command may 2395differ from the program of the same name because the 2396built-in command remembers what the current directory 2397is rather than recomputing it each time. 2398This makes 2399it faster. 2400However, if the current directory is 2401renamed, 2402the built-in version of 2403.Xr pwd 1 2404will continue to print the old name for the directory. 2405.Pp 2406If the 2407.Fl P 2408option is specified, symbolic links are resolved. 2409If the 2410.Fl L 2411option is specified, the shell's notion of the current directory 2412is printed (symbolic links are not resolved). 2413This is the default. 2414.It Ic read Oo Fl p Ar prompt Oc Oo 2415.Fl t Ar timeout Oc Oo Fl er Oc Ar variable ... 2416The 2417.Ar prompt 2418is printed if the 2419.Fl p 2420option is specified 2421and the standard input is a terminal. 2422Then a line is 2423read from the standard input. 2424The trailing newline 2425is deleted from the line and the line is split as 2426described in the section on 2427.Sx White Space Splitting (Field Splitting)\& 2428above, and 2429the pieces are assigned to the variables in order. 2430If there are more pieces than variables, the remaining 2431pieces (along with the characters in 2432.Va IFS 2433that separated them) 2434are assigned to the last variable. 2435If there are more variables than pieces, the remaining 2436variables are assigned the null string. 2437.Pp 2438Backslashes are treated specially, unless the 2439.Fl r 2440option is 2441specified. 2442If a backslash is followed by 2443a newline, the backslash and the newline will be 2444deleted. 2445If a backslash is followed by any other 2446character, the backslash will be deleted and the following 2447character will be treated as though it were not in 2448.Va IFS , 2449even if it is. 2450.Pp 2451If the 2452.Fl t 2453option is specified and the 2454.Ar timeout 2455elapses before a complete line of input is supplied, 2456the 2457.Ic read 2458command will return an exit status as if terminated by 2459.Dv SIGALRM 2460without assigning any values. 2461The 2462.Ar timeout 2463value may optionally be followed by one of 2464.Ql s , 2465.Ql m 2466or 2467.Ql h 2468to explicitly specify seconds, minutes or hours. 2469If none is supplied, 2470.Ql s 2471is assumed. 2472.Pp 2473The 2474.Fl e 2475option exists only for backward compatibility with older scripts. 2476.Pp 2477The exit status is 0 on success, 1 on end of file, 2478between 2 and 128 if an error occurs 2479and greater than 128 if a trapped signal interrupts 2480.Ic read . 2481.It Ic readonly Oo Fl p Oc Op Ar name ... 2482Each specified 2483.Ar name 2484is marked as read only, 2485so that it cannot be subsequently modified or unset. 2486The shell allows the value of a variable to be set 2487at the same time as it is marked read only 2488by using the following form: 2489.Pp 2490.D1 Ic readonly Ar name Ns = Ns Ar value 2491.Pp 2492With no arguments the 2493.Ic readonly 2494command lists the names of all read only variables. 2495If the 2496.Fl p 2497option is specified, the read-only variables are printed as 2498.Dq Ic readonly Ar name Ns = Ns Ar value 2499lines, suitable for re-input to the shell. 2500.It Ic return Op Ar exitstatus 2501See the 2502.Sx Functions 2503subsection. 2504.It Ic set Oo Fl /+abCEefIimnpTuVvx Oc Oo Fl /+o Ar longname 2505.Oc Op Fl - Ar arg ... 2506The 2507.Ic set 2508command performs three different functions: 2509.Bl -item 2510.It 2511With no arguments, it lists the values of all shell variables. 2512.It 2513If options are given, 2514either in short form or using the long 2515.Dq Fl /+o Ar longname 2516form, 2517it sets or clears the specified options as described in the section called 2518.Sx Argument List Processing . 2519.It 2520If the 2521.Dq Fl - 2522option is specified, 2523.Ic set 2524will replace the shell's positional parameters with the subsequent 2525arguments. 2526If no arguments follow the 2527.Dq Fl - 2528option, 2529all the positional parameters will be cleared, 2530which is equivalent to executing the command 2531.Dq Li "shift $#" . 2532The 2533.Dq Fl - 2534flag may be omitted when specifying arguments to be used 2535as positional replacement parameters. 2536This is not recommended, 2537because the first argument may begin with a dash 2538.Pq Ql - 2539or a plus 2540.Pq Ql + , 2541which the 2542.Ic set 2543command will interpret as a request to enable or disable options. 2544.El 2545.It Ic setvar Ar variable value 2546Assigns the specified 2547.Ar value 2548to the specified 2549.Ar variable . 2550The 2551.Ic setvar 2552command is intended to be used in functions that 2553assign values to variables whose names are passed as parameters. 2554In general it is better to write 2555.Dq Ar variable Ns = Ns Ar value 2556rather than using 2557.Ic setvar . 2558.It Ic shift Op Ar n 2559Shift the positional parameters 2560.Ar n 2561times, or once if 2562.Ar n 2563is not specified. 2564A shift sets the value of 2565.Li $1 2566to the value of 2567.Li $2 , 2568the value of 2569.Li $2 2570to the value of 2571.Li $3 , 2572and so on, 2573decreasing the value of 2574.Li $# 2575by one. 2576For portability, shifting if there are zero positional parameters 2577should be avoided, since the shell may abort. 2578.It Ic test 2579A built-in equivalent of 2580.Xr test 1 . 2581.It Ic times 2582Print the amount of time spent executing the shell process and its children. 2583The first output line shows the user and system times for the shell process 2584itself, the second one contains the user and system times for the 2585children. 2586.It Ic trap Oo Ar action Oc Ar signal ... 2587.It Ic trap Fl l 2588Cause the shell to parse and execute 2589.Ar action 2590when any specified 2591.Ar signal 2592is received. 2593The signals are specified by name or number. 2594In addition, the pseudo-signal 2595.Cm EXIT 2596may be used to specify an 2597.Ar action 2598that is performed when the shell terminates. 2599The 2600.Ar action 2601may be an empty string or a dash 2602.Pq Ql - ; 2603the former causes the specified signal to be ignored 2604and the latter causes the default action to be taken. 2605Omitting the 2606.Ar action 2607and using only signal numbers is another way to request the default action. 2608In a subshell or utility environment, 2609the shell resets trapped (but not ignored) signals to the default action. 2610The 2611.Ic trap 2612command has no effect on signals that were ignored on entry to the shell. 2613.Pp 2614Option 2615.Fl l 2616causes the 2617.Ic trap 2618command to display a list of valid signal names. 2619.It Ic true 2620A null command that returns a 0 (true) exit value. 2621.It Ic type Op Ar name ... 2622Interpret each 2623.Ar name 2624as a command and print the resolution of the command search. 2625Possible resolutions are: 2626shell keyword, alias, special shell builtin, shell builtin, command, 2627tracked alias 2628and not found. 2629For aliases the alias expansion is printed; 2630for commands and tracked aliases 2631the complete pathname of the command is printed. 2632.It Ic ulimit Oo Fl HSabcdfklmnopstuvw Oc Op Ar limit 2633Set or display resource limits (see 2634.Xr getrlimit 2 ) . 2635If 2636.Ar limit 2637is specified, the named resource will be set; 2638otherwise the current resource value will be displayed. 2639.Pp 2640If 2641.Fl H 2642is specified, the hard limits will be set or displayed. 2643While everybody is allowed to reduce a hard limit, 2644only the superuser can increase it. 2645The 2646.Fl S 2647option 2648specifies the soft limits instead. 2649When displaying limits, 2650only one of 2651.Fl S 2652or 2653.Fl H 2654can be given. 2655The default is to display the soft limits, 2656and to set both the hard and the soft limits. 2657.Pp 2658Option 2659.Fl a 2660causes the 2661.Ic ulimit 2662command to display all resources. 2663The parameter 2664.Ar limit 2665is not acceptable in this mode. 2666.Pp 2667The remaining options specify which resource value is to be 2668displayed or modified. 2669They are mutually exclusive. 2670.Bl -tag -width indent 2671.It Fl b Ar sbsize 2672The maximum size of socket buffer usage, in bytes. 2673.It Fl c Ar coredumpsize 2674The maximal size of core dump files, in 512-byte blocks. 2675Setting 2676.Ar coredumpsize 2677to 0 prevents core dump files from being created. 2678.It Fl d Ar datasize 2679The maximal size of the data segment of a process, in kilobytes. 2680.It Fl f Ar filesize 2681The maximal size of a file, in 512-byte blocks. 2682.It Fl k Ar kqueues 2683The maximal number of kqueues 2684(see 2685.Xr kqueue 2 ) 2686for this user ID. 2687.It Fl l Ar lockedmem 2688The maximal size of memory that can be locked by a process, in 2689kilobytes. 2690.It Fl m Ar memoryuse 2691The maximal resident set size of a process, in kilobytes. 2692.It Fl n Ar nofiles 2693The maximal number of descriptors that could be opened by a process. 2694.It Fl o Ar umtxp 2695The maximal number of process-shared locks 2696(see 2697.Xr pthread 3 ) 2698for this user ID. 2699.It Fl p Ar pseudoterminals 2700The maximal number of pseudo-terminals for this user ID. 2701.It Fl s Ar stacksize 2702The maximal size of the stack segment, in kilobytes. 2703.It Fl t Ar time 2704The maximal amount of CPU time to be used by each process, in seconds. 2705.It Fl u Ar userproc 2706The maximal number of simultaneous processes for this user ID. 2707.It Fl v Ar virtualmem 2708The maximal virtual size of a process, in kilobytes. 2709.It Fl w Ar swapuse 2710The maximum amount of swap space reserved or used for this user ID, 2711in kilobytes. 2712.El 2713.It Ic umask Oo Fl S Oc Op Ar mask 2714Set the file creation mask (see 2715.Xr umask 2 ) 2716to the octal or symbolic (see 2717.Xr chmod 1 ) 2718value specified by 2719.Ar mask . 2720If the argument is omitted, the current mask value is printed. 2721If the 2722.Fl S 2723option is specified, the output is symbolic, otherwise the output is octal. 2724.It Ic unalias Oo Fl a Oc Op Ar name ... 2725The specified alias names are removed. 2726If 2727.Fl a 2728is specified, all aliases are removed. 2729.It Ic unset Oo Fl fv Oc Ar name ... 2730The specified variables or functions are unset and unexported. 2731If the 2732.Fl v 2733option is specified or no options are given, the 2734.Ar name 2735arguments are treated as variable names. 2736If the 2737.Fl f 2738option is specified, the 2739.Ar name 2740arguments are treated as function names. 2741.It Ic wait Op Ar job ... 2742Wait for each specified 2743.Ar job 2744to complete and return the exit status of the last process in the 2745last specified 2746.Ar job . 2747If any 2748.Ar job 2749specified is unknown to the shell, it is treated as if it 2750were a known job that exited with exit status 127. 2751If no operands are given, wait for all jobs to complete 2752and return an exit status of zero. 2753.El 2754.Ss Command Line Editing 2755When 2756.Nm 2757is being used interactively from a terminal, the current command 2758and the command history 2759(see 2760.Ic fc 2761in 2762.Sx Built-in Commands ) 2763can be edited using 2764.Nm vi Ns -mode 2765command line editing. 2766This mode uses commands similar 2767to a subset of those described in the 2768.Xr vi 1 2769man page. 2770The command 2771.Dq Li "set -o vi" 2772(or 2773.Dq Li "set -V" ) 2774enables 2775.Nm vi Ns -mode 2776editing and places 2777.Nm 2778into 2779.Nm vi 2780insert mode. 2781With 2782.Nm vi Ns -mode 2783enabled, 2784.Nm 2785can be switched between insert mode and command mode by typing 2786.Aq ESC . 2787Hitting 2788.Aq return 2789while in command mode will pass the line to the shell. 2790.Pp 2791Similarly, the 2792.Dq Li "set -o emacs" 2793(or 2794.Dq Li "set -E" ) 2795command can be used to enable a subset of 2796.Nm emacs Ns -style 2797command line editing features. 2798.Sh ENVIRONMENT 2799The following environment variables affect the execution of 2800.Nm : 2801.Bl -tag -width ".Ev LANGXXXXXX" 2802.It Ev ENV 2803Initialization file for interactive shells. 2804.It Ev LANG , Ev LC_* 2805Locale settings. 2806These are inherited by children of the shell, 2807and is used in a limited manner by the shell itself. 2808.It Ev OLDPWD 2809The previous current directory. 2810This is used and updated by 2811.Ic cd . 2812.It Ev PWD 2813An absolute pathname for the current directory, 2814possibly containing symbolic links. 2815This is used and updated by the shell. 2816.It Ev TERM 2817The default terminal setting for the shell. 2818This is inherited by children of the shell, and is used in the history 2819editing modes. 2820.El 2821.Pp 2822Additionally, environment variables are turned into shell variables 2823at startup, 2824which may affect the shell as described under 2825.Sx Special Variables . 2826.Sh FILES 2827.Bl -tag -width "/etc/suid_profileXX" -compact 2828.It Pa ~/.profile 2829User's login profile. 2830.It Pa /etc/profile 2831System login profile. 2832.It Pa /etc/shells 2833Shell database. 2834.It Pa /etc/suid_profile 2835Privileged shell profile. 2836.El 2837.Sh EXIT STATUS 2838If the 2839.Ar script 2840cannot be found, the exit status will be 127; 2841if it cannot be opened for another reason, the exit status will be 126. 2842Other errors that are detected by the shell, such as a syntax error, will 2843cause the shell to exit with a non-zero exit status. 2844If the shell is not an interactive shell, the execution of the shell 2845file will be aborted. 2846Otherwise the shell will return the exit status of the last command 2847executed, or if the 2848.Ic exit 2849builtin is used with a numeric argument, it 2850will return the argument. 2851.Sh SEE ALSO 2852.Xr builtin 1 , 2853.Xr chsh 1 , 2854.Xr echo 1 , 2855.Xr ed 1 , 2856.Xr emacs 1 , 2857.Xr kill 1 , 2858.Xr printf 1 , 2859.Xr pwd 1 , 2860.Xr test 1 , 2861.Xr vi 1 , 2862.Xr execve 2 , 2863.Xr getrlimit 2 , 2864.Xr umask 2 , 2865.Xr wctype 3 , 2866.Xr editrc 5 , 2867.Xr shells 5 2868.Sh HISTORY 2869A 2870.Nm 2871command, the Thompson shell, appeared in 2872.At v1 . 2873It was superseded in 2874.At v7 2875by the Bourne shell, which inherited the name 2876.Nm . 2877.Pp 2878This version of 2879.Nm 2880was rewritten in 1989 under the 2881.Bx 2882license after the Bourne shell from 2883.At V.4 . 2884.Sh AUTHORS 2885This version of 2886.Nm 2887was originally written by 2888.An Kenneth Almquist . 2889.Sh BUGS 2890The 2891.Nm 2892utility does not recognize multibyte characters other than UTF-8. 2893Splitting using 2894.Va IFS 2895does not recognize multibyte characters. 2896