bash - 如何在没有 Readline 支持的情况下在 bash 实例中设置不区分大小写的完成

标签 bash shell gnu readline bash-completion

我有一个没有 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 实例中, Readline 支持一切如预期:compgen -o default I 打印

IMG_1234.JPG
index.html

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

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

最佳答案

测试程序:

#!/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;我把它留在原处,这样您就可以看到它正在执行)。

关于bash - 如何在没有 Readline 支持的情况下在 bash 实例中设置不区分大小写的完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29385630/

相关文章:

linux - 如何在子进程中正确接收来自键盘的标准输入?

linux - 在 Linux (UBUNTU) 上使用 AWK 将 csv 文件中的格式从 %d/%m/%y 更改为 %Y-%m-%d

c - 使用 curses 中的 initscr() 时在错误的位置打印

macos - sed -i 命令用于就地编辑,可与 GNU sed 和 BSD/OSX 一起使用

parallel-processing - 使用 GNU parallel 并行化具有各种参数的脚本

linux - 将 Maildir 内容移动到父目录

bash - 用于使用 bash 读取两个文件的嵌套循环

linux - 如何同时运行两个命令?

linux - 如果在 bash 中运行特定命令,如何获得通知

r - R的格式化输出