'\r': command not found - .bashrc / .bash_

2019-01-01 01:33发布

I have windows, using Cygwin, trying to set JAVA_HOME permanently through my .bashrc file.

.bashrc:

export PATH="$JAVA_HOME/bin:$PATH"  
export JAVA_HOME=$JAVA_HOME:"/cygdrive/c/Program Files (x86)/Java/jdk1.7.0_05"

.bash_profile:

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi

running cygwin:

-bash: $'\377\376if': command not found
-bash: $'then\r': command not found
: No such file or directorysu//.bashrc
-bash: /cygdrive/c/Users/jhsu//.bash_profile: line 3: syntax error near unexpected token `fi'
-bash: /cygdrive/c/Users/jhsu//.bash_profile: line 3: `fi'

I am not sure if I took the commands from a tutorial that was meant for another system or if I am missing a step. Or whitespace is causing my commands not to run properly.

I've looked at multiple similar questions but I haven't found one where the question has my error exactly.


My home path:

$ echo $HOME
/cygdrive/c/Users/jhsu
$ echo ~
/cygdrive/c/Users/jhsu/

So I believe the files should be placed in the correct spot.

19条回答
无色无味的生活
2楼-- · 2019-01-01 02:03

Resolved with Notepad++ :

1) Menu->Edit->EOL Conversion -> Unix/OSX Format

2) Then Save

Fixed

查看更多
与君花间醉酒
3楼-- · 2019-01-01 02:09

I am using cygwin and Windows7, the trick was NOT to put the set -o igncr into your .bashrc but put the whole SHELLOPTS into you environment variables under Windows. (So nothing with unix / cygwin...) I think it does not work from .bashrc because "the drops is already sucked" as we would say in german. ;-) So my SHELLOPTS looks like this

braceexpand:emacs:hashall:histexpand:history:igncr:interactive-comments:monitor
查看更多
情到深处是孤独
4楼-- · 2019-01-01 02:10

For the Emacs users out there:

  1. Open the file
  2. M-x set-buffer-file-coding-system
  3. Select "unix"

This will update the new characters in the file to be unix style. More info on "Newline Representation" in Emacs can be found here:

http://ergoemacs.org/emacs/emacs_line_ending_char.html

Note: The above steps could be made into an Emacs script if one preferred to execute this from the command line.

查看更多
忆尘夕之涩
5楼-- · 2019-01-01 02:12

As per this gist, the solution is to create a ~/.bash_profile (in HOME directory) that contains:

export SHELLOPTS
set -o igncr
查看更多
皆成旧梦
6楼-- · 2019-01-01 02:12

1. Choice

EditorConfig EditorConfig — is my choice.


2. Relevance

This answer is relevant for March 2018. In the future, the data from this answer may be obsolete.

Author of this answer personally used EditorConfig at March 2018.


3. Limitations

You need to use one of supported IDE/editors.


4. Argumentation

  1. Simply usage. I need set my .editorconfig file 1 time, where I create my project, → I can forget some platform-, style- and IDE-specific problems.
  2. Cross-platform, cross-languages, cross-IDE, cross-editors.

5. Example

5.1. Simple

I install EditorConfig plugin for Sublime Text → my text editor. I edit files in Sublime Text.

For example, I have sashacrlf.sh file:

echo "Sasha" &
echo "Goddess!" &

I run this file in Cygwin:

$ bash sashacrlf.sh
Sasha
sashacrlf.sh: line 1: $'\r': command not found
Goddess!

I create a file .editorconfig in same project as sashacrlf.sh.

[*.sh]
end_of_line = lf

It means, that if you save any file with .sh extension in your project in your editor, EditorConfig set UNIX line endings for this file.

I save sashacrlf.sh in my text editor again. I run sashacrlf.sh again:

$ bash sashacrlf.sh
Sasha
Goddess!

I can't get unexpected output in console.

5.2. Multiple file extensions

For example, I want to have UNIX line endings in all files with extensions .sh, .bashrc and .bash_profile. I add these lines to my .editorconfig file:

[*.{sh,bashrc,bash_profile}]
end_of_line = lf

Now, if I save any file with .sh, .bashrc or .bash_profile extension, EditorConfig automatically set UNIX line ending for this file.


6. Additional links

查看更多
与君花间醉酒
7楼-- · 2019-01-01 02:12

For WINDOWS users with Notepad++ (checked with v6.8.3) you can correct the specific file using the option - Edit -> EOL conversion -> Unix/OSX format

And save your file again.

Edit: still works in v7.5.1 (Aug 29 2017)

查看更多
登录 后发表回答