shell - shell 脚本可以告诉它在哪个目录中。没有 bashisms

标签 shell sh

受到这个问题的启发。

Getting the source directory of a Bash script from within

是否可能,但不需要 bash。

我正在运行 Ubuntu,在我需要 Bash 问题的公认解决方案之前,我的哈希爆炸是这样的

#!/bin/sh

我喜欢这样,有没有办法保持这种状态?

编辑:

它应该输出正确的目录,即使这样运行,
./script.sh

最佳答案

怎么样$(dirname -- "$0") ?

如果您的 shell 仅使用反引号,则为:

`dirname -- "$0"`

关于shell - shell 脚本可以告诉它在哪个目录中。没有 bashisms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8468519/

相关文章:

unix - 在 POSIX sh 中循环环境变量

bash - 如何在fish shell中隐藏某些扩展名的文件?

c - gcc 选项 : pkg-config --libs --cflags gtk+-3. 0

sh - 如何在 Bourne Shell 中检查子字符串?

shell - 使用shell脚本将十六进制数转换为十进制数

docker - 什么时候必须在 Dockerfile 的 CMD 指令中使用 `/bin/sh -c ".. ."` ?

bash - scp 在复制大文件时停止

python - 使用shell脚本或python检查目录内容是否发生变化

bash - 错误 : Shell script returns unary operator is expected error message

linux - 在 shell 中从文件中删除模式线的快速方法