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

后端 未结 30 2032
有刺的猬
有刺的猬 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:05

    The hash-variant has one pitfall: On the command line you can for example type in

    one_folder/process
    

    to have process executed. For this the parent folder of one_folder must be in $PATH. But when you try to hash this command, it will always succeed:

    hash one_folder/process; echo $? # will always output '0'
    

提交回复
热议问题