method to get the absolute path of the script's directory in the shell _linux shell

Source: Internet
Author: User

To get the absolute path of the executing program/script itself, you can use DirName (Realpath (__file__) in PHP, and C # has System.Windows.Forms.Application.StartupPath; Java does not seem to have any more direct method, can only use Codesource to gain indirectly. In a Linux shell script, if you want the absolute path of the current script file, there's no out-of-the-box command to call, but you can get it by using the following statement:

Copy Code code as follows:

basedirforscriptself=$ (CD "$ (dirname" $) "; pwd)
echo "full path to currently executed script is: ${basedirforscriptself}"

Although most of the cases do not require such an absolute path to complete the work, but if you want to pack more than one script, data files, and other content as a whole to deliver others to use, but also want to no matter what directory Users copy to execute the script can correctly find the other contents of the package, Using such a script to automatically locate the root of a package should be a relatively robust approach.

Related Article

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.