I want find out if a command exists on UNIX from a bash shell script. If command does not exists in $PATH, then I would like to display an error message on screen. How do I find out if a command exists on Posix system or not?

You can use the following commands:

  1. command command – You can execute a simple command or display information about commands with the command called command. This is recommend for all Posix systems.
  2. type command – This is recommend for bash user. The -P option force a PATH search for each COMMAND NAME, even if it is an alias, builtin, or function, and returns the name of the disk file that would be executed.

Posix Command Example

The syntax is as follows:
command -v command1 >/dev/null && echo "command1 Found In \$PATH" || echo "command1 Not Found in \$PATH"

# POSIX command lookup example
CMDS="tar /usr/bin/mysqldump /path/to/other/command"
for i in $CMDS
        # command -v will return >0 when the $i is not found
	command -v $i >/dev/null && continue || { echo "$i command not found."; exit 1; }
# add rest of the script as we found all bins in $PATH
echo "Starting backup...."

type -P command Example

The syntax is as follows:
type -P command1 &>/dev/null && echo "Found" || echo "Not Found"
Here is a sample script:

# Use Bash builtin called type to determine whether a command exists or not
# Use full path if possible...
CMDS="tar /usr/bin/mysqldump ssh rsync"
for i in $CMDS
	type -P $i &>/dev/null  && continue  || { echo "$i command not found."; exit 1; }
# Add rest of the script as we found all bins in $PATH
echo "Starting backup...."
/bin/tar ....

You can use the exit status with which command as follows too:

which tar1 &>/dev/null
[ $? -eq 0 ] || echo "tar1 command not found."

I strongly recommend using full path for security reason for all sys admin activities. In this example, I’ve full path defined in /usr/local/nixcraft/redhat.paths.ini file:


You can use the following code to verify that all path exists and than call those hard coded part later on:

source  /usr/local/nixcraft/redhat.paths.ini
# get those path vars
paths=$(set | grep ^_path_*)
# verify those paths
for p in $paths
	type -P ${p##*=} &>/dev/null || { echo  "${p##*=} not found"; exit 1; }
# Alright, we got all binaries 
echo "Starting chroot() for ${_path_lighttpd}..."

Here is a sample script which use type -P:

# make sure rrdtool exists; else die with an error message
type -P $RRDTOOL &>/dev/null || { echo "$RRDTOOL not found. Set \$RRDTOOL in $0"; exit 1; }
# make sure dir exits; else create it
[ -d $OUTDIR ] || mkdir -p $OUTDIR
# make sure input file exits; else die with an error message
[ -f $INFILE ] || { echo "$INFILE input file not found. set \$INFILE in $0"; exit 2; }
DISP="-v bytes --title TrafficWebserver \
        DEF:binraw=$INFILE:InOctets:AVERAGE \
        DEF:binmaxraw=$INFILE:InOctets:MAX \
        DEF:binminraw=$INFILE:InOctets:MIN \
        DEF:bout=$INFILE:OutOctets:AVERAGE \
        DEF:boutmax=$INFILE:OutOctets:MAX \
        DEF:boutmin=$INFILE:OutOctets:MIN \
        CDEF:bin=binraw,-1,* \
        CDEF:binmax=binmaxraw,-1,* \
        CDEF:binmin=binminraw,-1,* \
        CDEF:binminmax=binmaxraw,binminraw,- \
        CDEF:boutminmax=boutmax,boutmin,- \
        AREA:binmin#ffffff: \
        STACK:binmax#f00000: \
        LINE1:binmin#a0a0a0: \
        LINE1:binmax#a0a0a0: \
        LINE2:bin#efb71d:incoming \
        GPRINT:bin:MIN:%.2lf \
        GPRINT:bin:AVERAGE:%.2lf \
        GPRINT:bin:MAX:%.2lf \
        AREA:boutmin#ffffff: \
        STACK:boutminmax#00f000: \
        LINE1:boutmin#a0a0a0: \
        LINE1:boutmax#a0a0a0: \
        LINE2:bout#a0a735:outgoing \
        GPRINT:bout:MIN:%.2lf \
        GPRINT:bout:AVERAGE:%.2lf \
        GPRINT:bout:MAX:%.2lf \
$RRDTOOL graph $OUTDIR/$OUTPRE-hour.png -a PNG --start -14400 $DISP -w $WIDTH -h $HEIGHT
$RRDTOOL graph $OUTDIR/$OUTPRE-day.png -a PNG --start -86400 $DISP -w $WIDTH -h $HEIGHT
$RRDTOOL graph $OUTDIR/$OUTPRE-month.png -a PNG --start -2592000 $DISP -w $WIDTH -h $HEIGHT
DISP="-v req --title RequestsperSecond -u 1 \
        DEF:req=$INFILE:Requests:AVERAGE \
        DEF:reqmax=$INFILE:Requests:MAX \
        DEF:reqmin=$INFILE:Requests:MIN \
        CDEF:reqminmax=reqmax,reqmin,- \
        AREA:reqmin#ffffff: \
        STACK:reqminmax#00f000: \
        LINE1:reqmin#a0a0a0: \
        LINE1:reqmax#a0a0a0: \
$RRDTOOL graph $OUTDIR/$OUTPRE-hour.png -a PNG --start -14400 $DISP -w $WIDTH -h $HEIGHT
$RRDTOOL graph $OUTDIR/$OUTPRE-day.png -a PNG --start -86400 $DISP -w $WIDTH -h $HEIGHT
$RRDTOOL graph $OUTDIR/$OUTPRE-month.png -a PNG --start -2592000 $DISP -w $WIDTH -h $HEIGHT

Further readings:

Our Linux shell scripting tutorial and the following man pages:
man bash
man ksh
help type
help command

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 6 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
6 comments… add one
  • tonf Apr 7, 2010 @ 20:38

    use “which”
    like “which somecommand”
    where the result will be blank if it does not exist or the full path of the command if it does

  • Clay May 4, 2010 @ 20:03

    Using “which” won’t show if the command that will be executed is an alias. “type” will show if there’s an alias.

  • darko Sep 17, 2010 @ 21:43

    According to The Open Group Base Specifications, `command -v` is not completely portable and `type -P` is not portable either:


  • Spam Assasin Dec 9, 2011 @ 15:59

    I have aliased ll=”ls -l” and it is there in $PATH. Yet “type -P” doesn’t show it. So @tonf is correct, which is simpler and easier to remember.

  • Daniel Hahler Jul 21, 2014 @ 0:01

    FWIW: `type -P` does not work with zsh:

    % type -P mountpoint
    zsh: bad option: -P

    And “command” is not available with “ash”, a minimal shell used with BusyBox v1.16.1 (e.g. on a Synology Diskstation).

  • Daniel Hahler Jul 21, 2014 @ 0:07

    `type -P` appears to be specific to Bash.

    `type -p` (lowercase “p”) works with bash, busybox/ash and zsh, and is similar to `-P`.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum