Ubuntu – Environment variable in /etc/environment with pound (hash) sign in the value

environment-variablesUbuntu

On Ubuntu 12.04, I have an environment variable defined in /etc/environment like this:

FOO="value_before#value_after"

When I ssh into the server to check the value, I get this:

$ env | grep FOO
FOO=value_before

I'm guessing it is treating the # as a comment and stripping it out, however, this works:

$ . /etc/environment
$ export FOO
$ env | grep FOO
FOO=value_before#value_after

I've tried escaping the # like this:

FOO="value_before\#value_after"

But that doesn't work, instead I just get this:

FOO=value_before\

Any ideas on how to make the hash be treated like part of the value? Any help would be great.

Values I've tried in the /etc/environment file:

FOO='value_before#value_after'
FOO="value_before#value_after"
FOO='"value_before#value_after"'
FOO="value_before\#value_after"
FOO='value_before\#value_after'

And other various combinations of the above. A lot of these will work when you just normally set them in the shell. But they don't seem to work in the /etc/environment file.

Best Answer

This is read by the pam_env module. Given that the pam_env module expects them to be "simple" KEY=VALUE pairs (doesn't need quotes) and also supports comments identified by #, it assumes that a # and anything following it in a VALUE are a comment. Also, note that it does not support any concept of escaping.

This can be seen in the following snippet from the _parse_env_file function in pam_env.c.

/* now find the end of value */
mark = key;
while(mark[0] != '\n' && mark[0] != '#' && mark[0] != '\0')
    mark++;
if (mark[0] != '\0')
    mark[0] = '\0';

The above snippet walks each character of the VALUE portion until it finds a \n, # or \0. It then overwrites that character with a \0.

This effectively strips off the # and everything following. Note: This is a feature not a bug. It is the comment feature.

So, at this point you cannot have values in /etc/environment that include a # or a \n or \0 in the middle of the value. It also looks like from the code that the keys need to be alpha-numeric.