Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

After taking a look at the env command man page I was curious to see an example of a utility program that would be used in the usage noted below?


env [-i] [name=value ...] [utility [argument ...]]


env executes utility after modifying the environment as specified on the command line. The option name=value specifies an environmental variable, name, with a value of value. The option `-i' causes env to completely ignore the environment it inherits.

share|improve this question
up vote 5 down vote accepted

A common use is to launch interpreters, by making use of the fact that env will search $PATH for the command it is told to launch. Since the shebang line requires an absolute path to be specified, and since the location of various interpreters (perl, bash, python) may vary a lot, it is common to use:

#!/usr/bin/env perl

instead of trying to guess whether it is /bin/perl, /usr/bin/perl, /usr/local/bin/perl, /usr/local/pkg/perl, /fileserver/usr/bin/perl, or /home/MrDaniel/usr/bin/perl on the user's system...

On the other hand, env is almost always in /usr/bin/env. (Except in cases when it isn't; some systems might use /bin/env, but that's a fairly rare occassion and only happens on non-Linux systems.)

Another use is to quickly clear the environment using the -i option. In legacy sysvinit initscripts, which are just shell scripts often launched directly by the sysadmin, it is necessary to make sure the admin's environment does not propagate to the started daemon. (For example, a rogue $TZ or $HOME might make things really confusing – especially with certain cron daemons.)

In this case, the initscript prepares a short environment and starts the daemon using something like:

env -i "PATH=/bin:/usr/bin" "LANG=$system_locale" /usr/sbin/crond
share|improve this answer

It's really any program. It is most useful for all those that use any value defined as environment variable. You can pass an environment variable value to any program without actually setting it in your shell (and thereby influencing other programs down the line).

env ls runs ls without modifying the environment. env -i ls resets the environment. On my system, this removes colors from its output, since it unsets LSCOLORS and CLICOLOR. env LSCOLORS=GxfxcxhxdxegdggbgdAhAg ls will run ls, passing the specified LSCOLORS string and possibly changing how the ls output is colored.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .