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


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?

  • if it's about making things easy , why don't you put your both cmds and vars in one alias ?!; e.g: alias watchaccess="less $lh/access_log" .
    – Yunus
    Jul 15, 2017 at 10:50
  • 7
    Try shopt -s direxpand Jul 15, 2017 at 10:55
  • by the way maybe something wrong with your bash , because bash expands anykind of path after any command ,
    – Yunus
    Jul 15, 2017 at 10:57
  • @ArkadiuszDrabczyk: that solved the issue
    – yolenoyer
    Jul 15, 2017 at 10:59

1 Answer 1


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

shopt -s direxpand

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .