Jump to: navigation, search

Prompt Magic

114 bytes added, 7 years ago
no edit summary
As Linux/UNIX people, we spend a lot of time working in the shell, and in many cases, this is what we have staring back at us:
If you happen to be root, you're entitled to the "prestige" version of this beautiful prompt:
These prompts are not exactly pretty. It's no wonder that several Linux distributions have upgraded their default prompts that add color and additional information to boot. However, even if you happen to have a modern distribution that comes with a nice, colorful prompt, it may not be perfect. Maybe you'd like to add or change some colors, or add (or remove) information from the prompt itself. It isn't hard to design your own colorized, tricked-out prompt from scratch.
In bash, you can set your prompt by changing the value of the PS1 environment variable, as follows:
<preconsole>$ ##bl## export PS1="> "</preconsole>
Changes take effect immediately, and can be made permanent by placing the "export" definition in your <span style="color:green">~/.bashrc</span> file. <span style="color:green">PS1</span> can contain any amount of plain text that you'd like:
<preconsole>$ ##bl## 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:
<preconsole>$ ##bl## 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.
Colors are selected by adding special sequences to <span style="color:green">PS1</span> -- basically sandwiching numeric values between a <span style="color:green">\e[</span> (escape open-bracket) and a <span style="color:green">m</span>. 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:
<preconsole>$##bl## export PS1="\w> "</preconsole>
<preconsole>$##bl## export PS1="\e[32;40m\w> "</preconsole>
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 <span style="color:green">\e[0m</span> sequence:
<preconsole>$##bl## export PS1="\e[32;40m\w> \e[0m"</preconsole>
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 <span style="color:green">40</span>, 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, <span style="color:green">\[</span> and <span style="color:green">\]</span>. 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:
Anonymous user

Navigation menu