Get the name of the directory where a script is executed

I have some script, that uses files in directories around it. It uses

dirname $0

command. It should work from any directory where I run this script, but when I run a symbolic link that points to that script I get the path of symbolic link. So I get the output of dirname rather than the path of the script itself.

Any one know a way to get the path of where the script is run?


获取脚本的真实路径

if [ -L $0 ] ; then
    ME=$(readlink $0)
else
    ME=$0
fi
DIR=$(dirname $ME)

如果您安装了realpath

$(dirname $(realpath $0))

Unless I misunderstand you, the problem should be the same as the one in: How do you normalize a file path in Bash?

An option not mentioned there is the following python one-liner:

python2.6 -c "import os,sys; print os.path.realpath(sys.argv[1])" "$0"

Finally, remember to use double quotes around "$0".

链接地址: http://www.djcxy.com/p/9724.html

上一篇: bash脚本中$ {0%/ *}的含义是什么?

下一篇: 获取执行脚本的目录的名称