Signup/Sign In

[SOLVED] Getting Error while executing a .sh file: /bin/bash^M: bad interpreter

Posted in Tricks   LAST UPDATED: AUGUST 11, 2021

    If you are trying to run a shell script and getting the following error,

    /bin/bash^M: bad interpreter: No such file or directory
    

    You may think that this is a permission issue and might try running the chmod 777 command to provide all the permissions to the shell script file, but that will not fix the issue.

    The script indicates that it must be executed by a shell located at /bin/bash^M. There is no such file, it's called /bin/bash.

    The ^M is a carriage return character. Linux uses the line feed character to mark the end of a line, whereas Windows uses the two-character sequence CR LF. Your file has Windows line endings, which is confusing Linux.

    Using sed Command

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

    sed -i -e 's/\r$//' NAME-OF-FILE.sh


    Using dos2unix Program

    Or, you can install the dos2unix program and then run the dosrunix command to fix the shell script as per unix operating system.

    dos2unix NAME-OF-FILE.sh

    To install dos2unix utility in your UNIX system, run the following command:

    sudo apt-get install dos2unix
    

    To convert the file back to DOS formatting, you cna use the unix2dos command.


    Using vim Editor

    If you do not have the dos2unix utility installed, you can use the Vim editor to convert the formatting of your shell script to unix. Open the file in the vim editor.

    vi NAME-OF-FILE.sh

    Run the following command,

    :set ff=unix
    

    Then write the file and close it using the following command:

    :wq!

    And you are done.

    You can use any of the above mentioned techniques to change the formatting of your shell script to UNIX. We hope this article helped you with your issue.

    You may also like:

    About the author:
    This is the official profile of the Studytonight content team.
    Tags:LinuxShell Scripting
    IF YOU LIKE IT, THEN SHARE IT

    Want to learn coding and don't know where to start?

    Try out our Interactive Courses for Free 🥳 😯 🤩
    learn to code footer Ad
     

    RELATED POSTS