'\ r':找不到命令 - .bashrc / .bash_profile [duplicate]

时间:2022-01-11 20:11:00

This question already has an answer here:

这个问题在这里已有答案:

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

我有窗口,使用Cygwin,试图通过我的.bashrc文件永久设置JAVA_HOME。

.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 ~/.bashrcfi

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 个解决方案

#1


When all else fails in Cygwin...

当Cygwin中所有其他方法都失败时......

Try running the dos2unix command on the file in question.

尝试在有问题的文件上运行dos2unix命令。

It might help when you see error messages like this:

当您看到如下错误消息时,它可能会有所帮助:

-bash: '\r': command not found

-bash:'\ r':找不到命令

Windows style newline characters can cause issues in Cygwin.

Windows样式的换行符可能会导致Cygwin出现问题。

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

dos2unix命令修改换行符,因此它们与Unix / Cygwin兼容。

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

注意:dos2unix命令会修改文件,因此必要时请采取预防措施。

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.

Mac用户注意事项:Mac OS X上不存在dos2unix命令。

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

使用不同的工具查看各种解决方案的答案。


There is also a unix2dos command that does the reverse:

还有一个unix2dos命令执行相反的操作:

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

它修改了Unix换行符,因此它们与Windows工具兼容。

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

如果您使用记事本打开文件并且所有行一起运行,请尝试unix2dos filename。

#2


For those who don't have dos2unix installed (and don't want to install it):

Remove trailing \r character that causes this error:

删除导致此错误的尾随\ r \ n字符:

sed -i 's/\r$//' filename


Explanation:

Option -i is for in-place editing, we delete the trailing \r directly in the input file. Thus be careful to type the pattern correctly.

选项-i用于就地编辑,我们直接在输入文件中删除尾随\ r \ n。因此,请小心正确键入图案。

#3


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

对于使用Notepad ++的WINDOWS用户(使用v6.8.3检查),您可以使用选项 - 编辑 - > EOL转换 - > Unix / OSX格式更正特定文件

And save your file again.

并再次保存您的文件。

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

编辑:仍然适用于v7.5.1(2017年8月29日)

#4


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

我正在使用cygwin和Windows7,诀窍不是将set -o igncr放入你的.bashrc中,而是将整个SHELLOPTS放到Windows下的环境变量中。 (所以没有unix / cygwin ...)我认为它不起作用.bashrc因为“滴已经被吸了”正如我们在德语中所说的那样。 ;-)所以我的SHELLOPTS看起来像这样

braceexpand:emacs:hashall:histexpand:history:igncr:interactive-comments:monitor

#5


If you are using a recent Cygwin (e.g. 1.7), you can also start both your .bashrc and .bash_profile with the following line, on the first non commented line:

如果您使用的是最近的Cygwin(例如1.7),您还可以在第一个非注释行上使用以下行启动.bashrc和.bash_profile:

# ~/.bashrc: executed by bash(1) for non-login shells.# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)# for examples(set -o igncr) 2>/dev/null && set -o igncr; # this comment is needed

This will force bash to ignore carriage return (\r) characters used in Windows line separators.

这将强制bash忽略Windows行分隔符中使用的回车符(\ r)。

See http://cygwin.com/ml/cygwin-announce/2010-08/msg00015.html.

#6


SUBLIME TEXT

With sublime you just go to

有了崇高,你就去吧

View - > Line Endings -> (select)Unix

查看 - >行结尾 - >(选择)Unix

Then save the file. Will fix this issue.

然后保存文件。将解决这个问题。

Easy as that!

很容易!

#7


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

您还可以将选项-o igncr添加到bash调用,例如

bash -x -o igncr script.sh

#8


The error:

'\r': command not found

'\ r':找不到命令

is caused by shell not able to recognise Windows-like CRLF line endings (0d 0a) as it expects only LF (0a).

是由shell无法识别类似Windows的CRLF行结尾(0d 0a)引起的,因为它只需要LF(0a)。


Git

If you using Git on Windows, make sure you selected 'Checkout as-is' during setup. Then make sure that you run: git config --global core.autocrlf false, so Git will not perform any conversions when checking out or committing text files.

