Join Scott Simpson for an in-depth discussion in this video System basics: The Bash prompt, part of Linux Tips Weekly.
- [Instructor] The prompt is an important part of a text shell. Here in Bash the prompt shows us some useful information. By default we see the current user name, at the current host name, and then after a colon we see the current working directory. Right now that's the tilde for my user's home folder. But if I changed over to, say, e t c, that would change as I go. And the last character in the prompt string here is a dollar sign. It would be a pound sign if I were a root or using the super user privileges.
While this information is helpful we can change it around to support whatever kind of configuration we want. Some people prefer to see different information in the prompt string. And some people just take their prompt down to the one character that shows whether they're root or a regular user. This prompt is represented as PS1. Prompt string one. And we can set in the bashrc file or set it temporarily by exporting a new value. I'll go back to my home folder and I'll open up my bashrc file here with nano.bashrc. I'll search for PS1.
There's a few occurrences here. And this first one is the one that I'm after. This will be used if the color prompt variable is set to yes. That variable is determined up here. If the variable's not set to yes, then we'll use this alternate line. The actual content of the PS1 line is, well, it's less than easily readable. That's because there's a lot of ANSI color formatting codes and placeholders for particular fields. In here the first thing we see is a piece that displays the name of the chroot, if we're using one.
After that there's a block that sets a color with ANSI escape codes. 033 is the escape sequence, and in my case, 38;5;18m sets the colors. Yours may be set to the default, which is likely 1;32m for bold and green. \u is the user name, @ is the at sign, and \h is the host name. The next escape here uses 00m to reset all of the color antributes.
You can think of it like ending an inline style tag in HTML. Then we have a colon in clear unformatted text, and then another escape sequence for another color with \w for the working directory. I'll scroll to the end of the line here. And there's another escape code to clear the colors. Then we clear the format code again. And we have \$ which as I mentioned before shows a dollar sign for regular users and a pound for root. Easy, right? Well, if you can't quite bring yourself to think in ANSI escape codes, there's an easier way of setting up a PS1 string.
Before we do that, I'll duplicate this line and comment it out just in case something goes terribly wrong. Here in nano, I'll cut the line with control k, and then uncut it twice with control u. Then I'll just comment out one to save for later. And I'll save my file with control o. Now I'll switch over to an online prompt builder that I like to use. And here, we can experiment with different variables and formatting codes and see what the results look like. Take a moment to experiment with this builder and make a string that suits your needs.
I'll drag over the user name and the space and the day and another space and then the ending character. There are a lot of variables you can use in the prompt. And to set the color of an item, you can click on it and choose one. The string gets built up in this box over here. Notice that tput sgr0 works like the 00m ANSI attribute and clears any previously set formatting antributes.
This builder is pretty intelligent, and it'll keep track of what things are what color so you won't have repetitive color settings and clear settings for adjacent items of the same color. When you're happy with the prompt string, copy the portion of the command over here inside the double quotes to your clipboard. And let's go back to the terminal. I'll bounce out of nano really quickly here with control z. And at the command line we can use the export command to temporarily switch the prompt. I'll write export PS1=" and then I'll paste with control shift v.
And I'll add an ending quote. That'll only work for this session. And it's good to test settings before committing to them. If your prompt looks how you like you can go back into nano. In my case it's suspended in the background as job1 so I can bring it to the foreground with fg1. Or you can just edit .bashrc in your home folder again if you closed the editor. I'll move to the end of the line and delete everything inside the quotes, but I'll keep they're chroot part. The slash square brackets starts off the block I want to replace. So I'll delete all of that and then I'll paste my string with control shift v.
I'll double check to make sure that I don't have any extra or missing stuff and then I'll save. Now, I can test out my changes to the bashrc file with source .bashrc. Great. And now when I open a bash session with my user my prompt will look like this. You can also edit PS2 or prompt string two. Which is what appears for continuations. If I type ls and a slash, it's still expecting input. And here on the continued line I see a greater than symbol.
I'll press control c and really quickly I'll export a different value for PS2. How about ". . .and?" Now, with that previous command with the line continuation I see my new PS2 here. Let's get some more lines in there. With cat and a little haiku. If you want to make the change to the PS2 line permanent you can add it into your bashrc file along with the PS1 line.
There's a lot of really creative and useful bash prompt customizations out there. Just search online for bash prompt gallery and you'll see links to discussions and tools for further personalizing your prompt.
Note: Because this is an ongoing series, there is no certificate of completion available for this course.