This works but I don't understand it. I can't find any information on what ${*%%
means. The information I found says it needs to be ${varname%%
.
Also, what exactly is $()
used for? Does it make it process the function one time for each line in the file list?
strip_mod_c() {
# What is "*" in the variable name spot?
result="${*%%*.mod.c}"
echo "$result"
}
SOURCES_ALL=`find $BASEDIR -maxdepth 1 -name "*.cpp" -or -name "*.c"`
# Also, why is this done inside $( ) ?
SOURCES_ALL=$( strip_mod_c $SOURCES_ALL )
CodePudding user response:
I can't find any information on what
${*%%
means when the information I found says it needs to be${varname%%
.
It is ${varname%%
, where varname
is *
.
The documentation has this to say about it:
${parameter%word}
${parameter%%word}
The
word
is expanded to produce a pattern and matched according to the rules described below (see Pattern Matching). If the pattern matches a trailing portion of the expanded value ofparameter
, then the result of the expansion is the value ofparameter
with the shortest matching pattern (the ‘%’ case) or the longest matching pattern (the ‘%%’ case) deleted. Ifparameter
is ‘@’ or ‘*’, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list.
You also asked,
Also, what exactly is
$()
used for?
$()
delimits a command substitution. The shell code within is executed, and its standard output captured and substituted in place of the $(...)
. This is pretty much the same as `...`, but there are some differences in how quoting is handled within.
Does it make it process the function one time for each line in the file list?
No. See above. Overall, the script fragment runs find
and captures the result in variable SOURCES_ALL
. Then it runs the result (all at once, split on whitespace) through function strip_mod_c
, captures the result, and assigns it back to SOURCES_ALL
.
CodePudding user response:
${*}
is the variable $*
, which is the script or function arguments expanded as a single string. Per the bash manual:
3.4.2 Special Parameters
The shell treats several parameters specially. These parameters may only be referenced; assignment to them is not allowed.
($*)
Expands to the positional parameters, starting from one. When the expansion is not within double quotes, each positional parameter expands to a separate word. In contexts where it is performed, those words are subject to further word splitting and filename expansion. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is,"$*"
is equivalent to"$1c$2c…"
, where c is the first character of the value of theIFS
variable. IfIFS
is unset, the parameters are separated by spaces. IfIFS
is null, the parameters are joined without intervening separators.
$()
is command substitution. It captures the output of a command as a string. VAR=$(cmd)
assigns the output of cmd
to $VAR
.