Displaying and updating a counter in bash

19,822

Solution 1

for i in {0..15}; do echo -ne "$i"'\r'; sleep 1; done; echo 

You don't need ..1 for stepwidth 1 which is default.

echo -n 

prevents newlines.

\r is returning to begin of line (without newline - \n), and better than my formerly used '\b' for backstepping a single character, unhandy, if you have more than one digit-numbers. Thanks to rozcietrzewiacz.

Solution 2

Are you looking for something like this?

for i in {1..10}; do 
  printf '\r%2d' $i
  sleep 1
done
printf '\n'
Share:
19,822

Related videos on Youtube

LanceBaynes
Author by

LanceBaynes

Updated on September 18, 2022

Comments

  • LanceBaynes
    LanceBaynes almost 2 years

    I think it's something like this: (Fedora14/bash)

    #!/bin/bash
    
    for i in {0..10..1}; do echo -e "$i"'\c'
    echo -e "\n\r"
    sleep 1
    done
    

    But it doesn't work. Purpose: like this, but without the "clear":

    #!/bin/bash
    
    for i in {0..10..1}; do echo -e "$i"
    sleep 1
    clear
    done
    

    So a counting script that doesn't deletes the whole screen to output +1 number, instead it only deletes the line, where the counting is, so that there could be ex.: a beatifull "progress bar"..