How to create a spinner for your Bash scripts. If you have a long running process and don't want to try to tell the user approximately how much of that process is left to run, showing them a spinner is a great alternative.

#!/usr/bin/env bash

#
# From Show a Bash spinner for long running tasks
# http://fitnr.com/showing-a-bash-spinner.html
spinner() {
    local pid=$1
    local delay=0.20
    # shellcheck disable=SC1003
    local spinstr='|/-\'

    while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
        local temp=${spinstr#?}
        printf " [%c]  " "$spinstr"
        local spinstr=$temp${spinstr%"$temp"}
        sleep $delay
        printf "\b\b\b\b\b\b"
    done

    printf "    \b\b\b\b"
}

#
# Example usage
#

do_fake_work() {
    echo -n "Doing work... "
    sleep 3
}

(do_fake_work) &
spinner $! && echo "Done"