What is the best method to test if a program exists for a shell script?

18,539

You probably want to use the command command, e.g.:

command -v <the_command>

For more discussion see the following StackOverflow posts:

Also see the following Unix StackExchange post:

Share:
18,539
Brandon Condrey
Author by

Brandon Condrey

Consider opposing apartheid in Palestine and signing onto the BDS Movement; #1 User for DBA.SE 2017. Available for contracting: 281.901.0011 PostgreSQL &amp; PostGIS / MySQL / SQL Server JavaScript, Typescript, Rx.js, Node.js, Angular Also: C / Perl / Python / Rust / x86 Assembly

Updated on September 18, 2022

Comments

  • Brandon Condrey
    Brandon Condrey almost 2 years

    I'm just curious to know how to best check if a program exists. I've seen solutions with

    • test
    • command
    • hash
    • type
    • which
    • [

    Which is the best method, is there a right answer? What does the POSIX and LSB specs say about this?

  • Brandon Condrey
    Brandon Condrey over 6 years
    Thanks but this a dupe of this, unix.stackexchange.com/questions/85249/…
  • igal
    igal over 6 years
    @EvanCarroll Yeah, I just saw that. Well... problem solved!