My system has Ubuntu Server 12.04 installed in text-mode (no X-Window) and a command prompt that is defined in my ~/.bashrc
file by following line:
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w \[\033[01;35m\] \[$(date "+%a %F %T %Z")\] \[\033[00m\]\$ '
It looks like:
username@hostname:~ Wed Jul 2015-07-22 18:00:12 JST $
If I press ↑ previous command in my history is displayed in front of above promp, e.g.:
username@hostname:~ Wed Jul 2015-07-22 18:00:12 JST $ vim /home/username/~/.bshrc
If I press ↑ second time, the previous command in the the history is displayed, but it is over written on some part of my prompt, i.e.:
username@hostname:~ Wedsudo su
Note that Jul 2015-07-22 18:00:12 JST $
is over written!
Also, it is not always over written by second command in the history. Some time first, second, third previous commands are displayed properly in front of complete prompt, one after another, and suddenly forth previous command is over written on last part of command prompt!
Always, the last part of command prompt is over written after day of week, i.e.
Wed
and this position is fixed if I press ↑ or ↓.If I run the command or press Ctrl+C, the command prompt is displayed completely again.
Any one can help to solve this problem?
That fails because you enclosed
$(date ...)
within\[
and\]
. Remove those and it should work as expected.\[ ... \]
, in the prompt, is used to tell bash that the characters within will not add to the length of the prompt. You need this around terminal escapes like\033[01;32m
because this only makes the following text a different colour, but those 8 bytes will not add to the length of the prompt; they are instead eaten by the terminal.On a side note; bash can already put the date in the prompt for you, without having to run the
date
command.See http://www.gnu.org/software/bash/manual/bash.html#Controlling-the-Prompt