【问题标题】:Set environment variable in /etc/environment file with value containing "#" character在 /etc/environment 文件中设置环境变量,其值包含“#”字符
【发布时间】:2019-02-08 17:02:02
【问题描述】:

我正在尝试设置系统范围的环境变量。

我正在编辑/etc/environment 文件。我在文件中添加var="name#12"。我注销并再次登录以显示更改。

但是echo $var 给了我name

我试过用\ 转义#,比如var="name\#12",但没有用。 我也尝试使用'' 而不是""

谁能帮我解决这个问题?

【问题讨论】:

标签: linux shell environment-variables


【解决方案1】:

男子重击:

COMMENTS
       In  a  non-interactive  shell, or an interactive shell in which the interactive_comments option to the shopt
       builtin is enabled (see SHELL BUILTIN COMMANDS below), a word beginning with #  causes  that  word  and  all
       remaining  characters  on  that  line  to be ignored.  An interactive shell without the interactive_comments
       option enabled does not allow comments.  The interactive_comments option is on  by  default  in  interactive
       shells.

在 /etc/profile.d/ 中创建一个文件 myvars.sh 并将其添加到文件中

var="name#12"
export var=$var

重新登录后

$ echo $var 
name#12

在交互式 shell 中禁用 cmets:

$ shopt -u interactive_comments

【讨论】:

  • 如果我使用 bash shell,$ var="name#12" $ echo $var name#12 如果我在环境文件中声明相同,$ echo $var name
猜你喜欢
  • 2020-12-13
  • 2019-05-15
  • 2013-03-03
  • 1970-01-01
  • 2014-02-15
  • 1970-01-01
  • 2019-04-02
  • 1970-01-01
  • 2022-07-20
相关资源
最近更新 更多