linux 系统变量 $-,在linux配置文件中经常用到。 比如在 /etc/profile配置文件中有如下代码:
for i in /etc/profile.d/*.sh ; do
for [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" > /dev/null 2>&1
fi
fi
done
对这段话的理解:如果是交互式系统则把执行profile.d 下文件信息,打印出来,如果不是则把全部信息丢进/dev/null中,废弃。
在我的centos 6.5系统中执行:echo $- ,执行的结果为HimBH。系统变量$-的值为HimBH。
在bash reference manual 中对$-的解释为:($-, a hyphen.) Expands to the current option flags as specified uponinvocation, by the set
builtin command, or those set by the shell itself(such as the -i option).。$-由lbash 内嵌命令set设置。在bash reference manual 中的“the set builitin"中可以查找到他们的示意。如下:
h
: Cache location of binaries in the$PATH
. Speeds up execution, but fails if you move binaries around during the shell session.i
: The current shell is interactivem
: Job control is enabledB
: Brace expansion is enabledH
: History substitution like!-1
上面有一句shell语句有点奇葩,说说对它的理解。
if [ "${-#*i}" != "$-" ]; 。 ${-#*i} 表示 在$-中,把从头到i的值去掉,留下剩余的值,如果没有找到i则不会截断,两者就是相等的。由上可知 ‘ i ’表示交互式,在我的CentOS中,因为是交互式的,所以这个表达式为真。。。。
在linux中,$-系统变量经常在配置文件中出现,但是我在百度里,不易查到详细的信息,特此写了这片博文,加深理解!
小弟第一篇博文,望大神们阅读后,指出错误和不好的地方,谢谢!!!!