Trouble editing ~/.bash_profile: -bash: $'\r': command not found

3

1

I installed CygWin on Windows 7. Using Notepad, I edited my ~/.bash_profile file to add on to the PATH variable …

PATH="${PATH}:/cygdrive/c/apache-ant-1.8.2/bin"

Now, when I SSH in to my Windows machine, I get this error …

-bash: $'\r': command not found
-bash: $'\r': command not found
-bash: $'\r': command not found
-bash: $'\r': command not found
-bash: $'\r': command not found
-bash: /home/dev/.bash_profile: line 39: syntax error: unexpected end of file

and my PATH is not set. Anyone know how I can correct this?

Dave

Posted 2011-09-01T13:37:35.777

Reputation:

1Egads, don't ever use notepad to edit a unix file. It adds the \r character at line endings, and as bash reported, it isn't tolerated very well. Run d2u .bash_profile to fix it. Also, you should ask questions like this on superuser not stackoverflow. – None – 2011-09-01T13:40:38.397

Answers

6

As mentioned already, Windows uses \n\r as line separator. Now that you've got the \rs in your .bash_profile, however, you can use the utility dos2unix to remove them.

carlpett

Posted 2011-09-01T13:37:35.777

Reputation: 285

3Windows uses \r\n, not \n\r. – Keith Thompson – 2011-09-01T14:41:08.900

2

Probably notepad has added some $'\r', i. e. CR characters - as it is usual under Windows to have CRLF as line terminator.

The solution would be to use another editor which is capable to keep the line endings as they are, e. g. vim for Windows, or Notepad2.

glglgl

Posted 2011-09-01T13:37:35.777

Reputation: 1 327

This is not an answer. This is a comment. – musiKk – 2011-09-01T13:54:36.573

1I'm usually bothered by answers that should be comments, but this does imply that the OP should just stop using Notepad, and as such I think it qualifies as an answer (if not a very good one). – Pops – 2011-09-01T20:04:52.367

Extended the answer in order to qualify as one. Are there any alternatives to changing the editor? – glglgl – 2011-09-02T07:46:33.763

2

Depending on the environment you are using, you might be stuck with scripts containing CRLF, i.e. when using Visual SourceSafe, Perforce for Windows etc. to store your files. However, bash has a universal way to get around this problem:

export SHELLOPTS
set -o igncr

If you put this at the start of your .bash_profile or other startup script you may be using, you are out of trouble, as the CRLF sequences before the first command in your startup script do not hurt.

mapsonyllaer

Posted 2011-09-01T13:37:35.777

Reputation: 31