seven hundred and eighty-eight

I'm using this tutorial to learn bash scripts to automate a few tasks for me.
I'm connecting to a server using putty.

The script, located in .../Documents/LOG , is:

 #!/ bin/bash # My first script echo "Hello World!"

And I executed the following for read/write/execute permissions

 chmod 755 my_script

Then, when I enter ./my_script , I'm getting the error given in the title.

Some similar questions wanted to see these, so I think they might help :

 $ which bash /bin/bash

and

 $ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/bin/mh

I tried adding the current directory to PATH , but that doesn't work …

seven
  • How did you create the file? E.g. Which editor / OS
    –  cowls
    Jan 8, 2013 at 16:05
  • I created the file in Windows using Notepad++, copied the file over to the server using WinSCP. And I know this isn't the ideal way to do things
    –  cartonn
    Jan 8, 2013 at 16:07
  • one
    See my answer, this is most likely the issue
    –  cowls
    Jan 8, 2013 at 16:08
  • I got this with a hashbang misspelling: #! / user/bin/env python . note user instead of usr , so check your env top-line statement
    –  kmiklas
    Jul 24, 2020 at 13:36
  • seventeen
    I voted to reopen this question because the title contained /bin/bash^M: bad interpreter: No such file or directory , which led me from a Google search directly to a solution. The "duplicate" question did not.
    –  Jim Tough
    Jan 11, 2021 at 22:51

11 Answers eleven

Reset to default
one thousand two hundred and seventy-seven

The reason might be that you saved the file on Windows, with CR LF as the line ending ( \r\n ).

Run the following command in your terminal:

 sed -i -e 's/\r$//' scriptname.sh

(Of course, change scriptname.sh to your file name)

The command will replace those CR characters with nothing, which will leave these lines with LF ( \n ) as the ending, and Bash will be able to read and execute the file by running

 ./scriptname.sh
nine
  • eight
    If this not worked, try sed -i -e 's/^M$//' scriptname.sh . (Press Ctrl+V Ctrl+M to insert that ^M .)
    –  youhans
    Jul 16, 2017 at 20:20
  • four
    This didn't work for me, including the . However, the original answer had a solution that did work for me: open the file in Vim and run the following command before saving: :set fileformat=unix listed under the SO question called Are shell scripts sensitive to encoding and line endings? did work for me.
    –  Jazzmine
    Jan 15, 2018 at 16:10
  • three
    bonus for working on docker containers that don't have dos2unix installed. Though I couldn't overwrite the script in my case so mine looked like cat scriptname.sh | sed -e 's/\r$//' > fixedscriptname.sh
    –  Rhubarb
    Feb 20, 2019 at 11:10
  • three
    If it helps, I used sed -i -e 's/\r$//' *.sh which converted all my sh files to make them work May 7, 2019 at 14:27
  • thirty
    @Robert Molina, windows adds a carriage return character in addition to \n at the end of lines. This command simply removes it by substituting \r for an empty string using the linux stream edit command (sed). Sep 2, 2020 at 16:59
eight hundred and fifty-three

I have seen this issue when creating scripts in Windows env and then porting over to run on a Unix environment.

Try running dos2unix on the script:

http://dos2unix.sourceforge.net/

Or just rewrite the script in your Unix env using vi and test.

Unix uses different line endings so can't read the file you created on Windows. Hence it is seeing ^M as an illegal character.

If you want to write a file on Windows and then port over, make sure your editor is set to create files in UNIX format.

In notepad++ in the bottom right of the screen, it tells you the document format. By default, it will say Dos\Windows . To change it go to

  • settings->preferences
  • new document / default directory tab
  • select the format as unix and close
  • create a new document
