36

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

Mohammad Reza Rezwani
  • 10,286
  • 36
  • 92
  • 128

2 Answers2

46

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/#
Radu Rădeanu
  • 169,590
  • thanks. but if the another terminal does not exist. what we should do? Or better to say pass it to newer one? – Mohammad Reza Rezwani Jul 12 '14 at 17:07
  • 5
    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 '14 at 17:25
8

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).

TuKsn
  • 4,370
  • 2
  • 26
  • 43
  • This will run the command directly in the new terminal. – Radu Rădeanu Jul 12 '14 at 17:42
  • Yes but what is the advantage to run it first in the old terminal ? – TuKsn Jul 12 '14 at 17:46
  • No advantage..., but in fact this answer to this comment. – Radu Rădeanu Jul 12 '14 at 17:53
  • Ok there is a advantage with your method the output of ls is colored, don't know why this is not the case with my function... – TuKsn Jul 12 '14 at 17:57
  • 4
    You will understand if you will run type ls and then ng type ls :) – Radu Rădeanu Jul 12 '14 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";} –  Jul 16 '14 at 01:28
  • @highwind thank you for the hint, I've changed it. But x-terminal-emulator didn't work for me. – TuKsn Jul 16 '14 at 07:25