PATH路径前缀

时间:2012-08-31 11:20:55

标签: linux bash

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

4 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:3)

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

如果要在路径中查找程序但不执行该程序,请使用command -v。 (还有其他内置函数具有相同的效果,command -v具有可移植性的优点(它是内置的bash并且在POSIX中)。不要使用which,它是外部命令,不可靠而不是便携式的。)

如果您想查找包含a=b的{​​{3}},可以使用type -a

type -aP a=b

答案 2 :(得分:1)

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.

答案 3 :(得分:0)

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

(exec a=b)