Make Bash Scripts Interactive: Animations, GUI Notifications, and Parallel Execution
This guide shows how to enhance Bash scripts with text‑based animations, local GUI notifications, parallel task execution, simple GUI dialogs, and styled terminal output using built‑in commands and utilities like printf, notify‑send, zenity, and tput.
Display Animation for Long‑Running Tasks
When a Bash script runs a time‑consuming command, a static echo message can be unfriendly; instead you can create a looping ASCII animation using printf with a carriage‑return (\r) to rewrite the current line.
#!/bin/bash
while true; do
# Frame #1
printf "\r< Loading..."
sleep 0.5
# Frame #2
printf "\r> Loading..."
sleep 0.5
doneThe script produces an endless two‑frame animation in the terminal.
For a spinner that runs until a background job finishes, use a frame list and kill -0 to poll the job:
#!/bin/bash
sleep 5 &
pid=$!
frames="/ | \\ -"
while kill -0 $pid 2>&1 > /dev/null; do
for frame in $frames; do
printf "\r${frame} Loading..."
sleep 0.5
done
done
printf "
"Show Local GUI Notifications from Bash
On GNU/Linux you can call notify-send to pop up a desktop notification:
#!/bin/bash
sleep 10
notify-send "notify.sh" "Task #1 completed"macOS users can invoke AppleScript via osascript:
#!/bin/bash
sleep 10
osascript -e "display notification \"Task #1 completed\" with title \"notify.sh\""Multiprocessing in Bash Scripts
Instead of running commands sequentially, define functions and start them in the background with &, then use wait to pause until all finish:
#!/bin/bash
function task1() {
echo "Running task1..."
sleep 5
}
function task2() {
echo "Running task2..."
sleep 5
}
task1 &
task2 &
wait
echo "All tasks completed"This reduces total execution time from ~10 seconds to ~5 seconds.
Display GUI Components from Bash
Utilities such as zenity (GNOME) or kdialog (KDE) let Bash scripts open file‑selection dialogs, message boxes, etc. Example:
zenity --file-selectionOn macOS the same can be achieved with AppleScript:
osascript -e "POSIX path of (choose file)"Modernize Terminal Output with Text Styles
Combine tput with echo to apply colors, bold, underline, and italic styles, making messages clearer:
#!/bin/bash
bold=$(tput bold)
underline=$(tput smul)
italic=$(tput sitm)
info=$(tput setaf 2)
error=$(tput setaf 160)
warn=$(tput setaf 214)
reset=$(tput sgr0)
echo "${info}INFO${reset}: This is an ${bold}info${reset} message"
echo "${error}ERROR${reset}: This is an ${underline}error${reset} message"
echo "${warn}WARN${reset}: This is a ${italic}warning${reset} message"These techniques let developers create more user‑friendly, interactive Bash scripts without needing external GUI frameworks.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