如果您在Windows上使用Git,请确保在安装过程中选择了“Checkout as-is”。然后确保你运行:git config --global core.autocrlf false,这样Git在签出或提交文本文件时不会执行任何转换。


dos2unix

If you're not using Git, you simply need to convert these affected files/scripts back into Unix-like line endings (LF), either by:

如果你不使用Git,你只需要将这些受影响的文件/脚本转换回类似Unix的行尾(LF),或者通过:

dos2unix ~/.bashrc

Note: The dos2unix command is part of dos2unix package.

注意:dos2unix命令是dos2unix包的一部分。

Ex/Vim editor + tr

If you've Vim installed, the following command should correct the files:

如果已安装Vim,则以下命令应更正文件:

ex +'bufdo! %! tr -d \\r' -scxa ~/.bash*

Useful alias: alias dos2unix="ex +'bufdo! %! tr -d \\\\r' -scxa".

有用的别名:alias dos2unix =“ex +'bufdo!%!tr -d \\\\ r'-scxa”。

tr

Here is the method by using tr:

这是使用tr的方法:

cat ~/.bashrc | tr -d '\r' > ~/.bashrc.fixed && mv -v ~/.bashrc.fixed ~/.bashrc

or:

tr -d '\r' < filename > new_filename

Note: The \r is equivalent to \015.

注意:\ r \ n相当于\ 015。


sed

You can try the following command:

您可以尝试以下命令:

sed -i'.bak' s/\r//g ~/.bash*

recode

The following aliases can be useful (which replaces dos2unix command):

以下别名可能很有用(它取代了dos2unix命令):

alias unix2dos='recode lat1:ibmpc'alias dos2unix='recode ibmpc:lat1'

Source: Free Unix Tools (ssh, bash, etc) under Windows.

来源:Windows下的免费Unix工具(ssh,bash等)。


perl

The following perl command can convert the file from DOS into Unix format:

以下perl命令可以将文件从DOS转换为Unix格式:

perl -p -i.bak -e 's/\015//g' ~/.bash*

Source: stripping the ^M.

来源:剥离^ M.


tofrodos

On Linux, like Ubuntu which doesn’t come standard with either dos2unix or unix2dos, you can install tofrodos package (sudo apt-get install tofrodos), and define the following aliases:

在Linux上,像Ubuntu一样,它没有标配dos2unix或unix2dos,你可以安装tofrodos包(sudo apt-get install tofrodos),并定义以下别名:

alias dos2unix=’fromdos’alias unix2dos=’todos’

Then use in the same syntax as above.

然后使用与上面相同的语法。


Vagrant

If you're using Vagrant VM and this happens for provisioning script, try setting binary option to true:

如果您正在使用Vagrant VM,并且这种情况发生在配置脚本中,请尝试将binary选项设置为true:

# Shell provisioner, see: https://www.vagrantup.com/docs/provisioning/shell.htmlconfig.vm.provision "shell" do |s|  s.binary = true # Replace Windows line endings with Unix line endings.  s.path = "script.sh"end

See: Windows CRLF to Unix LF Issues in Vagrant.

请参阅:Vagrant中的Windows CRLF到Unix LF问题。

#9


try execution the following command

尝试执行以下命令

vim .bashrc:set ff=unix:wq!

#10


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

根据这个要点,解决方案是创建一个〜/ .bash_profile(在HOME目录中),其中包含:

export SHELLOPTSset -o igncr

#11


Resolved with Notepad++ :

用Notepad ++解决:

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

1)菜单 - >编辑 - > EOL转换 - > Unix / OSX格式

2) Then Save

2)然后保存

Fixed

#12


folks who use notepad++(6.8.1) to ship shell scripts from windows to linux.

使用notepad ++(6.8.1)将shell脚本从windows发送到linux的人。

set the following in notepad ++Edit -> EOL Conversion -> Unix/OSX format

在记事本++ Edit - > EOL Conversion - > Unix / OSX格式中设置以下内容

#13


I had the same problem. Solution: I edit the file with pspad editor, and give it a unix format (Menu - Format -> UNIX)

