{"id":236,"date":"2013-01-30T18:10:05","date_gmt":"2013-01-30T18:10:05","guid":{"rendered":"http:\/\/www.vidarholen.net\/contents\/blog\/?p=236"},"modified":"2017-06-11T01:39:26","modified_gmt":"2017-06-11T01:39:26","slug":"making-bash-run-doswindows-crlf-eol-scripts","status":"publish","type":"post","link":"https:\/\/www.vidarholen.net\/contents\/blog\/?p=236","title":{"rendered":"Making bash run DOS\/Windows CRLF EOL scripts"},"content":{"rendered":"<p>If you for any reason use a Windows editor to write scripts, it can be annoying to remember to convert them and bash fails in mysterious ways when you don&#8217;t. Let&#8217;s just get rid of that problem once and for all:<\/p>\n<pre>\r\ncat > $'\/bin\/bash\\r' << \"EOF\"\r\n#!\/usr\/bin\/env bash\r\nscript=$1\r\nshift\r\nexec bash <(tr -d '\\r' < \"$script\") \"$@\"\r\nEOF\r\n<\/pre>\n<p>This allows you to execute scripts with DOS\/Windows \\r\\n line endings with <code>.\/yourscript<\/code> (but it will fail if the script specifies parameters on the shebang, or if you run it with <code>bash yourscript<\/code>). It works because from a UNIX point of view, DOS\/Windows files specify the interpretter as \"bash^M\", and we override that to clean the script and run bash on the result. <\/p>\n<p>Of course, you can also replace the helpful exec bash part with <code>echo \"Run dos2unix on your file!\" >&2<\/code> if you'd rather give your users a helpful reminder rather than compatibility or a crazy error.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you for any reason use a Windows editor to write scripts, it can be annoying to remember to convert them and bash fails in mysterious ways when you don&#8217;t. Let&#8217;s just get rid of that problem once and for all: cat > $&#8217;\/bin\/bash\\r&#8217;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[6,4],"tags":[11,53,21],"class_list":["post-236","post","type-post","status-publish","format-standard","hentry","category-basic-linux","category-linux","tag-bash","tag-linux","tag-shell-script"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/posts\/236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=236"}],"version-history":[{"count":10,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/posts\/236\/revisions"}],"predecessor-version":[{"id":630,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=\/wp\/v2\/posts\/236\/revisions\/630"}],"wp:attachment":[{"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vidarholen.net\/contents\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}