I have a bash application that is producing some result, and I'd like to echo the result to either
stdout or to a user chosen file. Because I also echo other interactive messages going to the screen, requiring the user to explicitly use the
> redirection when he wants to echo the result to a file is not an option (*), as those messages would also appear in the file.
Right now I have a solution, but it's ugly.
if [ -z $outfile ] then echo "$outbuf" # Write output buffer to the screen (stdout) else echo "$outbuf" > $outfile # Write output buffer to file fi
I tried to have the variable
$outfile to be equal to
&1 and perhaps something else but it would just write to file having that name and not actually to stdout. Is there a more elegant solution?
(*) I could cheat and use
stderr for that purpose, but I think it's also quite ugly, isn't it?