Reputation: 51039
This code is from apache2 service starting script.
What does it mean?
SCRIPTNAME="${0##*/}"
Upvotes: 6
Views: 6817
Reputation: 98368
The left side is easy: it assigns to the variable SCRIPTNAME
.
The right side is more complicated:
$0
or ${0}
is the name used to invoke the current shell or script.${VAR##pattern}
is the value of variable $VAR
with the longest string that matches the pattern
removed from the beggining (use one #
for the shortest or %
/%%
to remove the end.So your expression removes the start of the name used to call the script up to, and including, the last slash.
BTW, that is what the program basename
does.
Upvotes: 2
Reputation: 361605
It finds the name of the script being run, stripping out its directory. For instance if the script is /etc/init.d/httpd
then this would set SCRIPTNAME=httpd
.
$0
, or ${0}
, is the name of the script being executed. The ##
operator is used to remove any leading string that matches the pattern */
. *
is a wildcard character so */
means "any string followed by a forward slash".
The effect of this is to remove any leading directory names from $0
, leaving just the name of the script.
From man bash:
${parameter#word}
${parameter##word}The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches the beginning of the 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.
Upvotes: 8