I have setup a variable in .bashrc:

export SOME_DIR=/path/to/some/dir

Now, with ls I can type ls $SOME_DIR[tab] and the variable will be expanded, and I can then use tab for auto-completion when going deeper into the filesystem. However, if I try cd $SOME_DIR[tab] or cd $SOME_DIR/[tab], nothing happens.

Why does this only work with ls? Is it possible to make it work with cd?
I have tried adding shopt -s direxpand to .bashrc, to no effect:

  • I just tried on a couple different systems and the expansion worked with cd exactly as it did with ls. Are you sure you're not losing $SOME_DIR in your testing? Does echo $SOME_DIR still give you what you are expecting?
    – nelsonov
    Sep 24, 2014 at 3:49
  • As you can see, different shells (with different options) on different OSes might perform differently. What shell on what OS/version are you using?
    – Lee-Man
    Jun 2, 2016 at 20:04

5 Answers 5


Apparently shopt -s direxpand works with everything except the built-in cd. So I just added alias xy=cd to my profile, and xy works just like cd except direxpand works using TAB. (Who would have thought it would be so simple?)


In Cygwin, I had the same issue and had to enclose the shell variable in quotes. So for example cd $ccc would not expand but cd "$ccc" would, and your cursor is still before the right-hand quote.

  • Sorry, I have to add that "$ccc/" works. You need that ending /
    – Nat Kidder
    Nov 20, 2014 at 14:14

Maybe you don't have directories in /path/to/some/dir/? I just tried cd $SOME_DIR/[tab] on my system and it worked.


The only way to expand the variable for me is to add a slash at the end of it:

$ cd $SOME_DIR[tab] ## fails
$ cd $SOME_DIR/[tab] ## works

I get exactly the same behavior with cd or ls. Could you have added the slash in on and fogoten it in the other?


I get almost the same issue with (old) bash 4.2.37, not with bash 4.3 or 4.4.
That is: expansion works with ls but not with cd.

However, as a helper keybinding, I have defined (from eSpand ):

Ctrl-s in my ~/.inputrc:

Control-s: shell-expand-line

That allows me to type: cd $SOME_DIR and then Ctrl-s to get the variable expanded to:

cd /path/to/some/dir

A keybinding that is equally useful is (from Globbing):

Ctrl-g also in my ~/.inputrc:

Control-g: glob-expand-word

That allows me to type ls b* and then Ctrl-g to get the b* expanded:

$ ls b*<kbd>Ctrl</kbd>-<kbd>s</kbd>
$ ls bash.html_node.tar.gz bash.pdf

Which also allow me to edit the command line and add or remove elements as needed.

You must log in to answer this question.

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