I have been trying to figure out the relationship between file descriptors. One thing I don't understand is, how is:ls -l /bin/usr > ls-output.txt 2>&1different from:ls -l /bin/usr 2>&1 >ls-output.txt
redirecting std output and std error
io redirection;stdout;stderr
The order of the redirection is important as they are executed sequentially:>filename 2>&1stdout (fd 1) will point to filename and afterwards the stderr (fd 2) will point to the the target of stdout in this example filename.That means that both stdout and stderr get redirected to filename2>&1 >filenameHere stderr (fd 2) will point to the target of stdout and afterwards stdout (fd 1) will redirect to filename.This means that stderr will redirect to the original target of stdout and stdout gets redirected to filename.So in short the order of redirects is important as each filedescriptor is independent of each other.Additional InformationFor further information have a look at some other questions and answers such as: File descriptors & shell scripting What does 3>&1 1>&2 2>&3 do in a script? etc.