Commenting multi-line command chains?











up vote
2
down vote

favorite












I often have code where I format by making a long AND/OR statements. For example:



  # Get wifi router gateway
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
| cut -d' ' -f5
| tr --delete ;)


Sometimes a single step in a command like above can be complex. Thus I end up wanting to comment it.



For example say the cut command was more complicated than it really is here. So I want to do something like:



  # Get wifi router gateway
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
# Here is a note
| cut -d' ' -f5
| tr --delete ;)


I realize this is invalid syntax.



But I'm curious to see if anyone else has some strategies for commenting long command chains?










share|improve this question
























  • Do you mean "add a comment", or "comment out the line"?
    – Jeff Schaller
    Nov 21 '17 at 18:34










  • @JeffSchaller add comment like the part that says # Here is a note
    – Philip Kirkbride
    Nov 21 '17 at 18:44















up vote
2
down vote

favorite












I often have code where I format by making a long AND/OR statements. For example:



  # Get wifi router gateway
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
| cut -d' ' -f5
| tr --delete ;)


Sometimes a single step in a command like above can be complex. Thus I end up wanting to comment it.



For example say the cut command was more complicated than it really is here. So I want to do something like:



  # Get wifi router gateway
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
# Here is a note
| cut -d' ' -f5
| tr --delete ;)


I realize this is invalid syntax.



But I'm curious to see if anyone else has some strategies for commenting long command chains?










share|improve this question
























  • Do you mean "add a comment", or "comment out the line"?
    – Jeff Schaller
    Nov 21 '17 at 18:34










  • @JeffSchaller add comment like the part that says # Here is a note
    – Philip Kirkbride
    Nov 21 '17 at 18:44













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I often have code where I format by making a long AND/OR statements. For example:



  # Get wifi router gateway
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
| cut -d' ' -f5
| tr --delete ;)


Sometimes a single step in a command like above can be complex. Thus I end up wanting to comment it.



For example say the cut command was more complicated than it really is here. So I want to do something like:



  # Get wifi router gateway
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
# Here is a note
| cut -d' ' -f5
| tr --delete ;)


I realize this is invalid syntax.



But I'm curious to see if anyone else has some strategies for commenting long command chains?










share|improve this question















I often have code where I format by making a long AND/OR statements. For example:



  # Get wifi router gateway
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
| cut -d' ' -f5
| tr --delete ;)


Sometimes a single step in a command like above can be complex. Thus I end up wanting to comment it.



For example say the cut command was more complicated than it really is here. So I want to do something like:



  # Get wifi router gateway
gateway=$(cat $leases
| grep -A 5 -m 1 $wifi
| grep option routers
# Here is a note
| cut -d' ' -f5
| tr --delete ;)


I realize this is invalid syntax.



But I'm curious to see if anyone else has some strategies for commenting long command chains?







bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









Rui F Ribeiro

38.6k1479128




38.6k1479128










asked Nov 21 '17 at 18:21









Philip Kirkbride

2,3562779




2,3562779












  • Do you mean "add a comment", or "comment out the line"?
    – Jeff Schaller
    Nov 21 '17 at 18:34










  • @JeffSchaller add comment like the part that says # Here is a note
    – Philip Kirkbride
    Nov 21 '17 at 18:44


















  • Do you mean "add a comment", or "comment out the line"?
    – Jeff Schaller
    Nov 21 '17 at 18:34










  • @JeffSchaller add comment like the part that says # Here is a note
    – Philip Kirkbride
    Nov 21 '17 at 18:44
















Do you mean "add a comment", or "comment out the line"?
– Jeff Schaller
Nov 21 '17 at 18:34




Do you mean "add a comment", or "comment out the line"?
– Jeff Schaller
Nov 21 '17 at 18:34












@JeffSchaller add comment like the part that says # Here is a note
– Philip Kirkbride
Nov 21 '17 at 18:44




@JeffSchaller add comment like the part that says # Here is a note
– Philip Kirkbride
Nov 21 '17 at 18:44










3 Answers
3






active

oldest

votes

















up vote
4
down vote



accepted










This seems to work in Bash, dash, etc:



