What logic does the command “exec tail -n +3 $0” from grub2 config have?












7














Creating custom menu entry, got stuck on this command:



exec tail -n +3 $0


Tried it in terminal, got weird result, cannot understand, what this command exactly does and why grub needs it. Could you explain, please?










share|improve this question





























    7














    Creating custom menu entry, got stuck on this command:



    exec tail -n +3 $0


    Tried it in terminal, got weird result, cannot understand, what this command exactly does and why grub needs it. Could you explain, please?










    share|improve this question



























      7












      7








      7


      1





      Creating custom menu entry, got stuck on this command:



      exec tail -n +3 $0


      Tried it in terminal, got weird result, cannot understand, what this command exactly does and why grub needs it. Could you explain, please?










      share|improve this question















      Creating custom menu entry, got stuck on this command:



      exec tail -n +3 $0


      Tried it in terminal, got weird result, cannot understand, what this command exactly does and why grub needs it. Could you explain, please?







      shell grub2 tail exec






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 28 '18 at 12:06









      muru

      1




      1










      asked Jan 28 '18 at 11:28









      ImajouImajou

      383




      383






















          2 Answers
          2






          active

          oldest

          votes


















          10














          tail -n +3 prints its input, starting at line 3 (man page). $0 is the name of the script in a shell script (Bash special parameters) and exec (Bash builtins) replaces the script with the command. You probably have something like this (like in /etc/grub.d/40_custom on my system):



          #!/bin/sh
          exec tail -n +3 $0
          foo
          bar


          When you run the script, it replaces itself with tail reading the script itself, so the rest of the script gets copied to its output.



          I think grub has a bunch of scripts to create its config, they're probably executed as grubscript.sh >> grub-config-file or something to effect. The scripts could use any logic they need to produce the output, but the exec tail trick allows to just dump some fixed lines in the output without changing the logic the script is started with.



          In addition to that magic incantation, Debian's /etc/grub.d/40_custom also includes a comment telling the user to




          Simply type the menu entries you want to add after this comment.







          share|improve this answer























          • FWIW, squinting hard and assuming # is a comment character for grub anway, #!/bin/cat should work, too. (You'll have the shebang comment line in the output, though.)
            – Ulrich Schwarz
            6 hours ago



















          11














          If you're talking about /etc/grub.d/40_custom:



          $ cat /etc/grub.d/40_custom
          #!/bin/sh
          exec tail -n +3 $0
          # This file provides an easy way to add custom menu entries. Simply type the
          # menu entries you want to add after this comment. Be careful not to change
          # the 'exec tail' line above.


          Then note that:




          • this is a shell script, and is executed by grub-mkconfig to build GRUB configuration

          • this file is supposed to be "an easy way to add custom menu entries" - you just type in exactly whatever GRUB configuration you want.


          But this is a shell script, so usually you'd have to do something like echo "menuentry ...." etc. To avoid that, the exec tail magic is used. What does that do? $0, remember, is the name of the script as executed, so typically it would be 40_custom (or /etc/grub.d/40_custom, etc. depending on where and how it was run). So the script is essentially running tail on itself, but with -n +3, which tells tail to start from the third line.



          What do you get if you output everything from the third line onwards in /etc/grub.d/40_custom?



          # This file provides an easy way to add custom menu entries.  Simply type the
          # menu entries you want to add after this comment. Be careful not to change
          # the 'exec tail' line above.


          (And additionally whatever else you put below this.)



          The exec part replaces the shell that's executing the script with tail, so effectively nothing further from the script is executed.





          Running it in the terminal:





          • $0 is probably bash or something like that (it could be /bin/bash)

          • and because of the exec, you're replacing the running shell with tail -n+3 bash

          • and since you probably don't have a file named bash in your current directory, tail promptly quits.


          So the end result is likely that your terminal session ended there.






          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',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2funix.stackexchange.com%2fquestions%2f420205%2fwhat-logic-does-the-command-exec-tail-n-3-0-from-grub2-config-have%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









            10














            tail -n +3 prints its input, starting at line 3 (man page). $0 is the name of the script in a shell script (Bash special parameters) and exec (Bash builtins) replaces the script with the command. You probably have something like this (like in /etc/grub.d/40_custom on my system):



            #!/bin/sh
            exec tail -n +3 $0
            foo
            bar


            When you run the script, it replaces itself with tail reading the script itself, so the rest of the script gets copied to its output.



            I think grub has a bunch of scripts to create its config, they're probably executed as grubscript.sh >> grub-config-file or something to effect. The scripts could use any logic they need to produce the output, but the exec tail trick allows to just dump some fixed lines in the output without changing the logic the script is started with.



            In addition to that magic incantation, Debian's /etc/grub.d/40_custom also includes a comment telling the user to




            Simply type the menu entries you want to add after this comment.







            share|improve this answer























            • FWIW, squinting hard and assuming # is a comment character for grub anway, #!/bin/cat should work, too. (You'll have the shebang comment line in the output, though.)
              – Ulrich Schwarz
              6 hours ago
















            10














            tail -n +3 prints its input, starting at line 3 (man page). $0 is the name of the script in a shell script (Bash special parameters) and exec (Bash builtins) replaces the script with the command. You probably have something like this (like in /etc/grub.d/40_custom on my system):



            #!/bin/sh
            exec tail -n +3 $0
            foo
            bar


            When you run the script, it replaces itself with tail reading the script itself, so the rest of the script gets copied to its output.



            I think grub has a bunch of scripts to create its config, they're probably executed as grubscript.sh >> grub-config-file or something to effect. The scripts could use any logic they need to produce the output, but the exec tail trick allows to just dump some fixed lines in the output without changing the logic the script is started with.



            In addition to that magic incantation, Debian's /etc/grub.d/40_custom also includes a comment telling the user to




            Simply type the menu entries you want to add after this comment.







            share|improve this answer























            • FWIW, squinting hard and assuming # is a comment character for grub anway, #!/bin/cat should work, too. (You'll have the shebang comment line in the output, though.)
              – Ulrich Schwarz
              6 hours ago














            10












            10








            10






            tail -n +3 prints its input, starting at line 3 (man page). $0 is the name of the script in a shell script (Bash special parameters) and exec (Bash builtins) replaces the script with the command. You probably have something like this (like in /etc/grub.d/40_custom on my system):



            #!/bin/sh
            exec tail -n +3 $0
            foo
            bar


            When you run the script, it replaces itself with tail reading the script itself, so the rest of the script gets copied to its output.



            I think grub has a bunch of scripts to create its config, they're probably executed as grubscript.sh >> grub-config-file or something to effect. The scripts could use any logic they need to produce the output, but the exec tail trick allows to just dump some fixed lines in the output without changing the logic the script is started with.



            In addition to that magic incantation, Debian's /etc/grub.d/40_custom also includes a comment telling the user to




            Simply type the menu entries you want to add after this comment.







            share|improve this answer














            tail -n +3 prints its input, starting at line 3 (man page). $0 is the name of the script in a shell script (Bash special parameters) and exec (Bash builtins) replaces the script with the command. You probably have something like this (like in /etc/grub.d/40_custom on my system):



            #!/bin/sh
            exec tail -n +3 $0
            foo
            bar


            When you run the script, it replaces itself with tail reading the script itself, so the rest of the script gets copied to its output.



            I think grub has a bunch of scripts to create its config, they're probably executed as grubscript.sh >> grub-config-file or something to effect. The scripts could use any logic they need to produce the output, but the exec tail trick allows to just dump some fixed lines in the output without changing the logic the script is started with.



            In addition to that magic incantation, Debian's /etc/grub.d/40_custom also includes a comment telling the user to




            Simply type the menu entries you want to add after this comment.








            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 5 mins ago

























            answered Jan 28 '18 at 11:55









            ilkkachuilkkachu

            56.3k784156




            56.3k784156












            • FWIW, squinting hard and assuming # is a comment character for grub anway, #!/bin/cat should work, too. (You'll have the shebang comment line in the output, though.)
              – Ulrich Schwarz
              6 hours ago


















            • FWIW, squinting hard and assuming # is a comment character for grub anway, #!/bin/cat should work, too. (You'll have the shebang comment line in the output, though.)
              – Ulrich Schwarz
              6 hours ago
















            FWIW, squinting hard and assuming # is a comment character for grub anway, #!/bin/cat should work, too. (You'll have the shebang comment line in the output, though.)
            – Ulrich Schwarz
            6 hours ago




            FWIW, squinting hard and assuming # is a comment character for grub anway, #!/bin/cat should work, too. (You'll have the shebang comment line in the output, though.)
            – Ulrich Schwarz
            6 hours ago













            11














            If you're talking about /etc/grub.d/40_custom:



            $ cat /etc/grub.d/40_custom
            #!/bin/sh
            exec tail -n +3 $0
            # This file provides an easy way to add custom menu entries. Simply type the
            # menu entries you want to add after this comment. Be careful not to change
            # the 'exec tail' line above.


            Then note that:




            • this is a shell script, and is executed by grub-mkconfig to build GRUB configuration

            • this file is supposed to be "an easy way to add custom menu entries" - you just type in exactly whatever GRUB configuration you want.


            But this is a shell script, so usually you'd have to do something like echo "menuentry ...." etc. To avoid that, the exec tail magic is used. What does that do? $0, remember, is the name of the script as executed, so typically it would be 40_custom (or /etc/grub.d/40_custom, etc. depending on where and how it was run). So the script is essentially running tail on itself, but with -n +3, which tells tail to start from the third line.



            What do you get if you output everything from the third line onwards in /etc/grub.d/40_custom?



            # This file provides an easy way to add custom menu entries.  Simply type the
            # menu entries you want to add after this comment. Be careful not to change
            # the 'exec tail' line above.


            (And additionally whatever else you put below this.)



            The exec part replaces the shell that's executing the script with tail, so effectively nothing further from the script is executed.





            Running it in the terminal:





            • $0 is probably bash or something like that (it could be /bin/bash)

            • and because of the exec, you're replacing the running shell with tail -n+3 bash

            • and since you probably don't have a file named bash in your current directory, tail promptly quits.


            So the end result is likely that your terminal session ended there.






            share|improve this answer




























              11














              If you're talking about /etc/grub.d/40_custom:



              $ cat /etc/grub.d/40_custom
              #!/bin/sh
              exec tail -n +3 $0
              # This file provides an easy way to add custom menu entries. Simply type the
              # menu entries you want to add after this comment. Be careful not to change
              # the 'exec tail' line above.


              Then note that:




              • this is a shell script, and is executed by grub-mkconfig to build GRUB configuration

              • this file is supposed to be "an easy way to add custom menu entries" - you just type in exactly whatever GRUB configuration you want.


              But this is a shell script, so usually you'd have to do something like echo "menuentry ...." etc. To avoid that, the exec tail magic is used. What does that do? $0, remember, is the name of the script as executed, so typically it would be 40_custom (or /etc/grub.d/40_custom, etc. depending on where and how it was run). So the script is essentially running tail on itself, but with -n +3, which tells tail to start from the third line.



              What do you get if you output everything from the third line onwards in /etc/grub.d/40_custom?



              # This file provides an easy way to add custom menu entries.  Simply type the
              # menu entries you want to add after this comment. Be careful not to change
              # the 'exec tail' line above.


              (And additionally whatever else you put below this.)



              The exec part replaces the shell that's executing the script with tail, so effectively nothing further from the script is executed.





              Running it in the terminal:





              • $0 is probably bash or something like that (it could be /bin/bash)

              • and because of the exec, you're replacing the running shell with tail -n+3 bash

              • and since you probably don't have a file named bash in your current directory, tail promptly quits.


              So the end result is likely that your terminal session ended there.






              share|improve this answer


























                11












                11








                11






                If you're talking about /etc/grub.d/40_custom:



                $ cat /etc/grub.d/40_custom
                #!/bin/sh
                exec tail -n +3 $0
                # This file provides an easy way to add custom menu entries. Simply type the
                # menu entries you want to add after this comment. Be careful not to change
                # the 'exec tail' line above.


                Then note that:




                • this is a shell script, and is executed by grub-mkconfig to build GRUB configuration

                • this file is supposed to be "an easy way to add custom menu entries" - you just type in exactly whatever GRUB configuration you want.


                But this is a shell script, so usually you'd have to do something like echo "menuentry ...." etc. To avoid that, the exec tail magic is used. What does that do? $0, remember, is the name of the script as executed, so typically it would be 40_custom (or /etc/grub.d/40_custom, etc. depending on where and how it was run). So the script is essentially running tail on itself, but with -n +3, which tells tail to start from the third line.



                What do you get if you output everything from the third line onwards in /etc/grub.d/40_custom?



                # This file provides an easy way to add custom menu entries.  Simply type the
                # menu entries you want to add after this comment. Be careful not to change
                # the 'exec tail' line above.


                (And additionally whatever else you put below this.)



                The exec part replaces the shell that's executing the script with tail, so effectively nothing further from the script is executed.





                Running it in the terminal:





                • $0 is probably bash or something like that (it could be /bin/bash)

                • and because of the exec, you're replacing the running shell with tail -n+3 bash

                • and since you probably don't have a file named bash in your current directory, tail promptly quits.


                So the end result is likely that your terminal session ended there.






                share|improve this answer














                If you're talking about /etc/grub.d/40_custom:



                $ cat /etc/grub.d/40_custom
                #!/bin/sh
                exec tail -n +3 $0
                # This file provides an easy way to add custom menu entries. Simply type the
                # menu entries you want to add after this comment. Be careful not to change
                # the 'exec tail' line above.


                Then note that:




                • this is a shell script, and is executed by grub-mkconfig to build GRUB configuration

                • this file is supposed to be "an easy way to add custom menu entries" - you just type in exactly whatever GRUB configuration you want.


                But this is a shell script, so usually you'd have to do something like echo "menuentry ...." etc. To avoid that, the exec tail magic is used. What does that do? $0, remember, is the name of the script as executed, so typically it would be 40_custom (or /etc/grub.d/40_custom, etc. depending on where and how it was run). So the script is essentially running tail on itself, but with -n +3, which tells tail to start from the third line.



                What do you get if you output everything from the third line onwards in /etc/grub.d/40_custom?



                # This file provides an easy way to add custom menu entries.  Simply type the
                # menu entries you want to add after this comment. Be careful not to change
                # the 'exec tail' line above.


                (And additionally whatever else you put below this.)



                The exec part replaces the shell that's executing the script with tail, so effectively nothing further from the script is executed.





                Running it in the terminal:





                • $0 is probably bash or something like that (it could be /bin/bash)

                • and because of the exec, you're replacing the running shell with tail -n+3 bash

                • and since you probably don't have a file named bash in your current directory, tail promptly quits.


                So the end result is likely that your terminal session ended there.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 28 '18 at 12:09

























                answered Jan 28 '18 at 11:58









                murumuru

                1




                1






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • 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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2funix.stackexchange.com%2fquestions%2f420205%2fwhat-logic-does-the-command-exec-tail-n-3-0-from-grub2-config-have%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

                    CARDNET

                    Boot-repair Failure: Unable to locate package grub-common:i386

                    濃尾地震