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.

  • +1 In case you feel adventurous, here's the code.
    – l0b0
    Jan 22, 2014 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. Jan 22, 2014 at 23:23

2 Answers 2

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 also https://stackoverflow.com/questions/6418493/bash-variable-expansion-on-tab-complete


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.)

  • 1
    Had to install bash manually for this. 4.3 isn't in the fedora 20 repos.
    – jozxyqk
    Aug 4, 2014 at 7:17
  • !! Looks like zsh actually does even better: it keeps the environment variable as is but expands after it. I seriously might switch. Feb 5, 2021 at 21:00

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.