Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I overwrite multiple lines with echo or printf in shell?

I have some shell functions, one draws some ASCII art to the screen on multiple lines eg:

printf %s '
______________
||__________||

'
sleep 1
clear
printf %s '
______________
||__________||
      ||
      ||
      ||

'
sleep 1
clear
printf %s '
______________
||__________||
      ||
      ||
      ||
||‾‾‾‾‾‾‾‾‾‾||
‾‾‾‾‾‾‾‾‾‾‾‾‾‾
'

is there a way to overwrite the string without clearing the screen? I have tried using \r on every line and on the last line but that only seems to replace one line strings.

like image 651
TheRedPill Avatar asked May 27 '26 07:05

TheRedPill


1 Answers

This depends completely on your terminal. Many terminals allow to send an escape sequence move cursor to home position. This might be available as the terminfo(1) command

tput home
printf 'Cursor now home\n'

See man terminfo if you have it. If not, you need to tell us the value of your TERM environment variable (echo $TERM).

like image 96
Jens Avatar answered May 30 '26 08:05

Jens



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!