我已经看到通常在提到 shell 时使用的短语“sh compatible”。我不确定它是否也适用于可能在 shell 中运行的程序。
shell 或其他程序“sh 兼容”是什么意思?“sh不兼容”是什么意思?
编辑:这个问题询问 bash 和 sh 之间的区别非常相关: Difference between sh and bash
我仍然想直接回答“sh 兼容”的含义。一个合理的预期可能是“sh 兼容”意味着“实现了 Shell 命令语言”,但是为什么有这么多“sh 兼容”的 shell,它们为什么不同呢?
我通过运行 10 亿次循环来测试 Bash 和 Python 的速度。
$ cat python.py
#!/bin/python
# python v3.5
i=0;
while i<=1000000000:
i=i+1;
Run Code Online (Sandbox Code Playgroud)
重击代码:
$ cat bash2.sh
#!/bin/bash
# bash v4.3
i=0
while [[ $i -le 1000000000 ]]
do
let i++
done
Run Code Online (Sandbox Code Playgroud)
使用该time
命令,我发现 Python 代码只需要 48 秒即可完成,而 Bash 代码在我杀死脚本之前需要 1 个多小时。
为什么会这样?我预计 Bash 会更快。我的脚本有问题还是 Bash 使用这个脚本真的慢得多?