On my Debian servers I'm used to hitting Tab to "preview" the expansion of shell patterns:

$ cp *some*<Tab>
something  somewhat  have-some-cake
$ cp *some*_

When the pattern expands to one entry, Tab replaces the pattern with the actual entry; otherwise it shows a list of matching entries. This is intuitive and useful because it's consistent with the regular "prefix" Tab completion.

But my Ubuntu servers and desktops behave differently: even when it would expand to more than one entry, Tab replaces the pattern with the first entry.

I have checked the usual suspects (/etc/bash.bashrc, /etc/inputrc, and the local versions) and I couldn't find any difference.

Does anybody know which setting controls this behaviour?

  • Not true for me on Lubuntu 16.04.3, <Tab> shows the list of matching entries as expected. I use Ubuntu since 12.04 and never experienced a different behaviour – which flavour(s), version(s) and shell(s) do you use? – dessert Aug 9 '17 at 11:41

Contrary the other answer, this particular problem is probably a direct result of using bash-completion. The bash-completion package has several bugs (as noted in this U&L answer about a similar problem, for instance).

If I comment out this section in my .bashrc:

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion

and start a new instance of bash, then I get:

$ echo *o*<tab><tab>
foo     food    foo.sh  
$ echo *o*

And then if I source the /usr/share/bash-completion/bash_completion script like it was in the .bashrc:

$ . /usr/share/bash-completion/bash_completion
$ echo foo

The *o* was immediately autocompleted to foo without showing the other matches.

I'm using 16.04, by the way. I don't know if this has been fixed in newer releases.

$ dpkg-query --show --showformat='${Package} ${version}\n' bash bash-completion
bash 4.3-14ubuntu1.2
bash-completion 1:2.1-4.2ubuntu1.1
  • Awesome, you found the culprit! Now I know where to start debugging – Tobia Nov 22 '17 at 17:42

Smarter tab completion in Bash requires the bash-completion package.

sudo apt install bash-completion

Bash Completion is actually written and maintained by Debian directly.
(See also its Debian package and Ubuntu package pages.)

Once it is installed, it either loads automatically (e.g. via /etc/bash.bashrc) or else you'll need to instruct Bash to load it in your ~/.bashrc with a stanza like this:

if [[ -z "$BASH_COMPLETION" ]] && [[ -r /etc/bash_completion ]]; then
  . /etc/bash_completion

Before Red Hat and others included this package, I used to manually copy /etc/bash_completion* between systems. This will work quite well, but I do not suggest it; it also cuts off the opportunity for your package manager to update existing completions and install new ones (which now go in /usr/share/bash-completion/completions/ though they originally lived with your custom completions in /etc/bash_completion.d/).

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.