如何获取其中的目录路径的Bash脚本位于,里面那个脚本?
例如,假设我想使用Bash脚本作为另一个应用程序的启动器.我想将工作目录更改为Bash脚本所在的目录,因此我可以对该目录中的文件进行操作,如下所示:
$ ./application
如何在"$0"
和之间做出选择"${BASH_SOURCE[0]}"
GNU的这个描述对我没什么帮助.
BASH_SOURCE
An array variable whose members are the source filenames where the
corresponding shell function names in the FUNCNAME array variable are
defined. The shell function ${FUNCNAME[$i]} is defined in the file
${BASH_SOURCE[$i]} and called from ${BASH_SOURCE[$i+1]}
Run Code Online (Sandbox Code Playgroud) 如何获取文件的父目录?
我希望它对所有类型的名称都是安全的:
.
..
path/to/my/file
/absolute/path/to/my/file
'-rf --no-preserve-root whatever'/test.zip
(symbolic links)
`'"`'{(})
Run Code Online (Sandbox Code Playgroud)
我更感兴趣的是获取文件系统上的规范位置,而不是遍历文件名中指定的路径.
请注意,这个问题有类似的问题,但没有一个关注正确性,相对/绝对路径和"不安全"名称:
[1] bash获取当前目录的父目录
[2] 检索脚本的父目录
[3] bash文件路径到文件的父目录