Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwrite last line on terminal

My bash-script looks as following:

echo "Description:" while [ $finishInput -eq 0 ]; do   read tmp   desc="$desc"$'\n'"$tmp"   if [ -z "$tmp" ]; then     finishInput="1"   fi done echo -n "Maintainer:" read maintainer 

It reads to the desc var until a empty line is passed. After that, i want to read in other stuff.

When executing my current script it looks like this:

Description: Line 1 Line 2  Maintainer: 

I would like to overwrite the last empty line with the "Maintainer:".

I searched for a solution but only found suggestions which were like

echo -n "Old line" echo -e "\r new line" 

which stays on the line and overwrites it. This is not possible in my case.

like image 638
Zulakis Avatar asked Jul 01 '12 16:07

Zulakis


People also ask

How do you overwrite stdout?

To overwrite the current standard output line (or parts of it) use \r (or \b .) The special character \r (carriage return) will return the caret to the beginning of the line, allowing you to overwrite it.

How do you end a script in terminal?

If you are executing a Bash script in your terminal and need to stop it before it exits on its own, you can use the Ctrl + C combination on your keyboard.

Does Echo overwrite file?

The > redirection is done by shell, not by echo . In fact, the shell does the redirection before the command is even started and by default shell will overwrite any file by that name if exists.


1 Answers

In your example you delete the text at the same line. When you want to return to the previous line use \e[1A, and to clear that line, use \e[K:

echo 'Old line' echo -e '\e[1A\e[Knew line' 

When you want to go N lines up, use \e[<N>A

like image 177
Igor Chubin Avatar answered Sep 20 '22 09:09

Igor Chubin