Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > gnu.bash.bug > #15011 > unrolled thread
| Started by | Eduardo A. Bustamante López <dualbus@gmail.com> |
|---|---|
| First post | 2018-12-30 13:40 -0800 |
| Last post | 2018-12-30 13:40 -0800 |
| Articles | 1 — 1 participant |
Back to article view | Back to gnu.bash.bug
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: "COMMAND 2>&1 > PATH" doesn't work Eduardo A. Bustamante López <dualbus@gmail.com> - 2018-12-30 13:40 -0800
| From | Eduardo A. Bustamante López <dualbus@gmail.com> |
|---|---|
| Date | 2018-12-30 13:40 -0800 |
| Subject | Re: "COMMAND 2>&1 > PATH" doesn't work |
| Message-ID | <mailman.6590.1546206035.1284.bug-bash@gnu.org> |
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
Back to top | Article view | gnu.bash.bug
csiph-web