{"id":8714,"date":"2020-06-27T06:34:54","date_gmt":"2020-06-27T10:34:54","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=8714"},"modified":"2021-12-04T08:38:40","modified_gmt":"2021-12-04T08:38:40","slug":"bad-interpreter-no-such-file-or-directory","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/bad-interpreter-no-such-file-or-directory\/","title":{"rendered":"Bash script Error \/bin\/bash^M: bad interpreter: No such file or directory"},"content":{"rendered":"\n

Have you ever been slapped with the \/bin\/bash^M: bad interpreter: No such file or directory error when trying to run a bash script, be it from cron or command line?<\/p>\n\n\n\n

If your answer is yes then you have reached the right place. This error is more common than you think and is quite harmless but nevertheless, it is annoying.<\/p>\n\n\n\n

This issue is caused when you create scripts in Windows<\/a> environments and then port them over to run on a Unix<\/a> environment.<\/p>\n\n\n\n

Unix uses different line endings therefore it can’t properly read the file you created on Windows. Bash scripts on the other hand are quite sensitive to line-endings. This is not just limited to the script itself. it extends to the data that the script processes.<\/p>\n\n\n\n

With that in mind, it is important that one should have Unix-style line-endings, i.e., each line is terminated with a Line Feed character \\n<\/strong> which is (decimal 10, hex 0A in ASCII).<\/p>\n\n\n\n

With Windows or DOS-style line endings, each line is terminated with a Carriage Return followed by a Line Feed character \\r\\n<\/strong>.<\/p>\n\n\n\n

The Bash script sees the Carriage Return \\r<\/strong> as ^M<\/strong>. In this case, the Carriage Return (^M<\/strong> or \\r<\/strong>) is not treated as whitespace. and is appended to the line as text wherever it appears at line endings. So this:<\/p>\n\n\n\n

!\/bin\/bash<\/code><\/pre>\n\n\n\n

becomes:<\/p>\n\n\n\n

!\/bin\/bash^M<\/code><\/pre>\n\n\n\n

Since there is no interpreter, command, directory, or file called bash^M we get the bad interpreter: No such file or directory error.<\/p>\n\n\n\n

Solution to Fixing \/bin\/bash^M: bad interpreter: No such file or directory<\/h2>\n\n\n\n

There are several options one may have to solve this problem. Since we know ^M is an illegal character the simple solution is to get rid of it. We will just show you two simple ways to do it.<\/p>\n\n\n\n

1. This can be done using the dos2unix<\/em> program to convert the Carriage Return characters:<\/p>\n\n\n\n

$ dos2unix filename<\/code><\/pre>\n\n\n\n

You can download the dos2unix program from this location<\/a>.<\/p>\n\n\n\n

2. Correct the problem from the source. That is whichever editor (Sublime, Notepad++, VS Code, etc) you use in Windows you should be able to change the settings to use the Unix style line endings.<\/p>\n\n\n\n

For example in Notepad++ in the bottom right of the screen, you will be able to see the document format. By default, it will say Windows (CR LF)<\/strong>. To change it either:<\/p>\n\n\n\n