Adding Colors to the Terminal Prompt
The terminal, despite its seemingly monochrome appearance, possesses the capability to display a rainbow of colors, enhancing readability and adding a touch of personalization. Whether it's distinguishing between different kinds of information or just adding a bit of flair, using colors can transform the terminal experience. In this article, we'll explore how colors can be introduced into the terminal prompt, and the history and mechanics behind it.
The Genesis: ANSI Escape Codes
Historically, computers and terminals were separate entities. A terminal was a device with a keyboard and screen that connected to a computer, possibly remotely, to access its computing capabilities. As technology evolved and user demands increased, there arose a need to enrich the text-based interaction between terminals and computers. This was especially crucial when the only interface was text.
To address this, ANSI (American National Standards Institute) introduced a set
of standardized codes known as ANSI escape codes. These codes allow terminals to
control cursors, format text, and change text colors, among other things. They
are termed "escape codes" because they start with the ESC
character (often
represented as \033
or \e
in many programming languages), signaling the
terminal to interpret the following characters as a command, not as typical
text.
Changing Text Colors with Escape Sequences
ANSI escape codes for changing text colors generally follow this
format: \e[<code>m
. Here, <code>
represents the number corresponding to a
particular color or formatting.
For example, to set the text color to red, you would use:
echo -e "\e[31mThis is red text\e[0m"
Here, 31
is the code for red text, and \e[0m
is used to reset the formatting
to ensure subsequent text is not affected.
Table of Text Colors
Here's a list of some commonly used ANSI codes for text colors:
Code | Color |
---|---|
30 | Black |
31 | Red |
32 | Green |
33 | Yellow |
34 | Blue |
35 | Magenta |
36 | Cyan |
37 | White |
Customizing the PS1 Prompt to Display Cyan Text
One of the most interactive ways to apply color customization in the terminal is
by changing the appearance of the prompt. The PS1
environment variable
determines how the prompt looks, and by embedding the right ANSI escape
sequences into it, we can achieve a vibrant, colored prompt.
Changing the Prompt Text to Cyan
Cyan, a soothing color midway between blue and green, can add a touch of elegance to your terminal prompt. Here's how to set your prompt's text color to cyan:
Backup the Current PS1: It's always a good practice to keep a backup of your current
PS1
before making changes. You can do this with:ps1_backup=$PS1
Modify PS1: Now, insert the ANSI escape sequence for cyan text, which is
\e[36m
, into thePS1
variable. Remember to reset the formatting at the end using\e[0m
:PS1='\e[36m\u@\h:\w\$ \e[0m'
The above command changes the prompt to display the username (
\u
), the hostname (\h
), and the current working directory (\w
) in cyan. The dollar sign ($
) is a standard end-of-prompt symbol.Apply and Check: Once you've set the
PS1
, your prompt should immediately reflect the change. Open a new terminal window or tab to see the cyan-colored prompt in action.
Restoring the Original PS1
If you wish to revert back to the original prompt after experimenting, you can simply use the backup we created:
PS1=$ps1_backup
And there you have it — a cyan-colored prompt to brighten up your terminal interactions!
Setting Background Colors
In addition to text colors, background colors can be changed using a similar method. The background color enhances the distinction between different elements, providing a greater degree of emphasis.
For instance, to set the background color to blue, you would use:
echo -e "\e[44mThis has a blue background\e[0m"
Here, 44
represents a blue background.
Table of Background Colors
The following table outlines the ANSI escape codes for various background colors:
Code | Color |
---|---|
40 | Black |
41 | Red |
42 | Green |
43 | Yellow |
44 | Blue |
45 | Magenta |
46 | Cyan |
47 | White |
Customizing the PS1 Prompt with a Brown Background
Adding a background color to your prompt can make it stand out against other terminal text, providing a clear visual boundary for your command input. In this section, we'll venture into giving our prompt a brown background, a warm shade that lends a touch of earthiness to your terminal experience.
Setting the Prompt Background to Brown
While there isn't a direct ANSI escape code for brown as a background color, it's conventionally represented using the yellow color code. By combining it with appropriate text color, it can be perceived as brown. Here's how to achieve this:
Backup the Current PS1: As always, before altering the
PS1
, it's wise to store its current state so you can revert if desired:ps1_backup=$PS1
Modify PS1: To set the background color to "brown", we'll use the ANSI escape sequence for a yellow background, which is
\e[43m
. It's a good idea to choose a text color that contrasts well with brown, for instance, black (\e[30m
):PS1='\e[30m\e[43m\u@\h:\w\$ \e[0m'
This command configures the prompt to display the username (
\u
), the hostname (\h
), and the current working directory (\w
) with black text on a brownish background. The dollar sign ($
) signifies the end of the prompt.Apply and Observe: Post setting the
PS1
, your new background color should immediately come to life. Commence a new terminal session or tab to enjoy your brown-backgrounded prompt.
Reverting to the Original PS1
Should you wish to go back to the original prompt after your experimentation, recall the backup:
PS1=$ps1_backup
Your terminal is now back to its previous state.
Conclusion
While adding colors to the terminal might seem superficial at first glance, it
provides valuable differentiation and enhances readability. Whether you're
customizing your prompt or distinguishing output, understanding and using ANSI
escape codes can greatly enhance your terminal experience. Always remember to
reset your formatting with \e[0m
to ensure the intended appearance is
maintained throughout your session.
What Can You Do Next 🙏😊
If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.