Skip to content

Instantly share code, notes, and snippets.

@OlegGorj
Created September 22, 2019 19:48
Show Gist options
  • Save OlegGorj/906d43282dda87cdffab45b3565dfd6c to your computer and use it in GitHub Desktop.
Save OlegGorj/906d43282dda87cdffab45b3565dfd6c to your computer and use it in GitHub Desktop.
progress bar in a shell script

You can implement this by overwriting a line. Use \r to go back to the beginning of the line without writing \n to the terminal.

Write \n when you're done to advance the line.

Use echo -ne to:

  • not print \n and
  • to recognize escape sequences like \r

Here's a demo:

echo -ne '#####                     (33%)\r'
sleep 1
echo -ne '#############             (66%)\r'
sleep 1
echo -ne '#######################   (100%)\r'
echo -ne '\n'

In a comment below, puk mentions this "fails" if you start with a long line and then want to write a short line: In this case, you'll need to overwrite the length of the long line (e.g., with spaces).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment