Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print in the same line as a progress in bash script

Tags:

bash

I want to print the text in the following manner

Waiting for completion.
Waiting for completion..
Waiting for completion...

[Note :Not more than three dots]

The above should be in the same line and in a loop.

When the loop condition is false I want to get the following in the same line as well :

Waiting for completion... [OK]

How do I achieve this in bash script?

like image 233
Prasanta Biswas Avatar asked Oct 20 '25 12:10

Prasanta Biswas


1 Answers

You should use carriage return. Search information in echo about \r.

for example maybe you want something like this:

#!/bin/bash

while [ 3 -gt 2 ];
do
    echo -n -e 'Esperando.\r'
    sleep 1
    echo -n -e 'Esperando..\r'
    sleep 1
    echo -n -e 'Esperando...\r'
    sleep 1
    echo -n -e '             \r'
done

You need to sleep cause if you dont sleep you won't be able to watch the changing dots.

like image 100
Sergio Izquierdo Avatar answered Oct 23 '25 02:10

Sergio Izquierdo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!