1.\" Copyright (c) 1980, 1990, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" This code is derived from software contributed to Berkeley by 5.\" the Institute of Electrical and Electronics Engineers, Inc. 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" From @(#)printenv.1 8.1 (Berkeley) 6/6/93 31.\" From FreeBSD: src/usr.bin/printenv/printenv.1,v 1.17 2002/11/26 17:33:35 ru Exp 32.\" $FreeBSD$ 33.\" 34.Dd November 11, 2020 35.Dt ENV 1 36.Os 37.Sh NAME 38.Nm env 39.Nd set environment and execute command, or print environment 40.Sh SYNOPSIS 41.Nm 42.Op Fl 0iv 43.Op Fl L Ns | Ns Fl U Ar user Ns Op / Ns Ar class 44.Op Fl u Ar name 45.Op Ar name Ns = Ns Ar value ... 46.Nm 47.Op Fl iv 48.Op Fl L Ns | Ns Fl U Ar user Ns Op / Ns Ar class 49.Op Fl P Ar altpath 50.Op Fl S Ar string 51.Op Fl u Ar name 52.Op Ar name Ns = Ns Ar value ... 53.Ar utility Op Ar argument ... 54.Sh DESCRIPTION 55The 56.Nm 57utility executes another 58.Ar utility 59after modifying the environment as 60specified on the command line. 61Each 62.Ar name Ns = Ns Ar value 63option specifies the setting of an environment variable, 64.Ar name , 65with a value of 66.Ar value . 67All such environment variables are set before the 68.Ar utility 69is executed. 70.Pp 71The options are as follows: 72.Bl -tag -width indent 73.It Fl 0 74End each output line with NUL, not newline. 75.It Fl i 76Execute the 77.Ar utility 78with only those environment variables specified by 79.Ar name Ns = Ns Ar value 80options. 81The environment inherited 82by 83.Nm 84is ignored completely. 85.\" -L | -U 86.It Fl L | Fl U Ar user Ns Op / Ns Ar class 87Add the environment variable definitions from 88.Xr login.conf 5 89for the specified user and login class to the environment, after 90processing any 91.Fl i 92or 93.Fl u 94options, but before processing any 95.Ar name Ns = Ns Ar value 96options. 97If 98.Fl L 99is used, only the system-wide 100.Pa /etc/login.conf.db 101file is read; if 102.Fl U 103is used, then the specified user's 104.Pa ~/.login_conf 105is read as well. 106The user may be specified by name or by uid. 107.\" -P 108.It Fl P Ar altpath 109Search the set of directories as specified by 110.Ar altpath 111to locate the specified 112.Ar utility 113program, instead of using the value of the 114.Ev PATH 115environment variable. 116.\" -S 117.It Fl S Ar string 118Split apart the given 119.Ar string 120into multiple strings, and process each of the resulting strings 121as separate arguments to the 122.Nm 123utility. 124The 125.Fl S 126option recognizes some special character escape sequences and 127also supports environment-variable substitution, as described 128below. 129.\" -u 130.It Fl u Ar name 131If the environment variable 132.Ar name 133is in the environment, then remove it before processing the 134remaining options. 135This is similar to the 136.Ic unset 137command in 138.Xr sh 1 . 139The value for 140.Ar name 141must not include the 142.Ql = 143character. 144.\" -v 145.It Fl v 146Print verbose information for each step of processing done by the 147.Nm 148utility. 149Additional information will be printed if 150.Fl v 151is specified multiple times. 152.El 153.Pp 154The above options are only recognized when they are specified 155before any 156.Ar name Ns = Ns Ar value 157options. 158.Pp 159If no 160.Ar utility 161is specified, 162.Nm 163prints out the names and values of the variables in the environment. 164Each name/value pair is separated by a new line unless 165.Fl 0 166is specified, in which case name/value pairs are separated by NUL. 167Both 168.Fl 0 169and 170.Ar utility 171may not be specified together. 172.\" 173.Ss Details of -S (split-string) processing 174The processing of the 175.Fl S 176option will split the given 177.Ar string 178into separate arguments based on any space or <tab> characters found in the 179.Ar string . 180Each of those new arguments will then be treated as if it had been 181specified as a separate argument on the original 182.Nm 183command. 184.Pp 185Spaces and tabs may be embedded in one of those new arguments by using 186single 187.Pq Dq Li ' 188or double 189.Pq Ql \&" 190quotes, or backslashes 191.Pq Ql \e . 192Single quotes will escape all non-single quote characters, up to 193the matching single quote. 194Double quotes will escape all non-double quote characters, up to 195the matching double quote. 196It is an error if the end of the 197.Ar string 198is reached before the matching quote character. 199.Pp 200If 201.Fl S 202would create a new argument that starts with the 203.Ql # 204character, then that argument and the remainder of the 205.Ar string 206will be ignored. 207The 208.Ql \e# 209sequence can be used when you want a new argument to start 210with a 211.Ql # 212character, without causing the remainder of the 213.Ar string 214to be skipped. 215.Pp 216While processing the 217.Ar string 218value, 219.Fl S 220processing will treat certain character combinations as escape 221sequences which represent some action to take. 222The character escape sequences are in backslash notation. 223The characters and their meanings are as follows: 224.Pp 225.Bl -tag -width indent -offset indent -compact 226.It Cm \ec 227Ignore the remaining characters in the 228.Ar string . 229This must not appear inside a double-quoted string. 230.It Cm \ef 231Replace with a <form-feed> character. 232.It Cm \en 233Replace with a <new-line> character. 234.It Cm \er 235Replace with a <carriage return> character. 236.It Cm \et 237Replace with a <tab> character. 238.It Cm \ev 239Replace with a <vertical tab> character. 240.It Cm \e# 241Replace with a 242.Ql # 243character. 244This would be useful when you need a 245.Ql # 246as the first character in one of the arguments created 247by splitting apart the given 248.Ar string . 249.It Cm \e$ 250Replace with a 251.Ql $ 252character. 253.It Cm \e_ 254If this is found inside of a double-quoted string, then replace it 255with a single blank. 256If this is found outside of a quoted string, then treat this as the 257separator character between new arguments in the original 258.Ar string . 259.It Cm \e" 260Replace with a <double quote> character. 261.It Cm \e\' 262Replace with a <single quote> character. 263.It Cm \e\e 264Replace with a backslash character. 265.El 266.Pp 267The sequences for <single-quote> and backslash are the only sequences 268which are recognized inside of a single-quoted string. 269The other sequences have no special meaning inside a single-quoted 270string. 271All escape sequences are recognized inside of a double-quoted string. 272It is an error if a single 273.Ql \e 274character is followed by a character other than the ones listed above. 275.Pp 276The processing of 277.Fl S 278also supports substitution of values from environment variables. 279To do this, the name of the environment variable must be inside of 280.Ql ${} , 281such as: 282.Li ${SOMEVAR} . 283The common shell syntax of 284.Li $SOMEVAR 285is not supported. 286All values substituted will be the values of the environment variables 287as they were when the 288.Nm 289utility was originally invoked. 290Those values will not be checked for any of the escape sequences as 291described above. 292And any settings of 293.Ar name Ns = Ns Ar value 294will not effect the values used for substitution in 295.Fl S 296processing. 297.Pp 298Also, 299.Fl S 300processing cannot reference the value of the special parameters 301which are defined by most shells. 302For instance, 303.Fl S 304cannot recognize special parameters such as: 305.Ql $* , 306.Ql $@ , 307.Ql $# , 308.Ql $? 309or 310.Ql $$ 311if they appear inside the given 312.Ar string . 313.\" 314.Ss Use in shell-scripts 315The 316.Nm 317utility is often used as the 318.Ar interpreter 319on the first line of interpreted scripts, as 320described in 321.Xr execve 2 . 322.Pp 323Note that the way the kernel parses the 324.Ql #! 325(first line) of an interpreted script has changed as of 326.Fx 6.0 . 327Prior to that, the 328.Fx 329kernel would split that first line into separate arguments based 330on any whitespace (space or <tab> characters) found in the line. 331So, if a script named 332.Pa /usr/local/bin/someport 333had a first line of: 334.Pp 335.Dl "#!/usr/local/bin/php -n -q -dsafe_mode=0" 336.Pp 337then the 338.Pa /usr/local/bin/php 339program would have been started with the arguments of: 340.Bd -literal -offset indent 341arg[0] = '/usr/local/bin/php' 342arg[1] = '-n' 343arg[2] = '-q' 344arg[3] = '-dsafe_mode=0' 345arg[4] = '/usr/local/bin/someport' 346.Ed 347.Pp 348plus any arguments the user specified when executing 349.Pa someport . 350However, this processing of multiple options on the 351.Ql #! 352line is not the way any other operating system parses the 353first line of an interpreted script. 354So after a change which was made for 355.Fx 6.0 356release, that script will result in 357.Pa /usr/local/bin/php 358being started with the arguments of: 359.Bd -literal -offset indent 360arg[0] = '/usr/local/bin/php' 361arg[1] = '-n -q -dsafe_mode=0' 362arg[2] = '/usr/local/bin/someport' 363.Ed 364.Pp 365plus any arguments the user specified. 366This caused a significant change in the behavior of a few scripts. 367In the case of above script, to have it behave the same way under 368.Fx 6.0 369as it did under earlier releases, the first line should be 370changed to: 371.Pp 372.Dl "#!/usr/bin/env -S /usr/local/bin/php -n -q -dsafe_mode=0" 373.Pp 374The 375.Nm 376utility will be started with the entire line as a single 377argument: 378.Pp 379.Dl "arg[1] = '-S /usr/local/bin/php -n -q -dsafe_mode=0'" 380.Pp 381and then 382.Fl S 383processing will split that line into separate arguments before 384executing 385.Pa /usr/local/bin/php . 386.\" 387.Sh ENVIRONMENT 388The 389.Nm 390utility uses the 391.Ev PATH 392environment variable to locate the requested 393.Ar utility 394if the name contains no 395.Ql / 396characters, unless the 397.Fl P 398option has been specified. 399.Sh EXIT STATUS 400.Ex -std 401An exit status of 126 indicates that 402.Ar utility 403was found, but could not be executed. 404An exit status of 127 indicates that 405.Ar utility 406could not be found. 407.Sh EXAMPLES 408Since the 409.Nm 410utility is often used as part of the first line of an interpreted script, 411the following examples show a number of ways that the 412.Nm 413utility can be useful in scripts. 414.Pp 415The kernel processing of an interpreted script does not allow a script 416to directly reference some other script as its own interpreter. 417As a way around this, the main difference between 418.Pp 419.Dl #!/usr/local/bin/foo 420and 421.Dl "#!/usr/bin/env /usr/local/bin/foo" 422.Pp 423is that the latter works even if 424.Pa /usr/local/bin/foo 425is itself an interpreted script. 426.Pp 427Probably the most common use of 428.Nm 429is to find the correct interpreter for a script, when the interpreter 430may be in different directories on different systems. 431The following example will find the 432.Ql perl 433interpreter by searching through the directories specified by 434.Ev PATH . 435.Pp 436.Dl "#!/usr/bin/env perl" 437.Pp 438One limitation of that example is that it assumes the user's value 439for 440.Ev PATH 441is set to a value which will find the interpreter you want 442to execute. 443The 444.Fl P 445option can be used to make sure a specific list of directories is 446used in the search for 447.Ar utility . 448Note that the 449.Fl S 450option is also required for this example to work correctly. 451.Pp 452.Dl "#!/usr/bin/env -S -P/usr/local/bin:/usr/bin perl" 453.Pp 454The above finds 455.Ql perl 456only if it is in 457.Pa /usr/local/bin 458or 459.Pa /usr/bin . 460That could be combined with the present value of 461.Ev PATH , 462to provide more flexibility. 463Note that spaces are not required between the 464.Fl S 465and 466.Fl P 467options: 468.Pp 469.Dl "#!/usr/bin/env -S-P/usr/local/bin:/usr/bin:${PATH} perl" 470.Sh COMPATIBILITY 471The 472.Nm 473utility accepts the 474.Fl 475option as a synonym for 476.Fl i . 477.Sh SEE ALSO 478.Xr printenv 1 , 479.Xr sh 1 , 480.Xr execvp 3 , 481.Xr login.conf 5 , 482.Xr environ 7 483.Sh STANDARDS 484The 485.Nm 486utility conforms to 487.St -p1003.1-2001 . 488The 489.Fl 0 , L , P , S , U , u 490and 491.Fl v 492options are non-standard extensions supported by 493.Fx , 494but which may not be available on other operating systems. 495.Sh HISTORY 496The 497.Nm 498command appeared in 499.Bx 4.4 . 500The 501.Fl P , S 502and 503.Fl v 504options were added in 505.Fx 6.0 . 506The 507.Fl 0 , L 508and 509.Fl U 510options were added in 511.Fx 13.0 . 512.Sh BUGS 513The 514.Nm 515utility does not handle values of 516.Ar utility 517which have an equals sign 518.Pq Ql = 519in their name, for obvious reasons. 520.Pp 521The 522.Nm 523utility does not take multibyte characters into account when 524processing the 525.Fl S 526option, which may lead to incorrect results in some locales. 527