After updating bashrc in cygwin the cursor starts at column 0 instead of after the prompt

2

The issue is that, if I update PS1 in bashrc, I see that by default the cursor gets placed at column 0 instead of after the prompt, and if I type anything it starts overwriting the prompt. I tested the same PS1 string manually by deleting the bashrc file, starting a new session and typing in the PS1 string in the terminal and it worked just fine.

I found the answer to this problem but it took me an entire day and I didn't find the exact answer after googling a lot. The basic issue was with the bashrc file format. In my case the file was in DOS format with the two character end of line. The fix was to convert the file from DOS to UNIX format and it started working after that. I however don't know how exactly the file format caused the issue, it wouldn't work even if "PS1='XXX'" was the only thing in the entire file, without any new lines.

The following link gives information about how to convert the file: http://cs.nyu.edu/~yap/prog/cygwin/FAQs.html#bashrc

Rev

Posted 2013-09-30T05:51:45.480

Reputation: 21

Answers

0

The problem is, as you said, the DOS format. More specifically: the DOS newline format.

  • DOS newline format is a string of two characters: the carriage return character followed by the line feed character.
  • Unix/Linux newline format is just one character: the line feed character.

The line feed character is often written as \n while the carriage return character is written as \r.

When you have DOS formatted .bashrc file with a PS1 declaration and bash reads the file it will read the following:

PS1='XXX'\r\n

For bash The line ends just before \n. So the variable PS1 is defined as 'XXX'\r. That is the string XXX followed by a carriage return. Carriage return means to return the carriage (the blinking cursor) to the beginning of the line. Bash does exactly that.

Read more about the newline problematic in the wikipedia article about newline.


For a quick check whether a file uses dos or unix/linux newlines you can use the file command:

$ file bashrc
bashrc: ASCII text
$ file bashrc_dosnewlines 
bashrc_dosnewlines: ASCII text, with CRLF line terminators

CR is Carriage Return and LF is Line Feed.

For a detailed check use a hex dumper, for example xxd:

$ xxd bashrc
0000000: 5053 313d 2758 5858 270a                 PS1='XXX'.
$ xxd bashrc_dosnewlines 
0000000: 5053 313d 2758 5858 270d 0a              PS1='XXX'..

Short explanation: the output has three columns. The first column is the offset in the file. The second and following columns are the hexadecimal representation of the file. The last column is the human readable representation of the file. Dots are used for characters which have no sensible human readable representation.

Every pair of hexadecimal digits stands for a byte, which in the case of ASCII encoding stands for a character. For example 50 is P and 53 is S. Look up the ASCII codes for the other characters.

0a is \n and 0d0a is \r\n. Whenever you see a bunch of 0d0as in your file that is a sign that your file is using dos newlines.

lesmana

Posted 2013-09-30T05:51:45.480

Reputation: 14 930