Extending the Bash Prompt
Descended from the Bourne shell, Bash (Bourne Again Shell) is a GNU product that is the standard command-line interface on most Linux machines. It excels at interactivity, supporting command-line editing, completion and recall. It also supports configurable prompts—most people realize this, but may not realize how useful it can be.
Most Linux systems have a default prompt in one color (gray) that includes your user name, the name of the machine you are working on and your current working directory. In addition, you can display even more information, use ANSI colors and manipulate the title bar of an xterm to provide useful information.
Beyond looking cool, prompts are also useful for keeping track of system information. One idea with appeal to many is the use of different color prompts on different machines. If you have several xterms open on different machines or if you tend to forget which machine you are working on, you'll find this a great reminder.
To change your prompt, you need a basic understanding of shell programming and UNIX utilities. The more you know, the more complex the prompts you will be able to create.
The appearance of the prompt is governed by the shell variable PS1. Command continuations are indicated by the PS2 string, which can be modified in exactly the same way. Since controlling it is exactly the same, I'll mostly be modifying the PS1 string. (PS3 and PS4 strings are also available, but are never seen by the average user. See the Bash man page if you're interested in their purpose.) To change the way the prompt appears, change the PS1 variable. For experimentation purposes, the PS1 string can be entered at the prompt to show the results immediately. Doing so affects only your current session. If you want to make a permanent change, modify the ~/.bashrc file by adding the new definition of PS1. If you have root permission, you can modify the PS1= line in the /etc/profile file. On some distributions (Red Hat 5.1 at least), the /etc/bashrc file overrides the /etc/profile setting of PS1 and PS2.
My default prompt includes my user name “giles”, the name of my work machine “nikola” and my home directory /home/giles. The simplest prompt is a single character. I can change my default prompt to a simple $ by typing:
[giles@nikola giles]$ PS1="$ "
I use the quotes to force a space after the prompt, making it more readable.
Many escape sequences are offered by the Bash shell for insertion in the prompt. See the sidebar which shows the Bash 2.02 man page.
$ PS1="\u@\h \W> "<\n> giles@nikola giles>
This example creates a prompt that is close to the default on most Linux distributions. I wanted a slightly different appearance, so I changed it to include the time by typing:
giles@nikola giles> PS1="[ ][\u@\h:\w]$ "<\n> [21:52:01][giles@nikola:~]$Bash also provides an environment variable called PROMPT_COMMAND. The contents of this variable are executed as a regular Bash command just before Bash displays a prompt.
[21:55:01][giles@nikola:~] PS1="[\u@\h:\w]$ "<\n> [giles@nikola:~] PROMPT_COMMAND="date +%H%M" 2155 [giles@nikola:~] ls bin mail 2156 [giles@nikola:~]$ unset PROMPT_COMMAND [giles@nikola:~]In this example, I changed PS1 by eliminating the escape sequence, so that time was no longer a part of the prompt. Then I used date +%H%M to display the time in a format I like better. At the end, I used the unset command to remove the PROMPT_COMMAND environment variable.
As I discuss the use of external commands in prompts, I'll use the $(command) convention for command substitution; that is,
$(date +%H%M)
means “substitute the output from the date +%H%M command here.” This works in Bash 1.14.7 and 2.0+. In some older versions of Bash, you may need to use backquotes (`date +%H%M`). Backquotes can be used in Bash 2.0+ but are being phased out in favor of $(), which nests better. If you are using an earlier version of Bash, substitute backquotes wherever you see $(). If the command substitution is escaped (i.e., \$(command) ), then use backslashes to escape BOTH your backquotes (i.e., \`command\`).
You don't want to insert much material from an external command into the prompt, as a prompt of great length may be created. You also want to use a fast command, because it will be executed each time your prompt appears on the screen. Delays in the appearance of the prompt while you are working can be annoying.
[giles@nikola:~]$ PS1="[\$(date +%H%M)][\u@\h:\w]$ "[2159][giles@nikola:~]$
Note the backslash before the dollar sign of the command substitution. Without it, the external command is executed exactly once: when the PS1 string is read into the environment. For this prompt, it would display the same time no matter how long the prompt was used. The backslash prevents immediate shell interpretation of the command, so date is called each time a prompt is generated.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- I once had a better way I
2 hours 42 min ago - Not only you I too assumed
2 hours 59 min ago - another very interesting
4 hours 52 min ago - Reply to comment | Linux Journal
6 hours 45 min ago - Reply to comment | Linux Journal
13 hours 39 min ago - Reply to comment | Linux Journal
13 hours 56 min ago - Favorite (and easily brute-forced) pw's
15 hours 47 min ago - Have you tried Boxen? It's a
21 hours 39 min ago - seo services in india
1 day 2 hours ago - For KDE install kio-mtp
1 day 2 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
you can use the tput utility
you can use the tput utility to avoid typing escape sequences and have it work on any terminal. Just use for instance $(tput setaf 1) instead of \e[31m.
xterm/xfterm4
how about the title prompt in tthe bar up top? what is the code for that? I have:
function combo {
PS1="\[\033[01;34;01m\]\333\262\261\260\[\033[01;37;44m\]\u@\h\[\033[00;34;40m\]\260\261\262\333\[\033[00;34;40m\]\333\262\261\260\[\033[01;37;40m\] \d \$(date +%I:%M:%S%P)\n\[\033[01;33;40m\]$PWD>\[\033[00m\] "
PS2="\[\033[01;34;01m\]\333\262\261\260\[\033[00;34;40m\]\260\261\262\333\[\033[00;34;40m\]\260\261\262\333\[\033[00;34;40m\]\333\262\261\260\[\033[01;01;34m\]>\[\033[00m\] "
}
combo
and it just says "xterm" at the top title of the xterm--not the screen prompt, that is correct. Am I making sense? I have code for it, but i get my old hostname there, but the correct hostname at the screen prompt. I hope this makes sense...
USA bash prompt
patriot or pirate....latter or former.
here's the fruit of some wasted productivity.
USA the _______.
pieces
PS1="\033[44m*****\033[41m \033[0m\n\033[44m*****\033[47m \033[0m\n\033[44m*****\033[41m \033[0m\n\033[47m \033[0m\n\033[41m \033[0m \$ "
ps,,, BSD rules =P
BSD ruled
BSD did rule, but now is the time of GNU/Linux where newer, younger, stronger challengers are here, bring on Gentoo...
Also, death to 1000 distros, let's concentrate on 3 good ones...
(take note Mr Shuttleworth - you should be working for Debian, not seeking personal glory riding their coattails...)
Oh, and thanks for the article, LINUX Journal.