Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Softpanorama Search

Unix env command

News

BASH

Best Shell Books

Recommended Links Shell functions

Dotfiles

The external env command creates a new environment for executing a given command. Depending on the options, env either uses the current environment or completely ignores it. If env uses the environment, modifications are made based on the arguments given. After the new environment is set up, the command argument is executed.

The env command is useful for testing commands in controlled environments. It may be used to execute a command in a new environment without affecting your own personal environment. It is probably used more to display the current environment than as a command executor.

Following is the general format of the env command.

     env [ - ] [ name=value ... ] [ command [ options ] [ args ] ]

Options

The only valid option for env is described as follows:

- Do not pass the current environment to the command being executed by env.

Arguments

The following list describes the arguments that may be passed to the env command.

name=value Variables may be assigned values that will be exported for the command to use during execution.
command The command that env executes with the new environment. The options and arguments are those of the command being executed; they are not interpreted by env.

If no arguments are specified, env displays all exported variables.

 

Arguments must be of the form variable=value. These arguments are merged into the existing environment. The remainder of the arguments are executed as a normal command. The command executes with the newly defined environment. After the command exits, the environment returns to the state it was before the env command was executed.

The following command shows the changes made to the environment by env:

     cj> env HOME=/tmp TERM=adm5 env
     ENV=/u1/ts/mylogin/.kshrc
     FCEDIT=vi
     HOME=/tmp
     LOGNAME=mylogin
     MAIL=/usr/mail/mylogin
     PATH=:/u1/ts/mylogin/bin:/bin:/usr/bin:/usr/lbin:/usr/local/bin
     SHELL=/bin/ksh
     TERM=adm5
     TMPDIR=/tmp
     TZ=CST6CDT
     VISUAL=vi

The env command modifies your HOME and TERM variables and then executes the env command again to show the changes in the new temporary environment.

If you do not wish to have the current environment passed to the command for execution, you can use the - (hyphen) option. Only the variables you define on the env command line will be passed to the command. The following command demonstrates the use of the dash option:

     cj> env - HOME=/tmp TERM=adm5 env
     HOME=/tmp
     TERM=adm5

The env command will create a new temporary environment with only the HOME and TERM variables defined.

If no arguments are given, env lists out the current environment with one variable and value per line.


Copyright © 1996-2009 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Disclaimer:

Last modified: August 08, 2009