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