I meet the following code in a book and still not understand why. Anyone can help to explain it please?
[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]#
[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]#
The lesson here is probably that a command like ls > file2
creates the output-file (file2
) before actually executing the ls
command.
This behavior is confirmed by the bash reference manual:
3.6 Redirections
[...] Before a command is executed, its input and output may be redirected using a special notation interpreted by the shell. [...]
精彩评论