Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way I can see the environment variable of an other user? I want to do that as root, so permissions won't be a problem I guess.

For the user himself, I use echo $PATH or set | grep PATH (or set when I don't remember the variable name). What would be a similar instruction for an other user?

For what it's worth, I'm on Ubuntu Server 13.04.

share|improve this question

closed as not a real question by Gilles, jasonwryan, slm, Anthon, manatwork Jun 4 '13 at 7:45

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

Users don't have environment variables, processes do. It's common for the processes executed by one user to not all have the same environment. What problem are you trying to solve? – Gilles Jun 3 '13 at 18:51
@Gilles for example, I want to know what MAIL folder a user has. I could login as that user, then do echo $MAIL, but I thought there might be a shortcut. – Camil Staps Jun 3 '13 at 19:04
That still doesn't define the question usefully (and for example the answer you've accepted may or may not work depending on a lot of factors). Are you looking for the default mailbox location? Are you looking for the location where the user actually receives his local mail (which may be different, and may not exist, if the user has a .forward)? Users often set a few variables in their .profile or other configuration files, and there's no way to reliably enumerate them all. Again, what problem are you trying to solve? – Gilles Jun 3 '13 at 19:14
up vote 5 down vote accepted

Another option is to use env. Run this as root :

sudo -Hiu $user env | grep $var

For example

sudo -Hiu terdon env | grep HOME
share|improve this answer
When I log in as root and execute this command for my standard user name, I get HOME=/root. That's the home directory of root, but it's not my standard home directory. If you want to know the environment that a particular user would get when he logs in, you have to run the startup scripts of the login shell of this particular user (and remember that his login shell may be different from your login shell). – Uwe Jun 3 '13 at 18:23
@Uwe on what system? It works fine for me. Do you mean you log in as root from the login manager? It works fine if I drop to a tty and log in as root. – terdon Jun 3 '13 at 18:34
I'm on a Debian machine. I log in as uwe, open an xterm, run env | grep HOME and get HOME=/home/uwe; I execute su and then once more env | grep HOME and get HOME=/root, then I run sudo -u uwe env | grep HOME and still get HOME=/root. – Uwe Jun 3 '13 at 18:42
@uwe strange. I am also on a Debian (LMDE) machine, I did the exact same process you described (even used xterm instead of my usual terminal) and get the expected result. – terdon Jun 3 '13 at 18:46
According to the manual, sudo sets the HOME variable only if the -H option is present or if the corresponding entry in /etc/sudoers is set. It seems that our sudoers files differ. Anyway, it's probably a good idea to use -H here, and possibly also -i. – Uwe Jun 3 '13 at 19:06

For one user, you can do like this:

su - <username> -c '. ~/.profile; echo $PATH'

List $PATH of all user:

for user in $(cat /etc/passwd | awk -F: '{print $1}'); do
  su - $user -c '. ~/.profile; printf "%s\n" "$PATH"'

@Camil Staps

. ~/.profile is thus a trick I learn from my favourite person, Peteris Krumins. He explained the trick here . Maybe later bash version had building with option NON_INTERACTIVE_LOGIN_SHELLS.

share|improve this answer
Thanks! For $PATH it also works without the . ~/.profile; part - what is it for? – Camil Staps Jun 3 '13 at 17:46
@CamilStaps What do you mean by "it works"? Yes, you get a value for $PATH even without sourcing ~/.profile, but it's almost certainly not the one that you want (= the value that this user would see when he logged in). Even sourcing ~/.profile isn't fully reliable, since the user might use a shell whose startup file is different from ~/.profile. – Uwe Jun 3 '13 at 18:29
@Uwe I got the same output without, but I guess that's just a coincidence then. – Camil Staps Jun 3 '13 at 18:41
You're assuming the user is not using csh. – glenn jackman Jun 3 '13 at 19:09
@CamilStaps The first question is whether that particular user modifies his PATH in his ~/.profile at all. But in any case, the behavior of sudo can be configured quite a lot in /etc/sudoers (see my discussion with terdon), so when our systems behave differently, that's probably caused by differences in the configuration files. – Uwe Jun 3 '13 at 19:19

from root you can 'su -' to the user and then grep the environment variable that you want to see

share|improve this answer
I was hoping on a one-line instruction. Pretty please? :) – Camil Staps Jun 3 '13 at 17:37
su - <username> -c 'echo $PATH' – Raza Jun 3 '13 at 17:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.