在使用bash重定向时,我怎么能看到未来?

Jus*_*ner 4 bash redirect

我在书中遇到以下代码,仍然不明白为什么.有人可以帮忙解释一下吗?

[root@wd00070319 test]# ls
[root@wd00070319 test]# touch file1
[root@wd00070319 test]# ls
file1
[root@wd00070319 test]# ls > file2
[root@wd00070319 test]# ls
file1  file2
[root@wd00070319 test]# cat file2
file1
file2
[root@wd00070319 test]#
Run Code Online (Sandbox Code Playgroud)

aio*_*obe 6

[root@wd00070319 test]# ls            # list the files. (directory is empty!)
[root@wd00070319 test]# touch file1   # create an empty file called "file1"
[root@wd00070319 test]# ls            # list the files again (to see "file1")
file1                                 
[root@wd00070319 test]# ls > file2    # list the files, put the result in "file2"
[root@wd00070319 test]# ls            # list the files again,
file1  file2
[root@wd00070319 test]# cat file2     # show content of file2
file1
file2                                 # <-- Note that "file2" is present as well.
[root@wd00070319 test]#
Run Code Online (Sandbox Code Playgroud)

这里的教训可能是执行命令之前ls > file2创建输出文件(file2)的命令.ls


bash参考手册确认了此行为:

3.6重定向

[...] 执行命令之前,可以使用shell解释的特殊表示法重定向其输入和输出.[...]