Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
美好的一天如何删除 bash 中最后一个斜杠中的所有变量。
#!/bin/bash VAR="/foo/bar/abcd ef gh"
我需要删除:abcd ef gh
路径 /foo/bar/ 总是不同的
寻求简短的符号;-)谢谢你..
如果你的 $VAR 总是一个目录/文件的路径,你可以使用这个:
$ VAR=$(dirname "$VAR") $ echo $VAR /foo/bar
更新:您还可以在 bash 中使用参数替换:
$ echo ${VAR%/*} /foo/bar