Shell scripts are great tools that help you automate tasks on Unix-like operating systems. You can even make them look better by changing text color, font, and background color. This guide will show you how to do that in your shell scripts.

What Are Shell Scripts?

Shell scripts are just a list of commands that the shell (a program that runs commands) executes one after another. They help you automate tasks. A shell script can be as simple as showing some text or as complex as running a bunch of commands in a specific order.

Most shell scripts are written in Bash (Bourne Again SHell), which is similar to older shells but has more features. Bash helps you write programs and also use the terminal more easily.

How to Change Text Color and Font

To change text color or font, we use something called ANSI escape sequences. These are special codes that tell the terminal to change how the text looks.

Here are some useful codes:

  • Text Reset: "e[0m"
  • Black: "e[0;30m"
  • Red: "e[0;31m"
  • Green: "e[0;32m"
  • Yellow: "e[0;33m"
  • Blue: "e[0;34m"
  • Purple: "e[0;35m"
  • Cyan: "e[0;36m"
  • White: "e[0;37m"

Background Colors:

For background colors, change the second number to 4:

  • Black Background: "e[40m"
  • Red Background: "e[41m"
  • Green Background: "e[42m"
  • Yellow Background: "e[43m"
  • Blue Background: "e[44m"
  • Purple Background: "e[45m"
  • Cyan Background: "e[46m"
  • White Background: "e[47m"

How to Print Text in Color

If you want to print text in green, here’s what the command looks like:


echo -e "e[0;32mHello, world!"

The -e flag tells echo to use the escape sequences. e[0;32m makes the text green, and “Hello, world!” is the text you want to show. To reset the color back to normal, use:


echo -e "e[0;32mHello, world!e[0m"

You can also use variables to make your script easier to read:


RED='e[0;31m'
NC='e[0m' # No Color
echo -e "I ${RED}love${NC} shell scripting"

This will print “I love shell scripting”, with the word “love” in red.

Using Colors in Shell Scripts Bash Shell
Changing Colors in Bash

Change Text Style and Background Color

To make text bold or underlined, use these codes:

  • Bold: "e[1m"
  • Underline: "e[4m"

For example:


echo -e "e[1mBold text"
echo -e "e[4mUnderlined text"

Remember to reset the style when done:


echo -e "e[1mBold texte[0m"
echo -e "e[4mUnderlined texte[0m"

You can also change the background color like this:


echo -e "e[44mBlue background texte[0m"

Using Colors in Shell Scripts Bash Shell
Changing Font in Bash

Conclusion

Now you know how to change text color, style, and background in your shell scripts using ANSI escape sequences. These tricks can make your scripts look more interesting and easier to use. But remember, always keep your code simple and efficient. Happy scripting!