Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash parameter expansion ${parameter##word}

Tags:

bash

I'm reading through the apache startup script trying to troubleshoot some problems with my server but at the very beginning there is a parameter expansion I don't really understand.

SCRIPTNAME="${0##*/}"
SCRIPTNAME="${SCRIPTNAME##[KS][0-9][0-9]}"
if [ -n "$APACHE_CONFDIR" ] ; then
    if [ "${APACHE_CONFDIR##/etc/apache2-}" != "${APACHE_CONFDIR}" ] ; then
            DIR_SUFFIX="${APACHE_CONFDIR##/etc/apache2-}"
    else
            DIR_SUFFIX=
    fi
elif [ "${SCRIPTNAME##apache2-}" != "$SCRIPTNAME" ] ; then
    DIR_SUFFIX="-${SCRIPTNAME##apache2-}"
    APACHE_CONFDIR=/etc/apache2$DIR_SUFFIX
else
    DIR_SUFFIX=
    APACHE_CONFDIR=/etc/apache2
fi

I'm just looking for some clarification on what ${parameter##word} construct does because the bash reference manual from gnu is not clear to me. The manual defines it like this...

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

the word is expanded to produce a pattern just as in filename expansion (see Filename Expansion). If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ‘#’ case) or the longest matching pattern (the ‘##’ case) deleted. If parameter is ‘@’ or ‘*’, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with ‘@’ or ‘*’, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.

does this mean that the first line stores an empty string back into SCRIPTNAME or am I just way off base?

like image 283
Jordan Camp Avatar asked Jun 19 '26 18:06

Jordan Camp


1 Answers

The first line stores the basename of the current file in SCRIPTNAME. $0 is (generally) the name of the current script. See this Related question for discussion about it.

The second line then strips a prefix of K## or S## from the name (assuming /etc/init.d link naming conventions.

like image 160
Etan Reisner Avatar answered Jun 21 '26 08:06

Etan Reisner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!