help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Redirection and subshells and file position pointer


From: Andreas Kähäri
Subject: Re: Redirection and subshells and file position pointer
Date: Thu, 10 Jul 2025 07:17:22 +0200

On Wed, Jul 09, 2025 at 03:43:53PM -0400, Greg Wooledge wrote:
> > On Wed, Jul 9, 2025, 3:21 PM Stan Marsh <gazelle@xmission.com> wrote:
> > > Anyway, in the line containing the "file" command, the intent is to have
> > > the output of
> > > "file" show up in the log and also be used as the string to be compared to
> > > see if the
> > > file is a text file.  So, I figured that piping it to "tee /dev/stderr"
> > > would do what
> > > I want.
> 
> Unfortuantely, the semantics of /dev/stderr and its cousins are
> OS-dependent.  You'll get different behavior between Linux and BSD,
> and of course many commercial Unix systems don't have it at all.
> 
> If I understand the request correctly, you wanted to do this:
> 
>     result=$(file "$somefile" | tee /dev/stderr)


Just thinking aloud about the non-portability of "tee /dev/stderr"...

Bash handles /dev/stderr specially when it's used in a redirection, so
changing "tee /dev/stderr" into "tee >/dev/stderr" might get rid of the
portability issue?

The stream would be duplicated onto standard output and also redirected
to whatever Bash thinks /dev/stderr is (rather than duplicated to the
possibly non-existent /dev/stderr path and standard output).


> 
> You're forking an entire new process just to copy the output of one
> command to stderr.  You'd be better off doing it this way:
> 
>     result=$(file "$somefile")
>     printf 'file result: "%s"\n' "$result" >&2
> 
> Not only is this consistent across all operating systems, but it's
> also faster.

-- 
Matti Andreas Kähäri
Uppsala, Sweden

.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]