bash '\r': 命令未找到 - .bashrc / .bash_profile
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11616835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'\r': command not found - .bashrc / .bash_profile
提问by Jasmine
I have windows, using Cygwin, trying to set JAVA_HOME
permanently through my .bashrc
file.
我有 Windows,使用 Cygwin,试图JAVA_HOME
通过我的.bashrc
文件永久设置。
.bashrc:
.bashrc:
export PATH="$JAVA_HOME/bin:$PATH"
export JAVA_HOME=$JAVA_HOME:"/cygdrive/c/Program Files (x86)/Java/jdk1.7.0_05"
.bash_profile:
.bash_profile:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
running cygwin:
运行cygwin:
-bash: $'76if': 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.
所以我相信文件应该放在正确的位置。
回答by jahroy
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': command not found
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 用户注意事项:该dos2unix
命令在 Mac OS X 上不存在。
Check out this answerfor 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
.
回答by Michael P. Bazos
For those who don't have dos2unixinstalled (and don't want to install it):
对于那些没有安装dos2unix(并且不想安装它)的人:
Remove trailing \r
character that causes this error:
删除\r
导致此错误的尾随字符:
sed -i 's/\r$//' filename
Explanation:
说明:
Option -i
is for in-placeediting, we delete the trailing \r
directly in the input file. Thus be careful to type the pattern correctly.
选项-i
用于就地编辑,我们\r
直接在输入文件中删除尾随。因此要小心正确地输入模式。
回答by Ray Oei
For WINDOWS(shell) users with Notepad++(checked with v6.8.3) you can correct the specific file using the option - Edit -> EOL conversion -> Unix/OSX format
对于使用Notepad++(用 v6.8.3 检查)的WINDOWS(shell) 用户,您可以使用选项更正特定文件 - 编辑 -> EOL 转换 -> Unix/OSX 格式
And save your file again.
并再次保存您的文件。
Edit: still works in v7.5.1 (Aug 29 2017)
编辑:仍然适用于 v7.5.1(2017 年 8 月 29 日)
回答by DietmarTheMachine
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
回答by Alvaro Joao
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!
就这么简单!
回答by Alvaro Joao
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 忽略\r
Windows 行分隔符中使用的回车 ( ) 字符。
See http://cygwin.com/ml/cygwin-announce/2010-08/msg00015.html.
请参阅http://cygwin.com/ml/cygwin-announce/2010-08/msg00015.html。
回答by user1010997
You can also add the option -o igncr
to the bash call, e.g.
您还可以将选项添加-o igncr
到 bash 调用中,例如
bash -x -o igncr script.sh
回答by kenorb
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,请确保在安装过程中选择了“按原样结帐”。然后确保您运行: git config --global core.autocrlf false
,以便 Git 在检出或提交文本文件时不会执行任何转换。
dos2unix
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
Ex/Vim 编辑器 + 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
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
相当于\015
。
sed
sed
You can try the following command:
您可以尝试以下命令:
sed -i'.bak' s/\r//g ~/.bash*
recode
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
perl
The following perl
command can convert the file from DOS into Unix format:
以下perl
命令可以将文件从 DOS 转换为 Unix 格式:
perl -p -i.bak -e 's/5//g' ~/.bash*
Source: stripping the ^M.
来源:剥离 ^M。
tofrodos
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.html
config.vm.provision "shell" do |s|
s.binary = true # Replace Windows line endings with Unix line endings.
s.path = "script.sh"
end
回答by U2647
try execution the following command
尝试执行以下命令
vim .bashrc
:set ff=unix
:wq!