/bin/sh^M: bad interpreter: No such file or directory 异常

问题:

经常遇到一个问题,在windows里面编辑好的文件,比如说sh脚本,传输到linux里面执行不成功,报错如下:

1
/bin/sh^M: bad interpreter: No such file or directory

分析

这是不同系统编码格式引起的:在windows系统中编辑的.sh文件可能有不可见字符,所以在Linux系统下执行会报以上异常信息。

解决方案

假设我们的文件是:command.sh

1
2
3
4
5
6
7
chmod 777 command.sh
vim command.sh
:set ff 或 :set fileformat //查看文件格式
//可以看到如下信息,上述情况无法执行肯定是 = dos
fileformat=dos 或 fileformat=unix
:set ff=unix 或 :set fileformat=unix //修改文件格式
wq //保存退出