Shell, behavior of conflicting redirection2019 Community Moderator ElectionCalling shell commands from RubyCheck if a directory exists in a shell scriptHow to manage a redirect request after a jQuery Ajax callHow do I prompt for Yes/No/Cancel input in a Linux shell script?How do I redirect to another webpage?How do I make a redirect in PHP?In the shell, what does “ 2>&1 ” mean?How can I redirect and append both stdout and stderr to a file with Bash?How do I redirect with JavaScript?Check existence of input argument in a Bash shell script

Is there a place to find the pricing for things not mentioned in the PHB? (non-magical)

Aluminum electrolytic or ceramic capacitors for linear regulator input and output?

Why do newer 737s use two different styles of split winglets?

Are all passive ability checks floors for active ability checks?

Describing a chess game in a novel

How to terminate ping <dest> &

What did “the good wine” (τὸν καλὸν οἶνον) mean in John 2:10?

How can we have a quark condensate without a quark potential?

How to make healing in an exploration game interesting

How could a scammer know the apps on my phone / iTunes account?

Why Choose Less Effective Armour Types?

Why is the President allowed to veto a cancellation of emergency powers?

About the actual radiative impact of greenhouse gas emission over time

Are ETF trackers fundamentally better than individual stocks?

What's the meaning of a knight fighting a snail in medieval book illustrations?

How do you talk to someone whose loved one is dying?

PTIJ: Who should I vote for? (21st Knesset Edition)

How to explain that I do not want to visit a country due to personal safety concern?

Why is a white electrical wire connected to 2 black wires?

What exactly is this small puffer fish doing and how did it manage to accomplish such a feat?

If I can solve Sudoku, can I solve the Travelling Salesman Problem (TSP)? If so, how?

What are substitutions for coconut in curry?

English sentence unclear

Math equation in non italic font



Shell, behavior of conflicting redirection



2019 Community Moderator ElectionCalling shell commands from RubyCheck if a directory exists in a shell scriptHow to manage a redirect request after a jQuery Ajax callHow do I prompt for Yes/No/Cancel input in a Linux shell script?How do I redirect to another webpage?How do I make a redirect in PHP?In the shell, what does “ 2>&1 ” mean?How can I redirect and append both stdout and stderr to a file with Bash?How do I redirect with JavaScript?Check existence of input argument in a Bash shell script










0















I'm facing to a problem that I'm not able to explain. I've to recode a shell, and there is a weird behavior for me.



echo test >&2 2>&1



This kind of command is writing on stderr (so with the first redirection), why the second redirection isn't impacting the output destination ? Why the output isn't on stdout ?



I saw some stuff that redirection is happening before executing command, from left to right, so why the second redirection is not cancelling the first one ?



Thanks in advance.



edit: I'm running my script on bash.










share|improve this question
























  • BashFAQ #55 (Tell me all about 2>&1 -- what's the difference between 2>&1 >foo and >foo 2>&1, and when do I use which?) is pertinent.

    – Charles Duffy
    Mar 7 at 16:27
















0















I'm facing to a problem that I'm not able to explain. I've to recode a shell, and there is a weird behavior for me.



echo test >&2 2>&1



This kind of command is writing on stderr (so with the first redirection), why the second redirection isn't impacting the output destination ? Why the output isn't on stdout ?



I saw some stuff that redirection is happening before executing command, from left to right, so why the second redirection is not cancelling the first one ?



Thanks in advance.



edit: I'm running my script on bash.










share|improve this question
























  • BashFAQ #55 (Tell me all about 2>&1 -- what's the difference between 2>&1 >foo and >foo 2>&1, and when do I use which?) is pertinent.

    – Charles Duffy
    Mar 7 at 16:27














0












0








0








I'm facing to a problem that I'm not able to explain. I've to recode a shell, and there is a weird behavior for me.



echo test >&2 2>&1



This kind of command is writing on stderr (so with the first redirection), why the second redirection isn't impacting the output destination ? Why the output isn't on stdout ?



I saw some stuff that redirection is happening before executing command, from left to right, so why the second redirection is not cancelling the first one ?



Thanks in advance.



edit: I'm running my script on bash.










share|improve this question
















I'm facing to a problem that I'm not able to explain. I've to recode a shell, and there is a weird behavior for me.



echo test >&2 2>&1



This kind of command is writing on stderr (so with the first redirection), why the second redirection isn't impacting the output destination ? Why the output isn't on stdout ?



I saw some stuff that redirection is happening before executing command, from left to right, so why the second redirection is not cancelling the first one ?



Thanks in advance.



edit: I'm running my script on bash.







shell redirect






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 15:38







rSim

















asked Mar 7 at 15:29









rSimrSim

298




298












  • BashFAQ #55 (Tell me all about 2>&1 -- what's the difference between 2>&1 >foo and >foo 2>&1, and when do I use which?) is pertinent.

    – Charles Duffy
    Mar 7 at 16:27


















  • BashFAQ #55 (Tell me all about 2>&1 -- what's the difference between 2>&1 >foo and >foo 2>&1, and when do I use which?) is pertinent.

    – Charles Duffy
    Mar 7 at 16:27

















