NOTE: the steps below assumes you have a Bash shell. If you are using a Unix machine (Mac or Linux) and are not sure what type oh shell you have, it’s probably Bash.
In most systems, by default, the prompt in the terminal (or command window) displays the hostname and the working directory but this prompt can be easily customized to display other more relevant information to you and even styled!
The information about how the prompt must be displayed is stored in the environment shell variable PS1
– there are other variables but for most simple cases, PS1
is enough.
Check the value of PS1
in your system with:
echo $PS1
the output will look something like this:
\\u@\h \w]\\$
and in the screen this is decoded to:
myuser@hostname ~/projects $
Scroll down to see a list of the backslash escaped characters that can be used and what they mean.
Start testing prompt changes
It’s always safer to play around with options until you are satisfied with the result before saving the changes permanently.
First, save your current configuration as follows:
OLD_PS1=$PS1
Then you can test new changes to the prompt by using the export
command to set PS1
temporarily:
export PS1="[\d \u]: "
This will change your prompt right away but it will not save the changes permanently (if you log out and log back in the prompt will go back to original set). Keep adding/removing options until you find one combination you like.
Add colors to your prompt
Yup, that’s right. you can add colors and style to your prompt. To do that, you have to indicate a start and end of color information with \e[
and \e[m
, respectively.
Here’s a list of color codes:
| Color | Code |
| Black | 0;30 |
| Blue | 0;34 |
| Green | 0;32 |
| Cyan | 0;36 |
| Red | 0;31 |
| Purple | 0;35 |
| Brown | 0;33 |
Same idea as above: keep testing before saving the changes permanently (fair warning: this might turn into a time sucker).
The set below:
export PS1="\e[0;34m[\d \e[m \e[0;35m \u@\h]: \e[m"
will be displayed as:
[Sun Jan 06 myuser@myhost]:
Bold a section by adding 1 to the color as in:
export PS1="\e[0;34m[\d \e[m \e[01;35m \u@\h]: \e[m"
Locate where PS1 is set in your system
Different Unix distributions set this variable in different places so one simple way to find out is to check both .bashrc
and .bash_profile
files:
cat .bashrc
cat .bash_profile
It’s usually in .bashrc
, among a several other configurations.
Modify the content of PS1
Open .bashrc with your preferred terminal text editor as such:
vi .bashrc
or
nano .bashrc
And look for the block where PS1
is set. Apply your changes there, save and exit the text editor. You might need to log out and log back in the workstation to see the changes.
NOTE: Be careful when editing files such as
.bashrc
and.bash_profile
. Only change things you are 100% sure you know what they are or you risk messing up your system big time!
Full list of backslash escaped characters to use in the prompt:
- \a : an ASCII bell character (07)
- \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 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 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
Other references:
- How to: change /setup bash custom prompt (PS1)
- BASH Shell Change The Color of Shell Prompt on Linux or UNIX
- How to Customize your Terminal Prompt – for Mac OS
- Easy Bash Prompt Generator: choose the options you want and this tool displays the code for PS1
The post Customize your terminal prompt with colors was originally published at flaviabastos.ca
Top comments (0)