The rest of the answers are pretty good, but I just wanted to add more information in case someone comes here looking for a solution to replace / update a multi-line echo.
Therefore, I would like to share an example with all of you. The following script has been tested on CentOS and uses the "timedatectl" command, which basically displays some detailed information about your systemβs time.
I decided to use this command because its output contains several lines and works fine for the example below:
#!/bin/bash while true; do COMMAND=$(timedatectl) #Save command result in a var. echo "$COMMAND" #Print command result, including new lines. sleep 3 #Keep above output on screen during 3 seconds before clearing it #Following code clears previously printed lines LINES=$(echo "$COMMAND" | wc -l) #Calculate number of lines for the output previously printed for (( i=1; i <= $(($LINES)); i++ ));do #For each line printed as a result of "timedatectl" tput cuu1 #Move cursor up by one line tput el #Clear the line done done
Above will print the result of " timedatectl " forever and replace the previous echo with updated results.
I should mention that this code is just an example, but maybe not the best solution for you, depending on your needs. A similar command that will do almost the same thing (at least visually) is " watch -n 3 timedatectl ".
But this is a different story. :)
Hope this helps!
Antony Fuentes Artavia Dec 16 '16 at 19:40 2016-12-16 19:40
source share