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

I wrote a shell script under Windows, and when I upload it to Linux, I get an error:

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

Solution:

Since I am not very familiar with the shell, I found out the whole story after talking to my colleagues. The main reason is that I edited test.sh under Windows and uploaded it to the Linux system for execution. The format of the .sh file is dos format. And linux can only execute scripts in unix format. Because pressing the Enter key once under dos/window actually enters "Carriage Return (CR)" and "Line Feed (LF)", while under Linux/unix, pressing the Enter key once only enters "Line Feed (LF)", Therefore, the modified sh file will have one more CR in each line, so when running under Linux, it will report an error that the command cannot be found.
We can view the format of the file through the vi editor. The steps are as follows:
1. First, use the vi command to open the file
[root@localhost test]# vi test.sh  
2. Use the :set ff command in the vi command mode to
see that the format of the changed file is

 
fileformat=dos


3. Modify the file format to unix
and use vi/vim to modify the file format
command: :set ff=unix
or :set fileformat=unix

Then: wq save and exit

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326614649&siteId=291194637