Linux - How to Print Environment Variables
If you are working on Linux systems there is a good chance that you will want to print your environment variables. It is important to be able to see what variables are in use on your system. You may find things that you didn’t even realize were defined.
You can print environment variables with the following Linux commands:
- printenv
- env
- set
You can print the value of a single variable like this:
echo $PATH
Printenv Command
The printenv command will print out all of your environment variables.
It shows all environment variables as key/value pairs if you run it without any parameters, like this:
printenv
You can also specify the specific variables you want to see like this:
printenv PAM_KWALLET5_LOGIN TERM COLORTERM
I’ve tested this command out on the following shells and it seems to work fine ( although each shell may have different variables defined ):
- bash
- ksh
- csh
- fish
Env Command
The env command is used to run a program with specific environment variables set. If you run it without any parameters it will give you similar output to the printenv command. You can run it like this:
env
It generally matches the output of the printenv command but it could be different. The env command is only supposed to output variables that have been exported whereas the printenv command should output all environment variables.
I’ve also tested env in bash, ksh, csh, and fish. It seems to work on all of these.
Set Command
This command is used for setting variables. It can also be used to print out environment variables in a similar way to printenv and env.