OSX – Is It OK to Use ‘.’ to Run Files Instead of Source in .bashrc?

bashrcosxposix

OK, so source runs the script in the current shell and . separately, as detailed in running script with ". " and with "source " for example, but, specifically, in my .bashrc file, I have:

[ -f ~/.bash_aliases ] && source ~/.bash_aliases
[ -f ~/.git-completion.bash ] && source ~/.git-completion.bash
[ -s ~/.autojump/etc/profile.d/autojump.sh ] && source ~/.autojump/etc/profile.d/autojump.sh

Can I replace this with:

[ -f ~/.bash_aliases ] && . ~/.bash_aliases
[ -f ~/.git-completion.bash ] && . ~/.git-completion.bash
[ -s ~/.autojump/etc/profile.d/autojump.sh ] && . ~/.autojump/etc/profile.d/autojump.sh

Will that work on OS X – is that the "POSIX" issue?

I tried it and the above still seem to work on Ubuntu (so they actually work with both source and ., that is, they give me the desired functionality in the shell). Should I choose one over the other, or am I missing something?

FWIW, on OS X, I source my .bashrc from my .bash_profile.

Best Answer

This is POSIX's definition of .dot:

The shell shall execute commands from the file in the current environment.

If file does not contain a /<slash>, the shell shall use the search path specified by $PATH to find the directory containing file. Unlike normal command search, however, the file searched for by the .dot utility need not be executable. If no readable file is found, a non-interactive shell shall abort; an interactive shell shall write a diagnostic message to standard error, but this condition shall not be considered a syntax error.

Considering the above, you might as well just replace your [ -f ./file ] && source ./file with . ./file altogether. If the file is not there the worst that will happen is you'll get a notice at login - which is probably information you'd want to have, I think.

Of course if you'd rather keep the test you could do:

test -f ./file && . $_
Related Question