Results 1 to 3 of 3
I really dont understand how this double redirection works:
ls > dirlist 2>&1
directs both standard output and standard error to the file dirlist,
how?? if the final output is ...
- 03-13-2008 #1Just Joined!
- Join Date
- Mar 2008
- Posts
- 6
io redirection
I really dont understand how this double redirection works:
ls > dirlist 2>&1
directs both standard output and standard error to the file dirlist,
how?? if the final output is redirected to dirlist why is it not in the end? like
2>&1 >dirlist
on the other hand
dirlist 2>&1
seems to be concatenating both dirlist and 2 just like
cat file1 file2
Someone please elaborate. If possible with the step by step flow.
Thanks in advance.
- 03-13-2008 #2Linux User
- Join Date
- Jun 2007
- Posts
- 318
Look at the manpage for bash (or whatever shell you're using) and find the section on redirection. In the bash manpage it says:
Note that the order of redirections is significant. For example, the
command
ls > dirlist 2>&1
directs both standard output and standard error to the file dirlist,
while the command
ls 2>&1 > dirlist
directs only the standard output to file dirlist, because the standard
error was duplicated as standard output before the standard output was
redirected to dirlist.
- 03-13-2008 #3
Well first, we need to figure out exactly what we're typing:
In Bash, typing N> FILE has the effect of directing output stream number N to FILE. If N is unspecified, it defaults to '1', standard output. So standard output is IO stream 1, and standard error is IO stream 2.
So we have some command:
This command will be executed, and it will by default have its IO streams point wherever its parents do. In this case, the parent is the shell, so it points at the terminal. Now let's make a little change:Code:command_foo
Now command_foo has its standard output point to file, and its standard error point to wherever its parent's standard error points (again, in this case, the terminal). Finally, we have:Code:command_foo > file
Now command_foo has its standard output point to file, and its standard error point to wherever its standard output (IO stream 1) points.Code:command_foo > file 2>&1
Make sense?
As a side note, this "> DEST 2>&1" construct is so common that more recent Bashes have a special notation for it. If you use "&> DEST", then both standard output and standard error will point to DEST.DISTRO=Arch
Registered Linux User #388732


Reply With Quote