这是一个bash bug吗?(用ls列出目录)

Poz*_*nux 0 bash ls

如果我想列出以下目录中的所有文件和目录:

[root@centos2 dir1]# tree
.
+-- dir2
    +-- coucou.txt

1 directory, 1 file
Run Code Online (Sandbox Code Playgroud)

为什么dir2没有显示在下面?仅显示该文件.

[root@centos2 dir1]# ls -l *
total 0
-rwxr--r-- 1 root root 0 Dec  2 18:20 coucou.txt
Run Code Online (Sandbox Code Playgroud)

如果我复制目录和列表,它们都会显示:

[root@centos2 dir1]# cp -r dir2/ dir3
[root@centos2 dir1]# tree
.
+-- dir2
¦   +-- coucou.txt
+-- dir3
    +-- coucou.txt

2 directories, 2 files

[root@centos2 dir1]# ls -l *
dir2:
total 0
-rwxr--r-- 1 root root 0 Dec  2 18:20 coucou.txt

dir3:
total 0
-rwxr--r-- 1 root root 0 Dec  5 11:34 coucou.txt
Run Code Online (Sandbox Code Playgroud)

Tom*_*ech 5

不,这不是一个bug; 它是shell扩展*以匹配所有内容的预期行为.在运行命令之前*,将由一个参数列表替换,每个匹配路径对应一个参数.

在你的第一个例子中,ls -l *shell扩展*dir2,所以你的命令是ls -l dir2.ls然后只列出该目录的内容,以便获取文件coucou.txt.

在你的第二个例子中,*扩展为两个参数,dir2dir3.在这种情况下,行为ls -l是单独列出每个目录的内容.