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

后端 未结 30 2000
有刺的猬
有刺的猬 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 07:43

    Command -v works fine if the POSIX_BUILTINS option is set for the to test for, but it can fail if not. (It has worked for me for years, but I recently ran into one where it didn't work.)

    I find the following to be more failproof:

    test -x $(which )
    

    Since it tests for three things: path, existence and execution permission.

提交回复
热议问题