我有同样的问题。解决方案:我用pspad编辑器编辑文件,并给它一个unix格式(菜单 - 格式 - > UNIX)

I believe you can set this format to your file with many other editors

我相信您可以使用许多其他编辑器将此格式设置为您的文件

#14


For the Emacs users out there:

  1. Open the file
  2. 打开文件

  3. M-x set-buffer-file-coding-system
  4. 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:

这会将文件中的新字符更新为unix样式。有关Emacs中“Newline Representation”的更多信息,请访问:

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.

注意:如果首选从命令行执行此操作,则可以将上述步骤制作为Emacs脚本。

#15


May be you used notepad++ for creating/updating this file.

可能是您使用notepad ++来创建/更新此文件。

EOL(Edit->EOL Conversion) Conversion by default is Windows.

EOL(编辑 - > EOL转换)默认情况下,转换为Windows。

Change EOL Conversion in Notepad++

在Notepad ++中更改EOL转换

Edit -> EOL Conversion -> Unix (LF)

#16


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.

在EditPlus中,您可以从文档→文件格式(CR / LF)→更改文件格式...菜单执行此操作,然后选择Unix / Mac OS X单选按钮。

#17


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

由于从Windows机器创建/下载的文件/脚本,可能会发生问题。请尝试转换为linux文件格式。

dos2unix ./script_name.sh

or

dos2unix ~/.bashrc

#18


1. Choice

'\ r':找不到命令 -  .bashrc / .bash_profile [duplicate]EditorConfig — is my choice.

EditorConfig - 是我的选择。


2. Relevance

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

这个答案与2018年3月相关。将来,这个答案的数据可能已经过时了。

Author of this answer personally used EditorConfig at March 2018.

这个答案的作者在2018年3月亲自使用了EditorConfig。


3. Limitations

You need to use one of supported IDE/editors.

您需要使用一个受支持的IDE /编辑器。


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. 简单的用法。我需要设置我的.editorconfig文件一次,我创建我的项目,→我可以忘记一些平台,样式和IDE特定的问题。

  3. Cross-platform, cross-languages, cross-IDE, cross-editors.
  4. 跨平台,跨语言,跨IDE,交叉编辑。


5. Example

5.1. Simple

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

我为Sublime Text→我的文本编辑器安装了EditorConfig插件。我在Sublime Text中编辑文件。

For example, I have sashacrlf.sh file:

例如,我有sashacrlf.sh文件:

echo "Sasha" &echo "Goddess!" &

I run this file in Cygwin:

我在Cygwin中运行这个文件:

$ bash sashacrlf.shSashasashacrlf.sh: line 1: $'\r': command not foundGoddess!

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

我在与sashacrlf.sh相同的项目中创建了一个文件.editorconfig。

[*.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.

这意味着,如果在编辑器中将任何带有.sh扩展名的文件保存在项目中,EditorConfig会为此文件设置UNIX行结尾。

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

我再次在文本编辑器中保存sashacrlf.sh。我再次运行sashacrlf.sh:

$ bash sashacrlf.shSashaGoddess!

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的文件中都有UNIX行结尾。我将这些行添加到我的.editorconfig文件中:

[*.{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.

现在,如果我使用.sh,.bashrc或.bash_profile扩展名保存任何文件,EditorConfig会自动为此文件设置UNIX行结尾。


6. Additional links

#19


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.)

如果您在Cygwin安装上安装了vim软件包,则可以使用vim来修复此问题,而无需查找和替换。按如下方式启动vim:vim filename.sh(通常也将其别名为vi)。然后,键入:set fileformat = unix,然后:wq(写入和退出)以保存更改。 (:让你进入vim的编辑模式。)

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

我建议使用dos2unix,因为vim可能更常用。

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.

但是,最佳做法是将文本编辑器设置为将计划在Unix / Linux环境中使用的文件保存为具有Unix文本格式。上面给出的Notepad ++的答案就是一个很好的例子。

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).

附加说明:如果您不确定文件的类型(DOS或Unix),可以使用文件filename.sh。这尤其有助于调试更加模糊的问题(例如,在导入来自Windows的SQL转储时出现编码问题)。

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

有关如何修改文本文件格式的其他选项,请参阅此IU知识库文章

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

有关Bash脚本和行结尾的更多背景信息,请参阅此*问题。

#1


When all else fails in Cygwin...

当Cygwin中所有其他方法都失败时......

Try running the dos2unix command on the file in question.

尝试在有问题的文件上运行dos2unix命令。

It might help when you see error messages like this:

当您看到如下错误消息时,它可能会有所帮助:

-bash: '\r': command not found

-bash:'\ r':找不到命令

Windows style newline characters can cause issues in Cygwin.

Windows样式的换行符可能会导致Cygwin出现问题。

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

dos2unix命令修改换行符,因此它们与Unix / Cygwin兼容。

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

注意:dos2unix命令会修改文件,因此必要时请采取预防措施。

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.

Mac用户注意事项:Mac OS X上不存在dos2unix命令。

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

使用不同的工具查看各种解决方案的答案。


There is also a unix2dos command that does the reverse:

还有一个unix2dos命令执行相反的操作:

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

它修改了Unix换行符,因此它们与Windows工具兼容。

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

如果您使用记事本打开文件并且所有行一起运行,请尝试unix2dos filename。

#2


For those who don't have dos2unix installed (and don't want to install it):

