Differences between revisions 14 and 16 (spanning 2 versions)
Revision 14 as of 2020-04-04 01:37:26
Size: 1692
Editor: GreyCat
Comment: mark examples by requirements, make the POSIX one really POSIX, make them functions, and show how to call them
Revision 16 as of 2023-10-26 18:03:57
Size: 1593
Editor: emanuele6
Comment: check exit status of sleep, and avoid repetition of sleep command
Deletions are marked like this. Additions are marked like this.
Line 12: Line 12:
  sleep 0.1
  while true
; do
  while sleep 0.1; do
Line 15: Line 14:
    sleep 0.1
Line 33: Line 31:
  sleep 1
  while true
; do
  while sleep 1; do
Line 37: Line 34:
    sleep 1
Line 54: Line 50:
# Bash, with GNU sleep # Bash
Line 57: Line 53:
sn=${#sp}
Line 58: Line 55:
    printf "\b${sp:sc++:1}"
    ((sc==${#sp})) && sc=0
    sleep 0.1
    printf '\b%s' "${sp:sc++%sn:1}"
Line 64: Line 59:
    sleep 0.1

Can I do a spinner in Bash?

Sure!

# Bash, with GNU sleep
spin() {
  local i=0
  local sp='/-\|'
  local n=${#sp}
  printf ' '
  while sleep 0.1; do
    printf '\b%s' "${sp:i++%n:1}"
  done
}

Each time the loop iterates, it displays the next character in the sp string, wrapping around as it reaches the end. (i is the position of the current character to display and ${#sp} is the length of the sp string).

The \b string is replaced by a 'backspace' character. Alternatively, you could play with \r to go back to the beginning of the line.

To slow it down, the sleep command is included inside the loop (after the printf).

A POSIX equivalent would be:

# POSIX sh
spin() {
  sp='/-\|'
  printf ' '
  while sleep 1; do
    printf '\b%.1s' "$sp"
    sp=${sp#?}${sp%???}
  done
}

One way to use these spinners in a script is to run them as background processes, and kill them when you're done. For example,

# POSIX sh
spin & spinpid=$!
# long-running commands here
kill "$spinpid"

If you already have a loop which does a lot of work, you can write a function that "advances" the spinner one step at a time, and call it at the beginning of each iteration:

# Bash
sp='/-\|'
sc=0
sn=${#sp}
spin() {
    printf '\b%s' "${sp:sc++%sn:1}"
}
endspin() {
    printf '\r%s\n' "$*"
}

until work_done; do
   spin
   some_work ...
done
endspin

A similar technique can be used to build progress bars.


CategoryShell

BashFAQ/034 (last edited 2023-10-26 18:03:57 by emanuele6)