env

env

The env command in Unix-like operating systems is used to manage the environment for processes. It can be used to display the current environment variables, set new environment variables, or run commands with a modified environment. Here's an overview of the env command and its usage:

Overview of env

Purpose: The env command is used to run a command in a modified environment or to display the current environment variables.

Basic Syntax

env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]

Common Uses and Examples

  1. Display Environment Variables:

    • Running env without any arguments will list all the current environment variables and their values.

      env

      Example output:

      PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
      HOME=/home/user
      USER=user
      SHELL=/bin/bash
  2. Run a Command with Modified Environment Variables:

    • You can use env to set environment variables for a single command execution without altering the global environment.

      env VAR=value command

      For example, to run echo $VAR with VAR set to hello:

      env VAR=hello echo $VAR

      Output:

      hello
  3. Remove All Environment Variables:

    • You can use the -i or --ignore-environment option to clear all environment variables and run a command in a clean environment.

      env -i command

      For example, to run a shell with no environment variables:

      env -i /bin/bash --noprofile --norc
  4. Unset an Environment Variable:

    • You can use env to run a command with specific environment variables unset.

      env -u VAR command

      For example, to run printenv without the USER environment variable:

      env -u USER printenv

Practical Examples

  1. Temporary PATH Modification:

    • Run a command with a modified PATH variable without changing the global PATH.

  2. Debugging with a Clean Environment:

    • Run a script or command in a clean environment to debug issues caused by environment variables.

  3. Setting Multiple Variables:

    • You can set multiple environment variables for a command execution.

      For example:

Environment Variables

Environment variables are key-value pairs that influence the behavior of processes and applications in the shell. Common environment variables include:

  • PATH: Specifies the directories to search for executable files.

  • HOME: Indicates the home directory of the current user.

  • USER: Represents the name of the current user.

  • SHELL: Specifies the path to the current shell.

Conclusion

The env command is a versatile tool for managing environment variables and running commands in customized environments. It's particularly useful for testing, debugging, and scripting, allowing precise control over the execution environment.

help

Last updated