Remove trailing \r character that causes this error:

删除导致此错误的尾随\ r \ n字符:

sed -i 's/\r$//' filename


Explanation:

Option -i is for in-place editing, we delete the trailing \r directly in the input file. Thus be careful to type the pattern correctly.

选项-i用于就地编辑,我们直接在输入文件中删除尾随\ r \ n。因此,请小心正确键入图案。

#3


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

对于使用Notepad ++的WINDOWS用户(使用v6.8.3检查),您可以使用选项 - 编辑 - > EOL转换 - > Unix / OSX格式更正特定文件

And save your file again.

并再次保存您的文件。

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

编辑:仍然适用于v7.5.1(2017年8月29日)

#4


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

我正在使用cygwin和Windows7,诀窍不是将set -o igncr放入你的.bashrc中,而是将整个SHELLOPTS放到Windows下的环境变量中。 (所以没有unix / cygwin ...)我认为它不起作用.bashrc因为“滴已经被吸了”正如我们在德语中所说的那样。 ;-)所以我的SHELLOPTS看起来像这样

braceexpand:emacs:hashall:histexpand:history:igncr:interactive-comments:monitor

#5


If you are using a recent Cygwin (e.g. 1.7), you can also start both your .bashrc and .bash_profile with the following line, on the first non commented line:

如果您使用的是最近的Cygwin(例如1.7),您还可以在第一个非注释行上使用以下行启动.bashrc和.bash_profile:

# ~/.bashrc: executed by bash(1) for non-login shells.# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)# for examples(set -o igncr) 2>/dev/null && set -o igncr; # this comment is needed

This will force bash to ignore carriage return (\r) characters used in Windows line separators.

这将强制bash忽略Windows行分隔符中使用的回车符(\ r)。

See http://cygwin.com/ml/cygwin-announce/2010-08/msg00015.html.

#6


SUBLIME TEXT

With sublime you just go to

有了崇高,你就去吧

View - > Line Endings -> (select)Unix

查看 - >行结尾 - >(选择)Unix

Then save the file. Will fix this issue.

然后保存文件。将解决这个问题。

Easy as that!

很容易!

#7


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

您还可以将选项-o igncr添加到bash调用,例如

bash -x -o igncr script.sh

#8


The error:

'\r': command not found

'\ r':找不到命令

is caused by shell not able to recognise Windows-like CRLF line endings (0d 0a) as it expects only LF (0a).

是由shell无法识别类似Windows的CRLF行结尾(0d 0a)引起的,因为它只需要LF(0a)。


Git

If you using Git on Windows, make sure you selected 'Checkout as-is' during setup. Then make sure that you run: git config --global core.autocrlf false, so Git will not perform any conversions when checking out or committing text files.

如果您在Windows上使用Git,请确保在安装过程中选择了“Checkout as-is”。然后确保你运行:git config --global core.autocrlf false,这样Git在签出或提交文本文件时不会执行任何转换。


