Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

"${-#*i}" != "$-"

33 views
Skip to first unread message

Hongyi Zhao

unread,
Jan 15, 2018, 9:11:49 AM1/15/18
to
Hi all,

In the /etc/profile, I found the following snippet:

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

But, I cann't figure out the meaning of the following line in above code:

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

Any hints?

Regards
--
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.

Chris Elvidge

unread,
Jan 15, 2018, 9:42:37 AM1/15/18
to
On 15/01/2018 02:11 pm, Hongyi Zhao wrote:
> Hi all,
>
> In the /etc/profile, I found the following snippet:
>
> ------------------
> 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
> ------------------
>
> But, I cann't figure out the meaning of the following line in above code:
>
> if [ "${-#*i}" != "$-" ]; then
>
> Any hints?
>
> Regards
>

Hint 1:
https://unix.stackexchange.com/questions/315829/how-to-understand-the-output-of-echo


--

Chris Elvidge, England

Eric

unread,
Jan 15, 2018, 10:10:07 AM1/15/18
to
On 2018-01-15, Hongyi Zhao <hongy...@gmail.com> wrote:
> Hi all,
>
> In the /etc/profile, I found the following snippet:
>
> ------------------
> 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
> ------------------
>
> But, I cann't figure out the meaning of the following line in above code:
>
> if [ "${-#*i}" != "$-" ]; then
>
> Any hints?

Well, you should see that ${-#*i} is some sort of variable evaluation.

Firstly, the variable is the special variable $- (look it up).

Secondly, the #*i bit modifies the value - there is a set of these
beginning with # or ## or % or %% , searching the man page for the last
of these should take you to the right bit.

Thirdly, the whole line compares the modified value to the whole value
and either just sources the file, or sources it supressing all output.

eric
--
ms fnd in a lbry

David W. Hodgins

unread,
Jan 15, 2018, 1:01:20 PM1/15/18
to
On Mon, 15 Jan 2018 09:11:43 -0500, Hongyi Zhao <hongy...@gmail.com> wrote:

> But, I cann't figure out the meaning of the following line in above code:
> if [ "${-#*i}" != "$-" ]; then

Short answer. "Is the script running in interactive mode?".

Regards, Dave Hodgins

--
Change dwho...@nomail.afraid.org to davidw...@teksavvy.com for
email replies.

Thomas 'PointedEars' Lahn

unread,
Jan 15, 2018, 3:20:48 PM1/15/18
to
Hongyi Zhao wrote:

> But, I cann't figure out the meaning of the following line in above code:
>
> if [ "${-#*i}" != "$-" ]; then
>
> Any hints?

RTFM.

--
PointedEars

Twitter: @PointedEars2
Please do not cc me. /Bitte keine Kopien per E-Mail.

Ralf S. Hellersen

unread,
Jan 28, 2018, 6:35:49 AM1/28/18
to
>
> if [ "${-#*i}" != "$-" ]; then
>
> Any hints?
>
> Regards

$- The set of options which were supplied at invocation

Thanks for asking. I learned something new.
It should be asked to "RTM". The manuals are often helpfull.


Regards
Ralf
0 new messages