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

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

    hash foo 2>/dev/null: works with Z shell (Zsh), Bash, Dash and ash.

    type -p foo: it appears to work with Z shell, Bash and ash (BusyBox), but not Dash (it interprets -p as an argument).

    command -v foo: works with Z shell, Bash, Dash, but not ash (BusyBox) (-ash: command: not found).

    Also note that builtin is not available with ash and Dash.

提交回复
热议问题