Tuesday, 23 February 2016

The Bourne shell, Bourne-again shell, and Korn shell notation digit1>&digit2 says to redirect descriptor digit1 to the same file as descriptor digit2. What is the difference between the two commands ./a.out > outfile 2>&1 ./a.out 2>&1 > outfile

Since the shells process their command line from left to right, the command
./a.out > outfile 2>&1
first sets standard output to outfile and then dups standard output onto descriptor 2 (standard error). The result is that standard output and standard error are set to the same file. Descriptors 1 and 2 both point to the same file table entry. With
./a.out 2>&1 > outfile
however, the dup is executed first, causing descriptor 2 to be the terminal (assuming that the command is run interactively). Then standard output is redirected to the file outfile. The result is that descriptor 1 points to the file table entry for outfile, and descriptor 2 points to the file table entry for the terminal

No comments:

Post a Comment