Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question

3 Answers 3

up vote 10 down vote accepted

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.

share|improve this answer
    
Indeed, in the pipeline you show /dev/stdout is redirected. That is, if you were using tee /dev/stdout the effect would be that everything is sent to xargs twice, and nothing goes to the terminal. –  celtschk Apr 30 at 14:37
    
/dev/stdout is not redirected by a pipe, it will stay the current process standard output. However, if you encapsulate the command in a script and then redirect the stdout of this script, then /dev/stdout will be redirected. In an interactive console, tee /dev/tty and tee /dev/stdout has the same effect, even when using pipes. –  Vincent Robert Apr 30 at 17:24
    
If you don't believe me, start your shell and type echo foo | tee /dev/stdout | tr f b and echo foo | tee /dev/tty | tr f b. Here's a hint for you: It is tee which interprets the /dev/stdout as file name. –  celtschk Apr 30 at 17:31
1  
You are right, I do not know how I missed that. I did some tests in my own shell with tty and stdout and I do somehow missed that. Thanks for the clarification, I removed the wrong statement. –  Vincent Robert Apr 30 at 18:05
    
Well, given that stderr is not affected by the pipe, you would not have needed to remove that (of course the script redirection caveat remains, but for a script, redirectability would generally be a desired property). –  celtschk Apr 30 at 19:07

A more general solution than /dev/tty:

start cmd:> echo foo | tee /dev/stderr 
foo
foo
share|improve this answer

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
share|improve this answer
1  
I think you mean /proc/self/fd/2 –  celtschk Apr 30 at 19:09
    
@celtschk yes correct, thanks –  MolbOrg May 6 at 17:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.