#!/bin/sh
seq 20 | # make a long list
grep '[234]' # mut only take part of it


Similarly with && or || in place of the pipe, and also inside $( ... ).






share|improve this answer






























    up vote
    2
    down vote













    The only thing that makes your command have invalid syntax is the fact that you needlessly escape the newlines on each line.



    Your command,



    gateway=$(cat $leases 
    | grep -A 5 -m 1 $wifi
    | grep option routers
    | cut -d' ' -f5
    | tr --delete ;)


    is identical to



    gateway=$(cat $leases | grep -A 5 -m 1 $wifi | grep option routers  | cut -d' ' -f5 | tr --delete ;)


    In other words, from the shell's point of view, it's not a multi-line command at all.



    Inserting a comment into this before the cut makes the command substitution unterminated (the final ) is commended out):



    gateway=$(cat $leases | grep -A 5 -m 1 $wifi | grep option routers  # Here is a note | cut -d' ' -f5 | tr --delete ;)


    However, there is no need to escape the newlines. The following is totally valid code:



    gateway=$(cat $leases
    | grep -A 5 -m 1 $wifi
    | grep option routers
    | cut -d' ' -f5
    | tr --delete ;)


    Here, it's just fine to insert a comment:



    gateway=$(cat $leases
    | grep -A 5 -m 1 $wifi
    | grep option routers
    # Here is a note
    | cut -d' ' -f5
    | tr --delete ;)


    It's fine to do this because each part of a pipeline may be followed by one or several newlines, and a comment is ignored by the shell's parser.





    Looking at the actual code, this is a safer and more portable variant (except for the initial grep that still requires using non-standard extensions):



    gateway=$(grep -A 5 -m 1 -e "$wifi" <"$leases"
    | grep -F 'option routers'
    # Here is a note
    | cut -d ' ' -f 5
    | tr -d ';' )





    share|improve this answer






























      up vote
      0
      down vote













      Also, zsh, which can emulate bash (just call it as bash or run emulate bash), actually supports multi-line commands!



      Just run bindkey -e or bindkey "^[^M" self-insert-unmeta, and then you can go to a new line by hitting M-Return.



      It is saved as one history entry; you can move around with the arrow keys as you would expect. It will only switch entries when you go up/down at the top/bottom row.






      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: 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%2f406101%2fcommenting-multi-line-command-chains%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        4
        down vote



        accepted










        This seems to work in Bash, dash, etc:



        #!/bin/sh
        seq 20 | # make a long list
        grep '[234]' # mut only take part of it


        Similarly with && or || in place of the pipe, and also inside $( ... ).






        share|improve this answer



























          up vote
          4
          down vote



          accepted










          This seems to work in Bash, dash, etc:



          #!/bin/sh
          seq 20 | # make a long list
          grep '[234]' # mut only take part of it


          Similarly with && or || in place of the pipe, and also inside $( ... ).






          share|improve this answer

























            up vote
            4
            down vote



            accepted







            up vote
            4
            down vote



            accepted






            This seems to work in Bash, dash, etc:



            #!/bin/sh
            seq 20 | # make a long list
            grep '[234]' # mut only take part of it


            Similarly with && or || in place of the pipe, and also inside $( ... ).






            share|improve this answer














            This seems to work in Bash, dash, etc:



            #!/bin/sh
            seq 20 | # make a long list
            grep '[234]' # mut only take part of it


            Similarly with && or || in place of the pipe, and also inside $( ... ).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 '17 at 18:39

























            answered Nov 21 '17 at 18:28









            ilkkachu

            55k782150




            55k782150
























                up vote
                2
                down vote













                The only thing that makes your command have invalid syntax is the fact that you needlessly escape the newlines on each line.



                Your command,



                gateway=$(cat $leases 
                | grep -A 5 -m 1 $wifi
                | grep option routers
                | cut -d' ' -f5
                | tr --delete ;)


                is identical to



                gateway=$(cat $leases | grep -A 5 -m 1 $wifi | grep option routers  | cut -d' ' -f5 | tr --delete ;)


                In other words, from the shell's point of view, it's not a multi-line command at all.



                Inserting a comment into this before the cut makes the command substitution unterminated (the final ) is commended out):



                gateway=$(cat $leases | grep -A 5 -m 1 $wifi | grep option routers  # Here is a note | cut -d' ' -f5 | tr --delete ;)


                However, there is no need to escape the newlines. The following is totally valid code:



                gateway=$(cat $leases
                | grep -A 5 -m 1 $wifi
                | grep option routers
                | cut -d' ' -f5
                | tr --delete ;)


                Here, it's just fine to insert a comment:



                gateway=$(cat $leases
                | grep -A 5 -m 1 $wifi
                | grep option routers
                # Here is a note
                | cut -d' ' -f5
                | tr --delete ;)


                It's fine to do this because each part of a pipeline may be followed by one or several newlines, and a comment is ignored by the shell's parser.





                Looking at the actual code, this is a safer and more portable variant (except for the initial grep that still requires using non-standard extensions):



                gateway=$(grep -A 5 -m 1 -e "$wifi" <"$leases"
                | grep -F 'option routers'
                # Here is a note
                | cut -d ' ' -f 5
                | tr -d ';' )





                share|improve this answer



























                  up vote
                  2
                  down vote













                  The only thing that makes your command have invalid syntax is the fact that you needlessly escape the newlines on each line.



                  Your command,



                  gateway=$(cat $leases 
                  | grep -A 5 -m 1 $wifi
                  | grep option routers
                  | cut -d' ' -f5
                  | tr --delete ;)


                  is identical to



                  gateway=$(cat $leases | grep -A 5 -m 1 $wifi | grep option routers  | cut -d' ' -f5 | tr --delete ;)


                  In other words, from the shell's point of view, it's not a multi-line command at all.



                  Inserting a comment into this before the cut makes the command substitution unterminated (the final ) is commended out):



                  gateway=$(cat $leases | grep -A 5 -m 1 $wifi | grep option routers  # Here is a note | cut -d' ' -f5 | tr --delete ;)


                  However, there is no need to escape the newlines. The following is totally valid code:



                  gateway=$(cat $leases
                  | grep -A 5 -m 1 $wifi
                  | grep option routers
                  | cut -d' ' -f5
                  | tr --delete ;)


                  Here, it's just fine to insert a comment:



                  gateway=$(cat $leases
                  | grep -A 5 -m 1 $wifi
                  | grep option routers
                  # Here is a note
                  | cut -d' ' -f5
                  | tr --delete ;)


                  It's fine to do this because each part of a pipeline may be followed by one or several newlines, and a comment is ignored by the shell's parser.





                  Looking at the actual code, this is a safer and more portable variant (except for the initial grep that still requires using non-standard extensions):



                  gateway=$(grep -A 5 -m 1 -e "$wifi" <"$leases"
                  | grep -F 'option routers'
                  # Here is a note
                  | cut -d ' ' -f 5
                  | tr -d ';' )





                  share|improve this answer

























                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    The only thing that makes your command have invalid syntax is the fact that you needlessly escape the newlines on each line.



                    Your command,



                    gateway=$(cat $leases 
                    | grep -A 5 -m 1 $wifi
                    | grep option routers
                    | cut -d' ' -f5
                    | tr --delete ;)


                    is identical to



                    gateway=$(cat $leases | grep -A 5 -m 1 $wifi | grep option routers  | cut -d' ' -f5 | tr --delete ;)


                    In other words, from the shell's point of view, it's not a multi-line command at all.



                    Inserting a comment into this before the cut makes the command substitution unterminated (the final ) is commended out):



                    gateway=$(cat $leases | grep -A 5 -m 1 $wifi | grep option routers  # Here is a note | cut -d' ' -f5 | tr --delete ;)


                    However, there is no need to escape the newlines. The following is totally valid code:



                    gateway=$(cat $leases
                    | grep -A 5 -m 1 $wifi
                    | grep option routers
                    | cut -d' ' -f5
                    | tr --delete ;)


                    Here, it's just fine to insert a comment:



                    gateway=$(cat $leases
                    | grep -A 5 -m 1 $wifi
                    | grep option routers
                    # Here is a note
                    | cut -d' ' -f5
                    | tr --delete ;)


                    It's fine to do this because each part of a pipeline may be followed by one or several newlines, and a comment is ignored by the shell's parser.





                    Looking at the actual code, this is a safer and more portable variant (except for the initial grep that still requires using non-standard extensions):



                    gateway=$(grep -A 5 -m 1 -e "$wifi" <"$leases"
                    | grep -F 'option routers'
                    # Here is a note
                    | cut -d ' ' -f 5
                    | tr -d ';' )





                    share|improve this answer














                    The only thing that makes your command have invalid syntax is the fact that you needlessly escape the newlines on each line.



                    Your command,



                    gateway=$(cat $leases 
                    | grep -A 5 -m 1 $wifi
                    | grep option routers
                    | cut -d' ' -f5
                    | tr --delete ;)


                    is identical to



                    gateway=$(cat $leases | grep -A 5 -m 1 $wifi | grep option routers  | cut -d' ' -f5 | tr --delete ;)


                    In other words, from the shell's point of view, it's not a multi-line command at all.



                    Inserting a comment into this before the cut makes the command substitution unterminated (the final ) is commended out):



                    gateway=$(cat $leases | grep -A 5 -m 1 $wifi | grep option routers  # Here is a note | cut -d' ' -f5 | tr --delete ;)


                    However, there is no need to escape the newlines. The following is totally valid code:



                    gateway=$(cat $leases
                    | grep -A 5 -m 1 $wifi
                    | grep option routers
                    | cut -d' ' -f5
                    | tr --delete ;)


                    Here, it's just fine to insert a comment:



                    gateway=$(cat $leases
                    | grep -A 5 -m 1 $wifi
                    | grep option routers
                    # Here is a note
                    | cut -d' ' -f5
                    | tr --delete ;)


                    It's fine to do this because each part of a pipeline may be followed by one or several newlines, and a comment is ignored by the shell's parser.





                    Looking at the actual code, this is a safer and more portable variant (except for the initial grep that still requires using non-standard extensions):



                    gateway=$(grep -A 5 -m 1 -e "$wifi" <"$leases"
                    | grep -F 'option routers'
                    # Here is a note
                    | cut -d ' ' -f 5
                    | tr -d ';' )






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited yesterday

























                    answered yesterday









                    Kusalananda

                    120k16225369




                    120k16225369






















                        up vote
                        0
                        down vote













                        Also, zsh, which can emulate bash (just call it as bash or run emulate bash), actually supports multi-line commands!



                        Just run bindkey -e or bindkey "^[^M" self-insert-unmeta, and then you can go to a new line by hitting M-Return.



                        It is saved as one history entry; you can move around with the arrow keys as you would expect. It will only switch entries when you go up/down at the top/bottom row.






                        share|improve this answer



























                          up vote
                          0
                          down vote













                          Also, zsh, which can emulate bash (just call it as bash or run emulate bash), actually supports multi-line commands!



                          Just run bindkey -e or bindkey "^[^M" self-insert-unmeta, and then you can go to a new line by hitting M-Return.



                          It is saved as one history entry; you can move around with the arrow keys as you would expect. It will only switch entries when you go up/down at the top/bottom row.






                          share|improve this answer

























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Also, zsh, which can emulate bash (just call it as bash or run emulate bash), actually supports multi-line commands!



                            Just run bindkey -e or bindkey "^[^M" self-insert-unmeta, and then you can go to a new line by hitting M-Return.



                            It is saved as one history entry; you can move around with the arrow keys as you would expect. It will only switch entries when you go up/down at the top/bottom row.






                            share|improve this answer














                            Also, zsh, which can emulate bash (just call it as bash or run emulate bash), actually supports multi-line commands!



                            Just run bindkey -e or bindkey "^[^M" self-insert-unmeta, and then you can go to a new line by hitting M-Return.



                            It is saved as one history entry; you can move around with the arrow keys as you would expect. It will only switch entries when you go up/down at the top/bottom row.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 21 '17 at 19:32

























                            answered Nov 21 '17 at 19:00









                            SilverWolf

                            1215




                            1215






























                                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%2f406101%2fcommenting-multi-line-command-chains%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

                                サソリ

                                広島県道265号伴広島線

                                Setup Asymptote in Texstudio