Using the `env` Command (with examples)
The env
command in Linux allows you to either view the current environment variables or run a program in a modified environment. This article will explore different use cases of the env
command and provide code examples for each use case.
Use Case 1: Show the Environment
To view the current environment variables, simply execute the env
command:
env
Motivation:
Sometimes, you may need to check the environment variables set on your system. By using the env
command, you can easily view all the environment variables currently available.
Example Output:
USER=JohnDoe
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
HOME=/home/johndoe
...
Use Case 2: Run a Program
To run a program in the current environment, use the env
command with the name of the program as an argument:
env program
Motivation:
Using the env
command before running a program allows you to run the program even if its executable path is not specified or is not in your system’s default path. This is commonly used in scripts after the shebang (#!) to ensure the correct program is executed.
Example Output:
Hello World!
Use Case 3: Clear the Environment and Run a Program
To clear the environment variables and run a program, use the -i
option followed by the name of the program:
env -i program
Motivation: Clearing the environment variables can be useful when you want to run a program in an isolated environment, without any interference from the current environment variables.
Example Output:
Hello World!
Use Case 4: Remove Variable from the Environment and Run a Program
To remove a specific variable from the environment and run a program, use the -u
option followed by the name of the variable, and then specify the name of the program:
env -u variable program
Motivation: Removing a specific variable from the environment ensures that the program being executed will not access the value of that variable. This can be handy when you want to isolate the program from a particular variable.
Example Output:
Hello World!
Use Case 5: Set a Variable and Run a Program
To set a specific variable and run a program in the modified environment, use the variable=value
format before the name of the program:
env variable=value program
Motivation: Sometimes, you may need to set a specific variable before running a program to provide the program with necessary information or to override an existing variable temporarily.
Example Output:
Value of variable: 5
Use Case 6: Set Multiple Variables and Run a Program
To set multiple variables and run a program in the modified environment, use the variable=value
format for each variable before the name of the program:
env variable1=value1 variable2=value2 variable3=value3 program
Motivation:
When running a program, you may need to set multiple variables in order to customize its behavior or provide it with necessary information. The env
command allows you to set multiple variables easily.
Example Output:
Value of variable1: Hello
Value of variable2: World
Value of variable3: 123
By using the env
command with its various options and arguments, you can easily interact with the environment variables in Linux and run programs in modified environments. Whether you need to view, modify, or isolate the environment, the env
command offers flexibility and control.