Shell redirection i/o order

后端 未结 4 737
青春惊慌失措
青春惊慌失措 2020-11-28 08:32

I\'m playing with i/o shell redirection. The commands I\'ve tried (in bash):

ls -al *.xyz 2>&1 1> files.lst

and

l         


        
4条回答
  •  孤独总比滥情好
    2020-11-28 08:57

    Because order does matter. In the first case, you first redirect stderr (2) to stdout (1). Then you redirect (1) to a file. But stderr (2) is still pointed to stdout of the shell running the command. Pointing (1) to a file in this case doesn't change the output device that (2) is directed at, so it still goes to terminal.

    In the second case, you redirect stdout (1) to a file. Then you point stderr (2) to the same place 1 is pointed, which is the file, so the error message goes to the file.

提交回复
热议问题