From e465fbaa5a6d1e99cb59727f7b8ae75ed702b58d Mon Sep 17 00:00:00 2001 From: Michele Cereda Date: Tue, 29 Apr 2025 22:42:27 +0200 Subject: [PATCH] chore(kb): start notes about output redirection --- knowledge base/redirect outputs.md | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 knowledge base/redirect outputs.md diff --git a/knowledge base/redirect outputs.md b/knowledge base/redirect outputs.md new file mode 100644 index 0000000..db93c11 --- /dev/null +++ b/knowledge base/redirect outputs.md @@ -0,0 +1,33 @@ +# Redirect outputs + +1. [TL;DR](#tldr) +1. [Further readings](#further-readings) + +## TL;DR + +Pipes are set up **before** the I/O redirections are interpreted.
+The sequence of I/O redirections is interpreted **left-to-right**. + +```sh +command 2>&1 >'/dev/null' | grep 'something' +command 2>'/dev/stdout' 1>'/dev/null' | grep 'something' + +# Swap the standard error and standard output over, then close the spare descriptor used for the swap. +command 3>&1 1>&2 2>&3 3>&- +``` + +## Further readings + +- [How can I pipe stderr, and not stdout?] +- [Pipe only STDERR through a filter] +- [File Descriptors in Bourne shell] + + + + +[File Descriptors in Bourne shell]: https://mixedvolume.blogspot.com/2004/12/file-descriptors-in-bourne-shell.html +[How can I pipe stderr, and not stdout?]: https://stackoverflow.com/questions/2342826/how-can-i-pipe-stderr-and-not-stdout +[Pipe only STDERR through a filter]: https://stackoverflow.com/questions/3618078/pipe-only-stderr-through-a-filter#52575087