即使在路径中使用了 shell 变量,如何使用 Tab 扩展文件名?

yol*_*yer 6 bash autocomplete

在 my 中,.bash_profile我设置了短变量以简化对一些常见目录的访问。例如:

lh=/var/log/httpd
hc=/etc/httpd/conf
Run Code Online (Sandbox Code Playgroud)

所以我像这样使用它,例如:

$ cd $lh
$ less $lh/access_log
Run Code Online (Sandbox Code Playgroud)

但是当我想使用 Tab 键来自动完成文件名(在这样一个包含变量引用的参数中)时,bash 执行自动完成但还在\变量名的美元符号之前插入一个反斜杠。

例如,键入less $lh/acc 然后按 Tab 将扩展为:less \$lh/access_log

当然,我想要的是less $lh/access_log 甚至less /var/log/httpd/access_log. (奇怪的是,cd在这种情况下,自动完成命令根本不起作用,这个问题已经讨论过了)

我知道使用shell-expand-line(default key: Ctrl+ Alt+ E)有一种替代方法,但它远非完美,因为它也扩展了别名,并且它没有引用带有特殊字符(空格,...)的路径。

即使在路径中使用了 shell 变量,bash 中有没有办法用 Tab 扩展文件名?

yol*_*yer 9

问题是direxpand未设置shell 选项。以下解决了这个问题:

shopt -s direxpand
Run Code Online (Sandbox Code Playgroud)