为什么是“^?”使用带有 -n 参数的读取命令时出现?

Why does "^?" appears when using read command with -n argument?

这是我写的 shell 小脚本 :

#!/bin/bash

echo "Tell me something"
read -n 1000 text

但是当使用 -n 参数时,我无法删除我的拼写错误,因为使用退格键会导致插入“^?”进入输入。

示例输入:

t^?This is my ans^?wer

我的问题是为什么使用 read 有或没有 -n 参数会导致不同的行为,我如何使用 read使用 -n 参数,所以我通常可以使用退格键?

显然 -n 禁用了 Readline,它通常在 stdin 交互时默认打开。使用 -e 明确启用它。

read -n 1000 -e text

另一种方法是在用户提供输入后应用长度限制:

read -r text
text=${text:0:1000}
echo "You entered ${#text} characters"