2

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?

3
  • Personally I separate my prompt into 3 lines : blank line, username@hostname [working-dir], and the prompt where i actually type things. Like so paste.ubuntu.com/11918243 . Try this. Jul 22, 2015 at 5:29
  • @Serg How to separate prompt into lines? Jul 24, 2015 at 4:31
  • 1
    Here's a sample: paste.ubuntu.com/11928599 Notice the single quotes around the prompt. It starts with PS=' on one line, then next line with [!] for numbering and user@hostname , and then the prompt with space and single-quote. I change my prompt quite frequently, so don't get confused , >_ is same as $ in previous example. There's really nothing special, just open sigle quotes, create 3 lines, close it Jul 24, 2015 at 4:44

1 Answer 1

7

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.

PS1='... \D{%a %F %T %Z} ...'

See http://www.gnu.org/software/bash/manual/bash.html#Controlling-the-Prompt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .