Asked  7 Months ago    Answers:  5   Viewed   56 times

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 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:

  1. not print n and
  2. 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).

Tuesday, June 1, 2021
answered 7 Months ago

You need to use the -p flag to send a password. And it's tricky because you must have no space between -p and the password.

$ mysql -h "server-name" -u "root" "-pXXXXXXXX" "database-name" < "filename.sql"

If you use a space after -p it makes the mysql client prompt you interactively for the password, and then it interprets the next command argument as a database-name:

$ mysql -h "server-name" -u "root" -p "XXXXXXXX" "database-name" < "filename.sql"
Enter password: <you type it in here>
ERROR 1049 (42000): Unknown database 'XXXXXXXX'

Actually, I prefer to store the user and password in ~/.my.cnf so I don't have to put it on the command-line at all:

user = root
password = XXXXXXXX


$ mysql -h "server-name" "database-name" < "filename.sql"

Re your comment:

I run batch-mode mysql commands like the above on the command line and in shell scripts all the time. It's hard to diagnose what's wrong with your shell script, because you haven't shared the exact script or any error output. I suggest you edit your original question above and provide examples of what goes wrong.

Also when I'm troubleshooting a shell script I use the -x flag so I can see how it's executing each command:

$ bash -x
Wednesday, June 9, 2021
answered 6 Months ago

Maybe I can help you with some example code:

public class SwingProgressBarExample extends JPanel {

  JProgressBar pbar;

  static final int MY_MINIMUM = 0;

  static final int MY_MAXIMUM = 100;

  public SwingProgressBarExample() {
    // initialize Progress Bar
    pbar = new JProgressBar();
    // add to JPanel

  public void updateBar(int newValue) {

  public static void main(String args[]) {

    final SwingProgressBarExample it = new SwingProgressBarExample();

    JFrame frame = new JFrame("Progress Bar Example");

    // run a loop to demonstrate raising
    for (int i = MY_MINIMUM; i <= MY_MAXIMUM; i++) {
      final int percent = i;
      try {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
      } catch (InterruptedException e) {
Thursday, July 22, 2021
answered 5 Months ago

I do not know, how to do this, but you may set variable in parent script and check for it in child:

if [[ -z "$_BACKGROUNDED" ]] ; then
    _BACKGROUNDED=1 exec "$0" "$@" & exit
# Put code here

Works both in bash and zsh.

Friday, August 13, 2021
Marko Kevac
answered 4 Months ago

It's a bit of a mess, but you can pretend the %m is a parameter and use parameter expansion to strip the zoltan from the host name:

PROMPT="...${${(%):-%m}#1} ..."

A little explanation. First, you create a "parameter" expansion that doesn't actually have a parameter name; it just uses the text you provide as the "value":


Next, add the % expansion flag so that any prompt escapes found in the value are processed.


Finally, next it in a final expansion that uses the # operator to remove a prefix from the string:


You can tame your prompt a bit by building up piece by piece (and use zsh's prompt escapes to handle the color changes, rather than embedding terminal control sequences explicitly).

PROMPT="%F{magenta}%n%f"  # Magenta user name
PROMPT+="%F{blue}${${(%):-%m}#zoltan-}%f" # Blue host name, minus zoltan
PROMPT+="%F{yellow}%1~ %f" # Yellow working directory
PROMPT+=" %# "
Sunday, September 26, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :