Table of Contents#
- The Core Concept: How It Works
- Prerequisites
- Step 1: Creating Your ASCII Art Library
- Step 2: The Script to Select Random Art
- Step 3: Making It Run Automatically
- Taking It Further: Advanced Ideas
- Troubleshooting Common Issues
- Conclusion
- References
The Core Concept: How It Works#
The process is straightforward:
- Store Art: You keep a collection of ASCII art files (e.g.,
cat.txt,server.txt,quote.txt) in a dedicated directory. - Select Randomly: A script uses a command to pick one file from this directory at random.
- Display Art: The script then prints the contents of the chosen file to the terminal.
- Automate: This script is set to run automatically each time you open a new terminal shell by adding it to your shell's configuration file (like
~/.bashrcor~/.zshrc).
Prerequisites#
- A Linux distribution (this guide works on Ubuntu, Fedora, Arch, etc.).
- A basic understanding of the terminal and a text editor (like
nano,vim, orgedit). - A shell like Bash or Zsh.
Step 1: Creating Your ASCII Art Library#
First, create a directory to store your ASCII art. A good place is within your home directory.
mkdir ~/ascii_artNow, let's populate it. You can create your own art or find it online (websites like ASCII Archive are great resources). Create a few text files.
Example: ~/ascii_art/penguin.txt
\
\
.--.
|o_o |
|:_/ |
// \ \
(| | )
/'\_ _/`\
\___)=(___/
Example: ~/ascii_art/server.txt
.----.
|====|
|----|
|----|
|----|
|====|
'----'
[Server]
Example: ~/ascii_art/quote_hello.txt
+---------------------------------+
| "Hello, World!" isn't just for |
| programmers. It's for anyone |
| starting a new journey. |
+---------------------------------+
Step 2: The Script to Select Random Art#
We'll write a simple Bash script that does the random selection and display.
-
Create the script file, for example,
show_ascii_art.sh, in your home directory or a~/bin/directory.nano ~/bin/show_ascii_art.sh -
Copy and paste the following script into the file:
#!/bin/bash # Directory where your ASCII art is stored ART_DIR="$HOME/ascii_art" # Check if the directory exists if [ -d "$ART_DIR" ]; then # Find all .txt files in the directory and choose one at random # Using `shuf` for randomness (more modern than `$RANDOM`) RANDOM_FILE=$(find "$ART_DIR" -name "*.txt" | shuf -n 1) # Check if a file was found if [ -n "$RANDOM_FILE" ]; then # Display the file with some spacing echo "" cat "$RANDOM_FILE" echo "" else echo "No ASCII art files found in $ART_DIR." fi else echo "ASCII art directory $ART_DIR does not exist." fi -
Save the file (in
nano, pressCtrl+X, thenY, thenEnter). -
Make the script executable:
chmod +x ~/bin/show_ascii_art.sh
Explanation of the Script:
#!/bin/bash: This is the shebang, telling the system to use Bash to execute the script.ART_DIR="$HOME/ascii_art": Defines the path to your art directory.find "$ART_DIR" -name "*.txt": Finds all files ending with.txtin the directory.shuf -n 1: Theshufcommand shuffles its input lines, and-n 1picks the first one from the shuffle (i.e., a random line/file).cat "$RANDOM_FILE": Prints the contents of the randomly selected file.
You can test the script immediately by running:
~/bin/show_ascii_art.shStep 3: Making It Run Automatically#
To have the art display every time you open a terminal, you need to source the script from your shell's configuration file.
-
Identify your shell configuration file.
- For Bash:
~/.bashrc - For Zsh:
~/.zshrc
- For Bash:
-
Open the configuration file with a text editor.
# For Bash users nano ~/.bashrc # For Zsh users nano ~/.zshrc -
Add the following line to the very end of the file:
# Display random ASCII art /home/your_username/bin/show_ascii_art.sh(Make sure to replace
your_usernamewith your actual username.) -
Save and close the file.
-
For the changes to take effect in your current terminal, source the file:
# If you edited ~/.bashrc source ~/.bashrc # If you edited ~/.zshrc source ~/.zshrc
Now, close your terminal and open a new one. You should be greeted by a random piece of ASCII art!
Taking It Further: Advanced Ideas#
Using cowsay and fortune#
A classic combination for random terminal fun is cowsay (a program that generates an ASCII picture of a cow with a message) and fortune (a program that displays a random epigram or quote).
-
Install them (on Ubuntu/Debian):
sudo apt install cowsay fortune-mod -
You can replace your custom script call in
~/.bashrcwith this dynamic duo:# In your ~/.bashrc or ~/.zshrc fortune | cowsayThis will pipe a random fortune into the cowsay program. There are also other "cows" available (e.g.,
cowsay -f dragon).
Fetching Art from the Internet#
You can modify the script to fetch art from online APIs. For example, here's a simple script that uses curl to get a random quote from an API and display it with cowsay:
#!/bin/bash
curl -s https://api.quotable.io/random | jq -r '"\(.content)\n -- \(.author)"' | cowsay(This requires jq to be installed: sudo apt install jq)
Conditional Display#
To prevent the art from showing on every single prompt (which can be annoying), you can make it only show when you open a new terminal window (a login shell). Modify the call in your ~/.bashrc:
# Only run if it's a login shell (like a new terminal window/tab)
if shopt -q login_shell; then
/home/your_username/bin/show_ascii_art.sh
fiTroubleshooting Common Issues#
- "Permission denied" when running the script: You forgot to make it executable. Run
chmod +x ~/bin/show_ascii_art.shagain. - "No such file or directory" for the script: The path in your
~/.bashrcis incorrect. Double-check the path to the script. - Art doesn't show up in new terminals: You forgot to
source ~/.bashrcafter editing it, or you need to completely close and reopen your terminal application. - Script works manually but not from
~/.bashrc: Ensure the~/bindirectory is in yourPATH. You can addexport PATH="$HOME/bin:$PATH"to your~/.bashrcif it's not.
Conclusion#
You've now transformed your terminal from a purely functional interface into a dynamic and personalized workspace. By creating a simple script and leveraging your shell's startup file, you can add a touch of randomness and fun to your daily routine. The basic principle of storing files, selecting one randomly, and displaying it is powerful and can be extended in countless ways. Happy hacking!
References#
- Bash Reference Manual
- ASCII Art Archive - A great source for pre-made ASCII art.
- GNU
coreutilsmanual:shufinvocation - Documentation for theshufcommand. - Bash Startup Files - Explains the difference between
.bashrc,.bash_profile, etc.