【问题标题】:How does one clear user input in Bash when using 'read'?使用“读取”时如何清除 Bash 中的用户输入?
【发布时间】:2011-01-19 01:59:56
【问题描述】:

我在无限循环中有这段代码:

read -a output
echo ${output[@]}

当代码运行时,屏幕可能如下所示:

Hello, World # line where value was given to output (I want this gone)
Hello, World # echoed output
[] # <-- cursor waiting for next input

有没有办法清除用户输入值的行?就像完全摆脱它一样(不仅仅是将行更改为仍然会在所需输出中留下空白的空白)?

【问题讨论】:

    标签: bash input


    【解决方案1】:

    tputcuu1dl1 功能结合使用。

    echo "foobar42"
    echo "$(tput cuu1)$(tput dl1)baz123"
    

    【讨论】:

    • @Jonathan:在某些情况下,最好使用el(擦除到行尾)而不是dl1(删除行),因为后者会导致该行下方的文本向上滚动。此外,没有必要使用命令替换 echo 的输出 tputtput cuu1; tput cr; tput el; echo baz123cr 确保您位于行首)
    • 谢谢,丹尼斯。在我将 dl1 方法重定向到日志文件后,它在文本之前留下了不需要的字符。你的方式就像一个魅力。
    【解决方案2】:

    为什么不直接删除echo ${output[@]}?由于您没有引用数组变量,因此这似乎与使用 tput hax 的结果相同。

    【讨论】:

    • 实际输出不只是 ${output[@]},还有与数组内容连接的其他格式化内容。不过,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2019-03-05
    • 2021-06-17
    • 2010-11-06
    相关资源
    最近更新 更多