linux 系统变量 $-


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 setbuiltin 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 interactive
  • m: Job control is enabled
  • B: Brace expansion is enabled
  • H: History substitution like !-1

上面有一句shell语句有点奇葩,说说对它的理解。

             if [ "${-#*i}" != "$-" ]; 。 ${-#*i} 表示 在$-中,把从头到i的值去掉,留下剩余的值,如果没有找到i则不会截断,两者就是相等的。由上可知 ‘  i ’表示交互式,在我的CentOS中,因为是交互式的,所以这个表达式为真。。。。

           在linux中,$-系统变量经常在配置文件中出现,但是我在百度里,不易查到详细的信息,特此写了这片博文,加深理解!

小弟第一篇博文,望大神们阅读后,指出错误和不好的地方,谢谢!!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值