Extending the Bash Prompt

 in
Terminal and xterm prompts can be created incorporating standard escape sequences to give user name, current working directory, time and more.
Shell Scripts

Linux comes with many small utility programs such as date, grep and wc which allow you to manipulate data. If you wish to create complex combinations of these programs within a prompt, it may be easier to make a shell script and call it from the prompt. An example of a small shell script used within a prompt is given in Listing 1.

Listing 1. Shell Script for Use in Prompt

I keep this as a shell script in my ~/bin directory, which is in my path. Use it in a prompt in this way:

[2203][giles@nikola:~]$ PS1="[\u@\h:\w (\$(lsbytesum) Mb)]\$ "[giles@nikola:~ (0 Mb)]$ cd /bin
[giles@nikola:/bin (4.498 Mb)]$
Non-Printing Escape Sequence

Non-printing escape sequences can be used to produce interesting effects in prompts. to use these escape sequences, you need to enclose them in \[ and \], telling bash to ignore this material while calculating the size of the prompt. failing to include these delimiters results in line editing code placing the cursor in the wrong place, because it doesn't know the actual size of the prompt. escape sequences must also be preceded by \033[ in bash prior to version 2 or by either \033[ or \e[ in later versions.

xterm title bar

this example modifies the title bar of an xterm window. if you try to change the title bar of an xterm with your prompt when you are at the console, you'll produce garbage in your prompt. to avoid this problem, test the term environment variable to determine if your prompt is going to be in an xterm. if the shell is an xterm, the shell variable (${titlebar}) is defined. it consists of the appropriate escape sequences, and \u@\h:\w, which puts user@machine: working directory in the xterm title bar. this is particularity useful with minimized xterms, making them more rapidly identifiable. the other material in this prompt should be familiar from previous prompts we've created.

listing 2. Function to Set Titlebar

Listing 2 is a function that can be incorporated into ~/.bashrc. The function name can then be called to execute the function. The function, like the PS1 string, is stored in the environment. Once the PS1 string is set by the function, you can remove the function from the environment by typing unset proml. Since the prompt can't change from being in an xterm to being at the console, the TERM variable isn't tested each time the prompt is generated.

I used continuation markers (backslashes) in the definition of the prompt to allow it to be continued on multiple lines. This improves readability, making it easier to modify and debug.

I define this as a function because this is how the Bash Prompt package deals with prompts: it is not the only way to do it, but it works well. As the prompts you use become more complex, it becomes more and more cumbersome to type them in at the prompt and more practical to create them in a text file. To test this example at the prompt, save the function as a text file called “proml”. The Bash source command can be used to read the prompt function by typing:

[giles@nikola:~ (0 Mb)]$ source proml

To execute the prompt function, type:

[giles@nikola:~ (0 Mb)]$ proml

Color Text

As mentioned before, non-printing escape sequences must be enclosed in \[\033[ and \]. For color escape sequences, they must also be followed by a lowercase m. To include blue text in the prompt:

PS1="\[\033[34m\][\$(date +%H%M)][\u@\h:\w]$
"

The blue color that starts with the 34 color code is never switched back to the regular gray, so any text you type after the prompt is still in the color of the prompt. This is also a dark shade of blue (very hard to read), so combining it with the bold code might help:

PS1="\[\033[1;34m\][\$(date +%H%M)][\u@\h:\w]$\[\033[0;37m\] "
The prompt is now in light blue, and it ends by switching the color back to gray, which is the color most of us expect when we type.

Bash Color Equivalences

Background colors can be set by using 44 for Blue background, 41 for a Red background, etc. No bold background colors are available. Combinations can be used, e.g., Light Red text on a Blue background: \[\033[44;1;31m\]. Other codes available include 4 for Underscore, 5 for Blink, 7 for Inverse and 8 for Concealed.

Listing 3. elite Function

The prompt I use most of the time is based on one called “elite2” in the Bash Prompt package, which I have modified to work better on a standard console (Listing 2). (The original uses special xterm fonts.) I define the colors as temporary shell variables for the sake of readability—it is easier to work with. The GRAD1 variable is a check to determine what terminal you are on, and it needs to be done only once. The prompt you see looks like Figure 1.

Figure 1. My Prompt

The Bash Prompt package is available in beta at http://bash.current.nu/ and is the work of several people, co-ordinated by Rob Current. The package offers a simple way to use multiple prompts or “themes”. Several of these prompts use the extended VGA character set, so they look bad unless used with special xterm fonts. The “fire” theme shown in Figure 2 requires these fonts. See Stumpy's ANSI Fonts page at http://home.earthlink.net/~us5zahns/enl/ansifont.html for instructions on installing and using these fonts.

Figure 2. Fire Prompt from the Bash Prompt Package

You can change the prompt in your current terminal using the example elite function by typing source elite (assuming the elite function file is in your path) followed by elite. This leaves you with an extra function (elite) in your environment space—if you want to clean up the environment, type unset elite.

This would seem like an ideal candidate for a small shell script, but a script doesn't work here because a script cannot change the environment of your current shell—it can change only the environment of the subshell it runs in. Environment variables of your current shell can be changed by environment functions. The Bash Prompt package puts a function called callbashprompt into your environment, and while they don't document it, it can be called to load any Bash Prompt theme on the fly. It looks in the theme directory it installed, sources the function you requested, loads it, then unsets the function. callbashprompt wasn't intended to be used this way and has no error checking, but it works quite well.

Resources

Giles Orr is a Systems Librarian at Georgia College and State University. He's been using Linux for four years. He doesn't claim to be a master programmer and would welcome improvements to the code in this article. He can be reached at giles@interlog.com. He is the mainter of the Bash Prompt HOWTO at http://metalab.unc.edu/LDP/HOWTO/Bash-Prompt-HOWTO.html.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

you can use the tput utility

norpan's picture

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

Anonymous's picture

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

s7even's picture

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

humbletech99's picture

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.

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix