By using the export PS1
command you can alter the way the bash prompt looks. You can do this right at the command line which will change it just for that session or you can place the config in your .bashrc
which loads every time you login to your bash shell. To reload your .bashrc
contents either start a new shell by doing bash
or logout and login again.
|
The first part defines colors. This is optional but makes it easier to create a pretty prompt. The export PS1
command at the end is what is actually changing the prompt. The backslash codes that can be used are:
\u
display the current username\h
display the hostname of the system\w
display the working directory\d
display the date\e
ASCII escape character\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\!
the history number of this command\#
the command number of this command\[
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
Examples
With color codes already defined
These examples assume the color codes at the top of this page have been added to the .bashrc
file.
Single color:
export PS1="\[$bldgrn\]\u@\h\w$ \[$txtrst\]"
user@localhost~/dir$
Two Colors:
export PS1="\[$bldgrn\]\u\[$txtrst\]@\[$bldred\]\h\[$txtrst\\w$"
user@localhost~/dir$
Without color codes defined
It’s not necessary to add the color codes. These examples show how to use inline color codes to achieve the same results.
Single color:
export PS1="\e[1;32m\u@\h\w$ \e[0m"
user@localhost~/dir$
Two Colors:
export PS1="\e[1;32m\u\e[0m@\e[1;31m\h\e[0m\w$"
user@localhost~/dir$
Comments