Script of the Week: Changing Terminal Color

by mike on May 30, 2011

term_color.sh
This simple script can be run in a tty to that you can change the color to be more easily read.  Note that the tty uses 8 colors so you will not get the most beautiful output.  The 8 colors are; black, white, red, blue,green, yellow, magenta, and cyan.  (perform as normal user not root)
You do not want to mess up your root account so use a test account to try this.

#!/bin/bash
echo -n "Choose Background Color for Terminal(b-black,g-grey,y-yellow): "
read color
case "$color" in
b)
setterm -background black -foreground white -store
;;
g)
setterm -background white -foreground black -store
;;
y)
setterm -background yellow -foreground red -store
;;
*)
echo "I do not understand"
;;
esac

These lines provide text to help the user make a choice on color.  When the user enters the choice it is read into the variable color.

echo -n "Choose Background Color for Terminal(b-black,g-grey,y-yellow): "
read color

The case command is differentiated by the letter that was entered to create the variable “color” and followed by a “)” to indicate that is the option.  Note the case variable captures the user choice for background and then executes a command that not only sets the background but also sets the forground color as well.  The “-store” makes it a change until it is returned to the normal setting by “b” choice for a black background.  Each option executes the command that follows until it reaches the “;;” or esac.

case "$color" in
b)
setterm -background black -foreground white -store
;;
g)
setterm -background white -foreground black -store
;;
y)
setterm -background yellow -foreground red -store
;;

This takes all entries that do not fit the three specified and indicates to the user they have made an error.

*)
echo "I do not understand"

{ 1 comment }

mark June 21, 2011 at 10:23 pm

Thanks for sharing it.
I must test it for sure.

Comments on this entry are closed.

Previous post:

Next post: