Here's my guide on creating a customized bash shell prompt. This guide is written by a Gentoo Linux user and as such is known to work with that distribution only. That said, most of the steps given should translate across to other distributions with little to no changes needed and I will try to point out spots that may need to be altered, and why.
To start with lets look at what kind of things we can include in the command prompt. First up is escape codes, these are bash built-ins that we have at our disposal to customize the command prompt
6.9 Controlling the Prompt
The value of the variable PROMPT_COMMAND is examined just before Bash prints each primary prompt. If PROMPT_COMMAND is set and has a non-null value, then the value is executed just as if it had been typed on the command line.
In addition, the following table describes the special characters which can appear in the prompt variables:
- \a
- A bell character.
- \d
- The date, in "Weekday Month Date" format (e.g., "Tue May 26").
- \D{format}
- 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 escape character.
- \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
- A newline.
- \r
- A carriage return.
- \s
- The name of the shell, the basename of $0 (the portion following the final slash).
- \t
- The time, in 24-hour HH:MM:SS format.
- \T
- The time, in 12-hour HH:MM:SS format.
- \@
- The time, in 12-hour am/pm format.
- \A
- The 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 + patchlevel (e.g., 2.00.0)
- \w
- The current working directory, with $HOME abbreviated with a tilde (uses the $PROMPT_DIRTRIM variable).
- \W
- The basename of $PWD, with $HOME abbreviated with a tilde.
- \!
- The history number of this command.
- \#
- The command number of this command.
- \$
- If the effective uid is 0, #, otherwise $.
- \nnn
- The character whose ASCII code is the octal value nnn.
- \\
- A backslash.
- \[
- Begin a sequence of non-printing characters. This could be used to embed a terminal control sequence into the prompt.
- \]
- End a sequence of non-printing characters.
So, for example if I wanted a simple prompt that told me the username I'm logged in as and the name of the computer I'd do this by entering the following into a command prompt:
PS1="\u@\H>"
The result of which would look like this:
adrien@labcomputer>_
Ok so that's the basics. Play mix'n match as desired to get the information you want. That's pretty much the basics of the customization but there is still a lot more you can actually do with your prompt.