Bash syntax in / etc / profile

I just noticed this line in my /etc/profile , and I thought what it means, and when it's true.

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

i iterates over several *.sh files.

Sorry if this is a stupid question, but as you can imagine, finding mostly characters on Google is really not an option.

Thanks!

+6
source share
1 answer

In the OPTIONS section of the man page:

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

In the Special Parameters manpage section:

  - 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). 

In the Parameter Expansion section of the man page:

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

The word is expanded to create a template in the same way as in the pathname extension. If the pattern matches the beginning of the parameter value, then the extension result is the extended parameter value with the shortest match pattern ('' # case) or the longest match pattern ("case ##"). If the parameter is @ or *, the template deletion operation is applied to each positional parameter in turn, and the extension is the resulting list. If the parameter is an array variable, adjusted using @ or *, the template deletion operation is applied to each member of the array in turn, and the extension is the resulting list.

So, "${-#*i}" says it expands the variable $- and removes it from the beginning of the line to the letter i . Then this extension is compared != With the extension $- (the same variable is only unmodified).

If this is not the same, it means that the first extension has deleted some content, which means that the letter i appeared in the value $- , which means that (since -i not an argument to set ), that the -i argument was passed shell, and shell - interactive shell.

+7
source

Source: https://habr.com/ru/post/978418/


All Articles