Shell获取脚本所在目录的方法

Published: Tags: SHELL LINUX
SWD=$(dirname $(which $0)) && cd "$SWD"
pwd

path1=$(which $0)
path1=$(cd ${path1%/*}; pwd)
echo ${path1%/*}
pwd

path2=$(cd "$(dirname "$0")"; pwd)
echo $(dirname $path2)
pwd

path3=$(dirname $(readlink -f $0))
echo $(dirname $path3)
pwd