Bad interpreter: No such file or directory

Although not very common, RCS team receives messages from user who experience this error or issue. Fortunately, both the cause and the resolution are quite straightforward to understand.

Cause

The issue arises mostly because of how line endings work on Windows and Unix based systems.

CRLF (Carriage Return + Line Feed):

  • Representation: \r\n
  • Usage: Windows text files and many non-Unix systems.
  • Meaning:
    • Carriage Return (\r) moves the cursor to the beginning of the line.
    • Line Feed (\n) moves the cursor down to the next line.

LF (Line Feed):

  • Representation: \n
  • Usage: Unix-like systems, including Linux and macOS.
  • Meaning:
    • Line Feed (\n) moves the cursor down to the next line.
    • When you create a text file, the choice of line ending conventions depends on the operating system and the text editor used.

Windows

Text editors like Notepad on Windows typically use CRLF line endings by default. When you create or edit a text file on Windows, it often contains CRLF line endings.

Unix/Linux

Text editors on Unix-like systems, such as Vim or Nano, typically use LF line endings by default. Files obtained from Unix-like systems often contain LF line endings.

Example

To demonstrate an example, let us first consider that a user creates a simple script called my_script.sh on Windows using Notepad. The contents of file are as shown below:-

#!/bin/bash
# This is a simple Bash script with different line endings

echo "Hello, World!"

# Loop through numbers
for i in {1..5}
do
  echo "Number $i"
done

echo "Script execution complete!"

If you copy this file directly on our system (which are Unix based) and try to run the above script, you will see the following

-bash: ./line_ending_chk.sh: /bin/bash^M: bad interpreter: No such file or directory

Note

If for some reason, you actually want to try this out and you copy the contents of file from this page, you may be able to run the script directly without any issues. This is because of the formatting of this page. To see the error, you can use unix2dos command on file my_script.sh as shown below

unix2dos my_script.sh

Resolution

If you have managed to reproduce the above error or you were getting that error right from the start, you can force the line endings in the file to the Unix style by using the command dos2unix which is just the opposite of unix2dos as described above.

dos2unix my_script.sh

With this we hope that you should be able to resolve your error and continue with your work.