Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the ${-#*i} mean in shell script?

There is a for loop in the file /etc/profile on CentOS 6:

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 the ${-#*i} mean in the above for loop?
Thanks for any help.

like image 739
Ren Avatar asked Feb 05 '17 23:02

Ren


People also ask

What is ${ in shell script?

Here are all the ways in which variables are substituted in Shell: ${variable} This command substitutes the value of the variable. ${variable:-word} If a variable is null or if it is not set, word is substituted for variable.

What does ${ var mean?

In the bash shell, ${! var} is a variable indirection. It expands to the value of the variable whose name is kept in $var . The variable expansion ${var+value} is a POSIX expansion that expands to value if the variable var is set (no matter if its value is empty or not).

What does the variable $? Hold?

$? is a built-in variable that stores the exit status of a command, function, or the script itself. $? reads the exit status of the last command executed.

What does $? Mean in Linux?

echo $? - Gives the EXIT STATUS of the most recently executed command . This EXIT STATUS would most probably be a number with ZERO implying Success and any NON-ZERO value indicating Failure.


1 Answers

$- means shell flags.

${-#*i} means shell flags minus first match of *i.

If these two are not equal, then the shell is considered interactive (flag i is present).

like image 196
Andrey Avatar answered Oct 15 '22 21:10

Andrey