I wanted to customize my shell prompt to include the time. So, I did export PS1='\t\w\$'
.
My prompt now looks 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\$'
.
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
From
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
you can see that the escape codes forusername
andhostname
are\u
and\h
respectivelyIf you want to add the time and current working directory:
To get colours, you need to use the colour escape sequences. You can see some in the color_prompt assignment in
.bashrc
For example,
\033[01;34m
is blue:Oops! Now the text after is blue as well... better change it back to white:
We should surround colour assignments with escaped square brackets, otherwise 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:
When you have finished playing, you can return the prompt to default by closing the terminal and opening a new one ;) or by running
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 changed to set it:(there are more lines changed after this, but they aren't relevant)
You could do the same, but add a
\t
into thecolor_prompt
line somewhere, for exampleFor a list of ANSI escape codes for colours and more stuff, see this guide to customising the prompt.
I neglected to answer the last part of your question.
I'm not sure whether you expected running
export VAR=val
would cause your~/.profile
to be automatically modified. Theexport
command never does this. Exporting a variable will only pass it into the environment of commands run from the current shell. When you exit the shell (and all of its child processes have exited), anything you exported from the shell is gone.If you want to set an environment variable permanently, you generally need to add it to
~/.profile
explicitly. Some scripts you might use to install software may modify your~/.profile
or other shell configuration files.But
PS1
does not need to be exported into the environment. At the start of my answer I said that in an interactive shell we need a prompt, and I meant that only interactive shells need prompts (because the prompt helps the user interact with the shell). No other command needsPS1
.You might be thinking that it could be useful for
PS1
to be passed to any child shells of the current shell. When you start an interactive shell within a shell by runningbash
, the new shell will not inherit the shell variables of the calling shell; only its environment variables. So, to pass variables to a child shell, we shouldexport
them.But exporting
PS1
will usually* fail to pass its value to a child shell, because it is reset by the shell's configuration files,/etc/bash.bashrc
and~/.bashrc
. So, closing the terminal (as I suggested earlier) is not necessary; even runningbash
will return your prompt to its usual form:(if you
exit
this shell, the edited prompt comes back)* I say usually, because, although non-interactive shells always unset
PS1
, an interactive Bash shell will preserve the value ofPS1
if it is set. This isn't obvious, because as shown in the preceding example it typically gets reset by the configuration files. We can uncover it by changingPS1
and then starting a new shell that doesn't read our config files:So, to conclude, there is no
export
line forPS1
in~/.profile
becausePS1
is not an environment variable and has no business being one, since only interactive shells need it, and for the reason that interactive shells needPS1
, it is set in~/.bashrc
, because unless told not to, all interactive Bash shellssource
~/.bashrc
, so~/.bashrc
doesn't need toexport
PS1
to be inherited by child shells (but if you do really want toexport
some value ofPS1
other than the one in your.bashrc
to a child shell, you can do so by preventing that shell from sourcing.bashrc
).I have been able to extend my answer thanks to Eliah Kagan, who explained how Bash treats PS1 here in chat and in more detail in this answer.