How can I check if a program exists from a Bash script?

后端 未结 30 1982
有刺的猬
有刺的猬 2020-11-21 07:21

How would I validate that a program exists, in a way that will either return an error and exit, or continue with the script?

It seems like it should be easy, but it\

30条回答
  •  时光说笑
    2020-11-21 08:01

    Expanding on @lhunath's and @GregV's answers, here's the code for the people who want to easily put that check inside an if statement:

    exists()
    {
      command -v "$1" >/dev/null 2>&1
    }
    

    Here's how to use it:

    if exists bash; then
      echo 'Bash exists!'
    else
      echo 'Your system does not have Bash'
    fi
    

提交回复
热议问题