指定另一个目录来 compgen 自动完成目录,而不是使用 pwd

udi*_*209 5 bash bash-completion compgen

compgen -d -- $cur 将建议仅对 pwd 中的目录进行补全吗?

我可以让它在另一个目录中建议某些目录的目录完成吗?

例如。如果我在/curr_dir,并且我想为/other_dir我做什么生成目录完成?我如何指定它compgen,这样我就不需要去/other_dir

在我的 bash-completion 函数中,我尝试cd在调用之前对该目录执行 a操作,compgen但是在按下<Tab>.

nem*_*emo 5

compgen -d -- $cur 将建议仅对 pwd 中的目录进行补全吗?

不,它会完成$cur,假设内容是一个路径。通过直接在 shell 中输入命令来亲自尝试一下。例子:

$ compgen -d -- "/"
/run
/lib64
/usr
[...]
Run Code Online (Sandbox Code Playgroud)

所以你唯一需要做的就是使用“$other_dir”而不是“$cur”。另外,请记住引用字符串,否则 shell 可能会拆分目录名称:

compgen -d -- "$foo" # good
compgen -d -- $foo   # bad
Run Code Online (Sandbox Code Playgroud)