Get Script Path in Shell
#!/usr/bin/bash
dir_old=$(pwd)
absolute_script_path=$(cd $(dirname $0) && pwd)
relative_script_path=$(dirname $0)
wrong_absolute_script_path=$dir_old/$relative_script_path
echo "Current PATH: $dir_old"
echo "absolute_script_path is: $absolute_script_path"
echo "relative_script_path is: $relative_script_path"
echo "This path might be wrong: $wrong_absolute_script_path"
# Fail if use absolute path to invoke script
cd $wrong_absolute_script_path && pwd
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合终身会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步