When scripting in bash or any other shell in *NIX, while running a command that will take more than a few seconds, a progress bar is needed.
For example, copying a big file, opening a big tar file.
What ways do you recommend to add progress bars to shell scripts?
You can implement this by overwriting a line. Use
\rto go back to the beginning of the line without writing\nto the terminal.Write
\nwhen you’re done to advance the line.Use
echo -neto:\nand\r.Here’s a demo:
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).