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

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

    I second the use of "command -v". E.g. like this:

    md=$(command -v mkdirhier) ; alias md=${md:=mkdir}  # bash
    
    emacs="$(command -v emacs) -nw" || emacs=nano
    alias e=$emacs
    [[ -z $(command -v jed) ]] && alias jed=$emacs
    

提交回复
热议问题