How to detect the current directory in which I run

2019-03-18 00:45发布

How may I detect the name of the directory (or better yet the entire path) in which my shell script is run?

标签: shell
4条回答
ゆ 、 Hurt°
2楼-- · 2019-03-18 01:03

alternative method

pid=$$
path=$(ps -eo pid,args| awk -vp=$pid '$1~p{print $3}')
case "$path" in
    ./* ) pwd;;
    * ) echo $path;;
esac
查看更多
等我变得足够好
3楼-- · 2019-03-18 01:06

This, I believe, is the most portable way:

dir=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
查看更多
你好瞎i
4楼-- · 2019-03-18 01:12

what shell? What operating system?

For starters try

man pwd
$PWD
查看更多
Root(大扎)
5楼-- · 2019-03-18 01:26

This is not as trivial as it looks like. Check out this question and this

查看更多
登录 后发表回答