MY_PATH = $(readlink -f $ YOUR_ARG)将解析相对路径,例如“./”和“../”
原文链接:https://www.f2er.com/bash/390919.html考虑这一点(source):
#!/bin/bash dir_resolve() { cd "$1" 2>/dev/null || return $? # cd to desired directory; if fail,quell any error messages but return exit status echo "`pwd -P`" # output full,link-resolved path } # sample usage if abs_path="`dir_resolve \"$1\"`" then echo "$1 resolves to $abs_path" echo pwd: `pwd` # function forks subshell,so working directory outside function is not affected else echo "Could not reach $1" fi