outfile 2> & 1" and "2> & 1> outfile"? I can not understand the difference between these two cases: - ./a.out > ou...">

How should I understand "> outfile 2> & 1" and "2> & 1> outfile"?

I can not understand the difference between these two cases:

  • ./a.out > outfile 2>&

    I can see both standard output and error output in outfile

  • ./a.out 2>& > outfile

    I can only see the standard output int outfile, and the error output was printed on the screen

How should I understand that? I think they are the same!

+4
source share
3 answers

n> filecreates / truncates fileand associates it with a file descriptor n. If nnot specified, it is assumed 1(i.e. standard output).

n>&mcopy (using dup2()) the file descriptor mto n.

, ./a.out 2>& >outfile, stderr, stdout outfile.

, :

  • 2>& >file fd2 := fd1; fd1 := "file",
  • >file 2>& fd1 := "file"; fd2 := fd1
+7

. 2>&1 > file stderr stdout, stdou t . dup2 C.

+2

Bash "2 > 1 > " "", ; while " > file 2 > & 1" , "".

0

All Articles