有没有比echo“”擦除一条线更好的方法?

时间:2009-11-27 17:28:15

标签: bash

我想用bash擦除屏幕上的几行(比如说10行)。

我知道这可以通过以下方式完成:

for x in `seq 1 10`; do
  echo "                                                    "
done

但必须有更好的方法。

类似的东西:

echo -n10 --blank

echo -n10 space(80)

或类似的东西。

有什么想法吗?

4 个答案:

答案 0 :(得分:5)

没有必要在Bash中使用seq

for x in {1..10}
do
    dosomething
done

假设您要从屏幕的第8行开始清除10行,您可以使用tput移动光标并进行清除:

tput cup 8 0        # move the cursor to line 8, column 0
for x in {1..10}
do
    tput el          # clear to the end of the line
    tput cud1        # move the cursor down
done
tput cup 8 0        # go back to line 8 ready to output something there

有关详细信息,请参阅man 5 terminfo

答案 1 :(得分:2)

您仍然可以使用echo终端转义:

ceol=$(tput el)
for x in `seq 10 -1 10`; do
   echo -n -e "\r${ceol}Counting $x"
   sleep 1
done

或者如果您愿意:

echo -n -e "\033[1K\rCounting $x"
  • -n不在行尾输出\ n \ r \ n(因此光标停留在最后一个字符的末尾)
  • \r返回行首
  • ${ceol}清除到行尾(因此在\r之后)
  • \033[1K清楚地开始行(因此在\r之前)

的Nb。使它倒数以证明该线被清除;即,当打印9时,它表示10中的0已被清除。

答案 2 :(得分:1)

尝试

$ printf "%80s" ""

获得80个空格,没有尾随换行符。如果你想知道你需要多少空格,可能需要$ COLUMNS:

$ printf "%${COLUMNS}s" ""
即使你调整了窗口大小,

也会给你一个适当长度的空白行。 “clear”命令也将清除整个窗口。

答案 3 :(得分:0)

我会用这个:

for x in $(seq 10); do
  tput cup ($x)-1 0
  tput el                                               
done
相关问题