More Resources

The following are a few additional pages that cover a great deal more about the BASH prompt.

More Magic

Why stop at just having your prompt display useful information? Why not have it control the content of your titlebar as well. Leave it to the guys at DeveloperWorks to find more creative ways to mess with your system:

The simple BASH prompt

Letting it work for you…

If you do any amount of work from the command line in konsole, x-term, etc..., then your prompt should help you get work done. Why prompts tonight?? Who knows, but... Where virtually everything requires a copy, move, ssh, rsync, scp, etc.. why not have 1/2 the work already done by your prompt? The simple prompt I consider indispensable looks like this:

As user:

	20:42 alchemy:~/linux/scripts/config/zypp>

As root:

	[20:44 alchemy:/home/david/linux/scripts/config/zypp] #

Actual Colors:

Prompts shown in color

The first element in both is just the time and can be ignored for purposes of this discussion. What you also can't see in the text layout -- is the colors. The user prompt time is lightgray and the text of the prompt is lightblue, for root, time is lightred, host is lightblue and text of the prompt is red. The colors work fine on light or dark backgrounds. [1]

So, how can a prompt “work” for me?

The beauty of the prompt is its format of: hostname:/path/to/pwd. Which is also the format required by rsync and scp and the path information works with every other bash command. For every copy, move, scp or rsync command, the host and directory information is already displayed, ready to be copied and pasted to the command line. This really makes life easier in tabbed consoles like 'konsole' in KDE when working between a local and remote system or between 2 remote systems where switching between hosts (tabs) is as simple at [shift]+[left/right].

The prompt eliminates typing

rsync or scp then becomes the simple matter of:

  • (a)   select current host:/path from prompt;
  • (b)   [shift]+[left/right] to change konsole tabs;
  • (c)   type 'rsync -options' [middle-click] (pastes host:/path information);
  • (d)   type or copy '(file/dir)name', and your done (without typos)

Of course where there is no path information, simply typing is faster, but when you need to do a rsync pull from something like: nirvana:/srv/http/download/linux/scripts/utils/filename.ext, I prefer the [select]+[middle-click-paste] method any day.

Setting the prompt -- simple enough

From the command line:

	The user prompt is:

PS1="\[\e[0;37m\]\A\[\e[1;34m\] \[\e[1;34m\]\h:\w> \[\e[0m\]"

	Root prompt is:

PS1="\[\e[1;34m\][\[\e[1;31m\]\A \[\e[1;34m\]\h\[\e[0;31m\]:\w\[\e[1;34m\]] # \[\e[0m\]"

In your .bashrc to set the prompt so it is your default, just add export in front of it like this:

export PS1="\[\e[0;37m\]\A\[\e[1;34m\] \[\e[1;34m\]\h:\w> \[\e[0m\]"

