The env
command in Bash is used to run a command in a modified environment. It allows you to set or unset environment variables for the command you want to execute, making it a useful tool for testing and running applications with specific configurations.
The basic syntax of the env
command is as follows:
env [options] [arguments]
-i
: Start with an empty environment, ignoring the current environment variables.-u NAME
: Remove the variable NAME from the environment.-0
: Use a null character as the delimiter instead of a newline (useful for handling filenames with spaces).env VAR=value command
This sets VAR
to value
only for the execution of command
.
env -i bash
This starts a new Bash shell with no inherited environment variables.
env -u VAR command
This runs command
without the environment variable VAR
.
env
This displays all the environment variables currently set in the shell.
env PATH=/custom/path ./script.sh
This executes script.sh
with a modified PATH
.
env
to test scripts in a clean environment to avoid unexpected behavior due to inherited variables.env
to ensure that your scripts run with the correct interpreter by specifying it at the top of the script (e.g., #!/usr/bin/env python
).-i
, as it will remove all environment variables, which might affect the execution of commands that rely on them.