tags:

views:

74

answers:

2

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?

+1  A: 

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.

Anthony Towns
Perfect - thank you.
Brent
Is there a way to repeat this - other than embedding it in a loop?
Brent
If you are worried about readability you could wrap the loop in a function
CBFraser
+3  A: 

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.

Dennis Williamson
I didn't know tput. Thanks!
mouviciel