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:
Purpose: The env command is used to run a command in a modified environment or to display the current environment variables.
env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]-
Display Environment Variables:
- Running
envwithout any arguments will list all the current environment variables and their values.Example output:env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOME=/home/user USER=user SHELL=/bin/bash
- Running
-
Run a Command with Modified Environment Variables:
- You can use
envto set environment variables for a single command execution without altering the global environment.For example, to runenv VAR=value commandecho $VARwithVARset tohello:Output:env VAR=hello echo $VAR
hello
- You can use
-
Remove All Environment Variables:
- You can use the
-ior--ignore-environmentoption to clear all environment variables and run a command in a clean environment.For example, to run a shell with no environment variables:env -i commandenv -i /bin/bash --noprofile --norc
- You can use the
-
Unset an Environment Variable:
- You can use
envto run a command with specific environment variables unset.For example, to runenv -u VAR commandprintenvwithout theUSERenvironment variable:env -u USER printenv
- You can use
-
Temporary PATH Modification:
- Run a command with a modified
PATHvariable without changing the globalPATH.env PATH=/custom/path:$PATH some_command
- Run a command with a modified
-
Debugging with a Clean Environment:
- Run a script or command in a clean environment to debug issues caused by environment variables.
env -i /path/to/script.sh
- Run a script or command in a clean environment to debug issues caused by environment variables.
-
Setting Multiple Variables:
- You can set multiple environment variables for a command execution.
For example:
env VAR1=value1 VAR2=value2 commandenv DB_HOST=localhost DB_USER=root /path/to/db_connect_script.sh
- You can set multiple environment variables for a command execution.
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.
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.
env [options] [variable=value]
Print or set environment variables.
Options:
-i, --ignore-environment Do not inherit environment variables from the parent shell.
-u, --unset Unset environment variable.
-l, --list List all environment variables.
-v, --verbose Print each variable and its value.
-P, --path Print the current $PATH.
-S, --secure Print environment variables in a secure way.
Examples:
env
env HOME=/home/user
env -i
env -u HOME
env -l
env -v
env -P
env -S