shell 脚本第1行写的是 #!/bin/bash
,执行时却报错 #!/bin/bash: not found
,很是奇怪,请问如何解决?
./deploy-development.sh
./deploy-development.sh: 1: ./deploy-development.sh: #!/bin/bash: not found
是文件编码引起的,出问题的脚本的文件编码是 UTF-8
$ file deploy-development.sh
deploy-development.sh: Bourne-Again shell script, UTF-8 Unicode (with BOM) text executable
没这个问题的脚本的文件编码是 ASCII
Bourne-Again shell script, ASCII text executable
将文件编码从 UTF-8 改为 ASCII 问题就解决了。
file -i 命令可以查看文件编码
$ file -i deploy-development.sh
deploy-development.sh: text/x-shellscript; charset=utf-8
通过 iconv 命令可以将文件编码从 utf-8 转换为 ascii
iconv -f utf-8 -t ascii//translit deploy-development.sh -o deploy-development.sh
dudu的回复给了我灵感,我试了不是编码问题,而是行尾序列问题。因为是用的windows编写shell,行尾序列是CRLF,改为LF重新保存就ok了