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)

Alex Jones
  • 6,353
  • what do you have? Did you try something? Can you give some examples? – tachomi Feb 11 '16 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 Feb 11 '16 at 14:55

6 Answers6

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.

RealSkeptic
  • 1,130
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

tachomi
  • 7,592
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

Alex Jones
  • 6,353
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.

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}

-2

You can use wall also:

$ wall "Message here"
Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
  • you might wanna describe it in detail. I don't understand anything from this – Alex Jones Feb 11 '16 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. – Monty Harder Feb 11 '16 at 19:32