How to detect the current directory in which I run my shell script?

后端 未结 4 520
后悔当初
后悔当初 2021-02-07 03:40

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

相关标签:
4条回答
  • 2021-02-07 04:13

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

    0 讨论(0)
  • 2021-02-07 04:22

    alternative method

    pid=$$
    path=$(ps -eo pid,args| awk -vp=$pid '$1~p{print $3}')
    case "$path" in
        ./* ) pwd;;
        * ) echo $path;;
    esac
    
    0 讨论(0)
  • 2021-02-07 04:33

    what shell? What operating system?

    For starters try

    man pwd
    $PWD
    
    0 讨论(0)
  • 2021-02-07 04:35

    This, I believe, is the most portable way:

    dir=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
    
    0 讨论(0)
提交回复
热议问题