2012-08-31 103 views
3

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

+0

像这样命名您的可执行文件在一般情况下显然不是一个好主意。 – tripleee

+0

但考虑到命令可能具有看起来像句法实体的奇怪名称,这是一个有效的问题。 ''''伪装成'/ usr/bin/test'。 – Jens

回答

3

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

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

If你想查找包含a=ball the directories in the path,你可以使用type -a

type -aP a=b 
+0

感谢您的见解。迄今为止,我认为最好的答案。 –

4

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

+0

这对我也适用。 Bash 4.2.24 – Adam

0

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

(exec a=b) 
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.