Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to run a command foo on one terminal and pass the result to another terminal. Is it possible to do this ?

share|improve this question
3  
Also look into tmux and screen, both of which allow you to take over, mirror, or just execute commands on, other sessions. –  Kevin Jul 12 at 18:32

2 Answers 2

up vote 34 down vote accepted

Yes, it is. A picture worth a thousand words:

radu's terminal

So, you have to redirect the output of your command using > operator to /dev/pts/#. You can find # using who or w command. If tou want to redirect and the errors, use:

<command> >& /dev/pts/#
share|improve this answer
    
thanks. but if the another terminal does not exist. what we should do? Or better to say pass it to newer one? –  lion Jul 12 at 17:07
4  
You will need probably a script to do that: the script will take as argument your command then should open a new gnome-terminal and detect the tty (using tty command) of the new terminal and finally send the output to it. –  Radu Rădeanu Jul 12 at 17:25

Something like this for your $HOME/.bashrc :

ng() { gnome-terminal -x sh -c "$*; bash"; }

This will run a command and shows the result on a new terminal window.

Examples:

ng ls -l
ng echo foo

Edit: To consider aliases from the $HOME/.bashrc use this instead:

ng() { gnome-terminal -x bash -ic "$*; bash"; }

then the output of ls should be colored (thanks to Radu Rădeanu for this hint).

share|improve this answer
    
This will run the command directly in the new terminal. –  Radu Rădeanu Jul 12 at 17:42
    
Yes but what is the advantage to run it first in the old terminal ? –  TuKsn Jul 12 at 17:46
    
No advantage..., but in fact this answer to this comment. –  Radu Rădeanu Jul 12 at 17:53
3  
You will understand if you will run type ls and then ng type ls :) –  Radu Rădeanu Jul 12 at 18:01
1  
if write function in one line in bashrc, maybe need a semicolon to end it, or you will meet an "unexpected end of file" error. so ng() { gnome-terminal -x sh -c "$*; bash" ;} should be better. And if you are using xfce(xfce4-terminal as default emulator): ng() { xfce4-terminal -x sh -c "$*; bash" ;} or you can simply run by any default emulator ng() { x-terminal-emulator -x sh -c "$*; bash";} –  highwind Jul 16 at 1:28

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.