$‘\r‘: command not found的解决方法

问题说明

在Linux系统中,运行Shell脚本,出现了如下错误:

build.sh: line 1: $'\r': command not found

出现这样的错误,是因为Shell脚本在Windows系统编写时,每行结尾是\r\n,而在Linux系统中行每行结尾是\n,所以在Linux系统中运行脚本时,会认为\r是一个字符,导致运行错误。

问题解决

  • 安装 dos2unix
centos系统:yum install -y dos2unix

ubuntu系统:apt-get -y install dos2unix 
  • 格式转换
# dos2unix build.sh

出现以下字符,说明格式转换成功
dos2unix: converting file one-more.sh to Unix format ...

命令讲解

  • dos2unix

dos2unix是将Windows格式文件转换为Unix、Linux格式的实用命令

Windows格式文件的换行符为\r\n ,而Unix&Linux文件的换行符为\n

dos2unix命令其实就是将文件中的\r\n 转换为\n

命令语法:

dos2unix [options] [-c convmode] [-o file ...] [-n infile outfile ...]
  • unix2dos

而unix2dos则是和dos2unix互为孪生的一个命令

它是将Linux&Unix格式文件转换为Windows格式文件的命令

命令语法:

unix2dos [options] [-c convmode] [-o file ...] [-n infile outfile ...]

猜你喜欢

转载自blog.csdn.net/cljdsc/article/details/124864061