"export PS1=" for customizing shell prompting

by Smile   Last Updated December 07, 2017 11:02 AM

I wanted to customize my shell prompting to have time. So, I did export PS1='\t\w\$'.

My prompt now shows like 18:57:37~$. I don't know how to prepend it with username@hostname.

Also, I don't know how to change the color of the each parameter for \t, \w, and so on.

After all the testing how do I set it back to the default??

Finally, where does the export line go?? I looked in the ~/.profile, but there isn't the line export PS1='\t\w\$'.

Answers 1

PS1 is set in your ~/.bashrc. This file contains settings which will be applied in every interactive shell. An interactive Bash shell is what you get when you open a terminal in Ubuntu, unless you have set a different default shell for your user.

In an interactive shell, we need a prompt, and it's nice if the prompt gives us some useful info, like the current working directory, the current user and the hostname, as the Ubuntu PS1 does.

Here are the lines which set PS1 in the default version of .bashrc for my system, /etc/skel/.bashrc

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
        # We have color support; assume it's compliant with Ecma-48
        # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
        # a case would tend to support setf rather than setaf.)

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
unset color_prompt force_color_prompt

From PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' you can see that the escape codes for username and hostname are \u and \h respectively

zanna@toaster:~$ PS1="\u@\h"

If you want to add the time and current working directory:

zanna@toasterPS1="\u@\h \t \w "
zanna@toaster 10:43:32 ~ 

To get colours, you need to use the colour escape sequences. You can see some in the color_prompt assignment in .bashrc

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

For example, \033[01;34m is blue:

PS1 assignment

Oops! Now the text after is blue as well... better change it back to white:

PS1 with color reassignment

We should surround colour assignments with escaped square brackets, otherwise the Bash will think they are printing characters of the prompt and use them to calculate its size. This gives weird effects when you try to interact with your history, so here's the corrected version:

PS1="\[\033[01;34m\]\u@\h \t \w \[\033[00m\]"

When you have finished playing, you can return the prompt to default by closing the terminal and opening a new one ;) or by running

source ~/.bashrc

my PS1 back to normal

I set my PS1 like this using the code already in .bashrc, uncommenting #force_color_prompt=yes and changing the colour codes. Here you can see the lines I have changes to set it:

$ diff .bashrc /etc/skel/.bashrc 
< force_color_prompt=yes
> #force_color_prompt=yes
<     PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;35m\]\w\$\[\033[00m\] '
>     PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

(there are more lines changed after this, but they aren't relevant)

For a list of ANSI escape codes for colours and more stuff, see this guide to customising the prompt.

December 07, 2017 11:01 AM

Related Questions

problem setting up bash_profile

Updated February 20, 2017 10:02 AM

Prepend current git branch in terminal

Updated October 31, 2017 04:02 AM

change prompt of terminal, permanently | how

Updated November 21, 2017 12:02 PM

trim the terminal command prompt working directory

Updated February 19, 2017 00:02 AM