为什么源给出错误“无法执行二进制文件”


10

我有一个确实会初始化tmux会话然后创建一些窗口的小文件。经过一些调试和调整后,一切正常,直到我将文本文件(使用tmux命令)从重命名spamxset

$ source xset
bash: source: /usr/bin/xset: cannot execute binary file

我现在已将文件重命名回去并source spam可以再次使用,但是我想知道为什么会这样。该文件位于我的主目录中,而不是/usr/bin


有一个名为的二进制文件xset。尝试source ./xset
Faheem Mitha 2014年

Answers:


11

bash内部命令的来源,首先查找在PATH文件名,除非有斜线(/在文件名)。xset是您PATH中的可执行文件,因此出现了问题。

您可以使用以下命令执行source ./xset或将sourcepath选项更改为off:

shopt -u sourcepath

bash手册页:

      source filename [arguments]
          Read and execute commands from filename  in  the  current  shell
          environment  and return the exit status of the last command exe
          cuted from filename.  If filename does not contain a slash, file
          names  in  PATH  are used to find the directory containing file
          name.  The file searched for in PATH  need  not  be  executable.
          When  bash  is  not  in  posix  mode,  the  current directory is
          searched if no file is found in PATH.  If the sourcepath  option
          to  the  shopt  builtin  command  is turned off, the PATH is not
          searched.  If any arguments are supplied, they become the  posi
          tional  parameters  when  filename  is  executed.  Otherwise the
          positional parameters are unchanged.  The return status  is  the
          status  of  the  last  command exited within the script (0 if no
          commands are executed), and false if filename is  not  found  or
          cannot be read.

5

source命令将

从当前Shell上下文中的filename参数读取和执行命令。如果filename不包含斜杠,则该PATH变量用于查找filename

此行为由POSIX 定义(.为其别名)。为什么?好了,您可以将可源配置脚本放入PATH其中,而无需限定路径即可访问它们。要访问所需的文件,请提供绝对或相对路径:

source ./xset
source ~/xset
source /home/shawn/xset

以上所有功能均可按您最初的预期工作。您也可以禁用sourcepathshopt

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.