866

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 …

6
  • How did you create the file? E.g. Which editor / OS Commented 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 Commented Jan 8, 2013 at 16:07
  • I got this with a hashbang misspelling: #! /user/bin/env python. note user instead of usr, so check your env top-line statement Commented Jul 24, 2020 at 13:36
  • FYI - I had this problem when duplicating a .bat file and changed the extension to .sh. My plan was to rewrite the batch code to shell. Creating a new .sh file and pasting my code over did the trick. Commented Apr 2, 2021 at 15:53
  • I had this problem when working with WSL2 and running Docker backend at the same time. Shutting down Docker and doing wsl --shutdown, then starting it again fixed the issue. Commented Apr 14, 2021 at 10:15

11 Answers 11

1432

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
Sign up to request clarification or add additional context in comments.

9 Comments

If this not worked, try sed -i -e 's/^M$//' scriptname.sh. (Press Ctrl+V Ctrl+M to insert that ^M.)
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.
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
If it helps, I used sed -i -e 's/\r$//' *.sh which converted all my sh files to make them work
@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).
|
862

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

14 Comments

You can also just right click on the Dos\Windows text in the bottom right and change it to unix there.
+1 for such an unlikely solution!! I am using UltraEdit. To achieve the same there, select File -> Conversions -> DOS to Unix
I open the script in notepad and paste into putty shell , then save it. Everything fine for now. Thank you for your answer.
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
Under VSCode you can change the Linefeed option on the bottom right from CRLF to LF to achieve the same as in this post.
|
167

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

2 Comments

It worked. But can I know the reason which required to perform this action.
can we make this default behaviour? That is to say, any file edited and saved in Sublime accomplishes this automatically.
86

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

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

enter image description here

2 Comments

Worked for me. Saved my time and energy.
Worked for me for docker init script too
63

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

dos2unix -k -o filename should do the trick.

2 Comments

sorry, it didn't work for me.
-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.
21

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

Comments

20

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

1 Comment

This is the magic I need when I can't get scripts to run on windows filesystem..
10

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

1 Comment

Worked for me using Mousepad > Document>Line Ending>Unix (LF); Save.
9

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

2 Comments

File > Convert Line Delimiters To does it but Window > Preferences > General > Workspace > Unix does not seem to help.
@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.
1

Atom has a built-in line ending selector package

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.