453

I've been trying to install lpng142 on my fed 12 system. Seems like a problem to me. I get this error

[root@localhost lpng142]# ./configure bash: ./configure: /bin/sh^M: bad interpreter: No such file or directory [root@localhost lpng142]# 

How do I fix this? The /etc/fstab file:

# # /etc/fstab # Created by anaconda on Wed May 26 18:12:05 2010 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/VolGroup-lv_root / ext4 defaults 1 1 UUID=ce67cf79-22c3-45d4-8374-bd0075617cc8 /boot ext4 defaults 1 2 /dev/mapper/VolGroup-lv_swap swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 
7
  • 3
    thanks, dos2unix save my day. for osx user, it's getting easier. brew install dos2unix dos2unix Commented Oct 10, 2015 at 2:54
  • 6
    VIM :set fileformat=unix Commented Jun 22, 2016 at 12:41
  • 2
    Age of the question is generally not a factor when deciding duplicates. A general, focused question which is easy to find in Google is usually a better duplicate target than an obscure, specialized question where the answer happens to be fairly general. Commented Sep 25, 2017 at 10:12
  • 2
    @tripleee Not convincing enough because it's much easier for any user to copy-paste the error log onto web search Commented Sep 26, 2017 at 3:50
  • 3
    If you genuinely think the duplicate should be the other way around, please bring it up on Meta Stack Overflow. For the record, the "bad interpreter" symptom is one of many possible symptoms, and the details about configure and fstab in this question are distracting. If merging questions were easier, I would be happy to merge a couple of the answers here to the master duplicate, though. Commented Sep 26, 2017 at 4:43

15 Answers 15

871

To fix, open your script with vi or vim and enter in vi command mode (key Esc), then type this:

:set fileformat=unix 

Finally save it

:x! or :wq!

Sign up to request clarification or add additional context in comments.

11 Comments

I have the same issue with multiple files, how this can be applied for a directory tree? do I need to go to each directory and open the files one by one in vi and do ":set fileformat=unix" ?? Please help.
:set ff=unix works too :)
Why do you save with "!"? It is used only if you want to discard changes, which is not the case. If the save fails, then maybe you did something wrong.
May also be a good idea to add to your .vimrc file so you won't have to worry about this in the future.
Does not work in my vi (rapbian jessie).
|
384

Looks like you have a dos line ending file. The clue is the ^M.

You need to re-save the file using Unix line endings.

You might have a dos2unix command line utility that will also do this for you.

7 Comments

Yes, the ^M kinda seemed weird. I did see this dos2unix configure suggestion somewhere in the net, but didn't wanna run into other problems. Okay, fingers crossed, i'll give it a try :) Thanks for the quick replies, Konerak and Richard
If the project you are trying to build has many scripts that require conversion (this is the case with Qt) applying dos2unix recursively may be a good idea. Running "find . -type f | xargs dos2unix" from the top level directory will do it for you.
Freakin Qt configuration files (building from source here) have this garbage. sigh Really useful tool that saves a lot of manual labour. Then again why one should do that when downloading the sources of a library that is shipped with "everywhere" in the name of the ZIP file is beyond me.
dos2unix fixed it... ps: yum install dos2unix if required
Excellent ! For Sublime Text 3 users, this is fixed by choosing View -> Line Endings -> Unix.
|
131

Or if you want to do this with a script:

sed -i 's/\r//' filename 

3 Comments

+1, worked for me on debian linux
This is simple, but only works with GNU sed. With BSD/macOS sed, you need to use sed -i '' $'s/\r//' filename (from bash), or, slightly more robust: sed -i '' $'s/\r$//' filename
Worked for me on WSL2 Ubuntu 22.04
51

Your configure file contains CRLF line endings (windows style) instead of simple LF line endings (unix style). Did you transfer it using FTP mode ASCII from Windows?

You can use

dos2unix configure 

to fix this, or open it in vi and use :%s/^M//g; to substitute them all (use CTRL+V, CTRL+M to get the ^M)

4 Comments

Nope, I did use linux to download the .gz file. No idea how CRLF got there.
vim search and replace that's easier to type: %s/\r$//g
On some system (e.g. recent Debian / Ubuntu) is the binary called fromdos, not dos2unix.
Just wanted to comment that I just downloaded the original jpeg9-e zip direct from the JPEG website, and it had all the ^Ms in it, rendering the configure and makefiles useless without conversion. The error wasn't on the OP's end. Someone at IJG must have edited the files and not noticed they became incompatible with Unix. This is not the case with the jpeg 8x files.
30

You can use following command to fix

cat file_name.sh | tr -d '\r' > file_name.sh.new 

1 Comment

this works ok on MacOS X 10.11 too
16

If you could not found run the command,

CentOS:

# yum install dos2unix* # dos2unix filename.sh dos2unix: converting file filename.sh to Unix format ... 

Ubuntu / Debian:

# apt-get install dos2unix 

Comments

13

This usually happens when you have edited a file from Windows and now trying to execute that from some unix based machine.

The solution presented on Linux Forum worked for me (many times):

perl -i -pe's/\r$//;' <file name here> 

Hope this helps.

PS: you need to have perl installed on your unix/linux machine.

Comments

7

Thanks to pwc101's comment on this post, this command worked in Kali Linux .

sed -i s/{ctrl+v}{ctrl+m}// {filename}

Make sure you replace the bits in brackets, {}. I.e. {ctrl+m} means press Ctrl key and the M key together.

Comments

6

If you're on OS X, you can change line endings in XCode by opening the file and selecting the

View -> Text -> Line Endings -> Unix

menu item, then Save. This is for XCode 3.x. Probably something similar in XCode 4.

1 Comment

Or use an editor like TextWrangler (Dropdown in status bar) or Sublime (View > Line Endings > Unix) to do the same thing.
6

Following on from Richard's comment. Here's the easy way to convert your file to UNIX line endings. If you're like me you created it in Windows Notepad and then tried to run it in Linux - bad idea.

  1. Download and install yourself a copy of Notepad++ (free).
  2. Open your script file in Notepad++.
  3. File menu -> Save As ->
  4. Save as type: Unix script file (*.sh;*.bsh)
  5. Copy the new .sh file to your Linux system
  6. Maxe it executable with: chmod 755 the_script_filename
  7. Run it with: ./the_script_filename

Any other problems try this link.

3 Comments

There's also a setting in Notepad++ to set the default line ending, goto: "Preferences->New Document->Format" and select Unix. One caveat though: that is only the default for new files, it won't change the format if it detects something else on existing files.
You can switch line endings with the Replace functionality in NOtepad++. Switch on "Exteneded" search mode and enter "\r\n" in the find and "\n" in the replace. Click Replace All!
To fix in Notepad++ just right click DOS\Windows on the bottom right, and choose UNIX/OSX Format
6

When you write your script on windows environment and you want to run it on unix environnement you need to be careful about the encodage :

dos2unix $filePath

1 Comment

yum install dos2unix
6

Just adding sh before script name make it work in my case.

1 Comment

Thank you so much for posting this. None of all these dos2unix / line-ending-fix solutions worked for me. I finally got it to run by doing sudo sh ./MyScript.sh. Done on RedHat 6 / RHEL 6
5

If you are using TextMate or a similar programme, do save as, and then in encodings choose LF instead of CRLF.

Comments

4

Use the dos2unix command in linux to convert the saved file. example :

dos2unix file_name 

Comments

3

You can also do this in Kate.

  1. Open the file
  2. Open the Tools menu
  3. Expand the End Of Line submenu
  4. Select UNIX
  5. Save the file.

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.