dotlinux blog

How to Randomly Display ASCII Art on Your Linux Terminal

The Linux terminal is a powerful tool, often seen as a stark, text-only environment. But it doesn't have to be! ASCII art—images created from text characters—can add a splash of personality, humor, or information to your command-line experience. Imagine opening a new terminal window to be greeted by a random, witty quote, a system stats display, or a cute animal. This not only makes your workflow more enjoyable but can also be a practical way to display information.

In this guide, we'll walk through the steps to set up a system that displays a random piece of ASCII art every time you start a new terminal session. We'll cover everything from creating a library of ASCII art to automating the process.

2026-05

Table of Contents#

  1. The Core Concept: How It Works
  2. Prerequisites
  3. Step 1: Creating Your ASCII Art Library
  4. Step 2: The Script to Select Random Art
  5. Step 3: Making It Run Automatically
  6. Taking It Further: Advanced Ideas
  7. Troubleshooting Common Issues
  8. Conclusion
  9. References

The Core Concept: How It Works#

The process is straightforward:

  1. Store Art: You keep a collection of ASCII art files (e.g., cat.txt, server.txt, quote.txt) in a dedicated directory.
  2. Select Randomly: A script uses a command to pick one file from this directory at random.
  3. Display Art: The script then prints the contents of the chosen file to the terminal.
  4. 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 ~/.bashrc or ~/.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, or gedit).
  • 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_art

Now, 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.

  1. Create the script file, for example, show_ascii_art.sh, in your home directory or a ~/bin/ directory.

    nano ~/bin/show_ascii_art.sh
  2. 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
  3. Save the file (in nano, press Ctrl+X, then Y, then Enter).

  4. 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 .txt in the directory.
  • shuf -n 1: The shuf command shuffles its input lines, and -n 1 picks 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.sh

Step 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.

  1. Identify your shell configuration file.

    • For Bash: ~/.bashrc
    • For Zsh: ~/.zshrc
  2. Open the configuration file with a text editor.

    # For Bash users
    nano ~/.bashrc
     
    # For Zsh users
    nano ~/.zshrc
  3. 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_username with your actual username.)

  4. Save and close the file.

  5. 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).

  1. Install them (on Ubuntu/Debian):

    sudo apt install cowsay fortune-mod
  2. You can replace your custom script call in ~/.bashrc with this dynamic duo:

    # In your ~/.bashrc or ~/.zshrc
    fortune | cowsay

    This 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
fi

Troubleshooting Common Issues#

  • "Permission denied" when running the script: You forgot to make it executable. Run chmod +x ~/bin/show_ascii_art.sh again.
  • "No such file or directory" for the script: The path in your ~/.bashrc is incorrect. Double-check the path to the script.
  • Art doesn't show up in new terminals: You forgot to source ~/.bashrc after editing it, or you need to completely close and reopen your terminal application.
  • Script works manually but not from ~/.bashrc: Ensure the ~/bin directory is in your PATH. You can add export PATH="$HOME/bin:$PATH" to your ~/.bashrc if 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#

  1. Bash Reference Manual
  2. ASCII Art Archive - A great source for pre-made ASCII art.
  3. GNU coreutils manual: shuf invocation - Documentation for the shuf command.
  4. Bash Startup Files - Explains the difference between .bashrc, .bash_profile, etc.