**Note: you will rarely find the /A prompt option documented for whatever reason. (I suspect it has something to do with it being buried on line 1891 of the present 'man bash'. It is just the time in HH:MM format for the record

Further Prompt Experimentation?

You can also set the secondary prompt PS2 in the same manner, but I have never found a use for this. The default '>' is just fine for all my purposes. Additionally, what I've found is that the over-use of colors or other fancyness quickly becomes more annoying than useful. But, to each his own, the power of Linux - flexibility and customization. If you want to experiment, then the following will give you the basic color and format options.

Prompt Colors and Decoration

Decorative text:

	\[   ( prefix for each colors/effect, i.e.: '\[\e[1m' for bold )

	\e[1mBold Text\e[m\n\
	\e[4mUnderline Text\e[m\n\
	\e[5mBlink Text\e[m\n\
	\e[7mInverse Text\e[m\]\n\

Normal text

    Foreground colors:

	\e[0;30m30: Black\n\
	\e[0;31m31: Red\n\
	\e[0;32m32: Green\n\
	\e[0;33m33: Yellow\Orange\n\
	\e[0;34m34: Blue\n\
	\e[0;35m35: Magenta\n\
	\e[0;36m36: Cyan\n\
	\e[0;37m37: Light Gray\Black\n\
	\e[0;39m39: Default\n\

    Bright foreground colors:

	\e[1;30m30: Dark Gray\n\
	\e[1;31m31: Red\n\
	\e[1;32m32: Green\n\
	\e[1;33m33: Yellow\n\
	\e[1;34m34: Blue\n\
	\e[1;35m35: Magenta\n\
	\e[1;36m36: Cyan\n\
	\e[1;37m37: White\n\
	\e[0;39m39: Default\n\

    Background colors:

	\[\e[1;37m\e[40m40: Black\e[0;49m\n\
	\e[41m41: Red\e[0;49m\n\
	\e[42m42: Green\e[0;49m\n\
	\e[43m43: Yellow\Orange\e[0;49m\n\
	\e[44m44: Blue\e[0;49m\n\
	\e[45m45: Magenta\e[0;49m\n\
	\e[46m46: Cyan\e[0;49m\n\
	\e[47m47: Light Gray\Black\e[0;49m\n\
	\e[49m49: Default\e[m\]\n'

Prompt Option Tags

From man bash (1869 lines down)

When executing interactively, bash displays the primary prompt PS1 when it is ready to read a command, and the secondary prompt PS2 when it needs more input to complete a command. Bash allows these prompt strings to be customized by inserting a number of backslash-escaped special characters that are decoded as follows:

	\a     an ASCII bell character (07)
	\d     the date in "Weekday Month Date" format (e.g., "Tue May 26")
		the format is passed to strftime(3) and the result is inserted
		into the prompt string; an empty format results in a locale-
		specific time representation. The braces are required
	\e     an ASCII escape character (033)
	\h     the hostname up to the first `.'
	\H     the hostname
	\j     the number of jobs currently managed by the shell
	\l     the basename of the shell's terminal device name
	\n     newline
	\r     carriage return
	\s     the name of the shell, the basename of $0 (the portion following
		the final slash)
	\t     the current time in 24-hour HH:MM:SS format
	\T     the current time in 12-hour HH:MM:SS format
	\@     the current time in 12-hour am/pm format
	\A     the current time in 24-hour HH:MM format
	\u     the username of the current user
	\v     the version of bash (e.g., 2.00)
	\V     the release of bash, version + patch level (e.g., 2.00.0)
	\w     the current working directory, with $HOME abbreviated with a tilde
	\W     the basename of the current working directory, with $HOME
		abbreviated with a tilde
	\!     the history number of this command
	\#     the command number of this command
	\$     if the effective UID is 0, a #, otherwise a $
	\nnn   the character corresponding to the octal number nnn
	\\     a backslash
	\[     begin  a  sequence of non-printing characters, which could be used
		to embed a terminal control sequence into the prompt
	\]     end a sequence of non-printing characters

The command number and the history number are usually different: the history number of a command is its position in the history list, which may include commands restored from the history file (see HISTORY below), while the command number is the position in the sequence of commands executed during the current shell session. After the string is decoded, it is expanded via parameter expansion, command substitution, arithmetic expansion, and quote removal, subject to the value of the promptā€ vars shell option (see the description of the shopt command under SHELL BUILTIN COMMANDS below).

Further Reading

Also, if you want more information on the bash prompt, your first couple of stops should be IBM DeveloperWorks - Prompt Magic and IBM DeveloperWorks always does a good job with topics and Giles Orr has done a great job collecting a 'lot' of information on just about everthing related to the bash prompt. Good luck, and remember, too much of a good thing is still, too much. That applies to prompt cuteness as well.

  1. You can use any color you like, but I want something that works on a light or dark background. I generally work in a dark semi-transparent konsole schema with "linux-colors" like layout:
    Screenshot - konsole schema (147k)

Note: For footnotes, simply click on the black text to be taken back to the place in the document from whence you came, or click on the blue links to follow.

Developed in KDE3:

Quanta+ from KDE3 KDE3 now developed as Trinity Desktop