Bash – Why does source give an error “cannot execute binary file”

bashshell

I have small file that does initialize a tmux session and then creates some windows. After some debugging and tweaking things worked fine until I renamed the text file (with the tmux commands) from spam to xset:

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

I have now renamed the file back and source spam works again, but I am wondering why this is. The file is in my home directory, and not in /usr/bin.

Best Answer

the bash internal command source, first looks for the filename in PATH, unless there is a slash (/) in the filename. xset is an executable file in your PATH, hence the problem.

You can either execute source ./xset or change the sourcepath option to off with:

shopt -u sourcepath

From the bash man-page:

      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.
Related Question