IT 1100 Introduction to Operating Systems
Chapter 13 Customizing the Prompt
Displaying the Prompt
echo $PS1
Here are some common escape codes used:
\d
displays current date\h
displays the hostname\u
displays the username\w
displays the current working directory
Backing up the current prompt
We create a new variable, then copy the PS1 variable to it
ps1_old="$PS1"
Creating a new prompt
PS1=\u@\h:\w\$
This will make more sense after you have read the chapter, but to change your PS1 prompt using an alias contains no spaces outside of the quotes and requires alternating quotation marks:
alias prompt1="PS1='\u@\h:\w\$'"
You can even nest a command inside the prompt
alias prompt2="PS1='$(date) \u@\h:\w\$'"
Adding Color to Prompts
Refer to Table 13-2 for color schemes and their associated escape codes.
For example, \033[0;30m
is black
\033[0;34m
is blue
Shortcut: instead of using \033
we can simply use \e
Adding Color to Prompts
As an example, a simple prompt like:
PS1='\[\033[1;32m\][\u@\h \W]\$\[\033[0m\] '
is the same as
PS1='\[\e[1;32m\][\u@\h \W]\$\[\e[0m\] '
Can be broken down into these elements:
\[\e[1;32m] - an opening square bracket printed in green (1;32m)
[\u@h \W] - username@hostname and the basename of the current working directory
\$ - the prompt (a # if the UID is 0)
\[e[0m\] - the text reset escape signalling the end of the colour sequence.
Last Updated 03/27/2018