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 '17 at 10:50
  • 7
    Try shopt -s direxpand – Arkadiusz Drabczyk Jul 15 '17 at 10:55
  • by the way maybe something wrong with your bash , because bash expands anykind of path after any command , – Yunus Jul 15 '17 at 10:57
  • @ArkadiuszDrabczyk: that solved the issue – yolenoyer Jul 15 '17 at 10:59

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

shopt -s direxpand

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.