tput 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."
上面的例子输出:
第一行...第二行。读取替换。
我希望最终结果是:
第一行...
第二行。读取替换。
最佳答案
您的代码没有将光标移动到第 0 列。
一个简单的解决方案是在使用tput sc
read
打印提示之前保存光标位置。
读取用户输入后,您可以使用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."
希望这有帮助。
关于bash - 如何使用 tput 覆盖 read 打印的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40924187/