check if command exists in linux

How to Check if Program Exists in Shell Script

When you call a command from shell script in Linux it is advisable to check if it exists on the system where the script is being run. This will help you avoid errors and prevent script termination. This way, if the command does not exist, you can continue running the script, without interruption. In this article, we will learn how to check if program exists in shell script.


How to Check if Program Exists in Shell Script

You can easily check if a command exists or not using command keyword. Here is its syntax.

command -v <the_command>

The above command returns true if the command you specified after -v option exists, else it will return false.

For example, if you want to check whether a command exists or not, and display an error message if the command does not exist.

if ! command -v <the_command> &> /dev/null
then
    echo "<the_command> could not be found"
    exit
fi

Alternatively, you can also use hash and type commands to check if Linux commands and tools exist or not.

hash <the_command> # For regular commands. Or...
type <the_command> # To check built-ins and keywords

Typically, many developers and administrators use which command to check if a command exists or not.

which <the_command>

It is better to use command, hash and type, instead of using which command because which is an external command while the other 3 are internal commands. The result of internal commands are the same across most systems while the result of external commands vary from one system to another.

In many operating systems, which command does not even return an exit status so if you use it in an if condition it will always evaluate to true.

Instead, you can use command, hash and type commands to do the same thing. Here are sample commands to test if command foo exists on your system.

$ command -v foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed.  Aborting."; exit 1; }
$ type foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed.  Aborting."; exit 1; }
$ hash foo 2>/dev/null || { echo >&2 "I require foo but it's not installed.  Aborting."; exit 1; }

Here are a couple of things to keep in mind while using hash, type & command.

POSIX clearly defines outputs of command. So you can safely use it in almost all scripts. But if your hash bang (execution environment) is /bin/sh you should be careful about using hash and type since their exit status is not well defined by POSIX.

If your script uses bash, then hash and type don’t matter and both are perfectly safe to use. In fact, type provides a -P option to directly look into the folder paths mentioned in your system’s PATH variable and hash will automatically hash the path of command for faster lookup the next time.

Here’s a simple example to run gdate function if it exists, else run date function.

gnudate() {
    if hash gdate 2>/dev/null; then
        gdate "$@"
    else
        date "$@"
    fi
}

Alternatively, you can even use third-party scripts like scripts-common to check if a script exists or not. Here is its syntax.

checkBin <the_command> || errorMessage "This tool requires <the_command>. Install it please, and then run this tool again."

In this article, we have learnt how to check if program exists or not, and how to add it to an if..else condition to continue script execution in case the command does not exist.

Also read:

How to Insert Text to Beginning of File
How to Get My Public SSH Key
How to Revoke SSH Access & Keys in Linux
How to Create Man Pages for Package, Module, Script
How to Keep SSH Session Alive After Disconnect

Leave a Reply

Your email address will not be published. Required fields are marked *