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

This question already has an answer here:

  • Are shell scripts sensitive to encoding and line endings? 2 answers

  • I have seen this issue when creating scripts in Windows env and then porting over to run on a Unix environment.

    Try running this on the script:

    http://linuxcommand.org/man_pages/dos2unix1.html

    Updated link:

    http://dos2unix.sourceforge.net/

    Or just rewrite the script in your Unix env using vi and test.

    Unix uses different line feeds and carriage returns so can't read the file you created on Windows. Hence it is seeing ^M as an illegal character.

    If you want to write a file on Windows and then port over, make sure your editor is set to create files in UNIX format.

    In notepad++ in the bottom right of the screen, it tells you the document format. By default, it will say DosWindows . To change it go to

  • settings->preferences
  • new document / default directory tab
  • select the format as unix and close
  • create a new document

  • Run following command in terminal

    sed -i -e 's/r$//' scriptname.sh
    

    Then try

    ./scriptname.sh
    

    It should work.


    If you use Sublime Text on Windows or Mac to edit your scripts:

    Click on View > Line Endings > Unix and save the file again.

    在这里输入图像描述

    链接地址: http://www.djcxy.com/p/9814.html

    上一篇: 如何在Linux BASH中获得命令输出的一部分?

    下一篇: 脚本:/ bin / bash ^ M:错误的解释器:没有这样的文件或目录