Env: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(9 intermediate revisions by the same user not shown)
Line 8: Line 8:
=Overview=
=Overview=


Run a command with a modified environment.
Run a command with a modified environment, or just [[#No_Argument_Use|print the environment if no argument is provided]].


  /usr/bin/env [''utility'']
  /usr/bin/env [''utility'']


  /usr/bin/env bash
  /usr/bin/env bash
=Options=
==<tt>-u</tt>==
If the environment variable whose name follows <code>-u</code> is in the environment, remove it before processing the remaining options. The value for name must not include the ‘=’ character.
==<tt>-i</tt>==
Execute the command with only those environment variables specified by <code>name=value</code> options. The environment inherited by <code>env</code> is ignored completely.
=No Argument Use=
Displays environment values.
=Modify the Environment to Run a Command=
<syntaxhighlight lang='bash'>
env SOME_VAR='some value' printenv
</syntaxhighlight>
   
   
=Standalone Use=
will display, among other environment variables, SOME_VAR.


Displays environment values.
==Use <tt>env</tt> to Dynamically Modify <tt>PATH</tt>==
 
<syntaxhighlight lang='bash'>
python --version # returns one version
env PATH=/tmp/some-venv:${PATH} python --version # returns a different version
</syntaxhighlight>


=Use on the Shebang Line=
=Use on the Shebang Line=

Latest revision as of 02:07, 20 February 2024

External

Internal

Overview

Run a command with a modified environment, or just print the environment if no argument is provided.

/usr/bin/env [utility]
/usr/bin/env bash

Options

-u

If the environment variable whose name follows -u is in the environment, remove it before processing the remaining options. The value for name must not include the ‘=’ character.

-i

Execute the command with only those environment variables specified by name=value options. The environment inherited by env is ignored completely.

No Argument Use

Displays environment values.

Modify the Environment to Run a Command

env SOME_VAR='some value' printenv

will display, among other environment variables, SOME_VAR.

Use env to Dynamically Modify PATH

python --version # returns one version
env PATH=/tmp/some-venv:${PATH} python --version # returns a different version

Use on the Shebang Line

https://www.baeldung.com/linux/bash-shebang-lines

The env command works by instructing the system to look for the specified interpreter through the PATH variable and use the first occurrence found.