UnixLinux 执行 shell 报错:“$'': 未找到命令” 的解决办法

大多数情况下,shell 脚本在 windows 上编写导致的换行问题是主要原因。具体来说,windows 使用

crlf
\r\n
)作为换行符号,而 unix/linux 使用
lf
\n
)。

名称解释

缩写 全称 ASCII转义 说明 CR Carriage Return \\r 回车 LF Linefeed \\n 换行,Unix/Linux 的换行符 CRLF Carriage Return & Linefeed \\r\\n 回车并换行,Windows 的换行符

方法一(推荐):使用 vim 转换为 Unix 换行

# 测试脚本
$ cat windows.sh
#!/usr/bin/env bash
date

重现报错

$ sh windows.sh windows.sh:行2: $'date\r': 未找到命令

查看文件格式信息

$ file windows.sh windows.sh: a /usr/bin/env bash\015 script, ASCII text executable, with CRLF line terminators

转换为 Unix 换行

$ vim windows.sh :set ff=unix :wq

再次查看文件格式信息

$ file windows.sh windows.sh: a /usr/bin/env bash script, ASCII text executable

方法二:使用 dos2unix

# 安装 dos2unix
$ yum install dos2unix

转换为 unix 格式

$ dos2unix windows.sh dos2unix: converting file windows.sh to Unix format ...

转换为 dos 格式

$ unix2dos linux.sh unix2dos: converting file linux.sh to DOS format ...

方法三:删除回车(\r)符号

# tr 删除 \r 回车符号,^M 终端输入为Ctrl+V和Ctrl+M
$ cat windows.sh | tr -d "^M" > windows2unix.sh

sed 删除 \r 回车符号,^M 终端输入为Ctrl+V和Ctrl+M

$ sed -i "s/^M//g" windows.sh

方法四:使用文本编辑器工具转换换行符号(如:atom、notepad++ 等)