Using colours in the Terminal couldn't be easier...
We are going to be echo-ing out strings using the echo command and we are going to completely tailor the foreground and background colour that we use! So this is how you do it...
This will print out the word "Red" in red with no background specified...
echo -e "\e[31m Red \e[0m"
Theres a lot to read there so lets break it down bit by bit...
echo -e:
This is the echo command, that simply prints out to the terminal the string that you give it, our string being "\e[31m Red \e[0m"
The -e flag attached is very special and enables echo to interpret certain backslashed characters including the 'e' which creates an escape character (this is important later).
\e
The \e's that you see are those escape characters we spoke about before, these are special and allow other combinations of characters that follow them to be interpreted in a special way to, in our case it will enable our weird colour codes to actually be colours...
[31m
This is essentially our colour code. Codes always start with a square bracket and end with an 'm'. The number is changeable and in this case 31 is red. 34 is blue, 32 is green and so on...
A really well put together tutorial and explanation can be found here, along with an entire list of the colour codes...
FLOZz' MISC » bash:tip_colors_and_formatting
RED
Well this is easy, this is our string
\e[0m
Hopefully the \e makes sense and the square bracket with m combination, but the 0 thats abit weird, in this case 0 can be thought of as just another colour code and infact its the RESET code that basically lets your terminal colour reset to its default at the end of the statement. Its just a nice way to clean up after yourself and make sure there is no funny business in the terminal. I like to put resets after all my echos with colour just to make sure im not confusing the poor Terminal.
Cool, so now you know how to do foreground colour changes, what about background, well as you hopefully saw in the link before...
FLOZz' MISC » bash:tip_colors_and_formatting
...backgrounds are easy too and in-fact you can combine mixtures of foreground and background colours like this:
echo -e "\e[31;44m Red And Blue \e[0m"
This would print the string "Red And Blue" in Red foreground and Blue background... Ewwww gross I know! Im not a designer!
As you can see most of the pieces of the puzzle are the same, its just inside our square bracket and 'm' that things are different. Now we specify 2 colour codes with a ';' in the middle.
And thats just it, using the link from before you can pick your combination of foreground and background colour and put both of them inside the square bracket and 'm' and put a semi-colon between them and bobs your uncle!
Here are some more examples...
echo -e "\e[32;45m Green And Magenta \e[0m"
Green FG & Magenta BG
echo -e "\e[33;41m Yellow And Red \e[0m"
Yellow FG & Red BG
Hopefully you get the picture...
If you prefer a video format to explain all that then you are in luck!
Below is a video I created on the subject for a new YT channel that I am trying to get up and running, please let me know if it is any good or if you have any feedback at all ...
There is a bunch more stuff you can do with this like flashing text, bold or pale text and even inverted colours, however Ill leave you to discover all that on your own using (AGAIN) this awesome link...
FLOZz' MISC » bash:tip_colors_and_formatting
Enjoy mixing and matching all the crazy colour combinations you can think of... In fact feel free to post below some of the more gross combinations, they are always the best ;)
Thanks!
Top comments (0)