shell避坑 之cd

1、执行shell脚本,默认的$(pwd)为执行shell时的目录路径,而不是shell脚本所在路径;

2、在shell脚本中执行cd命令,当脚本执行完后,会退回到执行脚本的目录路径;

[root@localhost b21430]# pwd
/home/b21430
[root@localhost b21430]# ll | grep test.sh
-rw-r--r--  1 root root   122 Aug 30 10:41 test.sh

[root@localhost b21430]# cat test.sh
#!/bin/bash
set -exuo pipefail
echo " before exec  cd $0 $(pwd)"
cd `dirname $0`
echo " after exec cd $0  $(pwd)"

exit 0           --》 /home/b21430/test.sh

[root@localhost b21430]# cd ..
[root@localhost home]# pwd
/home

[root@localhost home]# ll | grep test.sh
-rw-r--r--  1 root      root          148 Aug 30 11:01 test.sh<

猜你喜欢

转载自blog.csdn.net/qq_29044159/article/details/108303838