I'm playing with i/o shell redirection. The commands I've tried (in bash):
ls -al *.xyz 2>&1 1> files.lst
and
ls -al *.xyz 1> files.lst 2>&1
There is no any *.xyz
file in current folder.
These commands gives me the different results. The first command shows an error message ls: *.xyz: No such file or directory
on the screen. But the second one prints this error message to the file. Why did the first command failed to write an err output to the file?
To redirect a file descriptor, we use N> , where N is a file descriptor. If there's no file descriptor, then stdout is used, like in echo hello > new-file .
I/O redirection can be employed to use an existing file as input file for a program. Redirection can be defined as changing the way from where commands read input to where commands sends output.
The Bash manual has a clear example (similar to yours) to show that the order matters and also explains the difference. Here's the relevant part excerpted (emphasis mine):
Note that the order of redirections is significant. For example, the command
ls > dirlist 2>&1
directs both standard output (file descriptor 1) and standard error (file descriptor 2) to the file dirlist, while the command
ls 2>&1 > dirlist
directs only the standard output to file dirlist, because the standard error was made a copy of the standard output before the standard output was redirected to dirlist.
This post explains it from the POSIX viewpoint.
Confusions happen due to a key difference. >
redirects not by making left operand (stderr
) point to right operand (stdout
) but by making a copy of the right operand and assigning it to the left. Conceptually, assignment by copy and not by reference.
So reading from left-to-right which is how this is interpreted by Bash: ls > dirlist 2>&1
means redirect stdout
to the file dirlist
, followed by redirection of stderr
to whatever stdout
is currently (which is already the file dirlist
). However, ls 2>&1 > dirlist
would redirect stderr
to whatever stdout
is currently (which is the screen/terminal) and then redirect stdout
to dirlist
.
Redirections are:
1
for stdout (the default), and 2
for stderr), later redirections targeting that stream refer to the already-redirected version.1
as the target in an earlier redirection, what 1
means at that time is locked in, even if 1
is redirected later.Applied to the example from the question:
>file 2>&1
:
>file
first redirects stdout (file descriptor 1
, implied by not prefixing >
with a file descriptor number) to output file file
2>&1
then redirects stderr (2
) to the already redirected stdout (1
).file
.2>&1 >file
:
2>&1
first redirects stderr to the then-original stdout; since file descriptor 2
participates in no further redirections, stderr output will therefore go to whatever stdout was defined as at that point - i.e., the original stdout, given that this is the first redirection. >file
then redirects the original stdout to file
- but that has no effect anymore on the already locked-in redirection of stderr.file
, while sent-to-stderr output is output to (the original, unredirected) stdout.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With