在/etc/profile中的Bash语法

6

我刚刚注意到我的/etc/profile文件中有这行代码,我想知道这个if语句的含义以及它何时为真。

if [ "${-#*i}" != "$-" ]; then

i 迭代遍历几个 *.sh 文件。

抱歉如果这个问题很傻,但是你可以想象,在 Google 中查找大部分符号确实不是一个选项。

谢谢!

1个回答

7

从manpage的OPTIONS部分:

-i        If the -i option is present, the shell is interactive.

来自manpage的特殊参数部分:

  -      Expands  to  the  current option flags as specified upon invoca-
         tion, by the set builtin command, or  those  set  by  the  shell
         itself (such as the -i option).

从man页的参数扩展部分:

  ${parameter#word}
  ${parameter##word}

The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches the beginning of the value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ‘‘#’’ case) or the longest matching pattern (the ‘‘##’’ case) deleted. If parameter is @ or *, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

所以"${-#*i}"表示展开$-变量并从字符串开头删除直到字母i。然后将该扩展与$-(相同的变量但未修改)的扩展进行比较。!=当它们不相同时,这意味着第一个扩展删除了一些内容,这意味着字母i出现在$-的值中,这意味着(因为-i不是set的参数),-i参数被传递给shell,并且shell是一个交互式shell。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接