EXAMPLES
Since the env utility is often used as part of the first line of an interpreted script, the following examples show a number of ways that the env utility can be useful in scripts. The kernel processing of an interpreted script does not allow a script to directly reference some other script as its own interpreter. As a way around this, the main difference between
#!/usr/local/bin/foo
and
"#!/usr/bin/env /usr/local/bin/foo"
is that the latter works even if /usr/local/bin/foo is itself an interpreted script.
Probably the most common use of env is to find the correct interpreter for a script, when the interpreter may be in different directories on different systems. The following example will find the perl interpreter by searching through the directories specified by PATH.
"#!/usr/bin/env perl"
One limitation of that example is that it assumes the users value for PATH is set to a value which will find the interpreter you want to execute. The -P option can be used to make sure a specific list of directories is used in the search for utility. Note that the -S option is also required for this example to work correctly.
"#!/usr/bin/env -S -P/usr/local/bin:/usr/bin perl"
The above finds perl only if it is in /usr/local/bin or /usr/bin. That could be combined with the present value of PATH, to provide more flexibility. Note that spaces are not required between the -S and -P options:
"#!/usr/bin/env -S-P/usr/local/bin:/usr/bin:${PATH} perl"
COMPATIBILITY
The env utility accepts the
option as a synonym for -i .
SEE ALSO
printenv(1), sh(1), execvp(3), environ(7)
STANDARDS
HISTORY
BUGS