Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have played around with PS1 and PROMPT_COMMAND in bash to create a zsh-style right side prompt. I have a solution who almost works.

The problem is that if I write a long line of input, the second line overwrites the first one. The third line will appear nicely on a new line.

Maybe some line counter are of-by-one because my cursor movement, or is this a limitation/bug?

A simple example:

export PS1="prompt>\[\033[s\033[10C\]test\[\033[u\]"

Print prompt>, save position, move 10 characters to the left, print test, restore position.

The prompts looks nice and works perfectly, until i write more then one line of text.

Example 1, expected behaviour:

prompt>          test
prompt>ls        test
prompt>1 2 3 4 5 6 7 8 9 10 11 12 13
14 15 16 17 18 19 20 21 22 23 24 25
26 27 28

Example 2, current behaviour:

prompt>          test
prompt>ls        test
14 15 16 17 18 19 20 21 22 23 24 253
26 27 28
share|improve this question
possible duplicate of Home key acting strange in bash (tty and X) on long input strings – l0b0 Oct 4 '12 at 15:37
up vote 5 down vote accepted

Ah, of course. Test should not be counted as a visible character and should be included between \[ and \].

Working example:

export PS1="prompt>\[\033[s\033[10Ctest\033[u\]"

The reason was because if bash count test as a visible character it will assume it's left if the cursor and the calculation of available characters left on the current line will be off by four characters (length of 'test').

share|improve this answer
I will not accept my own answer if someone else want to give a better and more informative answer who explains the situation in more detail. – nsg Oct 4 '12 at 13:29
You pretty much got it - the \[ and \] are hints to bash to tell it how much physical space your prompt takes up, and since the test part is sort of floating outside your main prompt it shouldn't be included. – jw013 Oct 4 '12 at 14:13
@nsg You may want to accept your answer, there is nothing wrong with that and it will prevent your question from floating around as having no accepted answer. – Adrian Frühwirth Oct 18 '13 at 9:01
True, I had forgotten this one. Thanks for the reminder. – nsg Oct 25 '13 at 15:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.