Bash redirect output to tty and file

I am trying to write a piece of script execution. Logs should be displayed in the second tty, as well as written to the log file.

I can do this with a simple:

echo "Hello log" > /dev/tty2
echo "Hello log" > /var/log/my_logs

But it is very inconvenient. I could also redirect the echo to a specific location:

exec 1<>/var/log/my_logs
exec 2>&1

But how can I redirect STDOUT to both / dev / tty 2 and / var / log / my_logs at once?

+4
source share
1 answer

Use tee.

echo "Hello log" | tee /dev/tty2 /var/log/my_logs > /dev/null

( , . echo "Hello log" | tee /dev/tty2 > /var/log/my_logs, . tee , .)

, exec.

exec > >(tee /dev/tty2 /var/log/my_logs)
+7

All Articles