是否可以在一个命令中使用 # 和 % 运算符进行左右 bash 修整?

Is it possible left and right bash trimming with the # and % operators in one command?

是否可以重写这段代码

$ x="/foo/somefile.txt"
$ y=${x%.*}
$ echo ${y##*/}
somefile

没有变量 y?

我的意思是像一次左右修剪的一个命令。

谢谢!

您不能进行链式扩展,但在这种特殊情况下,您仍然可以通过一次扩展实现您的预​​期:

/$ x="/foo/somefile.txt"
/$ shopt -s extglob
/$ echo ${x//@(*\/|.*)/}
somefile

因此它将用空字符串替换两个模式:*\/.*

Is it possible left and right bash trimming with the # and % operators in one command?

不,在 bash 中不可能。