Software development or operation and maintenance engineers, the development environment, test environment and production environment at work are likely to be different, so there is a carriage return / line feed problem that must be understood and paid attention to, because the carriage return / line break character It is different from MAC OS system.
The concept of “carriage return / line feed” comes from the typewriter. After typing a line, restarting the line requires two operations: carriage return (return to the leftmost CR of the line, Carriage Return) and line feed (switch to the next LF, Line Feed) .
Computer operating systems apply this concept, but different systems have different implementations. When we hit the return key on the keyboard and come to a new line, specific to the file encoding, the differences are as follows:
- Windows uses CRLF, the character is "\ r \ n"
- Linux uses LR, the character is "\ n"
- MAC used CR early, the character is "\ r", now it is also "\ n"
For example, when developing a shell script in Windows, an error occurs when running in a Linux environment.
[centos@localhost Downloads]$ ./test.sh
bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory
[centos@localhost Downloads]$ bash test.sh
test.sh: line 2: $'\r': command not found
hello world
test.sh: line 4: $'\r': command not found
[centos@localhost ~]$ cat -e ~/Downloads/test.sh
#!/bin/bash^M$
^M$
echo "hello world"^M$
^M$
^ M is the character "\ r", and $ is the character "\ n"
In this case, you need to convert the file to a newline character, such as commonly used dos2unix, unix2dos, etc.
[root@localhost centos]# yum install dos2unix
[root@localhost centos]# dos2unix /home/centos/Downloads/test.sh
dos2unix: converting file /home/centos/Downloads/test.sh to Unix format ...
[root@localhost centos]# bash /home/centos/Downloads/test.sh
hello world