specify another directory to compgen for auto completing directories, than using pwd

bash, bash-completion, compgen

Solution

`compgen -d -- $cur` will suggest completions for the directories in pwd only right?

No, it will complete `$cur`, assuming the content is a path. Try it yourself by entering the command directly into your shell. Example:

$ compgen -d -- "/"
/run
/lib64
/usr
[...]

So the only thing you have to do is to use "$other_dir" instead of "$cur". Also, remember to quote the strings or the shell might split the directory name:

compgen -d -- "$foo" # good
compgen -d -- $foo   # bad

Problem

`compgen -d -- $cur` will suggest completions for the directories in pwd only right? Can I get it to suggest directory completions for some dir while being in another dir. For eg. if I am in `/curr_dir`, and I want to generate directory completions for `/other_dir` what do I do? How do I specify it to `compgen`, so that I don't need to go to `/other_dir` In my bash-completion function, I tried doing a `cd` to that dir before calling `compgen` but that leaves me in the other dir after pressing `<Tab>`.

Original source