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 March 3, 2021 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.\" 177.Ss Details of -S (split-string) processing 178The processing of the 179.Fl S 180option will split the given 181.Ar string 182into separate arguments based on any space or <tab> characters found in the 183.Ar string . 184Each of those new arguments will then be treated as if it had been 185specified as a separate argument on the original 186.Nm 187command. 188.Pp 189Spaces and tabs may be embedded in one of those new arguments by using 190single 191.Pq Dq Li ' 192or double 193.Pq Ql \&" 194quotes, or backslashes 195.Pq Ql \e . 196Single quotes will escape all non-single quote characters, up to 197the matching single quote. 198Double quotes will escape all non-double quote characters, up to 199the matching double quote. 200It is an error if the end of the 201.Ar string 202is reached before the matching quote character. 203.Pp 204If 205.Fl S 206would create a new argument that starts with the 207.Ql # 208character, then that argument and the remainder of the 209.Ar string 210will be ignored. 211The 212.Ql \e# 213sequence can be used when you want a new argument to start 214with a 215.Ql # 216character, without causing the remainder of the 217.Ar string 218to be skipped. 219.Pp 220While processing the 221.Ar string 222value, 223.Fl S 224processing will treat certain character combinations as escape 225sequences which represent some action to take. 226The character escape sequences are in backslash notation. 227The characters and their meanings are as follows: 228.Pp 229.Bl -tag -width indent -offset indent -compact 230.It Cm \ec 231Ignore the remaining characters in the 232.Ar string . 233This must not appear inside a double-quoted string. 234.It Cm \ef 235Replace with a <form-feed> character. 236.It Cm \en 237Replace with a <new-line> character. 238.It Cm \er 239Replace with a <carriage return> character. 240.It Cm \et 241Replace with a <tab> character. 242.It Cm \ev 243Replace with a <vertical tab> character. 244.It Cm \e# 245Replace with a 246.Ql # 247character. 248This would be useful when you need a 249.Ql # 250as the first character in one of the arguments created 251by splitting apart the given 252.Ar string . 253.It Cm \e$ 254Replace with a 255.Ql $ 256character. 257.It Cm \e_ 258If this is found inside of a double-quoted string, then replace it 259with a single blank. 260If this is found outside of a quoted string, then treat this as the 261separator character between new arguments in the original 262.Ar string . 263.It Cm \e" 264Replace with a <double quote> character. 265.It Cm \e\' 266Replace with a <single quote> character. 267.It Cm \e\e 268Replace with a backslash character. 269.El 270.Pp 271The sequences for <single-quote> and backslash are the only sequences 272which are recognized inside of a single-quoted string. 273The other sequences have no special meaning inside a single-quoted 274string. 275All escape sequences are recognized inside of a double-quoted string. 276It is an error if a single 277.Ql \e 278character is followed by a character other than the ones listed above. 279.Pp 280The processing of 281.Fl S 282also supports substitution of values from environment variables. 283To do this, the name of the environment variable must be inside of 284.Ql ${} , 285such as: 286.Li ${SOMEVAR} . 287The common shell syntax of 288.Li $SOMEVAR 289is not supported. 290All values substituted will be the values of the environment variables 291as they were when the 292.Nm 293utility was originally invoked. 294Those values will not be checked for any of the escape sequences as 295described above. 296And any settings of 297.Ar name Ns = Ns Ar value 298will not effect the values used for substitution in 299.Fl S 300processing. 301.Pp 302Also, 303.Fl S 304processing cannot reference the value of the special parameters 305which are defined by most shells. 306For instance, 307.Fl S 308cannot recognize special parameters such as: 309.Ql $* , 310.Ql $@ , 311.Ql $# , 312.Ql $? 313or 314.Ql $$ 315if they appear inside the given 316.Ar string . 317.\" 318.Ss Use in shell-scripts 319The 320.Nm 321utility is often used as the 322.Ar interpreter 323on the first line of interpreted scripts, as 324described in 325.Xr execve 2 . 326.Pp 327Note that the way the kernel parses the 328.Ql #! 329(first line) of an interpreted script has changed as of 330.Fx 6.0 . 331Prior to that, the 332.Fx 333kernel would split that first line into separate arguments based 334on any whitespace (space or <tab> characters) found in the line. 335So, if a script named 336.Pa /usr/local/bin/someport 337had a first line of: 338.Pp 339.Dl "#!/usr/local/bin/php -n -q -dsafe_mode=0" 340.Pp 341then the 342.Pa /usr/local/bin/php 343program would have been started with the arguments of: 344.Bd -literal -offset indent 345arg[0] = '/usr/local/bin/php' 346arg[1] = '-n' 347arg[2] = '-q' 348arg[3] = '-dsafe_mode=0' 349arg[4] = '/usr/local/bin/someport' 350.Ed 351.Pp 352plus any arguments the user specified when executing 353.Pa someport . 354However, this processing of multiple options on the 355.Ql #! 356line is not the way any other operating system parses the 357first line of an interpreted script. 358So after a change which was made for 359.Fx 6.0 360release, that script will result in 361.Pa /usr/local/bin/php 362being started with the arguments of: 363.Bd -literal -offset indent 364arg[0] = '/usr/local/bin/php' 365arg[1] = '-n -q -dsafe_mode=0' 366arg[2] = '/usr/local/bin/someport' 367.Ed 368.Pp 369plus any arguments the user specified. 370This caused a significant change in the behavior of a few scripts. 371In the case of above script, to have it behave the same way under 372.Fx 6.0 373as it did under earlier releases, the first line should be 374changed to: 375.Pp 376.Dl "#!/usr/bin/env -S /usr/local/bin/php -n -q -dsafe_mode=0" 377.Pp 378The 379.Nm 380utility will be started with the entire line as a single 381argument: 382.Pp 383.Dl "arg[1] = '-S /usr/local/bin/php -n -q -dsafe_mode=0'" 384.Pp 385and then 386.Fl S 387processing will split that line into separate arguments before 388executing 389.Pa /usr/local/bin/php . 390.\" 391.Sh ENVIRONMENT 392The 393.Nm 394utility uses the 395.Ev PATH 396environment variable to locate the requested 397.Ar utility 398if the name contains no 399.Ql / 400characters, unless the 401.Fl P 402option has been specified. 403.Sh EXIT STATUS 404.Ex -std 405An exit status of 126 indicates that 406.Ar utility 407was found, but could not be executed. 408An exit status of 127 indicates that 409.Ar utility 410could not be found. 411.Sh EXAMPLES 412Since the 413.Nm 414utility is often used as part of the first line of an interpreted script, 415the following examples show a number of ways that the 416.Nm 417utility can be useful in scripts. 418.Pp 419The kernel processing of an interpreted script does not allow a script 420to directly reference some other script as its own interpreter. 421As a way around this, the main difference between 422.Pp 423.Dl #!/usr/local/bin/foo 424and 425.Dl "#!/usr/bin/env /usr/local/bin/foo" 426.Pp 427is that the latter works even if 428.Pa /usr/local/bin/foo 429is itself an interpreted script. 430.Pp 431Probably the most common use of 432.Nm 433is to find the correct interpreter for a script, when the interpreter 434may be in different directories on different systems. 435The following example will find the 436.Ql perl 437interpreter by searching through the directories specified by 438.Ev PATH . 439.Pp 440.Dl "#!/usr/bin/env perl" 441.Pp 442One limitation of that example is that it assumes the user's value 443for 444.Ev PATH 445is set to a value which will find the interpreter you want 446to execute. 447The 448.Fl P 449option can be used to make sure a specific list of directories is 450used in the search for 451.Ar utility . 452Note that the 453.Fl S 454option is also required for this example to work correctly. 455.Pp 456.Dl "#!/usr/bin/env -S -P/usr/local/bin:/usr/bin perl" 457.Pp 458The above finds 459.Ql perl 460only if it is in 461.Pa /usr/local/bin 462or 463.Pa /usr/bin . 464That could be combined with the present value of 465.Ev PATH , 466to provide more flexibility. 467Note that spaces are not required between the 468.Fl S 469and 470.Fl P 471options: 472.Pp 473.Dl "#!/usr/bin/env -S-P/usr/local/bin:/usr/bin:${PATH} perl" 474.Sh COMPATIBILITY 475The 476.Nm 477utility accepts the 478.Fl 479option as a synonym for 480.Fl i . 481.Sh SEE ALSO 482.Xr printenv 1 , 483.Xr sh 1 , 484.Xr execvp 3 , 485.Xr login.conf 5 , 486.Xr environ 7 487.Sh STANDARDS 488The 489.Nm 490utility conforms to 491.St -p1003.1-2001 . 492The 493.Fl 0 , L , P , S , U , u 494and 495.Fl v 496options are non-standard extensions supported by 497.Fx , 498but which may not be available on other operating systems. 499.Sh HISTORY 500The 501.Nm 502command appeared in 503.Bx 4.4 . 504The 505.Fl P , S 506and 507.Fl v 508options were added in 509.Fx 6.0 . 510The 511.Fl 0 , L 512and 513.Fl U 514options were added in 515.Fx 13.0 . 516.Sh BUGS 517The 518.Nm 519utility does not handle values of 520.Ar utility 521which have an equals sign 522.Pq Ql = 523in their name, for obvious reasons. 524.Pp 525The 526.Nm 527utility does not take multibyte characters into account when 528processing the 529.Fl S 530option, which may lead to incorrect results in some locales. 531