Using exec 1>&2 in a POSIX shell script function. Will it interfere with other io-redirects?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite














I am in process of writing a POSIX shell script.



I have one function, which redirects (almost) all output to standard error stream like that:



# print something to stderr
printf "..." $var 1>&2


It has been pointed out to me that there is a way to redirect all of the function's output with:



# redirect all output to standard error stream
exec 1>&2


keeping this line in the beginning of the function.



If there were no other redirects, I would simply agree probably, but I have there a test for color support:



# check if we have color support
if command -v tput > /dev/null 2>&1 && tput setaf 1 > /dev/null 2>&1


Basically, I don't know how the exec works, and I don't need to know much in detail now, I just need to know if that exec line will interfere with the above color support test anyhow?










share|improve this question

























    up vote
    1
    down vote

    favorite














    I am in process of writing a POSIX shell script.



    I have one function, which redirects (almost) all output to standard error stream like that:



    # print something to stderr
    printf "..." $var 1>&2


    It has been pointed out to me that there is a way to redirect all of the function's output with:



    # redirect all output to standard error stream
    exec 1>&2


    keeping this line in the beginning of the function.



    If there were no other redirects, I would simply agree probably, but I have there a test for color support:



    # check if we have color support
    if command -v tput > /dev/null 2>&1 && tput setaf 1 > /dev/null 2>&1


    Basically, I don't know how the exec works, and I don't need to know much in detail now, I just need to know if that exec line will interfere with the above color support test anyhow?










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite













      I am in process of writing a POSIX shell script.



      I have one function, which redirects (almost) all output to standard error stream like that:



      # print something to stderr
      printf "..." $var 1>&2


      It has been pointed out to me that there is a way to redirect all of the function's output with:



      # redirect all output to standard error stream
      exec 1>&2


      keeping this line in the beginning of the function.



      If there were no other redirects, I would simply agree probably, but I have there a test for color support:



      # check if we have color support
      if command -v tput > /dev/null 2>&1 && tput setaf 1 > /dev/null 2>&1


      Basically, I don't know how the exec works, and I don't need to know much in detail now, I just need to know if that exec line will interfere with the above color support test anyhow?










      share|improve this question















      I am in process of writing a POSIX shell script.



      I have one function, which redirects (almost) all output to standard error stream like that:



      # print something to stderr
      printf "..." $var 1>&2


      It has been pointed out to me that there is a way to redirect all of the function's output with:



      # redirect all output to standard error stream
      exec 1>&2


      keeping this line in the beginning of the function.



      If there were no other redirects, I would simply agree probably, but I have there a test for color support:



      # check if we have color support
      if command -v tput > /dev/null 2>&1 && tput setaf 1 > /dev/null 2>&1


      Basically, I don't know how the exec works, and I don't need to know much in detail now, I just need to know if that exec line will interfere with the above color support test anyhow?







      scripting io-redirection posix






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 4 at 4:06









      Vlastimil

      6,8871149124




      6,8871149124




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted












          exec <redirection> will redirect the current shell's (or subshell's) I/O streams. And since commands started by the shell inherit the shell's I/O streams, those redirections will affect them, with command-specific redirections applied on top.



          So, with something like:



          exec >/some/file 2>&1
          foo >/another/file


          • After the exec, the shell's stdout goes /some/file, and stderr also goes to /some/file.

          • When starting foo, the shell redirects foo's stdout to /another/file, but stderr is inherited unchanged and will go to /some/file.

          In these commands:



          command -v tput > /dev/null 2>&1 && tput setaf 1 > /dev/null 2>&1


          You're not using the inherited stdout/stderr streams at all. First, stdout is redirected to /dev/null, then stderr as well. So that particular exec line won't affect these commands.






          share|improve this answer






















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473131%2fusing-exec-12-in-a-posix-shell-script-function-will-it-interfere-with-other-i%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted












            exec <redirection> will redirect the current shell's (or subshell's) I/O streams. And since commands started by the shell inherit the shell's I/O streams, those redirections will affect them, with command-specific redirections applied on top.



            So, with something like:



            exec >/some/file 2>&1
            foo >/another/file


            • After the exec, the shell's stdout goes /some/file, and stderr also goes to /some/file.

            • When starting foo, the shell redirects foo's stdout to /another/file, but stderr is inherited unchanged and will go to /some/file.

            In these commands:



            command -v tput > /dev/null 2>&1 && tput setaf 1 > /dev/null 2>&1


            You're not using the inherited stdout/stderr streams at all. First, stdout is redirected to /dev/null, then stderr as well. So that particular exec line won't affect these commands.






            share|improve this answer


























              up vote
              2
              down vote



              accepted












              exec <redirection> will redirect the current shell's (or subshell's) I/O streams. And since commands started by the shell inherit the shell's I/O streams, those redirections will affect them, with command-specific redirections applied on top.



              So, with something like:



              exec >/some/file 2>&1
              foo >/another/file


              • After the exec, the shell's stdout goes /some/file, and stderr also goes to /some/file.

              • When starting foo, the shell redirects foo's stdout to /another/file, but stderr is inherited unchanged and will go to /some/file.

              In these commands:



              command -v tput > /dev/null 2>&1 && tput setaf 1 > /dev/null 2>&1


              You're not using the inherited stdout/stderr streams at all. First, stdout is redirected to /dev/null, then stderr as well. So that particular exec line won't affect these commands.






              share|improve this answer
























                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted








                exec <redirection> will redirect the current shell's (or subshell's) I/O streams. And since commands started by the shell inherit the shell's I/O streams, those redirections will affect them, with command-specific redirections applied on top.



                So, with something like:



                exec >/some/file 2>&1
                foo >/another/file


                • After the exec, the shell's stdout goes /some/file, and stderr also goes to /some/file.

                • When starting foo, the shell redirects foo's stdout to /another/file, but stderr is inherited unchanged and will go to /some/file.

                In these commands:



                command -v tput > /dev/null 2>&1 && tput setaf 1 > /dev/null 2>&1


                You're not using the inherited stdout/stderr streams at all. First, stdout is redirected to /dev/null, then stderr as well. So that particular exec line won't affect these commands.






                share|improve this answer
















                exec <redirection> will redirect the current shell's (or subshell's) I/O streams. And since commands started by the shell inherit the shell's I/O streams, those redirections will affect them, with command-specific redirections applied on top.



                So, with something like:



                exec >/some/file 2>&1
                foo >/another/file


                • After the exec, the shell's stdout goes /some/file, and stderr also goes to /some/file.

                • When starting foo, the shell redirects foo's stdout to /another/file, but stderr is inherited unchanged and will go to /some/file.

                In these commands:



                command -v tput > /dev/null 2>&1 && tput setaf 1 > /dev/null 2>&1


                You're not using the inherited stdout/stderr streams at all. First, stdout is redirected to /dev/null, then stderr as well. So that particular exec line won't affect these commands.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Oct 4 at 6:28









                Vlastimil

                6,8871149124




                6,8871149124










                answered Oct 4 at 4:34









                Olorin

                1,35411




                1,35411



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473131%2fusing-exec-12-in-a-posix-shell-script-function-will-it-interfere-with-other-i%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?