日期:2014-05-16  浏览次数:20591 次

linux shell下,取得当前SHELL的目录
写个SHELL,名字叫 test.sh,放在任意位置,
启用如下命令执行 
sh test.sh
test.sh
sh /application/search/test.sh
/application/search/test.sh 等
即可通过下面的SHELL取得他所在的文件夹


DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo $DIR



具体含义
${BASH_SOURCE[0]}  取得执行shell命令例如  tmp/test.sh
dirname 取得前面的路径
cd 进到目录里
&& pwd 打印当前路径