获取 Shell Script 所在脚本

脚本

ScriptPath=$(dirname $(readlink -f "$0"))

拓展

1. dirname 参数

用法:dirname [选项] 名称...
Output each NAME with its last non-slash component and trailing slashes
removed; if NAME contains no /'s, output '.' (meaning the current directory).

  -z, --zero                    end each output line with NUL, not newline
      --help                    显示此帮助信息并退出
      --version                 显示版本信息并退出

Examples:
  dirname /usr/bin/          -> "/usr"
  dirname dir1/str dir2/str  -> "dir1" followed by "dir2"
  dirname stdio.h            -> "."

2. readlink 参数

用法:readlink [选项]... 文件...
输出符号链接值或规范文件名。

  -f, --canonicalize            递归的跟随给定文件名的所有符号链接以规范化,
                                除最后一个外所有组件必须存在
  -e, --canonicalize-existing   递归跟随给定文件名的所有符号链接以规范化,
                                所有组件都必须存在
  -m, --canonicalize-missing    canonicalize by following every symlink in
                                every component of the given name recursively,
                                without requirements on components existence
  -n, --no-newline              do not output the trailing delimiter
  -q, --quiet,
  -s, --silent                  suppress most error messages (on by default)
  -v, --verbose                 report error messages
  -z, --zero                    end each output line with NUL, not newline
      --help                    显示此帮助信息并退出
      --version                 显示版本信息并退出

3. pwd

-L, --logical
        use PWD from environment, even if it contains symlinks
-P, --physical
        avoid all symlinks
--help display this help and exit
--version
        output version information and exit
If no option is specified, -P is assumed.