Home > bash > the command “source” doesn’t exactly work the way you thought

the command “source” doesn’t exactly work the way you thought

We know that “source” is a bash shell built-in command that executes the content of the file passed as argument in the current shell. It has a synonym: ‘.‘ (dot).

The classical use case is “source .bashrc” in your HOME directory. You do it when you modify your ~/.bashrc file and you want to activate the changes in the current terminal.

TIL something new. I had a discussion about it at reddit and the user geirha gave such a clear answer that I would quote him:

“The . (source is an alternative name for . in bash) command behaves more or less the same as when executing a command.

In POSIX sh, the following two will search for a file named foo in PATH (only).

    . foo

while the following two will specifically run and source, respectively, the file named foo in the current directory (only)

    . ./foo

Bash differs slightly (when not running in posix mode) in that when you give . or source an argument without any slash (/) characters (e.g. source foo), it searches through the PATH variable, like POSIX sh, but then also checks the current directory, if it was not found in PATH.” (end of quote)

Because of this I ran into a strange problem. I had a file called “.venv” that I wanted to source (source .venv) but I got some error. If I renamed the file, it worked well. But I wanted to call it “.venv”. What’s wrong?

As explained above, it turned out that “source” started to look for “.venv” in the PATH first. Somehow it found a different file with the very same name and that caused the problem. When I renamed the file to “not_hidden” or “.jabba”, it worked.

Solution #1
The trivial way is to use “./” by telling source to take the argument from the current directory:

source ./.venv

However, in my entire life I thought that “source .bashrc” takes the file .bashrc from the current directory. It should, in my opinion :)

Solution #2
If you also think that “source” shouldn’t do anything with the PATH variable, there is a cure. This feature can be switched off. The command “shopt” is a builtin that allows you to change additional shell optional behavior.

The guilty option is this one:


    If set, the source builtin uses the value of PATH to find 
    the directory containing the file supplied as an argument. 
    This option is enabled by default.

Simply run “shopt” to see if “sourcepath” is on for you. You can switch it off easily:

shopt -u sourcepath

To make it permanent, add this line to the end of your ~/.bashrc.

After all this ado, I could finally write this:

. .venv

And this is the beauty of Linux. You learn something new every day :)


Categories: bash Tags: , , , ,
  1. No comments yet.
  1. November 23, 2014 at 08:33

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: