DIR = "$ (cd" $ (dirname "$ {BASH_SOURCE [0]}") "& pwd )"
This is the recommended method on stackoverflow.
Http://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself
I have read it twice.
Later I changed it:
# DIR = "$ (cd" $ (dirname "$ {BASH_SOURCE [0]}") "& pwd )"
S0 = "$ {BASH_SOURCE [0]}"
DIRNAME = "$ (dirname" $ S0 ")"
DIR = "$ (cd" $ DIRNAME "& pwd )"
Echo "$ S0"
Echo "$ DIRNAME"
Echo "$ DIR"
Run the script and output:
./Build/jenkins/tesh. sh
./Build/jenkins
/Users/heliclei/. jenkins/jobs/dailybuild-develop-ios/workspace/build/jenkins
S0 is the path of the script relative to the current directory and relative path.
Run the dirname command to obtain the script directory name, which is also a relative path.
Run the cd command to switch to the directory where the script is located, and then run the pwd command,
The absolute path of the script directory is obtained.
We recommend a good book "bash Beginners Guide" to learn Bash scripts.