Ubuntu – How to create a fake process bar

command line

I want the terminal to show some kind of process bar based on time, like 1% every 60 seconds for example.

Best Answer

Create a progress bar in bash lists approaches to get a progress bar, so I'll concentrate on the How to fake part here. I'll use 2 seconds instead of your 60 here just for testing, adjust the sleep value to your exact needs.

Using dialog, whiptail or zenity (GUI)

for i in {1..100}; do sleep 2; echo $i; done | dialog --gauge 'Running...' 6 60 0

This for loop loops1 over the numbers one to hundred and echos them every 2 seconds, the output is then piped to dialog, which shows the number as the progress on a progress bar. This approach works for whiptail and zenity --progress (GUI) as well. dialog's output looks like this with a colored progress bar using 'curses' in text mode:

dialog progress bar

Using pv

for i in {1..100}; do sleep 2; echo; done | pv -pWs100 >/dev/null

This loop is very similar, just that it prints only a newline (=1 byte of data) every 2 seconds, pv is then told to expect exactly 100 bytes of data and show a progress bar. In a terminal window with a width of 80 characters the output looks like this:

[===============>                                                          ] 22%

Constructing your own progress bar

With a simple loop you can also construct your own progress bar. Here are some examples that just print 100 # in one line, one per 2 seconds:

# number signs only
$ for i in {1..100}; do sleep 2; echo -n \#; done; echo
####################################################################################################

# with progress in % on the right
$ for i in {1..100}; do sleep 2; printf "%0.s#" $(seq 1 $i); printf "%0.s " $(seq $i 100); printf "%3d%%\r" "$i"; done; echo
######################################################                                                54%

# with progress in % on the left
$ for i in {1..100}; do sleep 2; printf "%3d%% " "$i"; printf "%0.s#" $(seq 1 $i); printf "%0.s " $(seq $i 100); printf "\r"; done; echo
 39% ####################################### 

1 Look, a Polyptoton!

Related Question