寫了一個簡單的 shell 腳本,內容如下:
#!/bin/sh echo "test shell"
執行時卻報錯:
-bash: ./test.sh: /bin/sh^M: bad interpreter: No such file or directory
問題出在這個 .sh 檔是在 Windows 下編輯後上傳到 Linux 執行,檔案格式為 DOS 格式(換行含 ^M),而 Linux 只能執行 UNIX 格式的腳本。
用 vi 開啟檔案,在命令模式下輸入:
:set ff
會看到 fileformat=dos。


:set ff=unix
再用 :set ff 確認已變為 unix。


dos2unix test.sh
顯示 converting file test.sh to UNIX format ... 即完成。此後再執行 ./test.sh 便會正確輸出 test shell。