Reputation: 1668
I've written a bash script. It has a few echo statements. It invokes a few 3rd party tools. I would like to redirect everything that goes to stdout (from my echo statements and all the tools output) to stderr. How do I do that?
Upvotes: 2
Views: 1775
Reputation: 1668
The solution that worked for me was to enclose the text of the script within ()'s and redirect stdout to stderr like so:
(
echo 1
echo 2
tool1
) 1>&2
Upvotes: 1
Reputation: 361556
exec >&2
Put that at the top of your script to redirect all future output to stderr.
$ help exec
exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
Replace the shell with the given command.
Execute COMMAND, replacing this shell with the specified program.
ARGUMENTS become the arguments to COMMAND. If COMMAND is not specified,
any redirections take effect in the current shell.
Upvotes: 3
Reputation: 3273
You need to redirect the stdout of the command to stderr.
your_command.sh 1>&2
If you want to do this from within the script, you can wrap your whole script into one function and redirect its output to stderr:
main() {
echo hello
echo world
some_script.sh
}
main 1>&2
Upvotes: 4