/home

Icon

Informations, tips and technics.

Overwrite a console line in Bash

Sometimes in Bash, you want to display some text in the console and sometimes you may also want to have the ability to overwrite the line that you have just displayed with a new string.

For example, a bash script could display a text “Working..” on the console and then, instead of display “Finished!” to the next line, you want the line that first display “Working…” be cleared and replace with the “Finished!” string.

Here is a little piece of code that could do this:

#!/bin/bash
tput sc
echo -n “Working…”
for i in `seq 1 5000`;
do
echo $i > /dev/null
done
tput el1
tput rc
echo “Finished!”

As you can see, I use the tput command. Bascally, I use the echo -n command to display the first line of text, the -n parameter to prevent a carriage return (new line), then I save the current line/column position using tput sc. After that, I do some stuff, and then I clear the line on the left using tput el1 and finally restore the position of the line/column using tput rc. I can then write the line that I want, the previous one has been deleted.

Update

Progress bar example.

I have found this example on the net and here is the code. I could help you to acheive the same goal.

ref: http://tldp.org/LDP/abs/html/assortedtips.html#PROGRESSBAR

ref: http://forum.soft32.com/linux/echo-line-overwrite-ftopict470692.html

#!/bin/bash
# progress-bar.sh

# Author: Dotan Barak (very minor revisions by ABS Guide author).
# Used in ABS Guide with permission (thanks!).

BAR_WIDTH=50
BAR_CHAR_START=”[”
BAR_CHAR_END=”]”
BAR_CHAR_EMPTY=”.”
BAR_CHAR_FULL=”=”
BRACKET_CHARS=2
LIMIT=100

print_progress_bar()
{
# Calculate how many characters will be full.
let “full_limit = ((($1 – $BRACKET_CHARS) * $2) / $LIMIT)”

# Calculate how many characters will be empty.
let “empty_limit = ($1 – $BRACKET_CHARS) – ${full_limit}”

# Prepare the bar.
bar_line=”${BAR_CHAR_START}”
for ((j=0; j<full_limit; j++)); do
bar_line=”${bar_line}${BAR_CHAR_FULL}”
done

for ((j=0; j<empty_limit; j++)); do
bar_line=”${bar_line}${BAR_CHAR_EMPTY}”
done

bar_line=”${bar_line}${BAR_CHAR_END}”

printf “%3d%% %s” $2 ${bar_line}
}

# Here is a sample of code that uses it.
MAX_PERCENT=100
for ((i=0; i<=MAX_PERCENT; i++)); do
#
sleep 1
# … Or run some other commands …
#
print_progress_bar ${BAR_WIDTH} ${i}
echo -en “\r”
done

echo “”

exit

Advertisements

Filed under: Bash, Linux

14 Responses

  1. Alex Amiryan says:

    Thanx for useful post. It helped a lot 🙂

  2. saintsteele says:

    You can also use the “\r” (carriage return) with “echo -e”.

    For example:
    echo -e “working….\c” ; sleep 2 ; echo -e “\rdone. “

    • saintsteele says:

      — the above code won’t work unless the quotes are replaced by normal quotes (thanks wordpress), and I would add some spaces after the word “done.”, but before the endqoute, to completely overwrite “working….”.

  3. snibbets says:

    Thanks. I’ve wondered how to do this previously and now I know!

  4. snibbets says:

    Btw, for a complete list of capabilities and the capname associated with each, see the man page for terminfo.

  5. […] Le script est disponible sur paste.kde.org. Rien de bien folichon si ce n'est les Ă©chappements dans les echo avec -e. Pour en savoir plus lĂ -dessus: direction l'abs [fr]. On peut faire la mĂŞme chose avec des tput. En s'aidant par exemple de ce site [en]. […]

  6. […] The script is available at paste.kde.org. Nothing fantastic here, except may be for the escaped characters within the echo-e. More on this on the abs. You can do the same thing with tput if you prefer. A nice example can be found on this site. […]

  7. nyuszika7h says:

    Just to let you know, you used incorrect slashes—it should be /dev/null, not \dev\null. Your solution works as well, but it creates a file named 'devnull' in the current directory.

  8. nyuszika7h says:

    Since I can’t edit commands—at least it seems so, I’m reposting this.

    Just to let you know, you’ve used incorrect slashes—it should be /dev/null, not \dev\null. Your solution works, as well, but it creates a file named devnull in your current directory.

    [And if possible, don’t accept the previous comment, it had a formatting issue… ;)]

  9. nyuszika7h says:

    Since I can’t edit comments—at least it seems so, I’m reposting this.

    Just to let you know, you’ve used incorrect slashes—it should be /dev/null, not \dev\null. Your solution works, as well, but it creates a file named devnull in your current directory.

    [And if possible, don’t accept the previous comment, it had a formatting issue… ;)]

  10. Thanks BUT these examples are useless for my purpose =(
    You’ll see, all of your examples fail if the first sentence it’s much more longer than the second.
    For example try: “Working… working… working… working… working… working… working… working… ” and try to replace it with “Done!”
    You’ll get: “Done!ng… working… working… working… working… working… working… working… ”
    So in fact the line it’s not really being overwritten completely, it’s just being overlapped =(
    Any ideas to accomplish what I need??? u.u

    • Romain Pelissier says:

      Can you give here an example of the code you are trying to do? In fact I have found that the ‘echo -e’ option is easier to use. Anyway, put your code here and I will try to help you.

    • Romain Pelissier says:

      Ok, I have check another way to do this and found a possible solution. I have updated my initial post with the excellent progress bar example. Let me know if it helps you.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: