4.4.2 How do I customize my shell prompt?
On my machine, the prompt may look like this:
[stan@marie stan]$ _
Here "stan" is my login name, "marie" is the name of the computer, the second "stan" is the name of my current working directory, and "_" represents the cursor.
The prompt is set by the environmental variable called PS1. To display the current setting, I can use:
echo $PS1
The system-wide setting of the prompt (for all users on the system) is in the file /etc/bashrc which on my system contains such a line:
PS1="[\u@\h \W]\$ "
To customize the prompt, I can edit the file /etc/bashrc (as root) and insert almost any text inside the quotation marks. Here is the meaning of some special codes I may also choose to use:
\u - username of the current user (= $LOGNAME),
\h - the name of the computer running the shell (hostname),
\H - entire hostname,
\W - the base of the name of the current working directory,
\w - the full name of the current working directory,
\$ - display "$" for normal users and "#" for the root,
\! - history number of the current command,
\# - number of the current command (as executed in the current shell),
\d - current date,
\t - current time (24-hr),
\T - current time (12-hr) - bash 2.0 only,
\@ - current time (AM/PM format) - bash 2.0 only,
\s - name of the shell,
\a - sound alarm (beep),
\j - number of jobs the user has,
\n - new line,
\\ - backslash,
\[ - begin a sequence of non-printable characters,
\] - end a sequence of non-printable characters,
\nnn - the ASCII character corresponding to the octal number nnn.
$(date) - output from the date command (or any other command for that matter),
Here is an example on how to add colour. See the next chapter for details about colour:
PS1="\[\033[1;32m\][\u@\h \W]\$\[\033[0m\] "
There is also the second-level prompt, set by a variable called PS2. The shell uses the second level prompt when it expects additional input, and on my system the secondary prompt is "> ". I don't worry too much about PS2, but if I did I could set it the same way as PS1. There are even PS3 and PS4, but these are rarely seen.
Thank you from our readers like sqrt_-1@ezrs.com who wrote in with the following note:
“When I first started playing with the ANSI escape sequences, I had problems whenever the line I was typing in wrapped to the next line. The cursor would not move to the next line, overwriting the prompt; the line would wrap but overwrite itself below the prompt; etc. The I saw an extremely long (3 lines!) prompt definition, and it worked better that mine. It's because of \[\033[0m\] sprinkled throughout. The three line prompt is from www.dotfiles.com.”
Monstrous 3 line prompt by Robert:
export PS1='\[\033[0m\]\[\033[0;31m\].:\[\033[0m\]\[\033[1;30m\][\[\033[0m\]\[\033[0;28m\]Managing \033[1;31m\]\j\[\033[0m\]\[\033[1;30m\]/\[\033[0m\]\[\033[1;31m\]$(ps ax | wc -l | tr -d '\'' '\'')\[\033[0m\]\[\033[1;30m\] \[\033[0m\]\[\033[0;28m\]jobs.\[\033[0m\]\[\033[1;30m\]] [\[\033[0m\]\[\033[0;28m\]CPU Load: \[\033[0m\]\[\033[1;31m\]$(temp=$(cat /proc/loadavg) && echo ${temp%% *}) \[\033[0m\]\[\033[0;28m\]Uptime: \[\033[0m\]\[\033[1;31m\]$(temp=$(cat /proc/uptime) && upSec=${temp%%.*} ; let secs=$((${upSec}%60)) ; let mins=$((${upSec}/60%60)) ; let hours=$((${upSec}/3600%24)) ; let days=$((${upSec}/86400)) ; if [ ${days} -ne 0 ]; then echo -n ${days}d; fi ; echo -n ${hours}h${mins}m)\[\033[0m\]\[\033[1;30m\]]\[\033[0m\]\[\033[0;31m\]:.\n\[\033[0m\]\[\033[0;31m\].:\[\033[0m\]\[\033[1;30m\][\[\033[0m\]\[\033[1;31m\]$(ls -l | grep "^-" | wc -l | tr -d " ") \[\033[0m\]\[\033[0;28m\]files using \[\033[0m\]\[\033[1;31m\]$(ls --si -s | head -1 | awk '\''{print
* License

