Raspberry Pi

How to Colorize Bash Prompt in Raspberry Pi Terminal

Bash Prompt is a Linux command line shell that allows users to run different commands. The Raspberry Pi does have a command-line shell that is a powerful, yet simple way to execute various types of commands. One of the most useful and powerful features of the terminal is the ability to customize the command line prompt. By colorizing the command line, a user can make the terminal more visually appealing and easier to navigate.

In this writeup, we will discuss how to colorize the command line prompt in the Raspberry Pi.

Access Bash Prompt in Raspberry Pi Terminal

First, it is important to understand the syntax for customizing the command line prompt. The command line prompt setting is stored in a shell variable called “PS1“, and this variable can be customized by changing the string it contains.

The fundamental tool for writing data to a terminal’s standard output is the Linux echo command. For the sake of this tutorial, we just need to get the $PS1 variable’s current value, so the users can use echo followed by $PS1.

echo $PS1

To modify the PS1 variable, enter the following command:

PS1="Enter Command …"

This command allows you to modify the PS1 like this:

Colorize Bash Prompt in Raspberry Pi Terminal

To colorize the bash prompt, we need to use an escape sequence to set the color of the text. The escape sequence we will use to set the color is “\[\e[Xm\]“, where X is the color code for the color of the text. For instance, the color code for a dark blue color is “34“. Thus, if we wanted to color our command line prompt dark blue, we would need to set the PS1 variable to “\[\e[34m\]“.

PS1="\[\e[34m\]pi@raspberrypi:$"

Note: In place of “pi@raspberrypi:$”, you can add your own username and hostname or anything you want to write.

To make the command line even easier to navigate, we can also add symbols like “$” or “>” to the prompt, which will make it easier to distinguish between input and output.

To add symbols to the prompt, we can add any desired character at the end of the string. For example, if we wanted to add a command like “pi@raspberrypi:$” to our dark blue prompt, the string would be “\[\e[34m\]pi@raspberrypi:$“.

Once the prompt has been customized, the changes will take effect. To list all the files and directories, we may use the “ls” command.

To change the color from dark blue to magenta, we can use the code for magenta which is ‘35’. So, the command will be:

PS1=”\[\e[35m\]pi@raspberrypi:$”

Which will change the color of the command line prompt to magenta.

This will allow you to alter the Raspberry Pi’s bash prompt’s color.

Conclusion

The command line prompt in the Raspberry Pi terminal can be custom colorized to make the terminal more visually appealing and easier to navigate. This requires setting the string variable PS1 to contain the desired color code and characters, which should be enclosed in quotation marks.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.