Snippets: Shell: File / Dir / Path

1st December 2021 at 11:10am

获取脚本当前绝对目录:

# https://stackoverflow.com/a/246128
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

获取某一路径的目录:

echo $(dirname "/foo/bar/script.bash")  #=> /foo/bar

获取 basename:

echo $(basename "/path/to/foo.cpp")  #=> foo.cpp