Source multiple files via shell filename completion

2

1

I'm trying to enable autocompletion of my homebrew commands. Homebrew automatically creates the folder bash_completion.d and sym-links all completion files there.

$ ls -a `brew --prefix`/etc/bash_completion.d/
.  ..  brew_bash_completion.sh  git-completion.bash

As you can see I have completions for brew and git. So I try to run the files:

[ -d `brew --prefix`/etc/bash_completion.d ] && source `brew --prefix`/etc/bash_completion.d/*

This is in my ~/.bash_profile, and I'm expecting this to source all the files in bash_completion.d. When I try it out later in the terminal, only the completion for brew works.

Is there something I'm missing?

Johan Lindskogen

Posted 2012-08-03T10:30:18.233

Reputation: 123

Answers

2

The source help eludes that mystery:

source: source filename [arguments]
    Read and execute commands from FILENAME and return.  The pathnames
    in $PATH are used to find the directory containing FILENAME.  If any
    ARGUMENTS are supplied, they become the positional parameters when
    FILENAME is executed.

which means that when you feed it a list of filenames generated through a glob, only the first is sourced, while all filenames after it are considered its arguments. To source all files in your completion directory, you will need to loop over it, i.e.

for f in "$(brew --prefix)"/etc/bash_completion.d/*; do source "$f"; done

However, as you use homebrew’s bash completion, there is a much better way: simply

source "$(brew --prefix)"/etc/bash_completion

which will take charge of sourcing anything found in the pertinent completion directories, while also adding a plethora of useful completions of its own.

kopischke

Posted 2012-08-03T10:30:18.233

Reputation: 2 056