test_path.sh
#!/bin/bash
# 当前工作路径
current_working_path="$(pwd)"
echo "current_working_path = ${current_working_path}"
# 当前脚本所在路径(如在父脚本下执行子脚本,该方法获取的为父脚本路径)
current_script_location="$(cd "$(dirname "$0")"; pwd)"
echo "current_script_location = ${current_script_location}"
# 当前脚本所在路径与工作路径的相对路径
relative_script_to_working_path="$(dirname $0)"
echo "relative_script_to_working_path = ${relative_script_to_working_path}"
# echo "pwd: $(pwd)"
运行示例 :
20230209 注意脚本包含时的路径(父子脚本)
20230330 有时候shellcheck会提示你用更保险的方法
比如:
# 获取当前脚本所在目录绝对路径
DEPLOY_PATH=$(
cd "$(dirname $0)" || { echo "cd Failure"; exit 1; }
pwd
)