win系统使用WSL写shell,运行出错:syntax error near unexpected token `$’do\r”

notepad++下面改写shell,运行出错:syntax error near unexpected token `$’do\r”

问题

在win系统下,用notepad++写了个简单的shell脚本,内容如下:
win系统使用WSL写shell,运行出错:syntax error near unexpected token `$’do\r”
但是运行出错:
win系统使用WSL写shell,运行出错:syntax error near unexpected token `$’do\r”

解决过程

1.网上搜了下,参考:linux shell脚本报错:syntax error near unexpected token `

然后去notepad++中,视图->显示符号->显示所有符号,然后就看到了预料到的,所有的回车换行都是windows的CR LF:
win系统使用WSL写shell,运行出错:syntax error near unexpected token `$’do\r”
得知是Linux和windows下的回车换行符不兼容的问题。

所以,剩下的就是去将windows下面的CR LF,转换为Linux下面的LF,即可。

此处,不需要再麻烦地去用其他dos2unix等工具了,notepad++就可以帮忙搞定:

编辑->文档格式转换->转换为UNIX格式:
win系统使用WSL写shell,运行出错:syntax error near unexpected token `$’do\r”
即可将所有的 CR LF,转换为LF:
win系统使用WSL写shell,运行出错:syntax error near unexpected token `$’do\r”
所以,越加发现notepad++的强大之处了。

然后再运行此shell脚本,即可正常运行了。