在 csh 中,如何使用变量修饰符从 "a/b/c/" 生成 "a/b"?

In csh, how to make "a/b" from "a/b/c/" using variable modifier?

当 x 包含的目录名最后包含 / 时,如何删除 csh 中最后一个包含 / 模式的路径?
这是一个例子。

% set x=a/b/c
% echo $x:h
a/b
% set x=a/b/c/
% echo $x:h
a/b/c

所以当x是“a/b/c/”时,我想做“a/b”。我怎样才能使用变量修饰符来做到这一点? (在 csh 中)

我也很好奇如何在 bash 中做到这一点。 ${x%/} 将生成“a/b/c”,${x%%/} 将生成“a”。

ADD :当然我可以预处理输入文件以在末尾删除 / ,例如 sed -e '1,$s/\/$//g' file > file.1; \mv file.1 file' 但我很好奇是否可以使用变量修饰符。

您可以通过 csh 的任何方式捕获 dirname(1) 的输出:

$ dirname a/b/c
a/b
$ dirname a/b/c/
a/b