bash 获取脚本存放路径
In normal Bash script, $0
is the path to the script. However, when a script is sourced, such as
在普通的Bash 脚本中 , $0
是脚本的路径。 但是,当脚本是源文件时,例如
. a.sh
a.sh’s $0
does not give a.sh
. How to get the a.sh inside a.sh?
a.sh的$0
不会给出a.sh
如何在a.sh中获取a.sh?
Short answer: use ${BASH_SOURCE[0]}
.
简短的答案:使用${BASH_SOURCE[0]}
。
You can check this post for details and explanations: How to Get Bash Script’s Own Path.
您可以查看此帖子以获取详细信息和解释: 如何获取Bash脚本的自己的路径 。
Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。
翻译自: https://www.systutorials.com/how-to-get-the-scripts-own-path-in-sourced-bash-script/
bash 获取脚本存放路径