A while ago, I came across a Matrix-inspired Bash script and thought, this would be a cool project to build from scratch! The result? A lightweight, terminal-based Matrix digital rain effect that you can run instantly on any Linux system.
Whether you’re a Bash beginner looking for a fun scripting challenge or an experienced user interested in terminal animations, this guide will walk you through every step.
What We’re Building (Live Preview)
Before jumping into the code, here’s what the final effect looks like:
If you want to understand how it works and tweak it yourself, keep reading.
How the Matrix Effect Works in the Terminal
At its core, the Matrix rain effect is a combination of:
✅ Randomly generated characters (Katakana, alphanumeric, or symbols)
✅ A vertical falling effect using Bash’s tput
command to control the cursor
✅ Color manipulation to add a cyberpunk aesthetic
We’ll be using Bash’s ANSI escape codes to control text placement, color, and movement.
Step 1: Setting Up the Script
Create a new Bash script and open it in your favorite editor:
nano matrix.sh
At the top of the file, add:
#!/bin/bash
clear # Clears the terminal at the start
tput civis # Hides the cursor for a cleaner effect
Step 2: Generate Falling Characters
Let’s define the characters that will fall down the screen:
chars=("ア" "イ" "ウ" "エ" "オ" "カ" "キ" "ク" "ケ" "コ" "サ" "シ" "ス" "セ" "ソ" "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z")
To get the terminal dimensions dynamically, use:
rows=$(tput lines)
cols=$(tput cols)
🔹 Why this matters: The script should adapt to different screen sizes dynamically.
Step 3: Make It Rain
Now, let’s animate the falling characters using an infinite loop:
while true; do
col=$((RANDOM % cols)) # Pick a random column
char=${chars[$((RANDOM % ${#chars[@]}))]} # Pick a random character
tput cup 0 $col # Move cursor to the top of the column
echo -ne "\033[32m$char\033[0m" # Print the character in green
for ((i=1; i<rows; i++)); do
tput cup $i $col
echo -ne "\033[32m$char\033[0m"
sleep 0.1
done
done
🔹 Breakdown of what’s happening:
- The script randomly selects a column and places a random character at the top.
- It moves down the screen, refreshing each position with the next character.
-
tput cup
is used to position the cursor dynamically.
Step 4: Restore the Cursor on Exit
When you interrupt the script (Ctrl + C
), the cursor might remain hidden. Let’s ensure it restores properly:
trap "tput cnorm; exit" SIGINT
🔹 Why this matters: Prevents terminal glitches when exiting the script.
Step 5: Running the Script
Save the script (Ctrl + X, then Y, then Enter
) and make it executable:
chmod +x matrix.sh
./matrix.sh
🎉 Your terminal should now be streaming Matrix-style digital rain!
Tweaks & Customization
Once you’ve got the effect running, try modifying the script to customize the rain effect:
1️⃣ Change the Color (Replace \033[32m
with other ANSI color codes):
echo -ne "\033[36m$char\033[0m" # Cyan rain
2️⃣ Speed Up or Slow Down the Effect (Modify sleep 0.1
):
sleep 0.05 # Faster rain
sleep 0.2 # Slower rain
3️⃣ Add More Variability (Introduce randomness in delay time):
sleep $(awk -v min=0.05 -v max=0.2 'BEGIN{srand(); print min+rand()*(max-min)}')
Next Steps: Expanding the Project
If you enjoyed this project, you might like:
✅ Exploring Matrix scripts like for more optimized versions
✅ Adding interactivity (like pausing or changing colors dynamically)
✅ Converting this into a system screensaver
Let me know in the comments what improvements you’d like to see!
Final Thoughts
Building the Matrix rain effect in Bash is a fun way to explore terminal animations while improving your scripting skills. This project introduced you to:
✅ Cursor manipulation with tput
✅ Generating random characters dynamically
✅ Controlling terminal colors and output positioning
✅ Creating looping animations in Bash
Want a Structured Bash Reference?
If you need a Beginner Friendly Bash guide with easy to follow tips and explanations, check out my Bash Cheat Sheet:
👉 Download the Bash Cheat Sheet for just $3.99
Discussion: What’s Your Favorite Bash Animation Trick?
Drop a comment below if you have ideas for improving this script or other cool terminal animations you’ve built!
Top comments (0)