Many people don’t think of the command line prompt as a useful element, or even pay it much attention. However, a useful prompt can change the way you use the command line, and by extension, your system. This article shows you a number of useful and interesting Bash prompts with examples. Note that we begin with the prompts themselves, then offer some further instructions on how to work with them.
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:
PS1="\`if [ \$? = 0 ]; then echo \[\e[33m\]^_^\[\e[0m\]; else echo \[\e[31m\]O_O\[\e[0m\]; fi\`[\u@\h:\w]\<strong>\$</strong> "
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.
PROMPT_COMMAND='PS1="\[\033[0;33m\][\!]\`if [[ \$? = "0" ]]; then echo "\\[\\033[32m\\]"; else echo "\\[\\033[31m\\]"; fi\`[\u.\h: \`if [[ `pwd|wc -c|tr -d " "` > 18 ]]; then echo "\\W"; else echo "\\w"; fi\`]\$\[\033[0m\] "; echo -ne "\033]0;`hostname -s`:`pwd`\007"'
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.
PS1="<strong>\n</strong>\[\033[35m\]<strong>\$</strong>(/bin/date)<strong>\n</strong>\[\033[32m\]\w<strong>\n</strong>\[\033[1;31m\]\u@\h: \[\033[1;34m\]<strong>\$</strong>(/usr/bin/tty | /bin/sed -e 's:/dev/::'): \[\033[1;36m\]<strong>\$</strong>(/bin/ls -1 | /usr/bin/wc -l | /bin/sed 's: ::g') files \[\033[1;33m\]<strong>\$</strong>(/bin/ls -lah | /bin/grep -m 1 total | /bin/sed 's/total //')b\[\033[0m\] -> \[\033[0m\]"
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.
PS1='\[\e[1;32m\]\u@\H:\[\e[m\] \[\e[1;37m\]\w\[\e[m\]\n\[\e[1;33m\]hist:\! \[\e[0;33m\] \[\e[1;31m\]jobs:\j \$\[\e[m\] '
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.
PS1="<strong>\n</strong>\[\e[30;1m\]\[\016\]l\[\017\](\[\e[34;1m\]\u@\h\[\e[30;1m\])-(\[\e[34;1m\]\j\[\e[30;1m\])-(\[\e[34;1m\]\@ \d\[\e[30;1m\])->\[\e[30;1m\]<strong>\n</strong>\[\016\]m\[\017\]-(\[\[\e[32;1m\]\w\[\e[30;1m\])-(\[\e[32;1m\]<strong>\$</strong>(/bin/ls -1 | /usr/bin/wc -l | /bin/sed 's: ::g') files, <strong>\$</strong>(/bin/ls -lah | /bin/grep -m 1 total | /bin/sed 's/total //')b\[\e[30;1m\])--> \[\e[0m\]"
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.
PS1="<strong>\n</strong>\[\e[32;1m\](\[\e[37;1m\]\u\[\e[32;1m\])-(\[\e[37;1m\]jobs:\j\[\e[32;1m\])-(\[\e[37;1m\]\w\[\e[32;1m\])<strong>\n</strong>(\[\[\e[37;1m\]! \!\[\e[32;1m\])-> \[\e[0m\]"
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.
Frequently Asked Questions
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.
Our latest tutorials delivered straight to your inbox