Outputting the results of a nested for loop to a file in bash











up vote
0
down vote

favorite












I have been attempting to create a file out of my loop in bash , I was successful in generating the file with the following code



for ((i = 0; i <= 5; i++)); do
for ((j = i; j <= 5; j++)); do
echo -e "$i $j" >> numbers.txt
done
done


however I want my results to be out in the following fashion



 01

02

03


etc.



But the result I am getting is similar to this



0 0 1 0 2 0 3 0 4 0 5 1 1 1 2 1 3 1 4 1 5 2 2 2 3 2 4 2 5 3 3 3 4 3 5


How can I solve this ?










share|improve this question




















  • 2




    If you don't want the space between the two digits, then you can just omit it. Or - better - use printf for formatted output printf '%d%dn' "$i" "$j".
    – steeldriver
    Jun 24 '16 at 19:59










  • Hello, thank you for your reply .. i want the ouputs to be on a new line, meaning each pair generated. so the n pair is on a line and the n+1 pair on a new line and so on
    – JavaFreak
    Jun 24 '16 at 20:00










  • @heemayl my text was formatted right :P i am not getting the results on a new line am getting all of them on the same line
    – JavaFreak
    Jun 24 '16 at 20:01






  • 1




    Are you sure? Each should be on its own line unless you used echo -n. What platform are you on?
    – leekaiinthesky
    Jun 24 '16 at 20:44












  • If you're really getting everything on the same line, you didn't run the code you showed us.
    – Gilles
    Jun 24 '16 at 22:43















up vote
0
down vote

favorite












I have been attempting to create a file out of my loop in bash , I was successful in generating the file with the following code



for ((i = 0; i <= 5; i++)); do
for ((j = i; j <= 5; j++)); do
echo -e "$i $j" >> numbers.txt
done
done


however I want my results to be out in the following fashion



 01

02

03


etc.



But the result I am getting is similar to this



0 0 1 0 2 0 3 0 4 0 5 1 1 1 2 1 3 1 4 1 5 2 2 2 3 2 4 2 5 3 3 3 4 3 5


How can I solve this ?










share|improve this question




















  • 2




    If you don't want the space between the two digits, then you can just omit it. Or - better - use printf for formatted output printf '%d%dn' "$i" "$j".
    – steeldriver
    Jun 24 '16 at 19:59










  • Hello, thank you for your reply .. i want the ouputs to be on a new line, meaning each pair generated. so the n pair is on a line and the n+1 pair on a new line and so on
    – JavaFreak
    Jun 24 '16 at 20:00










  • @heemayl my text was formatted right :P i am not getting the results on a new line am getting all of them on the same line
    – JavaFreak
    Jun 24 '16 at 20:01






  • 1




    Are you sure? Each should be on its own line unless you used echo -n. What platform are you on?
    – leekaiinthesky
    Jun 24 '16 at 20:44












  • If you're really getting everything on the same line, you didn't run the code you showed us.
    – Gilles
    Jun 24 '16 at 22:43













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have been attempting to create a file out of my loop in bash , I was successful in generating the file with the following code



for ((i = 0; i <= 5; i++)); do
for ((j = i; j <= 5; j++)); do
echo -e "$i $j" >> numbers.txt
done
done


however I want my results to be out in the following fashion



 01

02

03


etc.



But the result I am getting is similar to this



0 0 1 0 2 0 3 0 4 0 5 1 1 1 2 1 3 1 4 1 5 2 2 2 3 2 4 2 5 3 3 3 4 3 5


How can I solve this ?










share|improve this question















I have been attempting to create a file out of my loop in bash , I was successful in generating the file with the following code



for ((i = 0; i <= 5; i++)); do
for ((j = i; j <= 5; j++)); do
echo -e "$i $j" >> numbers.txt
done
done


however I want my results to be out in the following fashion



 01

02

03


etc.



But the result I am getting is similar to this



0 0 1 0 2 0 3 0 4 0 5 1 1 1 2 1 3 1 4 1 5 2 2 2 3 2 4 2 5 3 3 3 4 3 5


How can I solve this ?







bash shell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 at 22:47









Rui F Ribeiro

38.3k1477127




38.3k1477127










asked Jun 24 '16 at 19:47









JavaFreak

4339




4339








  • 2




    If you don't want the space between the two digits, then you can just omit it. Or - better - use printf for formatted output printf '%d%dn' "$i" "$j".
    – steeldriver
    Jun 24 '16 at 19:59










  • Hello, thank you for your reply .. i want the ouputs to be on a new line, meaning each pair generated. so the n pair is on a line and the n+1 pair on a new line and so on
    – JavaFreak
    Jun 24 '16 at 20:00










  • @heemayl my text was formatted right :P i am not getting the results on a new line am getting all of them on the same line
    – JavaFreak
    Jun 24 '16 at 20:01






  • 1




    Are you sure? Each should be on its own line unless you used echo -n. What platform are you on?
    – leekaiinthesky
    Jun 24 '16 at 20:44












  • If you're really getting everything on the same line, you didn't run the code you showed us.
    – Gilles
    Jun 24 '16 at 22:43














  • 2




    If you don't want the space between the two digits, then you can just omit it. Or - better - use printf for formatted output printf '%d%dn' "$i" "$j".
    – steeldriver
    Jun 24 '16 at 19:59










  • Hello, thank you for your reply .. i want the ouputs to be on a new line, meaning each pair generated. so the n pair is on a line and the n+1 pair on a new line and so on
    – JavaFreak
    Jun 24 '16 at 20:00










  • @heemayl my text was formatted right :P i am not getting the results on a new line am getting all of them on the same line
    – JavaFreak
    Jun 24 '16 at 20:01






  • 1




    Are you sure? Each should be on its own line unless you used echo -n. What platform are you on?
    – leekaiinthesky
    Jun 24 '16 at 20:44












  • If you're really getting everything on the same line, you didn't run the code you showed us.
    – Gilles
    Jun 24 '16 at 22:43








2




2




If you don't want the space between the two digits, then you can just omit it. Or - better - use printf for formatted output printf '%d%dn' "$i" "$j".
– steeldriver
Jun 24 '16 at 19:59




If you don't want the space between the two digits, then you can just omit it. Or - better - use printf for formatted output printf '%d%dn' "$i" "$j".
– steeldriver
Jun 24 '16 at 19:59












Hello, thank you for your reply .. i want the ouputs to be on a new line, meaning each pair generated. so the n pair is on a line and the n+1 pair on a new line and so on
– JavaFreak
Jun 24 '16 at 20:00




Hello, thank you for your reply .. i want the ouputs to be on a new line, meaning each pair generated. so the n pair is on a line and the n+1 pair on a new line and so on
– JavaFreak
Jun 24 '16 at 20:00












@heemayl my text was formatted right :P i am not getting the results on a new line am getting all of them on the same line
– JavaFreak
Jun 24 '16 at 20:01




@heemayl my text was formatted right :P i am not getting the results on a new line am getting all of them on the same line
– JavaFreak
Jun 24 '16 at 20:01




1




1




Are you sure? Each should be on its own line unless you used echo -n. What platform are you on?
– leekaiinthesky
Jun 24 '16 at 20:44






Are you sure? Each should be on its own line unless you used echo -n. What platform are you on?
– leekaiinthesky
Jun 24 '16 at 20:44














If you're really getting everything on the same line, you didn't run the code you showed us.
– Gilles
Jun 24 '16 at 22:43




If you're really getting everything on the same line, you didn't run the code you showed us.
– Gilles
Jun 24 '16 at 22:43










2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










bash has C-style for loops, but stylistically they're a little odd to use when you don't have to. And if you really do absolutely have to, you might be using the wrong language. (For this use case shell is fine, of course.)



A much more shell-like way to do this (in my opinion) is:



for i in {0..5}; do for j in $(seq "$i" 5); do echo "$i$j"; done; done


Here is another approach:



(set {0..5}; for i; do for j; do [ "$i" -le "$j" ] && echo "$i$j"; done; done)


I write these as one-liners suitable for an interactive shell, but they work equally well in structured form (which would usually be preferable in a script):



set {0..5}
for i; do
for j; do
[ "$i" -le "$j" ] &&
printf '%d%dn' "$i" "$j"
done
done





