Put some colour in your life!


This one-liner is a pretty harmless and easy one, yet very very useful (as you might be seeing in one of my next entries).

for i ({000..255}){[ $((i%20)) = 0 ] && printf "\n";printf "\x1b[38;5;${i}m${i} "}

This will actually just work in the Z-Shell, there’s a slightly longer version you’d have to use if you don’t use the Z-Shel (you masochist! 😉 ).

i=0;while [ $i -le 255 ];do [ $((i%20)) = 0 ] && printf "\n";printf "\x1b[38;5;${i}m${i} ";i=$(($i+1));done

What does it do? Well I’ll show you a more comfortable version of it:

i=0;
while [ $i -le 255 ]
do
    if [ $((i%20)) = 0 ]
    then
        printf "\n"
    fi
    printf "\x1b[38;5;${i}m${i} "
    i=$(($i+1));
done

If you ever worked with colors in the shell, you might right away see it: It’s the CSI codes for colors defined by the ANSI escape sequences. So the script goes through all possible 255 color codes that are available on your systems shell and prints the corresponding number in its color. By making use of every shells lazy evaluation the if <cond>; then; <expr>; fi becomes the neat and compact <cond> && <expr>. Making use of Z-Shells highly sophisticated brace expansion our i=0; while <$i-cond>; do; <expr>; <$i-expr>; done becomes for i in <brace>; do <expr>; done. The brace expression is then evaluated to a list from 000, 001,… to 099 and then further on to 255, so it is as if all those numbers were actually written out as list (which is why we can use for in this case). Using the short form of for-loops in zsh it then becomes for i (<brace>){<expr>}. Neat and sweet, wouldn’t you say?

Now you could even make it more compact, if you don’t care about how it is broken into lines (that’s why there’s the modulo part printing an printf “\n” after each 20 numbers (thus with space 4 chars * 20 = 80 chars per line), you could remove it and end up with printing one huge line, or even one huge column:

for i ({000..255}){ printf "\x1b[38;5;${i}m${i}\n"}

Why bother? Well let me lose some short thoughts on that:

On my last one-liner illonis commented (loosely translated):

Sounds nice, but I’ll never need this.

My answer to this was: Yeah, that’s what I thought, and now here I am!. So if anybody else finds himself confronted with the same problem, here you go. Also, it often happens that I whip up something, sometimes it takes a while making it what it is – then I use it, and then I forget about it, because you actually never need it again. Except for this one time, in about one to five years later, when there’s suddenly again a problem, where knowing your shell script would have made it easy. These are the moments where you definitely can remember doing it, but can’t remember how you did it. Above script is just one of these examples. I once did took some time to whip up a script that nicely prints out these colors, and just some weeks ago in a nostalgic moment I found this old screenshot that was from a time, when I just got started with Gentoo and FVWM2 after having used SuSE and Debian for some years. I was just starting to play arround with colors, and because checking them all out by hand I invested a lot of time checking background and foreground colors, and finally this is what the output of the script looked like.

 

Add some color to your life

Add some color to your life

That screenshot is from 2005 and of course today I don’t remember anything of it. Sad I didn’t keep it. But there’s another sentiment in one-liners:

I remember a time when people bragged about their one-liners. The goal was to write something complex in as little characters as possible. This would make it fast to type it into the shell, and it was not only a demonstration of how powerful the shell was, but also a badge of wisdom the person possessed. Some of these became famous, e.g. the destructive

: () { : | :& };:

Don’t try this at home kids, it’s a fork bomb.

I remember myself looking at the one-liners people posted in German Linux and technic forums, trying to understand them, and often also trying to combine them to create something new. For a while it was even common to have a one-liner as your mail signature. Back in those days I had enormous respect for those guys and strived to one day become as a powerful Linux user as I deemed them to be.

I’m not trying to show off, with these one-liners or try to put myself on a same level with those idols of my youth. Looking back, I somehow feel that somewhere I took the wrong turn, ending up becoming all the wrong things – but again I digress.

I just want to keep  this sentiment alive somehow, and maybe rekindle it. And most of all, I want to strive again. That’s why whenever I write one, I want to preserve it. And of course I hope that if any of you is in the same or a similar situation you’ll find them handy 😉 A similar situation could be, that now after me finally doing it, you feel that you also have to change to tmux? 😉

But that’s a different Story for a different blog entry!

Advertisements

Please comment. I really enjoy your thoughts!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s