Bash – How to expand filenames with Tab, even when a shell variable is used in the path

autocompletebash

In my .bash_profile I set short variables to ease the access to some common directories. For example:

lh=/var/log/httpd
hc=/etc/httpd/conf

So I use it like this for example:

$ cd $lh
$ less $lh/access_log

But when I want to use the Tab key in order autocomplete filenames (in such a parameter containing a variable reference), bash performs the autocomplete but
also inserts a backslash \ before the dollar sign of the variable name.

For example, typing less $lh/acc
then hitting Tab will expand to: less \$lh/access_log.

Of course, what I would like instead is less $lh/access_log
or even less /var/log/httpd/access_log.
(weirdly, with the cd command the autocomplete doesn't work at all in that case, this question already talks about it)

I know that there is an alternative by using shell-expand-line (default key: Ctrl+Alt+E), but it's far from
perfect because it expands aliases as well, and it doesn't quote paths with special chars (spaces, …).

Is there a way in bash to expand filenames with Tab, even when a shell variable is used in the path?

Best Answer

The issue was that the shell option direxpand was not set. The following solved the issue:

shopt -s direxpand