Skip to content

Instantly share code, notes, and snippets.

@sqwxl
Last active September 7, 2022 13:39
Show Gist options
  • Save sqwxl/a24683a9d5a13356db8b2801ecd94156 to your computer and use it in GitHub Desktop.
Save sqwxl/a24683a9d5a13356db8b2801ecd94156 to your computer and use it in GitHub Desktop.
Simple bash spinner
#!/usr/bin/env bash
# source this file in your scripts and use as so:
# <command> & spin "Text to display while command is running"
function spin() {
local tag="$1"
local pid=$! # get the pid of the last background process
local sp="⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"
local i=1
while kill -0 $pid &>/dev/null; do
printf "\r\e[0;32m${sp:i++%${#sp}:1}\e[0m $tag"
sleep .1
done
# wait for the loop to finish
if wait $pid &>/dev/null; then
printf "\e[0;32m Done!\e[0m\n\n"
else
printf "\e[0;31m Failed!\e[0m\n\n"
exit 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment