I've written a script that has a few log outs with printf "..." 1>&2
inside a function. Running that script (build.sh) and I've noticed that if I want to redirect stderr to stdout and stdout to a file[0], success is dependent on order:
./build.sh 2> /tmp/log.txt
works (obviously)./build.sh 1>/tmp/log.txt 2>&1
works./build.sh 2>&1 1>/tmp/log.txt
doesn't redirect to the file
And with echo
:
echo "test" 2>&1 >/tmp/log.txt
worksecho "test" >/tmp/log.txt 2>&1
works
This was replicated in bash, zsh, and originally reported in mksh.
When does the order of redirection statements in the command matter? Thanks!
0: Not sure why I'd do this, but a user of the script tried it and got confused when the redirection failed.