我正在编写一个使用 的脚本read -i,因此它需要 Bash 4.0 或更新版本。
不幸的是,到 2020 年初,macOS 仍然附带 Bash 3.x,我的脚本的许多用户将使用 Mac。由于 Apple 已将默认 shell 更改为 zsh,因此我不希望他们更新捆绑版本。
因此,我希望我的脚本能够检测 Bash 是否足够新,然后退出并返回一条指导用户升级的有用错误消息。
我知道我可以比较${BASH_VERSION},但这是一个非数字格式的字符串,可能不可靠。对此做出假设感觉很脆弱。
实现这一目标的最佳方法是什么?
requireBash4() {
if <something here>; then
>&2 echo "Bash 4.0 or newer is required."
>&2 echo "Once you're upgraded, re-run this script."
exit 1
fi
}
Run Code Online (Sandbox Code Playgroud)