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

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

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

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

我现在已将文件重命名回来并source spam再次工作,但我想知道这是为什么。该文件位于我的主目录中,而不是/usr/bin.

答案1

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

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

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.

答案2

source命令将:

读取并执行来自的命令文件名当前 shell 上下文中的参数。如果文件名不包含斜杠,该PATH变量用于查找文件名

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

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

上述所有内容都将按您最初的预期工作。您还可以sourcepath禁用shopt

相关内容