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.

  • 4
    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? Jun 3, 2013 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.
    – user37050
    Jun 3, 2013 at 19:04
  • 3
    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? Jun 3, 2013 at 19:14

3 Answers 3


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

sudo -Hiu $user env | grep $var

For example

sudo -Hiu terdon env | grep HOME
  • 1
    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, 2013 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, 2013 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, 2013 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, 2013 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, 2013 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.

  • Thanks! For $PATH it also works without the . ~/.profile; part - what is it for?
    – user37050
    Jun 3, 2013 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, 2013 at 18:29
  • @Uwe I got the same output without, but I guess that's just a coincidence then.
    – user37050
    Jun 3, 2013 at 18:41
  • You're assuming the user is not using csh. Jun 3, 2013 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, 2013 at 19:19

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

su - <username> -c 'echo $PATH'

  • for some reason this doesn't work for me, even if command is executed as a different user. I ended up using "env", so sth like runuser -l <username> -c "env | grep <var>" | cut -d '=' -f 2 works fine for me. If you're not root, need to prefix command with sudo. Mar 28, 2019 at 20:37