Is there a better way to erase a line than echo " "?
I am wanting to erase several (let's say 10) lines on the screen using bash.
I know this can be done by开发者_运维知识库:
for x in `seq 1 10`; do
echo " "
done
but there must be a better way.
Something like:
echo -n10 --blank
or
echo -n10 space(80)
or something similar.
Any ideas?
It's not necessary to use seq
in Bash:
for x in {1..10}
do
dosomething
done
Let's say you want to clear 10 lines starting at the 8th line on the screen, you can use tput
to move the cursor and do the clearing:
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
See man 5 terminfo
for more information.
Try
$ printf "%80s" ""
to get 80 spaces, without a trailing newline. If you want to know how many spaces you need, $COLUMNS is probably want you want:
$ printf "%${COLUMNS}s" ""
will give you a blank line of the appropriate length even if you've resized your window. The "clear" command will clear the entire window, too.
You can use echo
still with terminal escapes:
ceol=$(tput el)
for x in `seq 10 -1 10`; do
echo -n -e "\r${ceol}Counting $x"
sleep 1
done
Or if you prefer:
echo -n -e "\033[1K\rCounting $x"
-n
do not output \n\r at end of line (so cursor stays at end of last character)\r
return to start of line${ceol}
clear to end of line (so comes after\r
)\033[1K
clear to start of line (so comes before\r
)
Nb. made it count backwards to prove that the line is cleared; i.e. when printing 9, it shows that the 0 from 10 has been cleared.
I would use this:
for x in $(seq 10); do
tput cup ($x)-1 0
tput el
done
精彩评论