We can combine this with the above to combine streams, by redirecting from one stream into another. A common construct is to combine standard error into standard output, so that both can be used together. We achieve this using 2>&1 – literally redirecting stream 2 into stream 1:
# ls -1 2>&1
We can sometimes use this to create new streams, simply by using new IDs. This stream must already have been used elsewhere first though, otherwise, it is an error. Most often this is used as a stream source first.
For example, we can swap standard output and standard error by going via a third stream, using 3>&2 2>&1 1>&3:
$ ls -1 3>&2 2>&1 1>&3
This construct doesn’t work correctly in all shells. In bash, the end result is that standard output and standard error are directly swapped. In zsh the result is that both standard output and standard error have ended up on standard output instead.
We can easily combine the above to redirect standard output and standard error at the same time. This mostly works exactly as we would expect – we simply combine the two different redirects on the same command:
$ ls -1 > stdout.log 2> stderr.log
Do'stlaringiz bilan baham: |