猛击。。。使用bash脚本检查是否安装了程序

猛击。。。使用bash脚本检查是否安装了程序,bash,Bash,我试图创建一个小脚本来检查是否安装了程序。我正在尝试使用tmux `tmux --help` | grep "tmux: command not found" &> /dev/null if [ $? == 1 ]; then echo "tmux is not installed" exit fi 安装tmux后,我得到: usage: tmux [-2lquvV] [-c shell-command] [-f file] [-L socket-name]

我试图创建一个小脚本来检查是否安装了程序。我正在尝试使用tmux

`tmux --help` | grep "tmux: command not found" &> /dev/null
if [ $? == 1 ]; then
    echo "tmux is not installed"
    exit
fi
安装tmux后,我得到:

usage: tmux [-2lquvV] [-c shell-command] [-f file] [-L socket-name]
            [-S socket-path] [command [flags]]
tmux is not installed
如果未安装程序,则appair字符串“tmux:command not found”。这可以解释为什么我grep
tmux--help
命令的输出。是否有正确的方法检查是否安装了tmux


脚本alwais呼应“未安装tmux”。即使我安装了tmux。有什么问题吗?

您可以使用
命令
类型
哈希
内置函数来测试给定命令在当前shell会话中是否可用

但是,这不会告诉您它是否在当前路径以外的某些位置可用


为此,您应该避免使用
(尽管这是许多人的默认建议),因为它不是一个标准化的工具(不完全),与上述工具相比,它是一个外部工具,这些工具都内置在shell中(因此检查成本更高).

您不希望在执行
tmux--help
然后尝试将返回的帮助字符串作为命令执行的
tmux--help
周围出现反勾号。Lol。你完全正确