tee stdout to stderr?

I’d like to send stdout from one process to the stdin of another process, but also to the console. Sending stdout to stdout+stderr, for instance.

For example, I’ve got git edit aliased to the following:

git status --short | cut -b4- | xargs gvim --remote

I’d like the list of filenames to be sent to the screen as well as to xargs.

So, is there a tee-like utility that’ll do this? So that I can do something like:

git status --short | 
    cut -b4- | almost-but-not-quite-entirely-unlike-tee | 
    xargs gvim --remote

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

tee can duplicate to the current console by using tee /dev/tty

git status --short | cut -b4- | tee /dev/tty | xargs gvim --remote

Alteratively, you can use /dev/stdoutor /dev/stderr but they could be redirected if your command is within a script. Note that /dev/tty will always be the console (and may not exist in a non-interactive shell). This is wrong, read the comments.

Method 2

A more general solution than /dev/tty:

start cmd:> echo foo | tee /dev/stderr 
foo
foo

Method 3

You can use tee command, just feed it with STDERR file, as example:

tee /dev/stderr
tee /proc/self/fd/2

so in that case your alias maybe:

git status --short | 
    cut -b4- | tee /dev/stderr | 
    xargs gvim --remote


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x