Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the meaning of `if [ "${-#*i}" != "$-"]`?

I was looking through the /etc/profile file and I noticed a strange if statement. Can someone let me know what this means?

I’m referring to the second if statement if [ "${-#*i}" != "$-"] .

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

$- outputs the list of options the shell is running with. i in the output of $- means that the shell is interactive.

${var#expr} removes from the value of var the part from the left that matches the expr. expr is a glob. In the case expr is not matched, the original string is just outputted.

So ${-#*i} gets the value of $- and removes the part from the left that matches *i.

For example: $- is himBHs. The part that matches *i from the left is hi. So hi from the left will be removed. So ${-#*i} will be mBHs.

https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html

Can someone let me know what this means?

I believe it’s an odd way of checking if the session is interactive.

I would very much prefer and recommend case "$-" in *i*) for readability and portability. Also https://www.gnu.org/software/bash/manual/html_node/Is-this-Shell-Interactive_003f.html .

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading