I want to run a command foo on one terminal and pass the result to another terminal. Is it possible to do this ?
-
4Also look into tmux and screen, both of which allow you to take over, mirror, or just execute commands on, other sessions.– KevinJul 12, 2014 at 18:32
2 Answers
Yes, it is. A picture worth a thousand words:
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/#
-
thanks. but if the another terminal does not exist. what we should do? Or better to say pass it to newer one? Jul 12, 2014 at 17:07
-
5You 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 (usingtty
command) of the new terminal and finally send the output to it. Jul 12, 2014 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).
-
-
-
-
4You will understand if you will run
type ls
and thenng type ls
:) Jul 12, 2014 at 18:01 -
1if 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 emulatorng() { x-terminal-emulator -x sh -c "$*; bash";}
– user305925Jul 16, 2014 at 1:28