Bash obtains the absolute path of the script being executed.

Source: Internet
Author: User
Bash obtains the absolute path of the script being executed, such as the question. when we write a Shell script, we tend to use the absolute path. in this way, no matter what directory the script is executed in, but sometimes, we design a tool script in a software package, which may use the opposite path... bash is used to obtain the absolute path of the script being executed, such as the question. when we write a Shell script, we tend to use the absolute path. in this way, no matter what directory the script is executed in, but sometimes, when we design a tool script in a software package, the relative path may be more flexible, because you don't know in which Directory the user will execute your program, you will have the topic in this article. A common misunderstanding is the use of the pwd command, which is used to "print name of current/working directory". This is the true meaning of this command, the current working directory, there is no meaning here. this directory is the directory where the script is stored. Therefore, this is incorrect. You can try bash shell/. sh,. sh content is pwd, you will find that the path to the command is/home/June, not. sh path:/home/June/shell/. sh's other mistaken answer is $0, which is also incorrect. $0 is a special variable in the Bash environment. its true meaning is: expands to the name of the shell or shell script. this is set at shell initialization. if bash is invoked with a file of commands, $0 is set to the name of that file. if bash is started with the-c option, then $0 is set to the first argument after String to be executed, if one is present. otherwise, it is set to the file name used to invoke bash, as given by argument zero. this $0 may be of several values, which are related to the method of calling: use a file to call bash. The value of $0 is the name of the file (not the absolute path) if you use the-c option to start bash, the actually executed command will be read from a string. if there are other parameters after the string, use special variable reference starting from $0 (path-independent). In addition, $0 is set to the name of the file that calls bash (not an absolute path) the correct answer is as follows: 1 #! /Bin/bash2echo "scriptPath1:" $ (cd 'dirname $ 0'; pwd) 3 echo "scriptPath2:" $ (pwd) 4 echo "scriptPath3: "$ (dirname $ (readlink-f $0) result: 1june @ Win7 192.168.1.100 00:31:24 ~> 2pwd3/home/june4june @ Win7 192.168.1.100 00:36:11 ~> 5 bash shell/a. sh6scriptPath1:/home/June/shell7scriptPath2:/home/june8scriptPath3:/home/June/shell9june @ Win7 192.168.1.100 00:36:15 ~> In this explanation, the first one is dirname $0. obtain the parent directory of the currently executed script file, cd 'dirname $ 0', and enter this directory (switch to the current working directory) pwd, display the current working directory (after cd execution). Therefore, we have obtained the path for storing the script currently being executed.
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.