获取除bash中最后2个字符串之外的所有字符串

Get all characters of string except the last 2 in bash

本问题已经有最佳答案,请猛点这里访问。

我想知道如何打印我拥有的字符串,但没有bash中的最后两个字符。
例如:

1
str ="hello.c"

然后只打印hello

谢谢你的回答。


答案是使用bash-builtin字符串操作:

1
2
str="hello.c"
echo"${str%.c}"

在线尝试!

引用文档:

${parameter%word}
${parameter%%word}

The word is expanded to produce a pattern just as in filename
expansion. 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.