Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using, among other things, bash 4.2.37 on Linux Mint 14.

When I autocomplete the name of an environment variable by typing Tab, it expands to the name of the variable followed by a space.

If the value of the variable happens to be a directory name, I want it to expand to the name of the variable followed by /.

For example, if I type:

$ ls $HOM<tab>

it expands to:

$ ls $HOME _

where _ marks the location of the cursor. The problem is that I then usually want to continue typing the name of some file or directory under my home directory, which means I have to type backspace and then /.

So I want the above to expand to:

$ ls $HOME/_

The same thing happens when I've typed the entire variable name; after ls $HOME, tab adds a space, and I want it to add a /.

(And of course I don't want it to add a / if the variable's value is not a directory name.)

Is there some way I can tell bash to behave the way I want, either via bash commands or by updating my $HOME/.inputrc?

Note: I'm specifically looking for a way to make tab behave the way I want. Ways to achieve the same behavior with different keystrokes would be interesting, but would not answer my question.

(I've disabled /etc/profile.d/bash_completion.sh on my system because it interacts poorly with set -o nounset. A quick experiment indicates that this is probably not relevant to my question.)

Incidentally, tcsh does this: given an environment variable $FOOBAR, typing $FOOBA<tab> expands to $FOOBAR/ if it refers to a directory, $FOOBAR (with a space) if it doesn't. No, that's not enough to persuade me to go back to tcsh. zsh does this as well.

share|improve this question
+1 In case you feel adventurous, here's the code. – l0b0 Jan 22 '14 at 23:15
@l0b0: (a) I already have it. (b) I'm not feeling quite that adventurous. (c) It's suppose to be programmable completion. – Keith Thompson Jan 22 '14 at 23:23

This feature is implemented in bash 4.3, released 2014-02-26.

Quoting the NEWS file (included at the top level of bash-4.3.tar.gz (see also the GPG signature):

The word completion code checks whether or not a filename containing a shell variable expands to a directory name and appends / to the word as appropriate. The same code expands shell variables in command names when performing command completion.

A quick experiment with a copy built from source shows that if I type

$ ls $HOM<tab>

it now expands to

$ ls $HOME/

(Incidentally, zsh also does this.)

share|improve this answer
Had to install bash manually for this. 4.3 isn't in the fedora 20 repos. – jozxyqk Aug 4 '14 at 7:17
shopt -s direxpand

will help, there will be no space so you can continue typing but it will replace the enviroment variable with its content.

See alsp http://stackoverflow.com/questions/6418493/bash-variable-expansion-on-tab-complete

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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