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

时间:2016-12-02 03:15:09

标签: bash shell sh

如果有多个sizeof *dtput cuu 1 && tput el的效果非常好。但是,如何替换由echo打印的行?

read

以上示例输出:

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

我希望最终结果是:

First line... Second line. read replaced.

1 个答案:

答案 0 :(得分:2)

您的代码未将光标移动到第0列。

一个简单的解决方案是在read使用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."

希望这有帮助。