Creating a Bash Script to Display an Animated Party Parrot in the Terminal
This tutorial will guide you through creating a small Bash script that
uses curl
to display an animated party parrot in the terminal. This fun script
utilizes parrot.live
, a neat little service found
at parrot.live GitHub repository.
Prerequisites
Ensure you have curl
installed on your Linux system. You can check this by
typing curl --version
in your terminal. If it's not installed, you can usually
install it using your distribution's package manager, for
example, sudo apt install curl
on Debian-based systems.
Step 1: Create the Bash Script
Open a Text Editor: Open your favorite text editor and create a new file.
Start with the Shebang: At the top of the file, add the shebang line for Bash:
#!/bin/bash
Add the Curl Command: Below the shebang, add the following line:
curl parrot.live
Save the Script: Save the file as
party_parrot.sh
or another name of your choosing.
Step 2: Add a Help Flag
It's good practice to include a help flag in scripts to provide users with information on how to use the script.
Handle Command-Line Arguments: Modify your script to handle a
-h
or--help
flag. Replace the curl line with the following script:if [[ "$1" == "-h" || "$1" == "--help" ]]; then
echo "Usage: $0 [-h | --help]"
echo "Displays an animated party parrot in the terminal."
exit 0
fi
curl parrot.live
This script checks if the first argument is -h
or --help
. If so, it prints
usage information and exits. Otherwise, it proceeds to run curl parrot.live
.
Let's break down the script line by line:
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
if
: Starts a conditional statement in Bash.[[ ... ]]
: The double brackets denote a test condition in Bash."$1"
: This represents the first argument passed to the script. In Bash scripts,$1
is the first positional parameter.== "-h" || "$1" == "--help"
: This checks if$1
is equal to-h
or--help
. The||
is a logical OR operator, meaning the condition is true if either$1
equals-h
or$1
equals--help
.then
: This indicates the start of the block of code that will execute if the condition is true.
echo "Usage: $0 [-h | --help]"
echo
: This command is used for displaying a line of text/string that is passed as an argument."Usage: $0 [-h | --help]"
: This is the text being echoed.$0
is a special variable in Bash that contains the name of the script. So, this line is essentially providing the user with information on how to use the script.
echo "Displays an animated party parrot in the terminal."
- This is another
echo
command, providing a description of what the script does.
- This is another
exit 0
exit
: This command terminates the script.0
: This is the exit status that the script returns to the shell. A0
status typically signifies that the script has completed successfully. It's a convention in Unix and Linux systems where '0' signifies success, and non-zero values signify an error or false status.
fi
- This marks the end of the
if
conditional block.
- This marks the end of the
Step 3: Make the Script Executable
- Change File Permissions: In the terminal, navigate to the directory
containing your script and run:
chmod +x party_parrot.sh
Step 4: Running the Script
Execute the Script: You can now run the script by typing:
./party_parrot.sh
To see the help message, run:
./party_parrot.sh --help
Conclusion
Congratulations! You've created a fun and simple Bash script that displays an
animated party parrot in the terminal using curl
. This script is a basic
example of how you can use Bash scripting to automate tasks and add a little fun
to your terminal sessions. The skills you've learned here can be applied to more
complex scripting tasks in the future. Enjoy your party parrot! 🎉🦜
What Can You Do Next 🙏😊
If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.