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, success is dependent on order:
./build.sh 2> /tmp/log.txtworks (obviously)
./build.sh 1>/tmp/log.txt 2>&1works
./build.sh 2>&1 1>/tmp/log.txtdoesn't redirect to the file
echo "test" 2>&1 >/tmp/log.txtworks
echo "test" >/tmp/log.txt 2>&1works
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.