How to show an environment variable's current value?

Just:

echo "$VARIABLENAME"

For example for the environment variable $HOME, use:

echo "$HOME"

Which then prints something similar to:

/home/username

Edit: according to the comment of Stéphane Chazelas, it may be better if you use printenv instead of echo:

printenv HOME

By executing:

printenv

You will see all environment variables. For more info you can take a look at:

https://www.digitalocean.com/community/tutorials/how-to-read-and-set-environmental-and-shell-variables-on-a-linux-vps


It is important to understand that every process has its own set of environment variables.

When a process calls the fork() system call, a second process (the child) identical to the first (the parent) is created (this copy includes the environment, which resides just above the stack (or just below, depending how you think of stacks :-) - but in unix/linux the stack grows down from high addresses).

Usually, the child process will then call the execve() system call, which will throw away everything in its (virtual) memory and reconstruct it from the code and data sections in the specified binary file.

However, when it reconstructs the stack, it copies the environment and argument strings passed to execve() onto the stack first (in that order), before calling the main() function (a lot of the work is done in the crt0 bootstrap code after the execve() returns (to the entry point specified in the binary)).

There are wrappers for the execve() system call in the C library that will pass the current environment (i.e. a copy of the parents environment), instead of the caller providing it (so in effect the child will inherit the parent's environment) - see environ(7).

Try running (as root) the command ps axeww | less ... this will show you the environment for all processes! An interesting one is process id 1 (i.e. the init process - the first process created by the kernel at boot time).

If you want to look at the environment for a specific process (and you know it's process id), try running the command cat /proc/<PID>/environ (replacing <PID> with the process id).

Note that if a process has enough privileges, it can rewrite its own stack, which can make it difficult to know what its environment is - you will see some daemon processes like this in the ps output.

But in the end, all this waffle boils down to what @chaos said above, if you want to look at the current value of a specific environment variable in your shell process, just use the (builtin) command echo "$<NAME>" (replacing <NAME> with the name of the environment variable you are interested in) ... just be aware that the same variable may have a different value, or not exist at all, in another process.