我刚刚注意到我的/etc/profile
文件中有这行代码,我想知道这个if语句的含义以及它何时为真。
if [ "${-#*i}" != "$-" ]; then
i
迭代遍历几个 *.sh
文件。
抱歉如果这个问题很傻,但是你可以想象,在 Google 中查找大部分符号确实不是一个选项。
谢谢!
从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。