Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Consider such code:

echo hello >> log.txt
echo world >> log.txt

I execute command with stdout redirection. Is it possible to create sticky redirection? Something like this:

redir stdout log.txt
echo hello
echo world

This would be equivalent to the first piece code.

What I really need -- make such sticky redirection for stdout and stderr (at the same time), execute some commands, stop redirection, and read log file.

share|improve this question
up vote 9 down vote accepted
exec 3>&1 4>&2 >log.txt 2>&1
echo hello
echo world
exec 1>&3 2>&4
less log.txt

I.e.: make file descriptors 3 and 4 copies of the current 1 (stdout) and 2 (stderr), redirect 1 to log.txt and 2 to the same as 1; then perform commands; then restore stdout and stderr from the saved values in file descriptors 3 and 4.

share|improve this answer
Thank you very much! – greenoldman Aug 21 '13 at 11:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.