ubuntu下获取当前文件路径以及执行路径

linux下获取当前文件路径:

1
2
current_dir=$(cd "$(dirname "${0}")"; pwd)
echo current_dir

linux下获取当前执行路径:

1
2
3
4
5
6
7
if [ $(echo $0 | grep '^/') ]; then
path=$(dirname $0)
else
path=$(pwd)/$(dirname $0)
fi
echo $path