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