为什么 echo 命令在从 CRLF 文件中读取 *(星号)时不列出目录内容?
Why echo command doesn't list directory contents when read * (asterisk) from CRLF file?
有两个文件:
file1.txt - 创建于 Linux
root@localhost:~# file file1.txt
file1.txt: ASCII text
root@localhost:~# od -c file1.txt
0000000 * \n
file2.txt - 创建于 Windows
root@localhost:~# file file2.txt
file1.txt: ASCII text, with CRLF line terminators
root@localhost:~# od -c file2.txt
0000000 * \r \n
root@localhost:~# ls
myFile1 myFile2 myFile3
root@localhost:~# echo `cat file1.txt`
myFile1 myFile2 myFile3
root@localhost:~# echo `cat file2.txt`
*
为什么第二个文件中的CR(回车return)导致echo命令不列出目录内容而只打印星号?
与echo *.txt
显示所有以.txt
结尾的文件相同,echo *$'\r'
显示所有以回车符结尾的文件returns。
因为你没有,所以它会逐字显示模式
如果您手动或不小心创建了一些 运行 带有 DOS 行终止符的脚本,它们将显示:
$ touch $'foo\r' $'bar\r'
$ echo *$'\r' | hexdump -C
00000000 62 61 72 0d 20 66 6f 6f 0d 0a |bar. foo..|
0000000a
(如果没有 hexdump
,回车符 returns 将导致输出重复覆盖自身,使其看起来已损坏或仅匹配一个文件)
有两个文件:
file1.txt - 创建于 Linux
root@localhost:~# file file1.txt
file1.txt: ASCII text
root@localhost:~# od -c file1.txt
0000000 * \n
file2.txt - 创建于 Windows
root@localhost:~# file file2.txt
file1.txt: ASCII text, with CRLF line terminators
root@localhost:~# od -c file2.txt
0000000 * \r \n
root@localhost:~# ls
myFile1 myFile2 myFile3
root@localhost:~# echo `cat file1.txt`
myFile1 myFile2 myFile3
root@localhost:~# echo `cat file2.txt`
*
为什么第二个文件中的CR(回车return)导致echo命令不列出目录内容而只打印星号?
与echo *.txt
显示所有以.txt
结尾的文件相同,echo *$'\r'
显示所有以回车符结尾的文件returns。
因为你没有,所以它会逐字显示模式
如果您手动或不小心创建了一些 运行 带有 DOS 行终止符的脚本,它们将显示:
$ touch $'foo\r' $'bar\r'
$ echo *$'\r' | hexdump -C
00000000 62 61 72 0d 20 66 6f 6f 0d 0a |bar. foo..|
0000000a
(如果没有 hexdump
,回车符 returns 将导致输出重复覆盖自身,使其看起来已损坏或仅匹配一个文件)