How to obtain the path in a bash script

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.