share|improve this answer






























    up vote
    1
    down vote













    for ((i = 0; i <= 5; i++)); do
    for ((j = i; j <= 5; j++)); do
    echo -e "${i}${j}n" >> numbers.txt
    done
    done


    Should do the trick. I removed the space, I prefer using {}'s around my variables and n puts in new line.






    share|improve this answer

















    • 1




      Adding n adds an empty line, since echo already outputs a newline character at the end.
      – Gilles
      Jun 24 '16 at 22:42










    • Yes, which is what they want according to the desired result specified.
      – Zachary Brady
      Jun 24 '16 at 22:44











    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%2f291945%2foutputting-the-results-of-a-nested-for-loop-to-a-file-in-bash%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








    up vote
    1
    down vote



    accepted










    bash has C-style for loops, but stylistically they're a little odd to use when you don't have to. And if you really do absolutely have to, you might be using the wrong language. (For this use case shell is fine, of course.)



    A much more shell-like way to do this (in my opinion) is:



    for i in {0..5}; do for j in $(seq "$i" 5); do echo "$i$j"; done; done


    Here is another approach:



    (set {0..5}; for i; do for j; do [ "$i" -le "$j" ] && echo "$i$j"; done; done)


    I write these as one-liners suitable for an interactive shell, but they work equally well in structured form (which would usually be preferable in a script):



    set {0..5}
    for i; do
    for j; do
    [ "$i" -le "$j" ] &&
    printf '%d%dn' "$i" "$j"
    done
    done





    share|improve this answer



























      up vote
      1
      down vote



      accepted










      bash has C-style for loops, but stylistically they're a little odd to use when you don't have to. And if you really do absolutely have to, you might be using the wrong language. (For this use case shell is fine, of course.)



      A much more shell-like way to do this (in my opinion) is:



      for i in {0..5}; do for j in $(seq "$i" 5); do echo "$i$j"; done; done


      Here is another approach:



      (set {0..5}; for i; do for j; do [ "$i" -le "$j" ] && echo "$i$j"; done; done)


      I write these as one-liners suitable for an interactive shell, but they work equally well in structured form (which would usually be preferable in a script):



      set {0..5}
      for i; do
      for j; do
      [ "$i" -le "$j" ] &&
      printf '%d%dn' "$i" "$j"
      done
      done





      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        bash has C-style for loops, but stylistically they're a little odd to use when you don't have to. And if you really do absolutely have to, you might be using the wrong language. (For this use case shell is fine, of course.)



        A much more shell-like way to do this (in my opinion) is:



        for i in {0..5}; do for j in $(seq "$i" 5); do echo "$i$j"; done; done


        Here is another approach:



        (set {0..5}; for i; do for j; do [ "$i" -le "$j" ] && echo "$i$j"; done; done)


        I write these as one-liners suitable for an interactive shell, but they work equally well in structured form (which would usually be preferable in a script):



        set {0..5}
        for i; do
        for j; do
        [ "$i" -le "$j" ] &&
        printf '%d%dn' "$i" "$j"
        done
        done





        share|improve this answer














        bash has C-style for loops, but stylistically they're a little odd to use when you don't have to. And if you really do absolutely have to, you might be using the wrong language. (For this use case shell is fine, of course.)



        A much more shell-like way to do this (in my opinion) is:



        for i in {0..5}; do for j in $(seq "$i" 5); do echo "$i$j"; done; done


        Here is another approach:



        (set {0..5}; for i; do for j; do [ "$i" -le "$j" ] && echo "$i$j"; done; done)


        I write these as one-liners suitable for an interactive shell, but they work equally well in structured form (which would usually be preferable in a script):



        set {0..5}
        for i; do
        for j; do
        [ "$i" -le "$j" ] &&
        printf '%d%dn' "$i" "$j"
        done
        done






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 13 '17 at 12:36









        Community

        1




        1










        answered Jun 24 '16 at 23:45









        Wildcard

        22.5k959164




        22.5k959164
























            up vote
            1
            down vote













            for ((i = 0; i <= 5; i++)); do
            for ((j = i; j <= 5; j++)); do
            echo -e "${i}${j}n" >> numbers.txt
            done
            done


            Should do the trick. I removed the space, I prefer using {}'s around my variables and n puts in new line.






            share|improve this answer

















            • 1




              Adding n adds an empty line, since echo already outputs a newline character at the end.
              – Gilles
              Jun 24 '16 at 22:42










            • Yes, which is what they want according to the desired result specified.
              – Zachary Brady
              Jun 24 '16 at 22:44















            up vote
            1
            down vote













            for ((i = 0; i <= 5; i++)); do
            for ((j = i; j <= 5; j++)); do
            echo -e "${i}${j}n" >> numbers.txt
            done
            done


            Should do the trick. I removed the space, I prefer using {}'s around my variables and n puts in new line.






            share|improve this answer

















            • 1




              Adding n adds an empty line, since echo already outputs a newline character at the end.
              – Gilles
              Jun 24 '16 at 22:42










            • Yes, which is what they want according to the desired result specified.
              – Zachary Brady
              Jun 24 '16 at 22:44













            up vote
            1
            down vote










            up vote
            1
            down vote









            for ((i = 0; i <= 5; i++)); do
            for ((j = i; j <= 5; j++)); do
            echo -e "${i}${j}n" >> numbers.txt
            done
            done


            Should do the trick. I removed the space, I prefer using {}'s around my variables and n puts in new line.






            share|improve this answer












            for ((i = 0; i <= 5; i++)); do
            for ((j = i; j <= 5; j++)); do
            echo -e "${i}${j}n" >> numbers.txt
            done
            done


            Should do the trick. I removed the space, I prefer using {}'s around my variables and n puts in new line.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jun 24 '16 at 20:34









            Zachary Brady

            3,386831




            3,386831








            • 1




              Adding n adds an empty line, since echo already outputs a newline character at the end.
              – Gilles
              Jun 24 '16 at 22:42










            • Yes, which is what they want according to the desired result specified.
              – Zachary Brady
              Jun 24 '16 at 22:44














            • 1




              Adding n adds an empty line, since echo already outputs a newline character at the end.
              – Gilles
              Jun 24 '16 at 22:42










            • Yes, which is what they want according to the desired result specified.
              – Zachary Brady
              Jun 24 '16 at 22:44








            1




            1




            Adding n adds an empty line, since echo already outputs a newline character at the end.
            – Gilles
            Jun 24 '16 at 22:42




            Adding n adds an empty line, since echo already outputs a newline character at the end.
            – Gilles
            Jun 24 '16 at 22:42












            Yes, which is what they want according to the desired result specified.
            – Zachary Brady
            Jun 24 '16 at 22:44




            Yes, which is what they want according to the desired result specified.
            – Zachary Brady
            Jun 24 '16 at 22:44


















            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%2f291945%2foutputting-the-results-of-a-nested-for-loop-to-a-file-in-bash%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

            Morgemoulin

            Scott Moir

            Souastre