Signup/Sign In

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

Posted in Tricks   LAST UPDATED: APRIL 28, 2023

    If you are trying to run a shell script and getting this 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.

    Frequently Asked Questions(FAQs)

    1. What is bad interpreter error?

    A "bad interpreter error" is an error message that appears when a script is executed but the interpreter specified in the shebang line (the first line in the script that begins with "#!") is not found or cannot be executed. This error occurs because the shell does not know how to interpret the script.

    2. Why is my bin bash script not working?

    There are several reasons why a bash script may not be working, but one common reason is the shebang line. If the shebang line is incorrect or missing, the shell will not know which interpreter to use to execute the script. Another possible reason could be incorrect permissions on the script file.

    3. What is the default interpreter for bash script?

    The default interpreter for a bash script is usually the bash shell (/bin/bash). This interpreter is specified in the shebang line at the beginning of the script, which tells the shell which interpreter to use to execute the script.

    4. How to run bin bash in shell script?

    To run a bin bash script in a shell script, you need to specify the path to the script in the shell script and then execute it using the bash interpreter. For example, if your bin bash script is located in the /usr/local/bin directory and is called "myscript", you can run it in a shell script by adding the following lines:

    #!/bin/bash
    /usr/local/bin/myscript
    

    You may also like:

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

    RELATED POSTS