5 Modern Bash Scripting Techniques That Only A Few Programmers Know
Displaying Animations to Indicate Long-Running Tasks
#!/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 "\n"
Displaying Native GUI Notifications from Bash
GNU/Linux-based operating systems
#!/bin/bash
sleep 10
notify-send "notify.sh" "Task #1 was completed successfully"
macOS users can execute the AppleScript interpreter from Bash to display native notifications as follows:
#!/bin/bash
sleep 10
osascript -e "display notification \"Task #1 was completed successfully\" with title \"notify.sh\""
Multiprocessing in Bash Scripts
#!/bin/bash
function task1() {
echo "Running task1..."
sleep 5
}
function task2() {
echo "Running task2..."
sleep 5
}
task1 &
task2 &
wait
echo "All done!"