且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

确定执行BASH脚本的路径

更新时间:2023-12-04 19:30:16

有关的相对路径(即直接等同的Windows%〜DP0 ):

For the relative path (i.e. the direct equivalent of Windows' %~dp0):

MY_PATH="`dirname \"$0\"`"
echo "$MY_PATH"

对于绝对的,标准化的路径:

For the absolute, normalized path:

MY_PATH="`dirname \"$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"