【问题标题】:How to set case-insensitive completion in a bash instance without Readline support如何在没有 Readline 支持的 bash 实例中设置不区分大小写的完成
【发布时间】:2015-04-01 07:58:37
【问题描述】:

我有一个不支持 Readline 的 bash 实例,即这个 bash 是使用 --noediting 选项调用的。原因是这个 bash 实例被另一个程序使用了。

另一个程序想知道这个 bash 将如何完成命令行,为此它在 bash shell 中发出 compgen 命令,例如

compgen -o default I

在没有 Readline 支持的情况下,这在我的 bash 中完美运行。这是一个例子:假设我们在一个包含三个文件的目录中:

IMG_1234.JPG
about.html
index.html

compgen -o default I 命令正确打印

IMG_1234.JPG

但是现在我想切换到不区分大小写的完成。通常我在 shell 中发布

bind 'set completion-ignore-case on'

在 bash 实例中 with Readline 支持一切都如预期:compgen -o default I 打印

IMG_1234.JPG
index.html

但是在我的 bash 实例中没有 Readline 支持 bind 命令什么都不做,我仍然只得到 IMG_1234.JPG 匹配。

所以,我的问题是:如何在没有 Readline 支持的情况下在 bash 实例中设置不区分大小写的完成建议(使用 compgen 时)?

【问题讨论】:

  • 尝试配置/etc/inputrc
  • 我的 ~/.inputrc 文件包含 set completion-ignore-case on,但没有 Readline 支持的 bash 似乎会忽略此文件。
  • inputrc 代表readline
  • @whjm 没错!正如问题明确指出的那样,我想在“--noediting”bash(不使用 inputrc)中通过compgen 切换到不区分大小写的完成建议。

标签: bash shell gnu readline bash-completion


【解决方案1】:

测试程序:

#!/bin/bash
set -e

echo "### CASE-SENSITIVE"
bash --noediting <<EOF
compgen -o default I
EOF
echo
echo "### CASE-FOLDING"
bash --noediting <<EOF
bind 'set completion-ignore-case on'
compgen -o default I
EOF

结果:

### CASE-SENSITIVE
IMG_1234.JPG

### CASE-FOLDING
bash: line 1: bind: warning: line editing not enabled
IMG_1234.JPG
index.html

Bash 版本:

GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)

如果您的 bash 版本不同,请考虑升级,因为我无法在 4.3.11 中重现您的问题。 (你可能想从bind 命令重定向stderr;我把它留在里面,这样你就可以看到它正在被执行)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多