我有一个小文件,它确实初始化了一个tmux会话,然后创建了一些窗口。经过一些调试和调整后,一切正常,直到我将文本文件(使用tmux命令)从重命名spam为xset:
$ source xset
bash: source: /usr/bin/xset: cannot execute binary file
Run Code Online (Sandbox Code Playgroud)
我现在已将文件重命名并source spam再次工作,但我想知道这是为什么。该文件在我的主目录中,而不是在/usr/bin.
Ant*_*hon 11
的bash内部命令的来源,首先查找在PATH文件名,除非有斜线(/在文件名)。xset是 PATH 中的一个可执行文件,因此是问题所在。
您可以执行source ./xset或将 sourcepath 选项更改为 off :
shopt -u sourcepath
Run Code Online (Sandbox Code Playgroud)
从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.
Run Code Online (Sandbox Code Playgroud)
从当前 shell 上下文中的filename参数读取和执行命令。如果filename不包含斜杠,则该
PATH变量用于查找filename。
此行为由 POSIX定义(对于.,其别名)。为什么?好吧,您可以将可获取的配置脚本放入其中PATH并在没有限定路径的情况下访问它们。要访问您想要的文件,请改为提供绝对或相对路径:
source ./xset
source ~/xset
source /home/shawn/xset
Run Code Online (Sandbox Code Playgroud)
以上所有内容都将按您最初的预期工作。您也可以禁用sourcepath用shopt。