Linux env command: A Deep Dive into Linux Environment Management

The env command in Linux is used to display or set environmental variables. It can be used to either run a program in a modified environment or to display the current one.

In this tutorial, we’ll delve into its various arguments, and uncover its integration into scripting.

 

 

Overview of parameters

The env command supports various parameters that extend its functionality:

  • No arguments: Displays all environment variables.
  • -u or --unset: Removes an environment variable.
  • -i or --ignore-environment: Launches a command in a clean environment.
  • -0 or --null: Uses a null character instead of newline as a separator.
  • -v or --debug: Provides verbose output to help debug.
  • -P or --path: Uses a custom PATH for command execution.

Each argument serves a unique purpose, which we will delve into further in the following sections.

 

Executing the env command with no arguments

When the env command is executed without any arguments, it displays all the environment variables available in the session.

$ env

Output:

USER=username
HOME=/home/username
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
...

These variables include user-specific settings (USER, HOME), system configurations (PATH, SHELL), and many others depending on the system and the applications installed.

 

Set New Environment Variables with env

By specifying a key-value pair after the env command, you can set a new environment variable.

$ env NEW_VARIABLE=value

Output:

... (other environment variables) ...
NEW_VARIABLE=value
... (other environment variables) ...

In the above example, a new environment variable named NEW_VARIABLE is set with the value value.

This change is temporary and will only be valid for the duration of the session unless exported or set in a configuration file.

 

Modifying Existing Environment Variables

You can modify an existing environment variable using env command like this:

$ env PATH=$PATH:/opt/my_new_path

Output:

... (other environment variables) ...
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/my_new_path
... (other environment variables) ...

Here, the existing PATH variable is extended by appending :/opt/my_new_path to its current value.

 

Unsetting Environment Variables

You can use the -u parameter of env command to unset environment variables:

$ env -u NEW_VARIABLE

After running the above command, if you check the list of environment variables, the NEW_VARIABLE will no longer be present.
Remember, this change is temporary and affects only the current session.

 

Set variables and execute commands in one line

You can set environment variables before command execution which will be valid during its execution only.

$ env TEMP_DIR=/tmp/temporary ls $TEMP_DIR

Output:

file1.txt  file2.txt  ...

In this scenario, the TEMP_DIR variable is set to /tmp/temporary, and immediately afterward, the ls command is executed to list the contents of the specified directory.

The environment variable TEMP_DIR is temporary and is valid only for the duration of the executed command.

 

Suppressing Environment Inheritance

The -i or --ignore-environment option allows you to run a command in an environment stripped of all existing variables.

$ env -i ls

Output:

... (standard output of ls, but no environment variables present) ...

In the above example, the ls command is executed, but no environment variables from the parent process are passed to it.

This ensures that the command runs in a completely clean environment, unaffected by any previous settings or configurations.

 

Using env -0

The -0 or --null option modifies the env command’s output to use a null character (\0) instead of newline as a separator between different environment variables.

$ env -0

Output:

USER=username\0HOME=/home/username\0SHELL=/bin/bash\0...

This can be particularly useful when working with tools or scripts that expect null-terminated strings, such as the xargs -0 command.

Let’s say you’re concerned about variables that contain the terms “password”, “secret”, “token”, or “key”.

You can use env -0 combined with grep -zi to search in a case-insensitive manner and handle null-terminated data:

$ env -0 | grep -ziE 'password|secret|token|key' | xargs -0 -n1

The grep command searches for any of the terms in a case-insensitive manner (-i) and uses extended regular expressions (-E) to match any of the terms.

The -z option ensures it operates on null-terminated lines.

 

Using -v or –debug for debugging

The -v or --debug option provides verbose information about the env command’s internal operations.

$ env -v

Output:

initializing ...
importing: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
importing: USER=username
... (more detailed output) ...

It is beneficial for debugging purposes, especially when you want to see how env is processing or modifying environment variables during execution.

 

Using a custom PATH for command execution

By using the -P or --path option, you can provide a custom PATH for the execution of the subsequent command.

$ env -P /custom/path/bin: ls

Output:

... (output of ls executed using binaries from the specified PATH) ...

This is especially useful if you want to run a command using binaries from a specific directory or to override the default system utilities temporarily.

 

Writing a script that uses env

Consider the following script named sample_script.sh:

#!/usr/bin/env bash

echo "Current USER: $USER"
echo "Shell: $SHELL"

When executed:
Output:

Current USER: username
Shell: /bin/bash

In this script, the shebang line (#!/usr/bin/env bash) ensures that the script runs with the bash interpreter as located by the system’s environment PATH.

This way, it becomes more portable across different systems where the bash binary might be in different locations.

 

Explanation of the shebang (#!/usr/bin/env)

The shebang (#!) is the initial two-byte sequence in a script that specifies its interpreter for execution.

When combined with /usr/bin/env, it ensures that the script uses the interpreter specified by the system’s environment PATH.
For example:

#!/usr/bin/env python3

This shebang ensures that the script is executed using the python3 interpreter as determined by the system’s PATH.

It’s a common approach to make scripts more portable since the exact path of the interpreter might differ across systems.
Using /usr/bin/env provides a level of indirection, allowing the system to locate the desired interpreter without having to specify its absolute path in the script.

 

Resources

https://man7.org/linux/man-pages/man1/env.1.html

Leave a Reply

Your email address will not be published. Required fields are marked *