1

When you set an environment variable in linux is it set only for the user who executes it, or can you specify a user when you store it, or instead is it always available for all users?

2
  • What exactly do you want to achieve ? – user9517 Mar 19 '14 at 16:51
  • 1
    I want to know if I can set an env var for a single user, so that if it tries to be recalled by another user it cannot be – J.Zil Mar 19 '14 at 16:53
2

If you set environment variables in the global shell startup scripts, they'll be applied to all users who log in. The location of these scripts depends on the shells, but common locations are /etc/environment, /etc/profile, /etc/profile.d, /etc/bashrc, and /etc/X11/Xsession.d. You need to read the manuals for your shell(s)/desktop environments to find out for sure. For example, for bash see the section "INVOCATION" in the bash manual.

To set an environment variable for just a single user, you could either set it in that user's startup scripts (~/.bashrc, ~/.profile, ~/.xsessionrc, ...), or in the global startup scripts with a test to apply it only to that user.

3
  • Can you tell apply it to one user only if that user does not have a home directory? – J.Zil Mar 19 '14 at 17:16
  • 1
    A simple line in your Global profile like this perhaps [ "$USER" = 'jwillson' ] && export foo=blah. – Zoredache Mar 19 '14 at 18:57
  • You could also look at pam_env linux.die.net/man/8/pam_env – Zoredache Mar 19 '14 at 18:58
1

You can set a user's variable in their ( /home/user ) .profile or .bashrc , they will be set at (Bash) login time and they won't be set for other users.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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