相关疑难解决方法(0)

从脚本本身获取Bash脚本的源目录

如何获取其中的目录路径的Bash脚本位于,里面那个脚本?

例如,假设我想使用Bash脚本作为另一个应用程序的启动器.我想将工作目录更改为Bash脚本所在的目录,因此我可以对该目录中的文件进行操作,如下所示:

$ ./application

directory bash

4672
推荐指数
44
解决办法
158万
查看次数

在$ 0和BASH_SOURCE之间选择

如何在"$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)

bash

84
推荐指数
4
解决办法
4万
查看次数

在bash中获取文件的父目录

如何获取文件的父目录?

我希望它对所有类型的名称都是安全的:

.
..
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文件路径到文件的父目录

linux bash shell sh

2
推荐指数
1
解决办法
6014
查看次数

标签 统计

bash ×3

directory ×1

linux ×1

sh ×1

shell ×1