文章目录 shell的dirname $0 shell的dirname $0 dirname_test.sh #!/bin/bash path=$(cd dirname $0;pwd) echo p a t h p a t h 2 = path path2= pathpath2=(dirname $0) echo $path2 sh dirname_test.sh /home/ltc . dirname $0 只是获取的当前脚本的相对路径. cd dirname $0;pwd 先cd到当前路径然后pwd,打印成绝对路径