Shell redirection i/o order

后端 未结 4 743
青春惊慌失措
青春惊慌失措 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:45

    This error:

    ls: *.xyz: No such file or directory
    

    is being written on stderr by ls binary.

    However in this command:

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

    You're first redirecting stderr to stdout which by default goes to tty (terminal)

    And then you're redirecting stdout to a file files.lst, however remember that stderr doesn't redirected to file since you have stderr to stdout redirection before stdout to file redirection. Your stderr still gets written to tty in this case.

    However in 2nd case you change the order of redirections (first stdout to file and then stderr to stdout) and that rightly redirects stderr to a file which is also being used by stdout.

提交回复
热议问题