解决Bash 脚本换行符问题
最近写了一个shell脚本,,是在Windows系统下写的,编写完成后复制到Linux系统中执行时报错了,大概是这样,,,
1 | build.sh: line 3: $'\r': command not found |
原因是换行符格式问题导致的,Windows系统中用的是 \r\n
换行,Linux系统则用 \n
,在Linux 系统中会将 Windows风格的换行符中的 \r
视为普通字符,从而导致错误,,
解决方法很简单,使用sed 删除每一行末尾的 \r
就可以了
1 | sed -i -e 's/\r$//' build.sh |
或者用dos2unix工具进行转换,dos2unix build.sh
All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.
Comments