'Dirname $ 0' in Linux'

Source: Internet
Author: User

'Dirname $ 0' in Linux'

In the Linux Command Line status, simply executing $ cd 'dirname $ 0' is meaningless. Because it returns "." of the current path ".".
This command works only when it is written in a script file. It returns the directory where the script file is stored and can locate the relative location of the program to be run based on the directory (except for the absolute location ).

Create test. sh under/home/admin/test/as follows:

  1. Cd 'dirname $0'
  2. Echo 'pwd'

Then return to/home/admin/and execute

  1. Sh test/test. sh

Running result:

  1. /Home/admin/test

In this way, you can know the location of the files deployed together with the script. You only need to know the relative location and locate the files based on the directory. You do not need to care about the absolute location. In this way, the portability of the script is improved and can be executed on any server (if the script is deployed.

This article permanently updates the link address:

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.