Shell script current directory?

后端 未结 5 1721
一生所求
一生所求 2020-12-23 16:18

What is current directory of shell script? I this current directory from which I called it? Or this directory where script located?

5条回答
  •  死守一世寂寞
    2020-12-23 16:58

    Most answers get you the current path and are context sensitive. In order to run your script from any directory, use the below snippet.

    DIR="$( cd "$( dirname "$0" )" && pwd )"
    

    By switching directories in a subshell, we can then call pwd and get the correct path of the script regardless of context.

    You can then use $DIR as "$DIR/path/to/file"

提交回复
热议问题