I am writing a shell script and I would like to write commands in the script but redirect the output to other konsole sessions. (using kde)


First, look for the number that identifies the current tty session; lets consider the number as X:

| terminal 1               | terminal 2                |
| $ tty                    | $                         |
| /dev/pts/X               |                           |

Then, use the given number to write to the tty selected:

| terminal 1               | terminal 2                |
| $ tty                    | $ echo "foo" > /dev/pts/X |
| /dev/pts/X               |                           |

And it will produce:

| terminal 1               | terminal 2                |
| $ tty                    | $ echo "foo" > /dev/pts/X |
| /dev/pts/X               |                           |
| foo                      |                           |

You can pipe the output to a log file which is tailed by other sessions.

In any other terminal:

touch foo && tail -f foo

In the "main" terminal:

script.sh > foo

Print terminal output to file, rather than the terminal your currently in:

commands >>outputfile

In new terminal, use tail to show contents of outputfile and update it automatically as more text is added:

tail -f outputfile

Late but maybe better then never... :-)

mkfifo ~/MyOutput           # Create the FIFO (Special file) 

./MyScript.sh > ~/MyOutput  # In the terminal/console 1
cat ~/MyOutput              # In another tty/console

from info coreutils 'mkfifo invocation':

A "FIFO" is a special file type that permits independent processes to communicate. One process opens the FIFO file for writing, and another for reading, after which data can flow as with the usual anonymous pipe in shells or elsewhere.

Side notes:

  • It's better to create the FIFO not in your home directory :-)
  • You can have more than one FIFO for the same process (std out, std err).
  • Untill you will not ask a dump from FIFOs it will hold all the output.
    As you will ask the first time, it will flush out all.
  • You can redirect (or append) to another file cat MyOutput >> NewRealFile
  • You can use cat MyOutput from another terminal too!
  • Warning: If you ask to a 2 diffent programs (or instances) to redirect output to the same FIFOs the flux will be merged (no a priori way to distinguish from which programs that line comes from).
  • Warning: If you ask 2 or more times (maybe from different terminals) it will give one line for each request splitting the output to the requester.
  • It can have more complex uses: How to properly launch a program and make possible to redirect its running output (stdout and stderr) at a later stage?

Use the tty command in each terminal to identify them:

$ tty

$ tty

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy