'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:
- Cd 'dirname $0'
- Echo 'pwd'
Then return to/home/admin/and execute
- Sh test/test. sh
Running result:
- /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: