which

在用户的路径中找到程序文件。

对于每个打印到stdout可执行文件完整路径的参数。它通过搜索环境变量PATH中列出的目录来完成此操作。

语法

  which [options] [--] program_name [...]

选项

--all, -a
    在PATH中打印所有匹配的可执行文件

--read-alias, -i
     读stdin中的别名,在stdout中报告匹配的别名.
     这与使用别名相结合非常有用,
     例:alias which='alias | which -i'

--skip-alias
    忽略选项 --read-alias, if any.

--skip-dot
   跳过PATH中以点开头的目录

--skip-tilde
    在以~和可执行文件开头的路径中跳过位于主目录中的目录

--show-dot
    如果路径中的目录以点开始,并且找到了匹配的可执行文件,那么打印
    "./program_name" 不是完整路径.

--show-tilde
   当目录与主目录匹配时,输出波浪线。此选项在作为根调用时被忽略。

--tty-only
    如果tty上没有,则停止右边的处理选项。

--version, -v, -V
    在标准输出上打印版本信息,然后成功退出。

--help
    在标准输出上打印使用信息,然后成功退出。

返回值

   Which 返回失败参数的数量,或者当没有提供程序名时返回-1

例子

   添加一个别名:

   alias which='which --tty-only --show-tilde --show-dot'

   这将从提示符开始打印可读的~/和./,
   而从脚本使用时仍然打印完整的路径:

          > which q2
          ~/bin/q2
          > echo `which q2`
          /home/carlo/bin/q2

   通过使用别名本身,别名也得到了支持。使用此特性的一个别名示例如下:

   alias which='alias | which --tty-only --read-alias --show-tilde --show-dot'

   这将为匹配给定参数的每个别名打印别名的输出。例如,在tcsh中使用这个别名:

          $ alias which alias \| /usr/bin/which -i !\*
          $ which which
          which   (alias | ./which -i !*)
                  /usr/bin/which

which 命令实例:

linux命令:which somecommand somecommand which which somecommandrootopen.com
linux命令:which somecommand somecommand which which somecommandrootopen.com
2018-06-06 11:05:48

linux命令:which <filename> <filename> which which <filename>rootopen.com
linux命令:which <filename> <filename> which which <filename>rootopen.com
2018-05-11 08:53:48

linux命令:which command command which which commandrootopen.com
linux命令:which command command which which commandrootopen.com
2018-05-26 07:34:48

linux命令:whichpkg() { rpm -qf "$@"; } } "$@"; -qf rpm { whichpkg() whichpkg() { rpm -qf "$@"; }rootopen.com
linux命令:whichpkg() { rpm -qf "$@"; } } "$@"; -qf rpm { whichpkg() whichpkg() { rpm -qf "$@"; }rootopen.com
2018-06-03 18:23:48

linux命令:whichpkg () { dpkg -S $1 | egrep -w $(readlink -f "$(which $1)")$; } } $1)")$; "$(which -f $(readlink -w egrep | $1 -S dpkg { () whichpkg whichpkg () { dpkg -S $1 | egrep -w $(readlink -f "$(which $1)")$; }rootopen.com
linux命令:whichpkg () { dpkg -S $1 | egrep -w $(readlink -f "$(which $1)")$; } } $1)")$; "$(which -f $(readlink -w egrep | $1 -S dpkg { () whichpkg whichpkg () { dpkg -S $1 | egrep -w $(readlink -f "$(which $1)")$; }rootopen.com
2018-06-10 11:17:48

linux命令:whichpath() { local -A path; local c p; for c; do p=$(type -P "$c"); p=${p%/*}; path[${p:-/}]=1; done; local IFS=:; printf '%s\n' "${!path[*]}"; } } "${!path[*]}"; '%s\n' printf IFS=:; local done; path[${p:-/}]=1; p=${p%/*}; "$c"); -P p=$(type do c; for p; c local path; -A local { whichpath() whichpath() { local -A path; local c p; for c; do p=$(type -P "$c"); p=${p%/*}; path[${p:-/}]=1; done; local IFS=:; printf '%s\n' "${!path[*]}"; }rootopen.com
linux命令:whichpath() { local -A path; local c p; for c; do p=$(type -P "$c"); p=${p%/*}; path[${p:-/}]=1; done; local IFS=:; printf '%s\n' "${!path[*]}"; } } "${!path[*]}"; '%s\n' printf IFS=:; local done; path[${p:-/}]=1; p=${p%/*}; "$c"); -P p=$(type do c; for p; c local path; -A local { whichpath() whichpath() { local -A path; local c p; for c; do p=$(type -P "$c"); p=${p%/*}; path[${p:-/}]=1; done; local IFS=:; printf '%s\n' "${!path[*]}"; }rootopen.com
2018-01-02 11:42:48

linux命令:which any_path/a_command.sh | sed "s|^./|$(pwd)|" "s|^./|$(pwd)|" sed | any_path/a_command.sh which which any_path/a_command.sh | sed "s|^./|$(pwd)|"rootopen.com
linux命令:which any_path/a_command.sh | sed "s|^./|$(pwd)|" "s|^./|$(pwd)|" sed | any_path/a_command.sh which which any_path/a_command.sh | sed "s|^./|$(pwd)|"rootopen.com
2018-02-12 14:16:48

linux命令:which <command> > /dev/null 2>&1 && echo Success! Success! echo && 2>&1 /dev/null > <command> which which <command> > /dev/null 2>&1 && echo Success!rootopen.com
linux命令:which <command> > /dev/null 2>&1 && echo Success! Success! echo && 2>&1 /dev/null > <command> which which <command> > /dev/null 2>&1 && echo Success!rootopen.com
2018-02-26 13:43:48

linux命令:which script | xargs less less xargs | script which which script | xargs lessrootopen.com
linux命令:which script | xargs less less xargs | script which which script | xargs lessrootopen.com
2018-06-07 01:52:48

Linux的Bash命令行(A-Z排序)