dos2unix

If you're not using Git, you simply need to convert these affected files/scripts back into Unix-like line endings (LF), either by:

如果你不使用Git,你只需要将这些受影响的文件/脚本转换回类似Unix的行尾(LF),或者通过:

dos2unix ~/.bashrc

Note: The dos2unix command is part of dos2unix package.

注意:dos2unix命令是dos2unix包的一部分。

Ex/Vim editor + tr

If you've Vim installed, the following command should correct the files:

如果已安装Vim,则以下命令应更正文件:

ex +'bufdo! %! tr -d \\r' -scxa ~/.bash*

Useful alias: alias dos2unix="ex +'bufdo! %! tr -d \\\\r' -scxa".

有用的别名:alias dos2unix =“ex +'bufdo!%!tr -d \\\\ r'-scxa”。

tr

Here is the method by using tr:

这是使用tr的方法:

cat ~/.bashrc | tr -d '\r' > ~/.bashrc.fixed && mv -v ~/.bashrc.fixed ~/.bashrc

or:

tr -d '\r' < filename > new_filename

Note: The \r is equivalent to \015.

注意:\ r \ n相当于\ 015。


sed

You can try the following command:

您可以尝试以下命令:

sed -i'.bak' s/\r//g ~/.bash*

recode

The following aliases can be useful (which replaces dos2unix command):

以下别名可能很有用(它取代了dos2unix命令):

alias unix2dos='recode lat1:ibmpc'alias dos2unix='recode ibmpc:lat1'

Source: Free Unix Tools (ssh, bash, etc) under Windows.

来源:Windows下的免费Unix工具(ssh,bash等)。


perl

The following perl command can convert the file from DOS into Unix format:

以下perl命令可以将文件从DOS转换为Unix格式:

perl -p -i.bak -e 's/\015//g' ~/.bash*

Source: stripping the ^M.

来源:剥离^ M.


tofrodos

On Linux, like Ubuntu which doesn’t come standard with either dos2unix or unix2dos, you can install tofrodos package (sudo apt-get install tofrodos), and define the following aliases:

在Linux上,像Ubuntu一样,它没有标配dos2unix或unix2dos,你可以安装tofrodos包(sudo apt-get install tofrodos),并定义以下别名:

alias dos2unix=’fromdos’alias unix2dos=’todos’

Then use in the same syntax as above.

然后使用与上面相同的语法。


Vagrant

If you're using Vagrant VM and this happens for provisioning script, try setting binary option to true:

如果您正在使用Vagrant VM,并且这种情况发生在配置脚本中,请尝试将binary选项设置为true:

# Shell provisioner, see: https://www.vagrantup.com/docs/provisioning/shell.htmlconfig.vm.provision "shell" do |s|  s.binary = true # Replace Windows line endings with Unix line endings.  s.path = "script.sh"end

See: Windows CRLF to Unix LF Issues in Vagrant.

请参阅:Vagrant中的Windows CRLF到Unix LF问题。

#9


try execution the following command

尝试执行以下命令

vim .bashrc:set ff=unix:wq!

#10


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

根据这个要点,解决方案是创建一个〜/ .bash_profile(在HOME目录中),其中包含:

export SHELLOPTSset -o igncr

#11


Resolved with Notepad++ :

用Notepad ++解决:

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

1)菜单 - >编辑 - > EOL转换 - > Unix / OSX格式

2) Then Save

2)然后保存

Fixed

#12


folks who use notepad++(6.8.1) to ship shell scripts from windows to linux.

使用notepad ++(6.8.1)将shell脚本从windows发送到linux的人。

set the following in notepad ++Edit -> EOL Conversion -> Unix/OSX format

在记事本++ Edit - > EOL Conversion - > Unix / OSX格式中设置以下内容

#13


I had the same problem. Solution: I edit the file with pspad editor, and give it a unix format (Menu - Format -> UNIX)

我有同样的问题。解决方案:我用pspad编辑器编辑文件,并给它一个unix格式(菜单 - 格式 - > UNIX)

I believe you can set this format to your file with many other editors

