env Command in Linux/Unix
The env
command is used to display and manipulate environment variables in a Unix or Linux shell. It can be employed to list existing environment variables, set new ones, or run a command with a modified environment.
Syntax:
env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]
Example:
env
When the env
command is executed without options, it prints the current environment variables along with their values.
Example Usage:
$ env
This command will display the current environment variables and their values.
Options:
- The
env
command can be used with various options to modify the behavior:
-i
or --ignore-environment
: Start with an empty environment.
$ env -i
-u
or --unset=NAME
: Remove variable from the environment.
$ env -u MY_VARIABLE
-0
or --null
: Delimit items with null bytes.
$ env -0
Example with Options
$ env -i HOME=/newhome USER=myuser /bin/bash
This command starts a new shell with a modified environment where the HOME
the variable is set to /newhome
and the USER
the variable is set to myuser
.
Purpose and Usage
The primary purpose of the env
command is to provide a clean and controlled environment for executing commands.
It is often used in scripts or shell commands to ensure a specific environment for a particular task.
The env
the command can be useful when you want to run a command with a modified environment without permanently changing the shell environment.