Published: 9. 1. 2012   Category: GNU/Linux

Matrix effect

I've tried to program famous „Matrix“ effect in bash and there is the result. Download here.

The code below is little bit obfuscated, so I'll try to understand it again, because I've created it few months ago. :-)

echo -e "\033[2J\033[?25l"R=`tput lines` C=`tput cols`;: $[R--] ; while true 
do ( e=echo\ -e s=sleep j=$[RANDOM%C] d=$[RANDOM%R];for in `eval $e {1..$R}`;
do c=`printf '\\\\0%o' $[RANDOM%57+33]` ### ###
$e "\033[$[i-1];${j}H\033[32m$c\033[$i;${j}H\033[37m"$c; $s 0.1;if [ $i -ge $d ]
then $e "\033[$[i-d];${j}";fi;done;for in `eval $e {$[i-d]..$R}`; #[mat!rix]
do echo -e "\033[$i;${j}";$s 0.1;done)& sleep 0.05;done #(c) 2011 -- [ BruXy ]

The first problem was to get terminal size inside the script, there are environment variables like $COLUMNS and $ROWS in some terminals, but these vars couldn't be set inside the script. I tested to grep output from stty, but this output is not same around operating systems, where I tested it (GNU/Linux, MacOSX). The correct solution is to use command tput lines and tput cols to get it.

The main effect is based on infinite loop that executes many processes (sub-shells) running on background and drawing vertical tears using some ANSI escape codes.