How does one properly escape a leading "#" character in linux etc/environment?

I would like to store a system-wide environment variable in etc/environment on AWS Linux AMI 2013.03. In this example, I am trying to store the phrase "#hello". See the examples below. echo $control produces

hello

I expected echo $test0 (below) to produce:

#hello

but instead it produces a blank line. The following is list of tests I typed into /etc/environment:

  • control="hello"
  • test0="#hello"
  • test1="h\\#ello"
  • test2="h\#ello"
  • test3="h//#ello"
  • test4="h/#ello"
  • test5=h#ello
  • test6=h\\#ello
  • test7=h\#ello
  • test8=h//#ello
  • test9=h/#ello
  • test10='h#ello'
  • test11='h\\#ello'
  • test12='h\#ello'
  • test13='h//#ello'
  • test14='h/#ello'

If I log out then log back into the machine, it will correctly parse /etc/environment. The control condition passes on echo $control. However, none of the test cases correctly respond. In each instance, the string beyond the hash (#) is either chomped or ignored. For example, echo $test5 produces h. Is there a better way to properly escape the #?

Interestingly, if I execute source /etc/environment then all works as expected and indeed both echo $control and echo $test produce the intended result. But, upon logout/login, the problem returns...


Solution 1:

Well it is tricky stuff you want to do /etc/environment is not shell syntax, it looks like one , but it is not, which does frustrates people. The idea behind /etc/environment was wonderful. A shell-independent way to set up variables! Yay! But the practical limitations make it useless.

You can't pass variables in there. Try for example put MAIL=$HOME/Maildir/ into it and see what happens. Best just to stop trying to use it for any purpose whatsoever, alas. So you can't do things with it that you would expect to be able to do if it were processed by a shell.

Use /etc/profile or /etc/bashrc.