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.
是否有一些本机函数(shell、linux 命令)来合并/计算完整路径?
例子:
old_path="~/test1/test2/../dir3//file.txt" new_path=FUN($old_path) echo "$new_path" // I want get this "/home/user/test1/dir3/file.txt"
做
new_path=$(eval cd "$old_path"; pwd)
为你工作?pwd -P如果您想解析符号链接,也可以使用。如果你使用$HOME而不是~in ,你可以让生活更轻松old_path。那么你不需要eval.
pwd -P
$HOME
~
old_path
eval
使用readlink:
readlink
$ readlink -m ~/foo.txt /home/user/foo.txt $ readlink -m ~/somedir/..foo.txt /home/user/foo.txt
它还处理符号链接。