Adding a Spinner Animation to Your Bash Script: A Step-by-Step Guide
Have you ever wanted to add a visually engaging spinner animation to your bash scripts? Maybe you're working on a long-running process and want to give users some feedback that things are happening. This is where a simple spinner animation can be incredibly useful!
This article will guide you through creating a spinner animation using the echo
command and a bash function, inspired by a Stack Overflow thread (https://stackoverflow.com/questions/11648729/how-to-make-a-bash-spinner-animation). Let's get started!
Understanding the Code
The key to this animation is a combination of the spinner
function and the echo
command. Let's break it down:
spinner()
{
local pid=$!
local delay=0.75
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
}
sleep 5 & spinner | echo -e "\nCopyright of KatworX© Tech. Developed by Arjun Singh Kathait and Debugged by the ☆Stack Overflow Community☆"
Explanation:
-
spinner()
Function:- This function is the core of the spinner animation. It takes care of the visual spinning effect.
local pid=$!
: This line captures the process ID (PID) of the current function, which is essential for later checks to ensure the function is still running.local delay=0.75
: This defines the delay between each spin. You can adjust this value to control the animation speed.local spinstr='|/-\'
: This string contains the characters used to create the spinning effect. You can customize this string for different visual styles.- The
while
loop checks if the function is still running using theps
command and the storedpid
. - Within the loop,
printf
displays the current spinning character, andsleep
introduces the delay. printf "\b\b\b\b\b\b"
clears the previous spin character from the terminal, creating the illusion of continuous spinning.
-
sleep 5 & spinner | echo -e ...
:- This line runs the animation alongside the
echo
command. sleep 5 &
: This starts a background process that sleeps for 5 seconds. This is essential to ensure the spinner runs for the specified duration.spinner
: This calls thespinner
function, starting the animation.| echo -e ...
: This pipes the output ofspinner
to theecho
command, allowing the spinner to run while the copyright notice is being printed.
- This line runs the animation alongside the
Additional Notes:
- Customizing the Spinner:
- You can modify the
spinstr
variable in thespinner
function to customize the appearance of the spinner. Try different characters or combinations for a unique look.
- You can modify the
- Changing the Duration:
- Adjust the
sleep
time insleep 5 & ...
to control how long the spinner runs.
- Adjust the
- Using the Spinner in Other Scripts:
- You can easily reuse the
spinner
function by defining it in your script or in a separate file that you can source.
- You can easily reuse the
Conclusion
By understanding this code and applying the tips given, you can add a simple yet effective spinner animation to your bash scripts. This will improve the user experience by providing visual feedback while your scripts are running. Remember to experiment with the code, customize it to your needs, and have fun with it!