<分区>
在Windows 命令脚本中,可以使用%~dp0
确定当前执行脚本的目录 路径。例如:
@echo Running from %~dp0
Bash 中的等价物是什么?脚本?
标签 bash
最佳答案
对于相对路径(即直接等效于 Windows 的 %~dp0
):
MY_PATH="$(dirname -- "${BASH_SOURCE[0]}")"
echo "$MY_PATH"
对于绝对的规范化路径:
MY_PATH="$(dirname -- "${BASH_SOURCE[0]}")" # relative
MY_PATH="$(cd -- "$MY_PATH" && pwd)" # absolutized and normalized
if [[ -z "$MY_PATH" ]] ; then
# error; for some reason, the path is not accessible
# to the script (e.g. permissions re-evaled after suid)
exit 1 # fail
fi
echo "$MY_PATH"
关于bash - 确定执行 Bash 脚本的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/630372/