Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if executable file is on user's PATH [duplicate]

Tags:

bash

In a bash script, I need to determine whether an executable named foo is on the PATH.

like image 835
Dónal Avatar asked Jul 04 '11 09:07

Dónal


2 Answers

You could also use the Bash builtin type -P:

help type

cmd=ls
[[ $(type -P "$cmd") ]] && echo "$cmd is in PATH"  || 
    { echo "$cmd is NOT in PATH" 1>&2; exit 1; }
like image 174
trevor Avatar answered Oct 16 '22 00:10

trevor


You can use which:

 path_to_executable=$(which name_of_executable)
 if [ -x "$path_to_executable" ] ; then
    echo "It's here: $path_to_executable"
 fi
like image 43
Michael Aaron Safyan Avatar answered Oct 16 '22 00:10

Michael Aaron Safyan