我相信您可以使用许多其他编辑器将此格式设置为您的文件

#14


For the Emacs users out there:

  1. Open the file
  2. 打开文件

  3. M-x set-buffer-file-coding-system
  4. 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:

这会将文件中的新字符更新为unix样式。有关Emacs中“Newline Representation”的更多信息,请访问:

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.

注意:如果首选从命令行执行此操作,则可以将上述步骤制作为Emacs脚本。

#15


May be you used notepad++ for creating/updating this file.

可能是您使用notepad ++来创建/更新此文件。

EOL(Edit->EOL Conversion) Conversion by default is Windows.

EOL(编辑 - > EOL转换)默认情况下,转换为Windows。

Change EOL Conversion in Notepad++

在Notepad ++中更改EOL转换

Edit -> EOL Conversion -> Unix (LF)

#16


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.

在EditPlus中,您可以从文档→文件格式(CR / LF)→更改文件格式...菜单执行此操作,然后选择Unix / Mac OS X单选按钮。

#17


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

由于从Windows机器创建/下载的文件/脚本,可能会发生问题。请尝试转换为linux文件格式。

dos2unix ./script_name.sh

or

dos2unix ~/.bashrc

#18


1. Choice

'\ r':找不到命令 -  .bashrc / .bash_profile [duplicate]EditorConfig — is my choice.

EditorConfig - 是我的选择。


2. Relevance

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

这个答案与2018年3月相关。将来,这个答案的数据可能已经过时了。

Author of this answer personally used EditorConfig at March 2018.

这个答案的作者在2018年3月亲自使用了EditorConfig。


3. Limitations

You need to use one of supported IDE/editors.

您需要使用一个受支持的IDE /编辑器。


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. 简单的用法。我需要设置我的.editorconfig文件一次,我创建我的项目,→我可以忘记一些平台,样式和IDE特定的问题。

  3. Cross-platform, cross-languages, cross-IDE, cross-editors.
  4. 跨平台,跨语言,跨IDE,交叉编辑。


5. Example

5.1. Simple

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

我为Sublime Text→我的文本编辑器安装了EditorConfig插件。我在Sublime Text中编辑文件。

For example, I have sashacrlf.sh file:

例如,我有sashacrlf.sh文件:

echo "Sasha" &echo "Goddess!" &

I run this file in Cygwin:

我在Cygwin中运行这个文件:

$ bash sashacrlf.shSashasashacrlf.sh: line 1: $'\r': command not foundGoddess!

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

我在与sashacrlf.sh相同的项目中创建了一个文件.editorconfig。

[*.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.

这意味着,如果在编辑器中将任何带有.sh扩展名的文件保存在项目中,EditorConfig会为此文件设置UNIX行结尾。

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

我再次在文本编辑器中保存sashacrlf.sh。我再次运行sashacrlf.sh:

$ bash sashacrlf.shSashaGoddess!

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的文件中都有UNIX行结尾。我将这些行添加到我的.editorconfig文件中:

[*.{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.

现在,如果我使用.sh,.bashrc或.bash_profile扩展名保存任何文件,EditorConfig会自动为此文件设置UNIX行结尾。


6. Additional links

#19


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.)

如果您在Cygwin安装上安装了vim软件包,则可以使用vim来修复此问题,而无需查找和替换。按如下方式启动vim:vim filename.sh(通常也将其别名为vi)。然后,键入:set fileformat = unix,然后:wq(写入和退出)以保存更改。 (:让你进入vim的编辑模式。)

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

我建议使用dos2unix,因为vim可能更常用。

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.

但是,最佳做法是将文本编辑器设置为将计划在Unix / Linux环境中使用的文件保存为具有Unix文本格式。上面给出的Notepad ++的答案就是一个很好的例子。

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).

附加说明:如果您不确定文件的类型(DOS或Unix),可以使用文件filename.sh。这尤其有助于调试更加模糊的问题(例如,在导入来自Windows的SQL转储时出现编码问题)。

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

有关如何修改文本文件格式的其他选项,请参阅此IU知识库文章

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

有关Bash脚本和行结尾的更多背景信息,请参阅此*问题。