Matrix(ish): a bash script with no practical application

[Tweet : ADN : nvALT]

Ok, just for (nerd) fun, here’s a Bash shell script that will output a Matrix-ish display in Terminal. I have no idea if it works outside of BSD on OS X, but it has a chance.

There’s a much better version of this effect written in C called “cmatrix” that you can install via Homebrew. That being said, I wanted to mess around with this in Bash anyway. Don’t ask me why.

Here’s what it looks like:

YouTube Video

Update: I modified the character set via the comment from Carl below, now it looks extra nifty:

YouTube Video

The script basically just takes an array of characters and an array of colors, randomly picking a combination of the two to stick in any character position. It uses cursor movements to randomly skip overwriting characters along the way, providing an illusion that characters are changing on the screen in random positions, rather than line by line.

The script takes two optional arguments. The first one is the spacing of the output. The higher the number, the more spaced out the characters are. The second argument is the scroll speed. Leaving that blank or setting it to 0 will result in no scrolling, things just change on the screen. The scrolling can add some interest to the effect, so experiment with it. The arguments must be used in order, if there’s only one argument it’s assumed to be the spacing. The defaults (no arguments) are 100 (spacing) and 0 (no scroll).

Here’s the script, just grab the raw version and save it in your path as matrixish and run chmod a+x on it to make it executable. Then try running matrixish, matrixish 50 2, etc. to get a feel for it. Hit Ctrl-C to stop the script (screen will be cleaned up automatically).

Colors can be customized in the script. Find the bit around line 24 that looks like:

colors=($green $brightgreen)

There’s a list of colors above that line that you can use in the array. Use as many or as few as you like (at least one) and those colors will be randomly picked from when display characters.

Here’s the script, have fun (raw version).

Ryan Irelan has produced a series of shell trick videos based on BrettTerpstra.com posts. Readers can get 10% off using the coupon code TERPSTRA.