If your work involves doing stuff through the command line in Linux, more likely than not, you'll be using the Bash shell. Did you know there's an environment associated with the processes that are executed through the shell? And, if you want, you can tweak this environment specifically for the task you're doing? Well, in this article we will discuss a command line tool - env - that not only lets you access the shell environment, but also allows you to make changes to it as an when required.
But before we do that, it's worth mentioning that all commands and instructions mentioned in this tutorial have been tested on Bash shell running on Ubuntu 16.04LTS.
Linux env command
By definition, the env command lets you run a program in modified environment. Following is the syntax of the command, as mentioned on its man page:
env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]
And here's what the man page says about env:
Set each NAME to VALUE in the environment and run COMMAND.
Following Q&A-styled examples should give you a better idea on how this command works:
Q1. How to access all environment variables using env command?
Bash environment, as you might already know, consists of VARNAME=VALUE entries. To access all environment variables as well as the values associated with them, execute the env command without any option.
Here's the output of the above command in our case:
Q2. How to temporarily change environment using env?
The key feature env offers is the ability to temporarily change environment for a process. For example, we created a small executable - dubbed env - that displays the value of the USER environment variable upon execution.
Here's the output in normal scenario:
Now, what we did was, we used the env command to temporarily change the value of USER environment variable from 'himanshu' to 'HTF' for the executable/process. Following is the command we used in this case:
env USER=HTF ./env
And here's the output produced in this case:
So you can see that the executable returned the new value.
Note: As suggested by the tool's generic syntax, you can tweak values of multiple environment variables and make the process use these new values.
Q3. How to make a process ignore existing environment using env?
If you want, you can also make a process ignore existing/inherited environment, and start with an empty one instead. This can be done using the -i or --ignore-environment option.
Q4. How make env use NUL instead of newline character in output?
In the first example we discussed above, the output lines produced by env are separated by newline. However, if you want, you can make env use the NUL character as separator. This feature can be accessed using the --null command line option.
Following is an example screenshot;
Q5. How to know the error based on env command exit status?
The env command produces the following exit codes: 0, 125, 126, and 127. Following are the associated error descriptions:
0 if no COMMAND is specified and the environment is output
125 if ‘env’ itself fails
126 if COMMAND is found but cannot be invoked
127 if COMMAND cannot be found
In case you get an error code other than the ones mentioned above, then that's the exit status returned by the process/command that was executed with modified environment.
If you are a complete command line newbie, chances are that you won't be requiring this tool on daily basis. However, that's not to say it's not worth knowing about - in fact, env comes in really handy in many situations. The examples we've shared in this tutorial should be enough to give you a head start. For more information, head to the command's man page, or better, execute the following command:
info coreutils env invocation