如何在 linux etc/environment 中正确转义前导“#”字符?

kur*_*ing 5 linux amazon-web-services

我想etc/environment在 AWS Linux AMI 2013.03 上存储系统范围的环境变量。在这个例子中,我试图存储短语“#hello”。请参阅下面的示例。echo $control产生

hello

我期望echo $test0(如下)产生:

#hello

但它会产生一个空行。以下是我输入的测试列表/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'

如果我注销然后重新登录机器,它将正确解析 /etc/environment.conf 。控制条件传递echo $control。但是,没有一个测试用例正确响应。在每种情况下,哈希 (#) 之外的字符串要么被压缩,要么被忽略。例如,echo $test5产生h. 有没有更好的方法来正确逃脱#

有趣的是,如果我执行source /etc/environment,那么所有的作品如预期的确都echo $controlecho $test产生预期的结果。但是,在注销/登录时,问题又回来了......

Dan*_*ner 4

好吧,你想做的事情很棘手 /etc/environment 不是 shell 语法,它看起来像一个,但事实并非如此,这确实让人沮丧。/etc/environment 背后的想法非常棒。一种独立于 shell 的方法来设置变量!耶!但实际的局限性使其毫无用处。

你不能在那里传递变量。例如,尝试将 MAIL=$HOME/Maildir/ 放入其中,看看会发生什么。唉,最好停止尝试将它用于任何目的。因此,如果它是由 shell 处理的,您就无法使用它执行您期望能够执行的操作。

使用 /etc/profile 或 /etc/bashrc。