Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way to erase a line than echo " "?

Tags:

bash

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?

like image 816
Brent Avatar asked Dec 07 '25 10:12

Brent


2 Answers

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.

like image 192
Dennis Williamson Avatar answered Dec 10 '25 11:12

Dennis Williamson


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.

like image 24
Anthony Towns Avatar answered Dec 10 '25 12:12

Anthony Towns



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!