Shell – Meaning of “${-#*i}” != “$-“

shellshell-script

In /etc/profile I see this:

for i in /etc/profile.d/*.sh ; do
if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null 2>&1
        fi
    fi
done

What does ${-#*i} mean. I cannot find a definition of a parameter expansion starting ${-.

Best Answer

$- is current option flags set by the shell itself, on invocation, or using the set builtin command:

$ echo $-
himBH
$ set -a
$ echo $-
ahimBH

"${-#*i}" is syntax for string removal: (from POSIX documentation)

${parameter#[word]}

Remove Smallest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the smallest portion of the prefix matched by the pattern deleted. If present, word shall not begin with an unquoted '#'.

${parameter##[word]}

Remove Largest Prefix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the largest portion of the prefix matched by the pattern deleted.

So ${-#*i} remove the shortest string till the first i character:

$ echo "${-#*i}"
mBH

In your case, if [ "${-#*i}" != "$-" ] checking if your shell is interactive or not.