1

I am facing /bin/bash^M: bad interpreter: No such file or directory issue and I have already got the solution for it from this stack flow answer

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

which works fine.

My question is every time when I restart my ubuntu machine I have to redo everything That is I execute dos2unix -k -o filename every time I start my system.

Is there any way this can be just once?

Please note: I had to create a new question because I was not able to ask the question or comment in the existing question due to less reputation

Ricky
  • 147
  • 1
  • 2
  • 9
  • Maybe you are running a virtual machine that doesn't save changes to disk? – Mark Setchell May 26 '19 at 16:23
  • 1
    You shouldn't need to do this more than once, which would seem to indicate that the file is being re-written. Is it being copied from a remote or something? – Alex Howansky May 26 '19 at 16:23
  • 2
    It is not normal for the line endings on a file to change after a reboot, so there must be something unusual in your environment. Can you expand a little bit more on your setup? Is the script on a network share, or are you using a VM with a shared directory with a host OS? Is the script being edited by something in Windows? – Brian Campbell May 26 '19 at 16:27

2 Answers2

2

The first line of your bash script should be the Shebang (#!/bin/bash).

I see the error says: /bin/bash But is should be changed to: #!/bin/bash

Then run:

$ dos2unix my_script

This will change all the line terminators from \r\n (Windows) to \n (Linux), this will modify the original my_script file so it will persist even after a reboot.

Reynaldo Aceves
  • 436
  • 2
  • 10
0

This is a very common problem of running a bash script from a file saved with Microsoft OS machine (a virtual machine maybe?) such as Windows or DOS.

So you know the fix to your problem.

Now you should prevent your problem reoccurring every time you login. Identify how the file is generated/copied/damaged by another resource. Like .bash_profile script or crontab script or any other management deamon.

Dudi Boy
  • 4,551
  • 1
  • 15
  • 30
  • You can also just [symlink #!/bin/bash^M to #!/bin/bash](https://natanyellin.com/posts/shebang-python-bad-interpreter-m/). – Natan Yellin Nov 22 '20 at 19:48