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

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

    If you guys/gals can't get the things in answers here to work and are pulling hair out of your back, try to run the same command using bash -c. Just look at this somnambular delirium. This is what really happening when you run $(sub-command):

    First. It can give you completely different output.

    $ command -v ls
    alias ls='ls --color=auto'
    $ bash -c "command -v ls"
    /bin/ls
    

    Second. It can give you no output at all.

    $ command -v nvm
    nvm
    $ bash -c "command -v nvm"
    $ bash -c "nvm --help"
    bash: nvm: command not found
    

提交回复
热议问题