how to change the bash command prompt to something useful and productive
If you work on Linux or Unix systems, then it is very likely that you have worked on terminals. A terminal can have different shells and it is very likely that you already have a favorite shell that you use. Bash is one the most popular command shells that is used, and it even comes standard on many distributions.
The command prompt is the text that you see when you are in a terminal or when you log into a system remotely through a command interface. This might have some useful information like the logged in user name and the working directory, or sometimes it might just have the name of the shell.
You can customize your command prompt to suit your needs and fancy. You can include many useful information in the prompt as well. The command prompt text is controlled by specific environment variables, namely PS1, PS2, PS3, and PS4. You can configure these just as you configure any other environment variables in Linux.
Most of the time, the only variable that needs to be set is PS1. There are many different values that you can use in PS1 that will print out various information. These are denoted by a back-slash (\) followed by a character.
Some of the useful information that can be shown as part of the command prompt includes date, time, hostname, user info etc. Find some of the commonly used ones along with the corresponding variables:
Date
\d: The date is printed in the format: "Weekday Month Date". Eg. Wed Dec 05
\D{format}: Custom format for date.
Time
\t: The current time in 24 hour HH:MM:SS format.
\T: The current time in 12 hour HH:MM:SS format
\A: The current time in 24 hour HH:MM format
\@: The current time in 12 hour HH:MM format
User
\u: The user name of the current logged in user
Host
\h: The short hostname. Only the first part of the hostname, upto the first '.' is printed.
\H: The full host name, whatever it is set to.
Directory
\w: The current working directory
\W: The base name of the current working directory
Command
\j: The number of jobs that is currently managed by the shell.
Version
\v: The current version of bash being used
\V: Complete version of the bash, including the short version and the patch level.
Miscellaneous
\a: an ASCII bell character
\n: new line character
\r: carriage return character
Let us take a look at some of the examples where these can be used and some of the useful prompts that you can use.
export PS1='[\u \t] \w \$ '
export PS1='[\u:\H:\t:\d] $ '
export PS1='[\t] \u@\H $ '
export PS1='[\t][\j] \u@\h:\w $ '
All of these can also be set for other command prompt variables, such as PS2, PS3 and PS4. While PS1 is the primary prompt, let us also see where the other prompts are used for:
PS2 : This is the secondary command prompt. The default value of this is usually >. You would usually see this command prompt when command is not complete and require entry in the next line or prompt, such when using the while or for loop command from the prompt. It is also sometimes referred to as the subshell prompt. Commands can be broken into multiple lines by using the \ character. PS3: This is the prompt that can be set in a script so that it shows an appropriate prompt for the select command inside the script. By default, this is usually set to #? . It is not necessary for this variable to be set in the environment. PS4: This prompt is useful when tracing scripts that is being executed. It marks the executed lines from the script. The default value for this prompt is +. Scripts can be traced or debugged while running, which will cause each command in the script to be printed out prefixed with this value, and then followed by the results command. Note that prompt may be printed multiple times to show the multiple levels of indirection, so it is best to keep it to one or two characters.