How to tell if a command exists in the shell

Source: Internet
Author: User
Tags builtin posix

Reference:

Http://www.cnblogs.com/tuzkee/p/3755230.html

https://segmentfault.com/q/1010000000156870

Http://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script

Avoid using which, which can be implemented with the following commands:

$command-v Foo >/dev/null2>&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;}         

If your hash bang is  /bin/sh  then You should care on what POSIX says.  type  and  Hash ' exit codes aren ' t terribly well defined by POSIX, and  Hash  is Seen to exit successfully if the command doesn ' t exist (haven ' t seen this with  type  yet).   command 's exit status is a defined by POSIX, so that one was probably the safest to use.

If your script uses bash though, POSIX rules don ' t really matter anymore and both type and hash become perfectly safe to Use. Now have a to search just the and have the side-effect that the command's location would be type -P PATH hash hashed (f Or faster lookup next time you use it), which are usually a good thing since you probably check for its existence in order To actually use it.

As a simple example, here's a function that runs gdate if it exists, otherwise date :

gnudate() {    if hash gdate 2>/dev/null; then        gdate "[email protected]"    else        date "[email protected]"    fi}
In summary:

Where bash is your Shell/hashbang, consistently use hash (for commands) or type (to consider built-ins & keywords).

When writing a POSIX script, use command -v .

The first thing to note is that you should not use which to judge for the following reasons:

1, which non-shell built-in commands, with more overhead than built-in commands, and non-built-in commands depend on the implementation of the Platform, the implementation of different platforms may be different.

# type Typetype is a shell builtin# type commandcommand are a shell builtin# type Whichwhich is hashed (/usr/bin/which)

2, many system which does not set the return value when exiting, even if the command to find does not exist, which also returns 0

# which ls/usr/bin/ls# Echo $?0# which Aaano AAA in/usr/bin/bin/usr/sbin/sbin/usr/local/bin/usr/local/bin/usr/local /sbin/usr/ccs/bin/usr/openwin/bin/usr/dt/bin # echo $?0

3, many systems of which implementation, have secretly done some "outsiders" things

So, instead of using which, you can use the following method:

$ 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; }

The sharp original, can be viewed here:

http://stackoverflow.com/questions/592620/how-to-check-if-a-program-exists-from-a-bash-script/677212#677212

How to tell if a command exists in the shell

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.