How do I store stdout in a variable?

These seems ridiculously simple but I can't get it to work. Using korn shell and I want to pass in a flag to tell my echo statements to either write to the screen for debugging or a file if not. So I have something like:

if [ $testmode = TRUE ]; then
  logout=&1
else
  logout='logfile.out'
fi  

Then later on:

echo messages>$logout

But I can't get it to write to the screen if testmode is TRUE. Tried logout=1,logout=stdout, pretty much every combination of escape/quote/backslashes around the 1 or &1 and nothing works.

What do I need to do?

Thanks,

Doug

From the title I thought you wanted: x=`cat`
but if you are lucky, your UNIX has /dev/stdout, so you can say logout=/dev/stdout and then >$logout does nothing but waste a few cycles (open() that string as a fd, close() fd 0, dup() the new fd (defaults to 0) and close() the new fd).

Even on UNIXes where there is no /dev/fd/1 or /proc/self/fd/1 = /dev/stdout, there is almost always /dev/tty = your screen (unless the script is running from cron, at, a web server, or ssh without -t or -tt, and so has no controlling terminal). However, once you go to /dev/tty, it is hard to redirect it back into a file (think calling it from expect or ssh to get a pseudo terminal that flows to some other process's stdout).

logout=/dev/stdout did the trick. That's what I was looking for.

Thanks for your help.

Doug