thirteen
  • forty-four
    You can also just right click on the Dos\Windows text in the bottom right and change it to unix there.
    –  Hardy
    Nov 19, 2015 at 19:00
  • four
    +1 for such an unlikely solution!! I am using UltraEdit. To achieve the same there, select File -> Conversions -> DOS to Unix
    –  imnd_neel
    May 11, 2016 at 12:05
  • I open the script in notepad and paste into putty shell , then save it. Everything fine for now. Thank you for your answer.
    –  Dylan B
    Feb 22, 2017 at 2:52
  • seven
    If you use Sublime Text 3 editor, you can convert line endings from Windows to Linux format by selecting View->Line Endings->Unix and then save your script with Ctrl+S Aug 17, 2017 at 21:53
  • five
    Under VSCode you can change the Linefeed option on the bottom right from CRLF to LF to achieve the same as in this post. Jul 24, 2019 at 7:07
one hundred and sixty-six

If you use Sublime Text on Windows or Mac to edit your scripts:

Click on View > Line Endings > Unix and save the file again.

 enter image description here

two
  • one
    It worked. But can I know the reason which required to perform this action. Apr 23, 2018 at 9:41
  • can we make this default behaviour? That is to say, any file edited and saved in Sublime accomplishes this automatically. Aug 13, 2021 at 7:55
seventy-nine

In notepad++ you can set it for the file specifically by pressing

Edit --> EOL Conversion --> UNIX/OSX Format

 enter image description here

four
  • two
    That worked.. Thanks Mar 21, 2017 at 11:38
  • this is it! thanks! Dec 7, 2021 at 2:17
  • Worked for me. Saved my time and energy. Jun 2, 2022 at 10:23
  • Worked for me for docker init script too Mar 17, 2023 at 15:33
sixty-one

This is caused by editing file in windows and importing and executing in unix.

dos2unix -k -o filename should do the trick.

two
  • three
    sorry, it didn't work for me.
    –  raja777m
    Sep 11, 2015 at 17:28
  • one
    -k : Keep the date stamp of output file same as input file. -o : Old file mode. Convert file FILE and overwrite output to it. The program defaults to run in this mode. Jan 28, 2020 at 9:10
twenty-one

problem is with dos line ending. Following will convert it for unix

 dos2unix file_name

NB: you may need to install dos2unix first with yum install dos2unix

another way to do it is using sed command to search and replace the dos line ending characters to unix format:

 $sed -i -e 's/\r$//' your_script.sh
twenty

Your file has Windows line endings, which is confusing Linux.

Remove the spurious CR characters. You can do it with the following command:

 $ sed -i -e 's/\r$//' setup.sh
one
  • This is the magic I need when I can't get scripts to run on windows filesystem.. Aug 19, 2016 at 18:44
ten

I was able to resolve the issue by opening the script in gedit and saving it with the proper Line Ending option:

File > Save As...

In the bottom left of the Save As prompt, there are drop-down menus for Character Encoding and Line Ending. Change the Line Ending from Windows to Unix/Linux then Save.

 Selecting the "Line Ending" option as "Linux/Unix" in the gedit "Save As" prompt

one
  • Worked for me using Mousepad > Document>Line Ending>Unix (LF); Save .
    –  ivan866
    Dec 20, 2019 at 11:44
nine

For Eclipse users, you can either change the file encoding directly from the menu File > Convert Line Delimiters To > Unix (LF, \n, 0 Α, ¶) :

 Eclipse change file encoding

Or change the New text file line delimiter to Other: Unix on Window > Preferences > General > Workspace panel:

 Eclipse workspace settings

two
  • File > Convert Line Delimiters To does it but Window > Preferences > General > Workspace > Unix does not seem to help. Nov 13, 2017 at 12:17
  • one
    @PanuHaaramo that setting is for all NEW created files. If you have a file that it's created already, then you have to convert it as in the first screenshot. Nov 13, 2017 at 14:35
one

Atom has a built-in line ending selector package

More details here: https://github.com/atom/line-ending-selector

zero

I develop on Windows and Mac/Linux at the same time and I avoid this ^M-error by simply running my scripts as I do in Windows:

$ php ./ my_script

No need to change line endings.

Not the answer you're looking for? Browse other questions tagged or ask your own question .