linux shell脚本如何获取“当前工作路径”,“执行脚本所在路径”,“执行脚本所在路径和执行路径的相对路径”?(当前目录、脚本目录、脚本路径)$(dirname “$0“)(当前路径)

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 注意脚本包含时的路径(父子脚本)

参考文章:linux shell脚本获取脚本目录时, ( d i r n a m e “ (dirname “ (dirname{BASH_SOURCE[0]}“)与$(dirname $0)有什么区别?

20230330 有时候shellcheck会提示你用更保险的方法

比如:

# 获取当前脚本所在目录绝对路径
DEPLOY_PATH=$(
    cd "$(dirname $0)" || { echo "cd Failure"; exit 1; }
    pwd
)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dontla

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值