452

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
    – Arejae
    Commented Oct 10, 2015 at 2:54
  • 6
    VIM :set fileformat=unix
    – PodTech.io
    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.
    – tripleee
    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.
    – tripleee
    Commented Sep 26, 2017 at 4:43

15 Answers 15

872

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!

11
  • 3
    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.
    – uss
    Commented Oct 10, 2013 at 9:22
  • 36
    :set ff=unix works too :)
    – Kyslik
    Commented Nov 22, 2013 at 4:17
  • 11
    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. Commented Jan 7, 2014 at 9:20
  • May also be a good idea to add to your .vimrc file so you won't have to worry about this in the future. Commented Oct 22, 2014 at 14:26
  • Does not work in my vi (rapbian jessie).
    – Pygmalion
    Commented Jun 2, 2016 at 17:15
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
  • 1
    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 Commented May 27, 2010 at 12:30
  • 15
    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.
    – Jeff Trull
    Commented Jan 4, 2014 at 16:17
  • 2
    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. Commented Aug 20, 2015 at 23:51
  • 11
    dos2unix fixed it... ps: yum install dos2unix if required
    – Sebas
    Commented Sep 16, 2015 at 21:22
  • 4
    Excellent ! For Sublime Text 3 users, this is fixed by choosing View -> Line Endings -> Unix.
    – weezilla
    Commented Mar 11, 2016 at 1:36
130

Or if you want to do this with a script:

sed -i 's/\r//' filename
2
  • 8
    +1, worked for me on debian linux
    – dbjohn
    Commented Dec 17, 2012 at 20:13
  • 4
    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
    – mklement0
    Commented Dec 12, 2016 at 14:53
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
  • 2
    Nope, I did use linux to download the .gz file. No idea how CRLF got there. Commented May 27, 2010 at 12:33
  • 2
    vim search and replace that's easier to type: %s/\r$//g Commented May 27, 2010 at 15:35
  • 1
    On some system (e.g. recent Debian / Ubuntu) is the binary called fromdos, not dos2unix.
    – pevik
    Commented Jan 17, 2014 at 18:12
  • 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. Commented Mar 28, 2022 at 1:55
30

You can use following command to fix

cat file_name.sh | tr -d '\r' > file_name.sh.new
1
  • 2
    this works ok on MacOS X 10.11 too Commented Jul 20, 2015 at 22:10
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
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.

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.

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
  • Or use an editor like TextWrangler (Dropdown in status bar) or Sublime (View > Line Endings > Unix) to do the same thing.
    – Al.
    Commented Apr 18, 2019 at 12:34
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
  • 3
    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. Commented Jan 27, 2014 at 14:17
  • 1
    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! Commented Jan 27, 2014 at 14:19
  • 3
    To fix in Notepad++ just right click DOS\Windows on the bottom right, and choose UNIX/OSX Format
    – Alaa M.
    Commented Jul 5, 2018 at 5:34
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
  • 1
    yum install dos2unix
    – Andre
    Commented Jan 19, 2019 at 3:12
6

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

1
  • 1
    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
    – velkoon
    Commented Oct 11, 2017 at 20:36
5

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

4

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

dos2unix file_name
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.

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