我有一个小文件,它初始化一个tmux
会话,然后创建一些窗口。经过一些调试和调整后,一切正常,直到我将文本文件(使用命令tmux
)从重命名spam
为xset
:
$ 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
读取并执行来自的命令文件名当前 shell 上下文中的参数。如果文件名不包含斜杠,该
PATH
变量用于查找文件名。
这种行为.
由 POSIX定义(对于,其别名)。为什么?好吧,您可以将可获取的配置脚本放入其中PATH
并无需限定路径即可访问它们。要访问所需的文件,请提供绝对或相对路径:
source ./xset
source ~/xset
source /home/shawn/xset
上述所有内容都将按您最初的预期工作。您还可以sourcepath
禁用shopt
。