Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shell脚本获取绝对路径: $(cd $(dirname $0);pwd) #12

Open
humingcheng opened this issue Sep 3, 2016 · 0 comments
Open

shell脚本获取绝对路径: $(cd $(dirname $0);pwd) #12

humingcheng opened this issue Sep 3, 2016 · 0 comments

Comments

@humingcheng
Copy link
Owner

humingcheng commented Sep 3, 2016

绝对路径(不含fileName):$(cd $(dirname $0);pwd)

解析:dirname $0表示脚本父目录;pwd即进入父目录后获取绝对路径;不能只执行pwd或者$0。
附注:单独执行pwd或$0的结果:
+----------------------------------
/tmp/test/f1.sh:
echo "pwd:" $(pwd)
echo '$0: ' $0
+----------------------------------
Test:/ # bash /tmp/test/f1.sh
pwd: /
$0: /tmp/test/f1.sh
+----------------------------------
Test:/tmp # bash /tmp/test/f1.sh
pwd: /tmp
$0: /tmp/test/f1.sh
+----------------------------------
Test:/tmp/test # bash /tmp/test/f1.sh
pwd: /tmp/test
$0: /tmp/test/f1.sh
+----------------------------------
Test:/tmp/test # bash f1.sh
pwd: /tmp/test
$0: f1.sh

@humingcheng humingcheng changed the title shell脚本中获取绝对路径 shell脚本获取绝对路径: $(cd $(dirname $0);pwd) Sep 3, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant