Multiline solution
In comment referencing @Um's answer, @Maëlan correctly identified that tput sc saves only the graphical position, not logical. You can clearly see it when you run the script, while being at the last line of a terminal. The printed text behaves like it is not being cleared at all.
To solve it, as well as make the solution more optimised, I've made the following clearLastLines function:
function clearLastLines() { local linesToClear=$1 for (( i=0; i<linesToClear; i++ )); do tput cuu 1 tput el done }
Passed as a parameter the number of lines to clear.
Example usage:
echo 'Text to be deleted 1.' echo 'Text to be deleted 2.' clearLastLines 2 echo 'New text.'
Notes:
- It works only when the number of lines to delete is known (shouldn't be hard to implement it when it is unknown).
- Would be great if anyone could actually verify whether the solution is actually better for terminals like
urxvt, etc. (as mentioned by @Maëlan).