Skip to main content
9 events
when toggle format what by license comment
Sep 18, 2024 at 22:44 comment added Kankaristo @SOUser I think that Op De Cirkel's version is a bit more "correct", since everything passes through stdout. This version "bypasses" stdout by outputting directly to the file descriptor. So, when redirecting the output, only the echo (and not the tee) goes to stdout, and that's why you only see one "aaa" (from the echo) when redirecting stdout to a file. That's my understanding, I could be wrong.
Mar 29, 2024 at 12:56 comment added SOUser @Russell Davis Op De Cirkel'version outputs 2 aaa to both stdout and when redirected to another file. However, if the FF=$(echo aaa|tee >(cat - >&5)) becomes FF=$(echo aaa|tee /dev/fd/5), this version outputs 2 aaa to stdout but 1 aaa when redirected to another file... Could you help to suggest how to understand this behavior ? Many thanks !
Aug 5, 2023 at 5:50 comment added Nick Why does @dirtside solution not work? Specifically, why can't you just do tee /dev/fd/1? I don't understand if you're going to tee /dev/fd/5 where 5 points 1, how is that different than just tee /dev/fd/1?
Dec 13, 2019 at 15:41 comment added Paul Donohue If this is run using sudo -u <other non-root user> <script> then Op De Cirkel's answer works but this answer does not. Writing to /dev/fd/5 is equivalent to writing directly to the terminal. /dev/fd/5 is a symlink to the /dev/pts/ file for the terminal, which is owned by the user that originally logged in and is not writable by the sudo'd user. However, cat - >&5 writes to a file descriptor that is opened by bash within the process (which is not the same as writing to /dev/fd/5). This file descriptor forwards the write through each parent process, avoiding any permissions issues.
May 11, 2018 at 8:32 comment added tlwhitec @akhan No it wouldn't, bash is said to be emulating this path should it not exist in the OS by itself.
May 11, 2018 at 8:30 comment added tlwhitec We could go simplifying even further and making this a oneliner without the exec: { FF=$(echo aaa|tee /dev/fd/5); } 5>&1 The braces allow for the redirection to happen before the subshell command is run, while $FF still remains in the scope of the current shell (that wouldn't work with normal brackets ( ). This way there's even no need to close FD 5 afterwards, which is a overlooked hygienic habit.
Sep 20, 2015 at 18:42 comment added dirtside I had been wondering if you could just use tee /dev/fd/1, but that doesn't work because the output still gets captured by $(). So in case anyone else is wondering the same thing, it is necessary to use an extra file descriptor (like 5).
Mar 11, 2014 at 5:25 comment added akhan Wouldn't /dev/fd/5 be OS specific?
Apr 30, 2013 at 4:25 history answered Russell Davis CC BY-SA 3.0