LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-27-2013, 03:35 PM   #1
ali.abry
Member
 
Registered: Feb 2013
Posts: 74

Rep: Reputation: Disabled
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$
 
Old 12-27-2013, 03:53 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
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.

Last edited by astrogeek; 12-27-2013 at 03:54 PM.
 
Old 12-27-2013, 03:56 PM   #3
gengisdave
Member
 
Registered: Dec 2013
Location: Turin, Italy
Distribution: slackware
Posts: 328

Rep: Reputation: 74
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"
 
Old 12-27-2013, 04:09 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by gengisdave View Post
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)...

Last edited by astrogeek; 12-27-2013 at 04:31 PM. Reason: Transposed first second case in my brain cell...
 
Old 12-28-2013, 12:38 PM   #5
ali.abry
Member
 
Registered: Feb 2013
Posts: 74

Original Poster
Rep: Reputation: Disabled
thanks every one . I got it completely .
 
Old 12-28-2013, 01:01 PM   #6
GNU/Linux
Member
 
Registered: Sep 2012
Distribution: Slackware-14
Posts: 120

Rep: Reputation: Disabled
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.

Last edited by GNU/Linux; 12-29-2013 at 04:22 AM. Reason: quoted the unreliable source.
 
Old 12-28-2013, 01:24 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by GNU/Linux View Post
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.
 
Old 12-28-2013, 02:02 PM   #8
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by astrogeek View Post
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).
 
Old 12-29-2013, 04:19 AM   #9
GNU/Linux
Member
 
Registered: Sep 2012
Distribution: Slackware-14
Posts: 120

Rep: Reputation: Disabled
@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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
difference in GUI & CLI in command execution gr8linux Linux - Newbie 6 05-28-2009 12:06 AM
Difference between Tomcat 1.5&1.6 and Apache 2.1 & 2.2 chnlinux Linux - Newbie 1 12-04-2007 09:44 PM
AOL UK && BT Voyager 100 && Slackware 10.2 && RP-PPPoE pitt0071 Linux - Networking 3 01-17-2006 06:10 AM
Japanese canna won't work : Warning: &#12363;&#12394;&#28450;&#23383;&#22793;&am OrganicOrange84 Debian 3 06-30-2005 02:28 PM
Ph&#7909;c h&#7891;i d&#7919; li&#7879;u b&#7883; m&#7845;t???, c&#7913; pollsite General 1 06-27-2005 12:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration