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