PATH路径前缀



是否有一些特殊的路径前缀,如~,表示"在path中到处搜索"?我知道,当只提供可执行文件的基本名称时,这是默认行为,但对于像a=b这样的花哨的可执行文件名称,我只能用路径来调用它,可以是完整的,也可以是相对的/a=b。如果我只提供basename a=b,bash将其解释为变量赋值。

它并不完全是一个前缀,但引用可执行文件名(如'a=b')会在我的PATH中找到它。(Bash 3.2.17)

没有这样的前缀。如果您的唯一目的是执行带有"奇怪"字符的文件名,则不需要:只需引用这些字符,例如'a=b'a=b。然后bash的解析和扩展导致命令的第一个单词是a=b,它和其他命令名一样在路径中查找。

如果您想在路径中查找程序但不执行它,请使用command -v。(还有其他具有相同效果的内置程序,command -v具有可移植性的优势(它是bash内置程序,位于POSIX中)。不要使用which,它是一个外部命令,不可靠且不可移植。)

如果要查找路径中包含a=b的所有目录,可以使用type -a

type -aP a=b

内置的command正是为此目的而设计的,即寻找命令(不是别名,也不是函数)。

command a=b

应该做到这一点。来自bash手册:

  command [-pVv] command [arg ...]
          Run  command  with  args  suppressing  the  normal  shell function
          lookup. Only builtin commands or commands found in  the  PATH  are
          executed.   If  the  -p option is given, the search for command is
          performed using a default value for PATH  that  is  guaranteed  to
          find all of the standard utilities.  If either the -V or -v option
          is supplied, a description of command is printed.  The  -v  option
          causes  a  single word indicating the command or file name used to
          invoke command to be displayed; the -V option produces a more ver‐
          bose  description.   If  the -V or -v option is supplied, the exit
          status is 0 if command was found, and 1 if not.  If neither option
          is  supplied and an error occurred or command cannot be found, the
          exit status is 127.  Otherwise, the exit  status  of  the  command
          builtin is the exit status of command.

就我个人而言,我会使用引号,但另一种可能性是:

(exec a=b)

最新更新