语句 f2=${f%?????} 是什么意思?

What does the statement, f2=${f%????} , mean?

我试图理解表达式 f2=${f%????} 在 bash 脚本中的含义。

我试着在网上搜索一些参考资料,但没找到有用的东西。

我使用的代码是:

for f in "$@"
do
f2=${f%????}
/usr/bin/openssl smime -in "$f" -verify -inform DER -noverify -out "$f2"
done

bash 手册中 Shell Parameter Expansion 下的记录:

${<em>parameter</em>%<em>word</em>}
${<em>parameter</em>%%<em>word</em>}

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 of parameter, then the result of the expansion is the 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.

也就是说,${f%????}$f去掉最后四个字符后的值。

你也可以写成${f:0:-4},这样可能更清楚一些。