# Geek # # Linux # # FreeBSD # # OpenBSD # # *NIX #

Colors in the terminal

I could have edited the earlier post from today to add it in there, but wanted to test a small change in wygibtwys... hence new post...

Bash scripts probably represent about 90% of all "coding" I do. As such I really enjoy using colors to make the output of my scripts easier to read.
For years and years, I have relied on the following string to initialise colors for my scripts:

def=$(tput sgr0);bol=$(tput bold);red=$(tput setaf 1;tput bold);gre=$(tput setaf 2;tput bold);yel=$(tput setaf 3;tput bold);blu=$(tput setaf 4;tput bold);mag=$(tput setaf 5;tput bold);cya=$(tput setaf 6;tput bold)

which allows me to

echo "$red Hello$gre there $def!!!"


printf "%b Hello%b there %b!!!" "$red" "$gre" "$def"

Just to recently arrive at the stern conclusion that this was not working in my terminal in FreeBSD/OpenBSD.
A little research pointed me in the direction of:

def='\033[0m'; red='\033[1;31m'; gre='\033[1;32m'; yel='\033[1;33m'; blu='\033[1;34m'; mag='\033[1;35m'; cya='\033[1;36m'; bol='\033[1'

Which implements exactly the same functionality, but works perfectly on Linux/FreeBSD/OpenBSD
I really appreciate the this allows me to port the whole color section of my old scripts by only changing a single line \o/
hope it helps

view_list Categories