10

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.

1
  • There is a binary called xset. Try source ./xset. Aug 28, 2014 at 8:13

2 Answers 2

11

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.
6

The source command will:

Read and execute commands from the filename argument in the current shell context. If filename does not contain a slash, the PATH variable is used to find filename.

This behaviour is defined (for ., its alias) by POSIX. Why? Well, you can put sourceable configuration scripts inside PATH and access them without a qualified path. To access the file you want, give an absolute or relative path instead:

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

All of the above will work as you initially expected. You can also disable sourcepath with shopt.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .