Bash 如何使用tput覆盖read打印的行

Bash 如何使用tput覆盖read打印的行,bash,shell,sh,Bash,Shell,Sh,tput cuu 1和&tput el在出现多个echo时工作良好。但是,如何替换由读取的打印的行 echo "First line..." read -p "Press any key to overwrite this line... " -n1 -s tput cuu 1 && tput el echo "Second line. read replaced." 上述示例输出: 第一行。。。第二行。阅读已替换。 我希望最终结果是: 第一行。。。 第二行。读替换。 您的代

tput cuu 1和&tput el
在出现多个
echo
时工作良好。但是,如何替换由
读取的
打印的行

echo "First line..."
read -p "Press any key to overwrite this line... " -n1 -s
tput cuu 1 && tput el
echo "Second line. read replaced."
上述示例输出:

第一行。。。第二行。阅读已替换。

我希望最终结果是:


第一行。。。
第二行。读替换。

您的代码没有将光标移动到第0列

一个简单的解决方案是保存光标位置,然后使用
tput sc
读取
打印提示

读取用户输入后,可以使用
tput rc
恢复光标位置

您的代码现在应该是这一行

echo "First line..."
tput sc
read -p "Press any key to overwrite this line... " -n1 -s
tput rc 1; tput el
echo "Second line. read replaced."

希望这有帮助。

t输出cuu1
将光标上移1,但行保持不变。您还需要将光标向左移动。@alvits不是基本上用来擦除整行的
tput el
吗?不是。
el
是从当前位置擦除行尾。您可能希望将其用作参考,最好是在读取之前保存光标位置:
tput sc
。然后读。现在恢复光标位置并清除行尾:
tput rc;t输出el
。最后打印第二行。