Re: Redirection and subshells and file position pointer
From:
Stan Marsh
Subject:
Re: Redirection and subshells and file position pointer
Date:
Fri, 11 Jul 2025 07:55:58 -0600
Re: Running tee with an argument of /dev/stderr
First notes:
1) Granted that this has nothing to do with bash or subshells. It is just
an artifact of some Linux-y things. Still, I find the thread
interesting, and
have no regrets over starting it.
2) There are, of course, zillions of workarounds. The easiest and most
straightforward being the one originally noted in the OP by the OP
(i.e., me),
and most recently mentioned by Greg. That is, to simply break it down
into 3
statements (an assignment, an echo, and a compare) and to not use "tee"
at all.
That all said, the bottom-line takeaway is that, in Linux (and Linux is all I
care
about), "/dev/stderr" does not mean "the current stderr". Rather, it means
"The file
that is the destination of the current stderr" (*). I.e., as another poster
notes, it is
simply an alias for (specifically, a symlink to) the file to which stderr is
currently
directed. What "tee" (or any other program) does with this file is up to the
authors of
the program. And what "tee" does is to truncate the file (unless -a is
supplied).
(*) This file might be a regular file on disk or a device file (e.g.,
/dev/pts/N) or
something else.
In fact, if you write/compile this C program and then pipe the "file" command
into it
instead of into "tee", it does the right thing. This suggests that it might be
nice if
the "tee" program had an option to just write to a list of (open) file
decriptors, rather
than to a list of filenames.
--- Cut Here ---
/* RealTee.c */
/* Insert appropriate list of header files here */
int main(void)
{
char buff[512];
int n;
while ((n = read(0,buff,sizeof buff)) > 0) {
write(1,buff,n);
write(2,buff,n);
}
}
--- Cut Here ---
=================================================================================
Please do not send me replies to my posts on the list.
I always read the replies via the web archive, so CC'ing to me is unnecessary.
Note that they always end up in my Spam file anyway, so it is annoying to have
to
periodically clean that out.