If you are like me sometimes, a lazy geek, you can now customize your bash prompt directly on a website. The author offers a way to generate the PS1 variable for your .bashrc file.
You can drag and drop the element you want to create your own and customized bash prompt. You can also change the color of the elements :
Go and customize your prompt here
- Active Directory
- Files and folders
- Operating System
- Service and process
Under bash, you can set your prompt by changing the value of the PS1 environment variable, as follows:
$ export PS1="> " >
Changes take effect immediately, and can be made permanent by placing the “export” definition in your ~/.bashrc file. PS1 can contain any amount of plain text that you’d like:
$ export PS1="This is my super prompt > " This is my super prompt >
While this is, um, interesting, it’s not exactly useful to have a prompt that contains lots of static text. Most custom prompts contain information like the current username, working directory, or hostname. These tidbits of information can help you to navigate in your shell universe. For example, the following prompt will display your username and hostname:
$ export PS1="\u@\H > " drobbins@freebox >
This prompt is especially handy for people who log in to various machines under various, differently-named accounts, since it acts as a reminder of what machine you’re actually on and what privileges you currently have.
In the above example, we told bash to insert the username and hostname into the prompt by using special backslash-escaped character sequences that bash replaces with specific values when they appear in the PS1 variable. We used the sequences “\u” (for username) and “\H” (for the first part of the hostname). Here’s a complete list of all special sequences that bash recognizes (you can find this list in the bash man page, in the “PROMPTING” section):
|\a||The ASCII bell character (you can also type \007)|
|\d||Date in “Wed Sep 06” format|
|\e||ASCII escape character (you can also type \033)|
|\h||First part of hostname (such as “mybox”)|
|\H||Full hostname (such as “mybox.mydomain.com”)|
|\j||The number of processes you’ve suspended in this shell by hitting ^Z|
|\l||The name of the shell’s terminal device (such as “ttyp4”)|
|\s||The name of the shell executable (such as “bash”)|
|\t||Time in 24-hour format (such as “23:01:01”)|
|\T||Time in 12-hour format (such as “11:01:01”)|
|\@||Time in 12-hour format with am/pm|
|\v||Version of bash (such as 2.04)|
|\V||Bash version, including patchlevel|
|\w||Current working directory (such as “/home/drobbins”)|
|\W||The “basename” of the current working directory (such as “drobbins”)|
|\!||Current command’s position in the history buffer|
|\#||Command number (this will count up at each prompt, as long as you type something)|
|\$||If you are not root, inserts a “$”; if you are root, you get a “#”|
|\xxx||Inserts an ASCII character based on three-digit number xxx (replace unused digits with zeros, such as “\007”)|
|\[||This sequence should appear before a sequence of characters that don’t move the cursor (like color escape sequences). This allows bash to calculate word wrapping correctly.|
|\]||This sequence should appear after a sequence of non-printing characters.|
So, there you have all of bash’s special backslashed escape sequences. Play around with them for a bit to get a feel for how they work. After you’ve done a little testing, it’s time to add some color.
Adding color is quite easy; the first step is to design a prompt without color. Then, all we need to do is add special escape sequences that’ll be recognized by the terminal (rather than bash) and cause it to display certain parts of the text in color. Standard Linux terminals and X terminals allow you to set the foreground (text) color and the background color, and also enable “bold” characters if so desired. We get eight colors to choose from.
Colors are selected by adding special sequences to PS1 — basically sandwiching numeric values between a “\e[” (escape open-bracket) and an “m”. If we specify more than one numeric code, we separate each code with a semicolon. Here’s an example color code:
When we specify a zero as a numeric code, it tells the terminal to reset foreground, background, and boldness settings to their default values. You’ll want to use this code at the end of your prompt, so that the text that you type in is not colorized. Now, let’s take a look at the color codes. Check out this screenshot:
To use this chart, find the color you’d like to use, and find the corresponding foreground (30-37) and background (40-47) numbers. For example, if you like green on a normal black background, the numbers are 32 and 40. Then, take your prompt definition and add the appropriate color codes. This:
export PS1="\w> "
export PS1="\e[32;40m\w> "
So far, so good, but it’s not perfect yet. After bash prints the working directory, we need to set the color back to normal with a “\e[0m” sequence:
export PS1="\e[32;40m\w> \e[0m"
This definition will give you a nice, green prompt, but we still need to add a few finishing touches. We don’t need to include the background color setting of 40, since that sets the background to black which is the default color anyway. Also, the green color is quite dim; we can fix this by adding a “1” color code, which enables brighter, bold text. In addition to this change, we need to surround all non-printing characters with special bash escape sequences, “\[” and “\]”. These sequences will tell bash that the enclosed characters don’t take up any space on the line, which will allow word-wrapping to continue to work properly. Without them, you’ll end up with a nice-looking prompt that will mess up the screen if you happen to type in a command that approaches the extreme right of the terminal. Here’s our final prompt:
export PS1="\[\e[32;1m\]\w> \[\e[0m\]"
Don’t be afraid to use several colors in the same prompt, like so:
export PS1="\[\e[36;1m\]\u@\[\e[32;1m\]\H> \[\e[0m\]"