BashFAQ #55 (Tell me all about 2>&1 -- what's the difference between 2>&1 >foo and >foo 2>&1, and when do I use which?) is pertinent.

– Charles Duffy
Mar 7 at 16:27






BashFAQ #55 (Tell me all about 2>&1 -- what's the difference between 2>&1 >foo and >foo 2>&1, and when do I use which?) is pertinent.

– Charles Duffy
Mar 7 at 16:27













2 Answers
2






active

oldest

votes


















1














cmd >&2 2>&1 is very different than cmd 2>&1 >&2, since the redirections occur in a different order. Suppose the command is invoked from a process which has fd 1 attached to a file named 'output' and fd 2 attached to a filed named 'error'. Then cmd >&2 2>&1 will redirect the stdout of cmd to the file named error and then redirect stderr of cmd to whatever fd 1 is connected to, namely the file named error. But cmd 2>&1 >&2 will first redirect fd 2 to 'output' and the redirect fd 1 to the same place. In other words, cmd >&2 2>&1 writes everything to stderr, and cmd 2>&1 >&2 writes everything on stdout.






share|improve this answer






























    2














    x>&y means "redirect file descriptor x to whatever fd y is currently pointing to.

    Redirections are processed strictly left to right.



    So, >&2 2>&1 points fd 1 to something like /dev/stderr, and then points fd 2 to /dev/stderr also.



    If you want to swap stderr and stdout, you need a 3rd file descriptor:



    (echo "test to stdout"; echo "test to stderr" >&2) 3>&1 1>&2 2>&3 3>&-
    # ................................................ A .. B .. C .. D
    # A. fd 3 = /dev/stdout
    # B. fd 1 = /dev/stderr
    # C. fd 2 = /dev/stdout
    # D. fd 3 is closed


    Let's put that in a function for easier testing



    fdtest() (echo "test to stdout"; echo "test to stderr" >&2) 3>&1 1>&2 2>&3 3>&-; 


    Run it



    $ fdtest
    test to stdout
    test to stderr


    Throw away standard error (we expect to see the "stderr" message on standard out).



    $ fdtest 2>/dev/null
    test to stderr


    Throw away standard out (we expect to see the "stdout" message on standard err).



    $ fdtest 1>/dev/null
    test to stdout





    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55047396%2fshell-behavior-of-conflicting-redirection%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      cmd >&2 2>&1 is very different than cmd 2>&1 >&2, since the redirections occur in a different order. Suppose the command is invoked from a process which has fd 1 attached to a file named 'output' and fd 2 attached to a filed named 'error'. Then cmd >&2 2>&1 will redirect the stdout of cmd to the file named error and then redirect stderr of cmd to whatever fd 1 is connected to, namely the file named error. But cmd 2>&1 >&2 will first redirect fd 2 to 'output' and the redirect fd 1 to the same place. In other words, cmd >&2 2>&1 writes everything to stderr, and cmd 2>&1 >&2 writes everything on stdout.






      share|improve this answer



























        1














        cmd >&2 2>&1 is very different than cmd 2>&1 >&2, since the redirections occur in a different order. Suppose the command is invoked from a process which has fd 1 attached to a file named 'output' and fd 2 attached to a filed named 'error'. Then cmd >&2 2>&1 will redirect the stdout of cmd to the file named error and then redirect stderr of cmd to whatever fd 1 is connected to, namely the file named error. But cmd 2>&1 >&2 will first redirect fd 2 to 'output' and the redirect fd 1 to the same place. In other words, cmd >&2 2>&1 writes everything to stderr, and cmd 2>&1 >&2 writes everything on stdout.






        share|improve this answer

























          1












          1








          1







          cmd >&2 2>&1 is very different than cmd 2>&1 >&2, since the redirections occur in a different order. Suppose the command is invoked from a process which has fd 1 attached to a file named 'output' and fd 2 attached to a filed named 'error'. Then cmd >&2 2>&1 will redirect the stdout of cmd to the file named error and then redirect stderr of cmd to whatever fd 1 is connected to, namely the file named error. But cmd 2>&1 >&2 will first redirect fd 2 to 'output' and the redirect fd 1 to the same place. In other words, cmd >&2 2>&1 writes everything to stderr, and cmd 2>&1 >&2 writes everything on stdout.






          share|improve this answer













          cmd >&2 2>&1 is very different than cmd 2>&1 >&2, since the redirections occur in a different order. Suppose the command is invoked from a process which has fd 1 attached to a file named 'output' and fd 2 attached to a filed named 'error'. Then cmd >&2 2>&1 will redirect the stdout of cmd to the file named error and then redirect stderr of cmd to whatever fd 1 is connected to, namely the file named error. But cmd 2>&1 >&2 will first redirect fd 2 to 'output' and the redirect fd 1 to the same place. In other words, cmd >&2 2>&1 writes everything to stderr, and cmd 2>&1 >&2 writes everything on stdout.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 7 at 16:25









          William PursellWilliam Pursell

          133k32206240




          133k32206240























              2














              x>&y means "redirect file descriptor x to whatever fd y is currently pointing to.

              Redirections are processed strictly left to right.



              So, >&2 2>&1 points fd 1 to something like /dev/stderr, and then points fd 2 to /dev/stderr also.



              If you want to swap stderr and stdout, you need a 3rd file descriptor:



              (echo "test to stdout"; echo "test to stderr" >&2) 3>&1 1>&2 2>&3 3>&-
              # ................................................ A .. B .. C .. D
              # A. fd 3 = /dev/stdout
              # B. fd 1 = /dev/stderr
              # C. fd 2 = /dev/stdout
              # D. fd 3 is closed


              Let's put that in a function for easier testing



              fdtest() (echo "test to stdout"; echo "test to stderr" >&2) 3>&1 1>&2 2>&3 3>&-; 


              Run it



              $ fdtest
              test to stdout
              test to stderr


              Throw away standard error (we expect to see the "stderr" message on standard out).



              $ fdtest 2>/dev/null
              test to stderr


              Throw away standard out (we expect to see the "stdout" message on standard err).



              $ fdtest 1>/dev/null
              test to stdout





              share|improve this answer





























                2














                x>&y means "redirect file descriptor x to whatever fd y is currently pointing to.

                Redirections are processed strictly left to right.



                So, >&2 2>&1 points fd 1 to something like /dev/stderr, and then points fd 2 to /dev/stderr also.



                If you want to swap stderr and stdout, you need a 3rd file descriptor:



                (echo "test to stdout"; echo "test to stderr" >&2) 3>&1 1>&2 2>&3 3>&-
                # ................................................ A .. B .. C .. D
                # A. fd 3 = /dev/stdout
                # B. fd 1 = /dev/stderr
                # C. fd 2 = /dev/stdout
                # D. fd 3 is closed


                Let's put that in a function for easier testing



                fdtest() (echo "test to stdout"; echo "test to stderr" >&2) 3>&1 1>&2 2>&3 3>&-; 


                Run it



                $ fdtest
                test to stdout
                test to stderr


                Throw away standard error (we expect to see the "stderr" message on standard out).



                $ fdtest 2>/dev/null
                test to stderr


                Throw away standard out (we expect to see the "stdout" message on standard err).



                $ fdtest 1>/dev/null
                test to stdout





                share|improve this answer



























                  2












                  2








                  2







                  x>&y means "redirect file descriptor x to whatever fd y is currently pointing to.

                  Redirections are processed strictly left to right.



                  So, >&2 2>&1 points fd 1 to something like /dev/stderr, and then points fd 2 to /dev/stderr also.



                  If you want to swap stderr and stdout, you need a 3rd file descriptor:



                  (echo "test to stdout"; echo "test to stderr" >&2) 3>&1 1>&2 2>&3 3>&-
                  # ................................................ A .. B .. C .. D
                  # A. fd 3 = /dev/stdout
                  # B. fd 1 = /dev/stderr
                  # C. fd 2 = /dev/stdout
                  # D. fd 3 is closed


                  Let's put that in a function for easier testing



                  fdtest() (echo "test to stdout"; echo "test to stderr" >&2) 3>&1 1>&2 2>&3 3>&-; 


                  Run it



                  $ fdtest
                  test to stdout
                  test to stderr


                  Throw away standard error (we expect to see the "stderr" message on standard out).



                  $ fdtest 2>/dev/null
                  test to stderr


                  Throw away standard out (we expect to see the "stdout" message on standard err).



                  $ fdtest 1>/dev/null
                  test to stdout





                  share|improve this answer















                  x>&y means "redirect file descriptor x to whatever fd y is currently pointing to.

                  Redirections are processed strictly left to right.



                  So, >&2 2>&1 points fd 1 to something like /dev/stderr, and then points fd 2 to /dev/stderr also.



                  If you want to swap stderr and stdout, you need a 3rd file descriptor:



                  (echo "test to stdout"; echo "test to stderr" >&2) 3>&1 1>&2 2>&3 3>&-
                  # ................................................ A .. B .. C .. D
                  # A. fd 3 = /dev/stdout
                  # B. fd 1 = /dev/stderr
                  # C. fd 2 = /dev/stdout
                  # D. fd 3 is closed


                  Let's put that in a function for easier testing



                  fdtest() (echo "test to stdout"; echo "test to stderr" >&2) 3>&1 1>&2 2>&3 3>&-; 


                  Run it



                  $ fdtest
                  test to stdout
                  test to stderr


                  Throw away standard error (we expect to see the "stderr" message on standard out).



                  $ fdtest 2>/dev/null
                  test to stderr


                  Throw away standard out (we expect to see the "stdout" message on standard err).



                  $ fdtest 1>/dev/null
                  test to stdout






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 7 at 16:29

























                  answered Mar 7 at 16:24









                  glenn jackmanglenn jackman

                  170k26147240




                  170k26147240



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55047396%2fshell-behavior-of-conflicting-redirection%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                      2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

                      Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme