5

I have seen other people on the Internet also having this problem but the solutions have been non-conclusive, so I wanted to bring it to attention again.

According to this page in Ubuntu wiki: https://help.ubuntu.com/community/EnvironmentVariables the recommended way to set session-wide environment variables is to modify ~/.pam_environment.

Here is what my goal is:

  1. I want to create an environment variable ANDROID_HOME that would have a path to a certain location in my home folder as a value.
  2. I want to add two folders to the PATH.

Here is what I did. The ~/.pam_environment file was non-existent, so I created it and added the following lines to it:

ANDROID_HOME=${HOME}/Android/Sdk
PATH DEFAULT=${PATH}:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools

Logged out and logged back in.

The result is not expected. I opened terminal and echoed $ANDROID_HOME and that's what I got:

${HOME}/Android/Sdk

Looks like the ${HOME} is not replaced with my home folder path. Why is that so?

Observe cd-ing to $ANDROID_HOME (the Android/Sdk actually exists in my home folder):

anvar@crazymachine:~$ cd $ANDROID_HOME
bash: cd: ${HOME}/Android/Sdk: No such file or directory

Also echoing $PATH gave me this:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:${HOME}/Android/Sdk/tools:${HOME}/Android/Sdk/platform-tools

Why are the variables not substituted with their values like would be logical to assume, especially when the example in the aformentioned site gave such an impression that variables can be used inside the values?

What is the right way to set session-wide environment variables that also GUI programs have access to?

1
  • The format for ~/.pam_environment is described at pam_env.conf(5).
    – Daniel
    Commented Jul 9, 2019 at 13:48

3 Answers 3

3

The example in the wiki, for setting session variables via ~/.pam_environment, states in a note:

The syntax used for modifying PATH, which syntax differs from script files, is required for variable expansion to work.

Hence your first row should look like:

ANDROID_HOME DEFAULT=${HOME}/Android/Sdk
5
  • Nice, actually this is indeed the answer. Thanks!
    – alvarez
    Commented Nov 22, 2015 at 20:55
  • +1..this seems very inconsistent..while the official doc says the HOME could not be set and that makes perfect sense as the file might be parsed by pam_env.so before parsing /etc/passwd while starting a session and pam_env is used by almost all known login sessions.....anyway thanks for correcting me, i will dig more..
    – heemayl
    Commented Nov 23, 2015 at 1:32
  • @heemayl: As long as lightdm is the display manager, I suppose it's safe. Please note that there are quite a few other PAM config files in the /etc/pam.d folder. Commented Nov 23, 2015 at 4:54
  • @GunnarHjalmarsson Yeah, i have checked them all..now almost all session initiators e.g. lightdm, login, su use pam and they use pam_env.so to set the environment..my concern was that if there were any precedence in timing that might affect the variable expansion..
    – heemayl
    Commented Nov 23, 2015 at 5:56
  • @heemayl: There are such variables. For instance, variables set in ~/.profile or /etc/profile.d/*.sh are set after ~/.pam_environment has been read, and can consequently not be referred to. But isn't HOME one of the first variables which is set in the session initialization process? Commented Nov 23, 2015 at 11:29
0

Two suggestions:

1st - the form of your environmental statements should be:

ANDROID_HOME=$HOME/Android/Sdk
PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools

second, place your new environment statements in ~/.profile

5
  • This looks so malformed that I was really reluctant to even try it out but still tried for experimentation purposes and of course, attempting to re-login gave me an error message box saying "PATH: command not found".
    – alvarez
    Commented Nov 22, 2015 at 16:35
  • Strange - it looks just like the statements in the default ./profile for setting path... Commented Nov 22, 2015 at 16:48
  • Nope. If you look more closely, your example has a "DEFAULT" written after the "PATH". Remember, bash syntax and .pam_environment syntax are completely different.
    – alvarez
    Commented Nov 22, 2015 at 17:08
  • Ah - I was not looking closely enough when I copied and pasted from your question. I'll update the answer Commented Nov 22, 2015 at 17:13
  • Sorry, still not good enough. The ANDROID_HOME in your example is not exported, so it will not be available to terminal or other programs. Alas, if I export the variable then yes, everything works but I was mostly interested in .pam_environment approach because it's above the .profile approach in the wiki. I guess the verdict is - "do not use .pam_environment because it is error prone and wiki is lying." and we can forget .pam_environment from now on and use plain old .profile.
    – alvarez
    Commented Nov 22, 2015 at 17:29
-1

I think you may need an export HOME after setting up the path variable (in ~/.bashrc we do)

1
  • 2
    You mean adding export HOME=/home/anvar? I have never had a situation this variable should be explicitly manually set because it is set by default. And when I echo $HOME, the value is correct.
    – alvarez
    Commented Nov 22, 2015 at 16:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .