I have a symlink:

/path/to/foo -> /path/to/bar/foo

When in /path/to/foo I might want to cd to /path/to/bar/foo:

$ cd ../ Tab

Then nothing autocompletes. I can still cd just fine by typing in the directory "bar", though.

Why is that? Is there a setting or trick I can apply to have that tab-complete too?


This is a bug in bash-completion (a third-party project, not affiliated with bash itself). /etc/bash_completion is sourced in ~/.bashrc by default in Ubuntu, and amongst the thousands of lines of code (most of which you likely never need), is a completion function for the cd builtin that doesn't handle symlinked directories properly. If you disable its completion for the cd builtin, your tab-completion for cd should work more like you expect.

complete -r cd
  • thanks. yet do you have a link to the bug report ? this to gain further insight on this
    – Julien
    Mar 4 '19 at 13:35

I think this is by design and how Bash handles the present working directory.

Compare the outputs of:

  1. pwd
  2. pwd -P

(The latter displays the physical current working directory instead of the symlinked one. Pwd manual.)

You can make Bash to always use the physical working directory by using:

set -P

The manual states:

-P If set, the shell does not follow symbolic links when executing commands such as cd that change the current working directory. It uses the physical directory structure instead. By default, bash follows the logical chain of directories when performing commands which change the current directory.

As a side effect, when you cd /path/to/foo, your pwd will be: /path/to/bar/foo.

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.