I learned that
sudo echo hello > root-owned-file.txt does not work because the redirection does not inherit root privilege, so you need something more like
echo hello | sudo tee root-owned-file.txt or
echo hello | sudo cp /dev/stdin root-owned-file.txt. However, grep can use
--color=tty to add color when redirected to a tty, but not to other destinations. Is redirection totally separate from the subject process, and if so, how does grep 'know' when to omit colors?
My intuition is that there is more to redirection than just channels 1/2/3; my current theory is that grep sends both colored and plain output to several variants of channel 1, and the parent shell selects the correct variant to redirect to the destination based on whether or not the destination is associated with a tty. Alternatively, the parent shell might prepare the destination process/file and send a signal or file descriptor of some kind to the source process; however, this would have implications on the sequential nature of the pipeline, which I had assumed was more strict. For example, the next command in the pipeline might set a flag before yielding a file descriptor, and the source command might depend on that flag's original value, causing a race condition if the pipeline sequence is not strict.