检测可执行文件是否在用户的PATH上

在bash脚本中,我需要确定名为foo的可执行文件是否在PATH上。


更好的是,你可以使用which

 path_to_executable=$(which name_of_executable)
 if [ -x "$path_to_executable" ] ; then
    echo "It's here: $path_to_executable"
 fi

你也可以使用Bash内建type -P

help type

cmd=ls
[[ $(type -P "$cmd") ]] && echo "$cmd is in PATH"  || 
    { echo "$cmd is NOT in PATH" 1>&2; exit 1; }

使用which

$ which myprogram
链接地址: http://www.djcxy.com/p/57063.html

上一篇: Detect if executable file is on user's PATH

下一篇: 'which' equivalent function in Python