首页 > 解决方案 > 在 /etc/environment 文件中设置环境变量,其值包含“#”字符

问题描述

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

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

但是echo $var给我name

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

谁能帮我解决这个问题?

标签: linuxshellenvironment-variables

解决方案


男子重击:

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 中禁用注释:

$ shopt -u interactive_comments

推荐阅读