LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   what's the difference between 1>&2 and 2>&1 in these two command (https://www.linuxquestions.org/questions/linux-newbie-8/whats-the-difference-between-1-and-2-and-2-and-1-in-these-two-command-4175489329/)

ali.abry 12-27-2013 03:35 PM

what's the difference between 1>&2 and 2>&1 in these two command
 
Hi every body
can you please tell me the difference between these two command :

Code:

aliali@lp:/tmp$ ls b* z* 1>&2 >/dev/null
ls: cannot access z*: No such file or directory
aliali@lp:/tmp$
aliali@lp:/tmp$ ls b* z* 2>&1 >/dev/null
ls: cannot access z*: No such file or directory
aliali@lp:/tmp$


astrogeek 12-27-2013 03:53 PM

It depends, are you asking what difference in effect, or what difference in how they work?

In the particular case you show, there is no net difference in effect between them.

In the general case they are quite different.

Research "unix file descriptors" to learn how they work, and why.

gengisdave 12-27-2013 03:56 PM

it's quite the same, the first means "send stdout to stderr and then to /dev/null", the second one means "send stderr to stdout and then to /dev/null"

astrogeek 12-27-2013 04:09 PM

Quote:

Originally Posted by gengisdave (Post 5087798)
it's quite the same, the first means "send stdout to stderr and then to /dev/null", the second one means "send stderr to stdout and then to /dev/null"

Actually, no.

The first says "send STDOUT to the same place STDERR is going, THEN send STDOUT to /dev/NULL/".

The second says "send STDERR to the same place STDOUT is going, THEN send STDOUT to /dev/NULL".

In the case of ls, STDERR is ALREADY being sent to the same place as STDOUT, so you redirect to the same place.

In the second case STDOUT is redirected to STDOUT, because STDERR is already going there, so we redirect to the same place again.

In both cases STDOUT is subsequently redirected to /dev/NULL, which has no effect on what went before (with regard to STDERR)...

ali.abry 12-28-2013 12:38 PM

thanks every one . I got it completely .

GNU/Linux 12-28-2013 01:01 PM

Just to add to the explanation given, when multiple output redirects are used they are interpreted by the shell from right to left.

Edit: ...they are interpreted by the shell from left to right, not from right to left.

astrogeek 12-28-2013 01:24 PM

Quote:

Originally Posted by GNU/Linux (Post 5088162)
Just to add to the explanation given, when multiple output redirects are used they are interpreted by the shell from right to left.

Nope...

Code:

man bash
...
REDIRECTION
      Before  a  command  is  executed, its input and output may be redirected using a special notation inter-
      preted by the shell.  Redirection may also be used to open and close files for the current shell  execu-
      tion  environment.  The  following redirection operators may precede or appear anywhere within a simple
      command or may follow a command.  Redirections are processed in the order  they  appear,  from  left  to
      right
.


rknichols 12-28-2013 02:02 PM

Quote:

Originally Posted by astrogeek (Post 5088171)
Redirections are processed in the order they appear, from left to right.

True, but you also have to keep in mind that any pipelines are set up before redirections for the individual commands are evaluated. So, for
Code:

proc1 2>&1 | proc2
the pipeline for stdout from proc1 gets established first, then stderr gets redirected to the place where stdout is now going (i.e., the pipeline).

GNU/Linux 12-29-2013 04:19 AM

@astrogeek: You are right in quoting man pages, redirection is parsed from left to right. I happened to be reading 'Study Guide for Linux Administration 1 (Lab work for LPI 101) - page 61' available at which obviously has a mistake. I'll edit my previous post.


All times are GMT -5. The time now is 05:47 PM.