如何解決執行 bash script 時遇到 " '\r': command not found" 的問題

Posted on Wed 24 April 2019 in Tips

前言

在使用WSL執行 bash script 有時會遇到像這樣的錯誤:

-bash: '\r': command not found

經過搜尋之後在stackoverflow找到解答,該錯誤訊息是因為 Windows 的 newline 符號與 Linux 不同所導致,由於我是在 Windows 的環境下編寫 bash script,然後在 WSL 執行,所以會遇到此錯誤。

解決方法

由 stackoverflow 提供的解答,方法為在 WSL 之中安裝dos2unix,以 Debian/Ubuntu 為範例,安裝方法為:

sudo apt-get install dos2unix

使用方法為:

dos2unix [options] [file ...] [-n infile outfile ...]

就能夠把 script 轉換成 bash 可以正確讀取的格式。

分享到: DiasporaTwitterFacebookLinkedInHackerNewsEmailReddit