I have a bash script with many commands, some of which might generate error messages. often, the error messages and output messages are mixed in an apparently incorrect order (for example, I see the output of command #2 and then an error message from command #1).
As far as I understand, the reason is that the commands use two output streams - stdout and stderr - and each of them is printed to the console asynchronously. So, one solution is to redirect stderr to stdout by adding "2>&1" after each command.
But since I have many commands, it would be better to do this once for all commands. Is there a single command that I can put at the start of the script, that will ensure that all error messages from all commands are redirected to stdout, so that all output appears in the correct order?
exec 2>&1