In Linux, PS1 is an environment variable that specifies the format of the command prompt displayed in the terminal. It stands for “Prompt String 1” and it is used to customize the appearance of the prompt.
By default, the bash prompt includes the current username, hostname, and current working directory, followed by the `$` symbol for a regular user or the `#` symbol for the root user. The prompt is displayed on the command line, and it indicates that the terminal is ready for input.
You can customize the bash prompt by modifying the value of the PS1 variable. For example, you can use special characters and codes to change the colors, font styles, and other formatting options of the prompt. You can also include other information, such as the current time or the git branch name, in the prompt.
In this tutorial, we will discuss how to change the PS1 bash prompt and make is colorful in Linux.
Change Bash Prompt (PS1) in Linux
To customize the bash prompt (PS1) in Linux, you can use the following steps:
- Open the `~/.bashrc` file in a text editor. This file is located in your home directory and contains configuration settings for the bash shell.
- Find the line that sets the value of `PS1`. It will look something like this:
Check the below screenshot:
- Modify the value of PS1 to customize the appearance of the prompt. You can use the following special characters to include information in the prompt:
- `\u`: The username of the current user
- `\h`: The hostname up to the first .
- `\H`: The full hostname
- `\w`: The current working directory
- `\W`: The basename of the current working directory
- `\$`: This code represents the prompt symbol, which is $ for a regular user and # for the root user.
- For example, to customize the prompt to display the current working directory and the $ symbol, you could use the following value for PS1:
Check the below screenshot:
- Save the `~/.bashrc` file and exit the text editor.
- Run the following command to apply the changes to your current session:
Your bash prompt will now be customized according to the value you set for PS1.
Make Colorful Bash Prompt (PS1) in Linux
To customize the bash prompt (PS1) in Linux, you can use special characters and codes to add colors and other formatting options.
Here’s an example of a bash prompt that includes a red username, a green hostname, and a blue current working directory:
To set the bash prompt in your current session, you can simply copy and paste the above code into the terminal and press Enter. To make the change permanent, you can add the same line to the .bashrc file in your home directory.
Here’s a breakdown of the different color codes used in the example above:
- \[\e[0;31m\] – This code sets the text color to red. The 0;31 value specifies the color, with 31 representing red.
- \[\e[0;32m\] -This code sets the text color to green. The 0;32 value specifies the color, with 32 representing green.
- \[\e[0;34m\] -This code sets the text color to blue. The 0;34 value specifies the color, with 34 representing green.
- \[\e[m\] – This code resets the text color to the default value. So the remaining text will be default in color.
You can use other codes to customize the bash prompt with different colors and formatting options. For example, to make the text bold, you can use
\[\e[1m\] before the text and
\[\e[m\] after the text. You can find a list of all the available codes in the PROMPTING section of the bash man page.
I hope this helps! Let me know if you have any questions.