On Sun, Dec 30, 2018 at 10:10:42PM +0100, Dušan Kreheľ wrote: > Hello. > > If I try in bash this command ex. "rmdir somethingA > somethingA.out > 2>&1" work right. But, if I try "rmdir somethingB 2>&1 > > somethingB.out" that work wrong. > > That work's wrong in Bash version 4.4.23(1) and too in 5.0.0(1)-rc1:
Why do you expect these two to behave in the same way? They are clearly distinct operations. COMMAND >FILE 2>&1 is not the same as: COMMAND 2>&1 >FILE The order of redirections matter, they are processed left-to-right. Let's look at these examples in more detail. Example 1. COMMAND >FILE 2>&1 Let's break it down into tokens and see what happens at each step. a) COMMAND file descriptor 0 -> terminal file descriptor 1 -> terminal file descriptor 2 -> terminal This is the "normal" state in an interactive shell, the three standard file descriptors (input: 0, output: 1, and error: 2) are pointing to the terminal device. b) >FILE file descriptor 0 -> terminal file descriptor 1 -> FILE file descriptor 2 -> terminal The `>FILE' redirection is processed, causing file descriptor 1 (standard output) to be directed to the file named `FILE'. c) 2>&1 file descriptor 0 -> terminal file descriptor 1 -> FILE file descriptor 2 -> FILE The `2>&1' redirection is processed, causing file descriptor 2 (the `2>' bit) to be directed to "wherever-fd1-points-to" (i.e. the `&1' bit). FD1 currently points to `FILE', so FD2 is updated to point to `FILE' too. VS Example 2. COMMAND 2>&1 FILE a) COMMAND file descriptor 0 -> terminal file descriptor 1 -> terminal file descriptor 2 -> terminal same as example 1. b) 2>&1 file descriptor 0 -> terminal file descriptor 1 -> terminal file descriptor 2 -> terminal * no change The `2>&1' redirection is processed, causing file descriptor 2 to be directed to "wherever-fd1-points-to". FD1 currently points to the terminal. FD2 points to the terminal too, so it stays the same. c) >FILE file descriptor 0 -> terminal file descriptor 1 -> FILE file descriptor 2 -> terminal The `>FILE' redirection is processed, causing file descriptor 1 (standard output) to be directed to the file named `FILE'. So, this is not a bug. It's the expected behavior of redirections. If you want to read more about it, I recommend: - https://mywiki.wooledge.org/BashFAQ/055 - http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_07