MacOS – Bash not running script at /opt/local/etc/bash_completion.d/

bashmacos

I wanted to have bash autocompletion for git commands, so I dropped a bash script file into /opt/local/etc/bash_completion.d/.

When I open a new Terminal, the script file in /opt/local/etc/bash_completion.d/ does not get executed. I have verified that the script file is working by running:

source /opt/local/etc/bash_completion.d/git-completion.bash

Are there any steps missing here to ask OS X to automatically execute scripts in /opt/local/etc/bash_completion.d/?

Best Answer

In order to automatically load all scripts from a directory, put this in your ~/.bashrc file

# LOAD ALL AUTOCOMPLETIONS IF ANY ARE INSTALLED
if [ -d /usr/local/etc/bash_completion.d ]; then
    for F in "/usr/local/etc/bash_completion.d/"*; do
        if [ -f "${F}" ]; then
            source "${F}";
        fi
    done
fi