本文共 870 字,大约阅读时间需要 2 分钟。
linux bash脚本
How to get a ’s directory reliably in Bash on ?
如何在 Bash中可靠地获取的目录?
For example, to get the directory of the executing script $0
.
例如,获取执行脚本$0
的目录。
can give you .
可以为您提供 。
You can get the absolute path of the script by to handle symbolic links (consider a symbolic link ./run.sh linked to ../../run.sh, dirname ./run.sh
will give you .
), and then pass the absolute path to dirname
.
您可以通过获取脚本的绝对路径来处理符号链接(考虑链接到../../run.sh的符号链接./run.sh, dirname ./run.sh
会为您提供.
) ,然后将绝对路径传递到dirname
。
In summary, the script the get the directory of current script is:
总之,获取当前脚本目录的脚本是:
directory=$(dirname $(readlink -f $0))
or
要么
directory=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
to also handle sourced bash script. Check for details of the ${BASH_SOURCE[0]}
meaning.
还可以处理源bash脚本。 有关${BASH_SOURCE[0]}
含义的详细信息,请查看 。
翻译自:
linux bash脚本
转载地址:http://kplwd.baihongyu.com/