'\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:25

You can also add the option -o igncr to the bash call, e.g.

bash -x -o igncr script.sh
查看更多
泛滥B
3楼-- · 2019-01-01 02:27

In EditPlus you do this from the Document → File Format (CR/LF) → Change File Format... menu and then choose the Unix / Mac OS X radio button.

查看更多
骚的不知所云
4楼-- · 2019-01-01 02:27

If you have the vim package installed on your Cygwin install, you can use vim to fix this without find & replace. Start vim as follows: vim filename.sh (often it is aliased to vi also). Then, type :set fileformat=unix, then :wq (write & quit) to save your changes. (The : puts you in vim's edit mode.)

I recommend this over dos2unix since vim is probably more commonly installed.

However, it is probably a best practice to set your text editor to save files that you plan to use in a Unix/Linux environment to have a Unix text format. The answers given above for Notepad++ are a good example.

Additional note: If you are unsure what type a file is (DOS or Unix), you may use the file filename.sh. This can especially help in debugging more obscure issues (such as encoding issues when importing SQL dumps that come from Windows).

For other options on how to modify text file formatting, see this IU knowledge base article

More background information on Bash scripts and line endings is found on this StackOverflow question.

查看更多
呛了眼睛熬了心
5楼-- · 2019-01-01 02:27

When all else fails in Cygwin...

Try running the dos2unix command on the file in question.

It might help when you see error messages like this:

-bash: '\r': command not found

Windows style newline characters can cause issues in Cygwin.

The dos2unix command modifies newline characters so they are Unix / Cygwin compatible.

CAUTION: the dos2unix command modifies files in place, so take precaution if necessary.

If you need to keep the original file, you should back it up first.

Note for Mac users: The dos2unix command does not exist on Mac OS X.

Check out this answer for a variety of solutions using different tools.


There is also a unix2dos command that does the reverse:

It modifies Unix newline characters so they're compatible with Windows tools.

If you open a file with Notepad and all the lines run together, try unix2dos filename.

查看更多
柔情千种
6楼-- · 2019-01-01 02:27

SUBLIME TEXT

With sublime you just go to

View - > Line Endings -> (select)Unix

Then save the file. Will fix this issue.

Easy as that!

查看更多
梦寄多情
7楼-- · 2019-01-01 02:28

Issue maybe occured because of the file/script created/downloaded from a windows machine. Please try converting into linux file format.

dos2unix ./script_name.sh

or

dos2unix ~/.bashrc
查看更多
登录 后发表回答