Page 1 of 1

About the method of executing scripts under LINUX to get the script path

Posted: Tue Jan 22, 2019 4:27 am
by lanzi
Usually, under LINUX SHELL script execution, a lot of people often use the pwd command, most people think that can get to where the script path through this command, in fact this is a myth, the pwd command can only access to the current, script execution path, only when the execution of the SHELL script path and at the same location the pwd to get the path to the location of the script, such as the execution ./test.sh, scripts can be got through the pwd path is the execution of the script path, if you execute the /home/test.sh, The path where the script is located is different from the path where the script is executed With the pwd command, you get the path where the script is executed, not the path where the script is located.

Re: About the method of executing scripts under LINUX to get the script path

Posted: Tue Jan 22, 2019 4:28 am
by lanzi
How to get the path of the script in the SHELL script, as shown below:
SH_PATH = $(cd`dirname $0`;pwd)

Mean by cd `dirname $0` into the script in the path, and then execute the pwd command, can obtain the script in the path.