Here we offer a few Bash prompts, and not all will be serious. For example, our first entry on the list could bring a little joy to you when using the command line!
1. Show a “Kaomoji” upon successful execution
This prompt is amusing yet useful! The idea is that as long as you execute successful commands, your prompt will show a happy kaomoji. In contrast, when a command fails, you’ll see a sad kaomoji. To apply this, use the following code: This may look confusing, but we’re using if statements to determine whether the command is good or bad. From there, we use some escaping around the kaomoji to make sure they display the right way.
2.Change the Bash Prompt’s Color When the Terminal Issues a Bad Command
Arguably, this prompt is the most useful on the list. Like above, the prompt changes color if your last command failed to run but also shortens long paths and contains the Bash history number of each command for easy retrieval. Again, this can look daunting to implement, but it’s straightforward. We use if statements again, along with color codes to highlight various messages. You can make these colors anything you’d like, though we’d exercise some logical thought the changed colors should help you rather than being changed just for the sake of making a change.
3. Span Your Bash Prompts Over Multiple Lines
Sometimes you may want to include a lot of information in your Bash prompts. If this describes you, this Bash prompt may interest you. It shows the date, time, full directory path, the user and host names, the active terminal, and even file count and space usage. Here, we don’t need if statements, as the prompt itself will have a static layout, yet dynamic content. You may also find it hard to parse the different elements in this one. Our advice is to apply it, then run through the prompt piece by piece. With a little cross-referencing, you’ll begin to understand how the prompt combines.
4. Use Colors to Make Your Bash Prompt’s More Readable
There’s nothing fancy about this prompt other than the good use of color to separate the different pieces of information. As you can see, it provides the time, user and host names, and current directory. This is one of the most useful Bash prompts and is an area you’ll look to first in order to customize your prompt. It can give you a good basis to learn how a custom prompt works.
5. Show the Full Directory Path
Much like using colors, this prompt is a clean and minimal two-line prompt (although there’s a blank line at the top). We display the full working directory path on the first line, followed by the user at the bottom. If you don’t want to use a blank line within the prompt, you can take out the first \n. However, play around with the layout and formatting, as each Terminal experience will be different depending on your computer and command line settings.
6. Show a Background Job Count
This two-liner displays information we haven’t yet used in our Bash prompts. We provide a running tally of background jobs. This will be useful if you multitask but need to keep an eye on any activity. The first line is the user and host name, along with the working directory path. On the second line, we have history number and a count of the jobs running in the background. There’s nothing much to this one, although it can give you some much needed information on system activity. You could even turn this into a more complex prompt and add if statements and color coding to notify you when a job reaches certain levels.
7. Display Information for the Working Directory
With this one we display the user and host name, the job count, and the date and time on the top line. Underneath is the current directory, along with the number of files in the directory and some information on disk usage. The biggest issue with all of these prompts is to figure out what’s happening around the formatting and escaping. However, as with multi-line prompts, you should tackle this one piece by piece until you understand the overall structure. You could even input a portion at a time and note whether it displays or whether there are errors, and work up to the full snippet.
8. Create Your Own Bash Prompts
You can combine any and all of the above to create your own custom Bash prompts. For example, this is a modification of number seven in this list yet changed to take up less space and include some specific and relevant information. A two-line style is always more compact, as you can see the full path without reducing the space for the commands you use. By now, you’ll likely understand what this prompt does, but our advice is to try to create your own rather than paste this one in. That way, you can begin to learn how the different elements work together and may even come up with your own custom Bash prompts.
1. Why would I want to use different Bash prompts?
There are many reasons for this, but productivity is a key consideration. If you often use the command line, you’ll see your prompt every few seconds. As such, if you put the most important information into it, you can always have it on hand. In many cases, using a custom Bash prompt is more efficient than having a number of windows open.
2. How do I add these Bash prompts to my Terminal output?
To use any of these prompts, you can copy and paste the code into your Terminal app. However, this will only apply to the current session, so in theory, you’d need to do this whenever you launch the Terminal. If you’d like to make the change permanent, you can paste the snippets onto the end of your ~/.bashrc file, and restart your Terminal.
3. Would these Bash prompts also work using other shells?
In many cases, you’ll find that there’s cross-compatibility with other types of shells. However, there’s no guarantee that changes will offer 100 percent parity. If you take a look at our post on customizing the Mac Terminal, this uses zsh. You’ll note that there are minor differences but many similarities.
4. How do I find out what information and elements I can add to my Bash prompts?
Pretty much any element of your system can show up in a Bash prompt. You’ll want to look into Bash variables, as this is how you “unlock” these different elements. A great resource for understanding what you can change is on the Linux.com website, and we’d recommend keeping that documentation on hand throughout your customization sessions.
5. What if I no longer want to use custom Bash prompts in my Terminal?
The simple answer here is to open the config file and delete the relevant lines. You don’t need to carry out any further commands, although it may be worth restarting your Terminal if you don’t see the changes you expect. If you use the command line on a regular basis, or have specific needs when you fire up a Terminal app, customizing the prompt could be a great idea. It doesn’t take much knowledge, and in many cases, you can find the prompt you need from another developer.