38

I am just fooling around on my terminal (Gnome terminal). I was wondering is there a way to send output of one terminal to another without having to make a new file or pipe.

for example: on first terminal I run ls and want its output to be displayed on second terminal (with or without using any command on second)

2
  • what do you have? Did you try something? Can you give some examples?
    – tachomi
    Commented Feb 11, 2016 at 14:53
  • @tachomi on first terminal I run ls and want its output to be displayed on second terminal (with or without using any command on second)
    – Alex Jones
    Commented Feb 11, 2016 at 14:55

6 Answers 6

48

If both terminals belong to the same user, you can send your output to the virtual device that is used as the particular terminal's tty.

So you can use the output from w, which includes the TTY information, and write directly to that device.

ls > /dev/pts/7

(If the device mentioned by w was pts/7)

Another option is to use the number of a process that is connected to that device. Send your output to /proc/<process number>/fd/1.

ls > /proc/5555/fd/1

Assuming the process number that you found that runs in that terminal is 5555.

Note that this direct write is only allowed if the user that attempts to write is the same user that owns the other terminal.

6
  • 1
    I dont get /dev/pts/7 or similar with w command, but second way works for me
    – Alex Jones
    Commented Feb 11, 2016 at 15:07
  • 3
    @edwardtorvalds The /proc/XXX/fd/1 is generally a soft link to the base device. Try using ls -l /proc/XXXX/fd/1 and see what it is pointing to. Does it have any relation to what appeared in the w command? Commented Feb 11, 2016 at 15:22
  • I am not relating w command with /proc/XXX/fd/0 I am relating w command with /dev/pts/7
    – Alex Jones
    Commented Feb 11, 2016 at 15:26
  • 4
    @edwardtorvalds I am saying that they are connected as /proc/XXX/fd/1 is supposed to be a soft link to whatever device the w command displays. What is the output of ls -l on it? Commented Feb 11, 2016 at 15:27
  • 1
    TTY information can also be found in the output from tty (only current) or who (all). Commented Mar 3, 2016 at 5:53
20

You can use write command.

As @MelBurslan commented, if write permission is off, first execute:

 $ mesg y

From man mesg

OPTIONS

y Allow write access to your terminal.

Usage of write:

$ write username tty

e.g. Send ls output to other terminal.

$ w
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
user     :0       :0               08:15   ?xdm?   7:37   0.25s init --user
user     pts/0    :0               08:19    1.00s  0.09s  0.01s w
user     pts/12   :0               08:50   54.00s  0.03s  0.03s bash

$ ls | write username pts/12
3
  • on arch linux, in gnome terminal, output of w command is edward tty2 19:53 6:05 2:48 23.12s firefox and if I write ls | write edward tty2 I get no output but a beep sound
    – Alex Jones
    Commented Feb 11, 2016 at 15:00
  • also if I try it on tty5 (ctrl + alt + 5) I get error you have turned write permission off
    – Alex Jones
    Commented Feb 11, 2016 at 15:04
  • that was typing error on this site
    – Alex Jones
    Commented Feb 11, 2016 at 15:04
15

I found a similar method.

On first terminal:

 $ tty
 /dev/pts/0
 $ <no need to run any command here, just see the output>

On second terminal:

$ ls > /dev/pts/0

Now you get the output on first terminal

2
  • 1
    tail -f instead of cat could be useful
    – tachomi
    Commented Feb 11, 2016 at 15:37
  • 3
    @tachomi actually there is no need of any command to read the output
    – Alex Jones
    Commented Feb 11, 2016 at 17:37
9

Use the tty command in each terminal to identify them:

$ tty
/dev/pts/0

$ tty
/dev/pts/1

Assuming these TTYs, to redirect the first's stdout to the second, run this in the first terminal:

exec 1>/dev/pts/1

Note: Now every command output will show on pts/1

To restore default behavior stdout of pts/0:

exec 1>/dev/pts/0

See this video for a demonstration.

0
2

you can write to the terminal's TTY; for example:

in terminal 1:

$ tty 
ttys000

in terminal 2:

$ tty
ttys029

$ exec &> >(tee >(cat >&/dev/ttys000))
ls 

Output will show in both terminals in real-time even as you type.

Works on linux and macOS. The macOS TTY path is /dev/{number} while on Linux it's /dev/pts/{number}

5
  • executing exec &> >(tee >(cat >&/dev/ttys000)) does not exists, making it impossible to type.
    – Alex Jones
    Commented Apr 18, 2019 at 19:15
  • @EdwardTorvalds on linux the tty will be something like /dev/pts/0 Commented Apr 18, 2019 at 19:17
  • yeah, that is what I used to test your code
    – Alex Jones
    Commented Apr 20, 2019 at 16:55
  • @EdwardTorvalds what command doesn't exist? Commented Apr 20, 2019 at 21:48
  • your command exec &> >(tee >(cat >&/dev/pts/1)). No control key combination (ctrl+c) work. I have to kill the terminal to exit
    – Alex Jones
    Commented Apr 21, 2019 at 8:11
-2

You can use wall also:

$ wall "Message here"
2
  • you might wanna describe it in detail. I don't understand anything from this
    – Alex Jones
    Commented Feb 11, 2016 at 15:08
  • 3
    You definitely do not want to use wall (short for "write all"), as it writes to every logged-in tty session, including the one you're sending from. Instead, using write allows a specific tty to be declared. Commented Feb 11, 2016 at 19:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .