{"id":2257,"date":"2022-10-06T22:15:05","date_gmt":"2022-10-06T20:15:05","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=2257"},"modified":"2022-10-12T01:00:14","modified_gmt":"2022-10-11T23:00:14","slug":"fixes-bash-script-error-bin-bashm-bad-interpreter-no-such-file-or-directory","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2022\/10\/06\/fixes-bash-script-error-bin-bashm-bad-interpreter-no-such-file-or-directory\/","title":{"rendered":"[ Fixes ] Bash script Error \/bin\/bash^M: bad interpreter: No such file or directory"},"content":{"rendered":"\n<p>Have you ever been slapped with the<span class=\"has-inline-color has-vivid-red-color\"> \/bin\/bash^M: bad interpreter: No such file or directory error<\/span> when trying to run a bash script, be it from cron or command line?<\/p>\n\n\n\n<p>This issue is caused when you create scripts in<strong> Windows&nbsp;<\/strong>environments and then port them over to run on a <strong>Unix<\/strong>&nbsp;environment : <em>Unix uses different line endings therefore it can\u2019t properly read the file you created on Windows<\/em>.&nbsp;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<p>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&nbsp;<strong>\\n<\/strong>&nbsp;which is (decimal 10, hex 0A in ASCII).<\/p>\n\n\n\n<p>With Windows or DOS-style line endings, each line is terminated with a Carriage Return followed by a Line Feed character&nbsp;<strong>\\r\\n<\/strong>.<\/p>\n\n\n\n<p>The Bash script sees the Carriage Return&nbsp;<strong>\\r<\/strong>&nbsp;as&nbsp;<strong>^M<\/strong>. In this case, the Carriage Return (<strong>^M<\/strong>&nbsp;or&nbsp;<strong>\\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<pre class=\"wp-block-code\"><code>!\/bin\/bash   <\/code><\/pre>\n\n\n\n<p>becomes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>!\/bin\/bash^M<\/code><\/pre>\n\n\n\n<p>Since there is no interpreter, command, directory, or file called bash^M we get the<span class=\"has-inline-color has-vivid-red-color\"><em><strong> bad interpreter: No such file or directory error.<\/strong><\/em><\/span><\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h5>How to fix it ?<\/h5>\n\n\n\n<p>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<ul><li><strong>This can be done using the&nbsp;<em><span class=\"has-inline-color has-vivid-cyan-blue-color\">dos2unix<\/span><\/em>&nbsp;program to convert the Carriage Return characters:<\/strong><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ dos2unix filename<\/code><\/pre>\n\n\n\n<p>You can&nbsp;<a rel=\"noreferrer noopener\" href=\"http:\/\/dos2unix.sourceforge.net\/\" target=\"_blank\">download the dos2unix program from this location<\/a>.<\/p>\n\n\n\n<ul><li><strong>Correct the problem from the source.<\/strong><\/li><\/ul>\n\n\n\n<p>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<p>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&nbsp;<strong>Windows (CR LF)<\/strong>. To change it either:<\/p>\n\n\n\n<ol><li>Go to&nbsp;<strong>Settings &gt; Preferences<\/strong><\/li><li>Select&nbsp;<strong>New Document<\/strong><\/li><li>In the Format (Line ending) section select&nbsp;<strong>Unix (LF)<\/strong><\/li><li>Click the&nbsp;<strong>Close<\/strong>&nbsp;button to save preferences<\/li><\/ol>\n\n\n\n<p>or<\/p>\n\n\n\n<ol><li>Right-click on the&nbsp;<strong>Windows (CR LF)<\/strong>&nbsp;label on the bottom right of the screen to trigger the context menu.<\/li><li>Select&nbsp;<strong>Unix LF<\/strong><\/li><\/ol>\n\n\n\n<p><strong>PS<\/strong>: <em>Each editor has their personalized ways of changing the Line ending format which should be shown in the bottom right corner of the editor window. Usually Left or right-clicking the label should display the options you have.<\/em><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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? This issue is caused when you create scripts in Windows&nbsp;environments and then port them over to run on a Unix&nbsp;environment : Unix uses different line [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2276,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[172,611,610,257,609,612,161,217],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2257"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=2257"}],"version-history":[{"count":2,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2257\/revisions"}],"predecessor-version":[{"id":2278,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2257\/revisions\/2278"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/2276"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=2257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=2257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=2257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}