Can I use a variable in a Bash brace expansion?

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












6














Below is some sort of pseudo-code for what I'm trying to accomplish:



#!/bin/bash

# I already have the variable below figured out (positive integer):
numlines=$([returns number of lines containing specific characters in a file])

# This is basically what I want to do with it:
for i in 1..$numlines; do
# the part below is already figured out as well:
do some other stuff
done


I can execute it fine from the command line by inserting the actual number in the `1..n' sequence. I just need to know if it's possible to include a variable here and how to go about doing it.



  • I have tried exporting it

  • I have tried putting the variable itself in curly braces inside the sequence: 1..$numlines

  • I have tried putting it in double-quotes hoping it would expand: 1.."$numlines"

  • I have tried escaping the $: 1..$numlines

Do I need to use a set -[something] command in order for this variable to get expanded? I have even tried some forms of using eval...all to no avail.



I just need to know if there is something simple or obscure that I am missing or if this is even possible before I waste anymore time on it.



I could throw together a really, really hackish way of doing it to make it work as needed, but I'd like to avoid that if at all possible and learn the right way to go about doing it.










share|improve this question























  • Related: How can I use $variable in a shell brace expansion of a sequence?
    – ilkkachu
    Dec 20 '18 at 11:26















6














Below is some sort of pseudo-code for what I'm trying to accomplish:



#!/bin/bash

# I already have the variable below figured out (positive integer):
numlines=$([returns number of lines containing specific characters in a file])

# This is basically what I want to do with it:
for i in 1..$numlines; do
# the part below is already figured out as well:
do some other stuff
done


I can execute it fine from the command line by inserting the actual number in the `1..n' sequence. I just need to know if it's possible to include a variable here and how to go about doing it.



  • I have tried exporting it

  • I have tried putting the variable itself in curly braces inside the sequence: 1..$numlines

  • I have tried putting it in double-quotes hoping it would expand: 1.."$numlines"

  • I have tried escaping the $: 1..$numlines

Do I need to use a set -[something] command in order for this variable to get expanded? I have even tried some forms of using eval...all to no avail.



I just need to know if there is something simple or obscure that I am missing or if this is even possible before I waste anymore time on it.



I could throw together a really, really hackish way of doing it to make it work as needed, but I'd like to avoid that if at all possible and learn the right way to go about doing it.










share|improve this question























  • Related: How can I use $variable in a shell brace expansion of a sequence?
    – ilkkachu
    Dec 20 '18 at 11:26













6












6








6


2





Below is some sort of pseudo-code for what I'm trying to accomplish:



#!/bin/bash

# I already have the variable below figured out (positive integer):
numlines=$([returns number of lines containing specific characters in a file])

# This is basically what I want to do with it:
for i in 1..$numlines; do
# the part below is already figured out as well:
do some other stuff
done


I can execute it fine from the command line by inserting the actual number in the `1..n' sequence. I just need to know if it's possible to include a variable here and how to go about doing it.



  • I have tried exporting it

  • I have tried putting the variable itself in curly braces inside the sequence: 1..$numlines

  • I have tried putting it in double-quotes hoping it would expand: 1.."$numlines"

  • I have tried escaping the $: 1..$numlines

Do I need to use a set -[something] command in order for this variable to get expanded? I have even tried some forms of using eval...all to no avail.



I just need to know if there is something simple or obscure that I am missing or if this is even possible before I waste anymore time on it.



I could throw together a really, really hackish way of doing it to make it work as needed, but I'd like to avoid that if at all possible and learn the right way to go about doing it.










share|improve this question















Below is some sort of pseudo-code for what I'm trying to accomplish:



#!/bin/bash

# I already have the variable below figured out (positive integer):
numlines=$([returns number of lines containing specific characters in a file])

# This is basically what I want to do with it:
for i in 1..$numlines; do
# the part below is already figured out as well:
do some other stuff
done


I can execute it fine from the command line by inserting the actual number in the `1..n' sequence. I just need to know if it's possible to include a variable here and how to go about doing it.



  • I have tried exporting it

  • I have tried putting the variable itself in curly braces inside the sequence: 1..$numlines

  • I have tried putting it in double-quotes hoping it would expand: 1.."$numlines"

  • I have tried escaping the $: 1..$numlines

Do I need to use a set -[something] command in order for this variable to get expanded? I have even tried some forms of using eval...all to no avail.



I just need to know if there is something simple or obscure that I am missing or if this is even possible before I waste anymore time on it.



I could throw together a really, really hackish way of doing it to make it work as needed, but I'd like to avoid that if at all possible and learn the right way to go about doing it.







bash shell-script scripting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 20 '18 at 11:20









ilkkachu

55.9k784155




55.9k784155










asked Nov 19 '15 at 23:20









rubynorails

1,247516




1,247516











  • Related: How can I use $variable in a shell brace expansion of a sequence?
    – ilkkachu
    Dec 20 '18 at 11:26
















  • Related: How can I use $variable in a shell brace expansion of a sequence?
    – ilkkachu
    Dec 20 '18 at 11:26















Related: How can I use $variable in a shell brace expansion of a sequence?
– ilkkachu
Dec 20 '18 at 11:26




Related: How can I use $variable in a shell brace expansion of a sequence?
– ilkkachu
Dec 20 '18 at 11:26










3 Answers
3






active

oldest

votes


















10














Unfortunately, there is no way to use a variable in that expansion (AFAIK), since variable expansion happens after brace expansion.



Fortunately, there's a tool that does the same job.



for i in $(seq 1 $numlines); do
# stuff
done


seq is from GNU coreutils; no idea how to do it in POSIX.






share|improve this answer


















  • 1




    (Plus 1). seq is good for GNU systems and, if I recall correctly, the most recent OSX. On other BSD systems, one can use jot instead.
    – John1024
    Nov 19 '15 at 23:31










  • seq works perfectly. Thank you so much for your prompt answer.
    – rubynorails
    Nov 19 '15 at 23:40










  • This was not part of my question -- but what is the syntax (if any) for doing this in reverse order, such as 16..1? $(seq $numlines 1) did not work. I guess I can always man seq, but just wondering if anyone knew off the top of their head.
    – rubynorails
    Nov 19 '15 at 23:50






  • 1




    Just figured out how to do it in reverse from this link -- for i in $(seq $numlines -1 1)
    – rubynorails
    Nov 19 '15 at 23:59











  • seq $numlines -1 0
    – DopeGhoti
    Nov 20 '15 at 0:00


















8














Sure. If you want a for loop that increments an integer variable, use the form of the for loop that increments an integer variable (or more generally performs arithmetic on the loop variable(s)).



for ((i=1; i<=numlines; i++)); do … done


This construct works in bash (and ksh93 and zsh), but not in plain sh. In plain sh, use a while loop and the test ([ … ]) construct.



i=1
while [ "$i" -le "$numlines" ]; do

i=$((i+1))
done





share|improve this answer




























    8














    If you must avoid seq, which as Tom Hunt points out seems to be the usual solution to this, then an eval definitely can do it (though, I wouldn't encourage it):



    eval 'for i in 1..'$numlines'; do echo $i; done'


    You can stay POSIX by avoiding the expansion, and simply do math and integer comparisons on $numlines:



    while [ ! "$numlines" -eq 0 ]; do
    echo "$numlines"
    : $((numlines-=1))
    done


    Outside of POSIX, bash and ksh and zsh also have C-style for loops:



    for((i=0; i<numlines; i++)); do echo $i; done





    share|improve this answer


















    • 1




      I really appreciate this answer as well. While seq worked fine for my scenario and seemed to be the simplest solution, it is good to know that there are other (even POSIX) alternatives. Thanks for this.
      – rubynorails
      Nov 19 '15 at 23:43







    • 2




      There's really no reason to use eval; if you have brace expansion, you have the C-style loop.
      – chepner
      Nov 20 '15 at 2:46










    • @PSkocik - If I could choose 2 answers, I would also choose this one. When I came across the fact that I needed to do this in reverse, your eval example was the simplest and would have saved me from having to search for an alternate way of doing it using seq. The while loop is a bit bulky for me. I like to keep things short and sweet, and I never could get the C-style for loop to work by giving i the value of 0 or 1. It never returned correctly and was always a little off. I'm sure it could be tweaked to work correctly, but these are definitely helpful solutions, nonetheless.
      – rubynorails
      Nov 20 '15 at 3:19











    • The eval approach is problematic if there is anything non-trivial inside the body of the loop. I imagine it would not be very readable if you needed to nest two such loops.
      – kasperd
      Nov 20 '15 at 10:38










    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%2f244233%2fcan-i-use-a-variable-in-a-bash-brace-expansion%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









    10














    Unfortunately, there is no way to use a variable in that expansion (AFAIK), since variable expansion happens after brace expansion.



    Fortunately, there's a tool that does the same job.



    for i in $(seq 1 $numlines); do
    # stuff
    done


    seq is from GNU coreutils; no idea how to do it in POSIX.






    share|improve this answer


















    • 1




      (Plus 1). seq is good for GNU systems and, if I recall correctly, the most recent OSX. On other BSD systems, one can use jot instead.
      – John1024
      Nov 19 '15 at 23:31










    • seq works perfectly. Thank you so much for your prompt answer.
      – rubynorails
      Nov 19 '15 at 23:40










    • This was not part of my question -- but what is the syntax (if any) for doing this in reverse order, such as 16..1? $(seq $numlines 1) did not work. I guess I can always man seq, but just wondering if anyone knew off the top of their head.
      – rubynorails
      Nov 19 '15 at 23:50






    • 1




      Just figured out how to do it in reverse from this link -- for i in $(seq $numlines -1 1)
      – rubynorails
      Nov 19 '15 at 23:59











    • seq $numlines -1 0
      – DopeGhoti
      Nov 20 '15 at 0:00















    10














    Unfortunately, there is no way to use a variable in that expansion (AFAIK), since variable expansion happens after brace expansion.



    Fortunately, there's a tool that does the same job.



    for i in $(seq 1 $numlines); do
    # stuff
    done


    seq is from GNU coreutils; no idea how to do it in POSIX.






    share|improve this answer


















    • 1




      (Plus 1). seq is good for GNU systems and, if I recall correctly, the most recent OSX. On other BSD systems, one can use jot instead.
      – John1024
      Nov 19 '15 at 23:31










    • seq works perfectly. Thank you so much for your prompt answer.
      – rubynorails
      Nov 19 '15 at 23:40










    • This was not part of my question -- but what is the syntax (if any) for doing this in reverse order, such as 16..1? $(seq $numlines 1) did not work. I guess I can always man seq, but just wondering if anyone knew off the top of their head.
      – rubynorails
      Nov 19 '15 at 23:50






    • 1




      Just figured out how to do it in reverse from this link -- for i in $(seq $numlines -1 1)
      – rubynorails
      Nov 19 '15 at 23:59











    • seq $numlines -1 0
      – DopeGhoti
      Nov 20 '15 at 0:00













    10












    10








    10






    Unfortunately, there is no way to use a variable in that expansion (AFAIK), since variable expansion happens after brace expansion.



    Fortunately, there's a tool that does the same job.



    for i in $(seq 1 $numlines); do
    # stuff
    done


    seq is from GNU coreutils; no idea how to do it in POSIX.






    share|improve this answer














    Unfortunately, there is no way to use a variable in that expansion (AFAIK), since variable expansion happens after brace expansion.



    Fortunately, there's a tool that does the same job.



    for i in $(seq 1 $numlines); do
    # stuff
    done


    seq is from GNU coreutils; no idea how to do it in POSIX.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 20 '15 at 2:48









    chepner

    5,3301423




    5,3301423










    answered Nov 19 '15 at 23:27









    Tom Hunt

    6,27021335




    6,27021335







    • 1




      (Plus 1). seq is good for GNU systems and, if I recall correctly, the most recent OSX. On other BSD systems, one can use jot instead.
      – John1024
      Nov 19 '15 at 23:31










    • seq works perfectly. Thank you so much for your prompt answer.
      – rubynorails
      Nov 19 '15 at 23:40










    • This was not part of my question -- but what is the syntax (if any) for doing this in reverse order, such as 16..1? $(seq $numlines 1) did not work. I guess I can always man seq, but just wondering if anyone knew off the top of their head.
      – rubynorails
      Nov 19 '15 at 23:50






    • 1




      Just figured out how to do it in reverse from this link -- for i in $(seq $numlines -1 1)
      – rubynorails
      Nov 19 '15 at 23:59











    • seq $numlines -1 0
      – DopeGhoti
      Nov 20 '15 at 0:00












    • 1




      (Plus 1). seq is good for GNU systems and, if I recall correctly, the most recent OSX. On other BSD systems, one can use jot instead.
      – John1024
      Nov 19 '15 at 23:31










    • seq works perfectly. Thank you so much for your prompt answer.
      – rubynorails
      Nov 19 '15 at 23:40










    • This was not part of my question -- but what is the syntax (if any) for doing this in reverse order, such as 16..1? $(seq $numlines 1) did not work. I guess I can always man seq, but just wondering if anyone knew off the top of their head.
      – rubynorails
      Nov 19 '15 at 23:50






    • 1




      Just figured out how to do it in reverse from this link -- for i in $(seq $numlines -1 1)
      – rubynorails
      Nov 19 '15 at 23:59











    • seq $numlines -1 0
      – DopeGhoti
      Nov 20 '15 at 0:00







    1




    1




    (Plus 1). seq is good for GNU systems and, if I recall correctly, the most recent OSX. On other BSD systems, one can use jot instead.
    – John1024
    Nov 19 '15 at 23:31




    (Plus 1). seq is good for GNU systems and, if I recall correctly, the most recent OSX. On other BSD systems, one can use jot instead.
    – John1024
    Nov 19 '15 at 23:31












    seq works perfectly. Thank you so much for your prompt answer.
    – rubynorails
    Nov 19 '15 at 23:40




    seq works perfectly. Thank you so much for your prompt answer.
    – rubynorails
    Nov 19 '15 at 23:40












    This was not part of my question -- but what is the syntax (if any) for doing this in reverse order, such as 16..1? $(seq $numlines 1) did not work. I guess I can always man seq, but just wondering if anyone knew off the top of their head.
    – rubynorails
    Nov 19 '15 at 23:50




    This was not part of my question -- but what is the syntax (if any) for doing this in reverse order, such as 16..1? $(seq $numlines 1) did not work. I guess I can always man seq, but just wondering if anyone knew off the top of their head.
    – rubynorails
    Nov 19 '15 at 23:50




    1




    1




    Just figured out how to do it in reverse from this link -- for i in $(seq $numlines -1 1)
    – rubynorails
    Nov 19 '15 at 23:59





    Just figured out how to do it in reverse from this link -- for i in $(seq $numlines -1 1)
    – rubynorails
    Nov 19 '15 at 23:59













    seq $numlines -1 0
    – DopeGhoti
    Nov 20 '15 at 0:00




    seq $numlines -1 0
    – DopeGhoti
    Nov 20 '15 at 0:00













    8














    Sure. If you want a for loop that increments an integer variable, use the form of the for loop that increments an integer variable (or more generally performs arithmetic on the loop variable(s)).



    for ((i=1; i<=numlines; i++)); do … done


    This construct works in bash (and ksh93 and zsh), but not in plain sh. In plain sh, use a while loop and the test ([ … ]) construct.



    i=1
    while [ "$i" -le "$numlines" ]; do

    i=$((i+1))
    done





    share|improve this answer

























      8














      Sure. If you want a for loop that increments an integer variable, use the form of the for loop that increments an integer variable (or more generally performs arithmetic on the loop variable(s)).



      for ((i=1; i<=numlines; i++)); do … done


      This construct works in bash (and ksh93 and zsh), but not in plain sh. In plain sh, use a while loop and the test ([ … ]) construct.



      i=1
      while [ "$i" -le "$numlines" ]; do

      i=$((i+1))
      done





      share|improve this answer























        8












        8








        8






        Sure. If you want a for loop that increments an integer variable, use the form of the for loop that increments an integer variable (or more generally performs arithmetic on the loop variable(s)).



        for ((i=1; i<=numlines; i++)); do … done


        This construct works in bash (and ksh93 and zsh), but not in plain sh. In plain sh, use a while loop and the test ([ … ]) construct.



        i=1
        while [ "$i" -le "$numlines" ]; do

        i=$((i+1))
        done





        share|improve this answer












        Sure. If you want a for loop that increments an integer variable, use the form of the for loop that increments an integer variable (or more generally performs arithmetic on the loop variable(s)).



        for ((i=1; i<=numlines; i++)); do … done


        This construct works in bash (and ksh93 and zsh), but not in plain sh. In plain sh, use a while loop and the test ([ … ]) construct.



        i=1
        while [ "$i" -le "$numlines" ]; do

        i=$((i+1))
        done






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '15 at 1:36









        Gilles

        528k12810581583




        528k12810581583





















            8














            If you must avoid seq, which as Tom Hunt points out seems to be the usual solution to this, then an eval definitely can do it (though, I wouldn't encourage it):



            eval 'for i in 1..'$numlines'; do echo $i; done'


            You can stay POSIX by avoiding the expansion, and simply do math and integer comparisons on $numlines:



            while [ ! "$numlines" -eq 0 ]; do
            echo "$numlines"
            : $((numlines-=1))
            done


            Outside of POSIX, bash and ksh and zsh also have C-style for loops:



            for((i=0; i<numlines; i++)); do echo $i; done





            share|improve this answer


















            • 1




              I really appreciate this answer as well. While seq worked fine for my scenario and seemed to be the simplest solution, it is good to know that there are other (even POSIX) alternatives. Thanks for this.
              – rubynorails
              Nov 19 '15 at 23:43







            • 2




              There's really no reason to use eval; if you have brace expansion, you have the C-style loop.
              – chepner
              Nov 20 '15 at 2:46










            • @PSkocik - If I could choose 2 answers, I would also choose this one. When I came across the fact that I needed to do this in reverse, your eval example was the simplest and would have saved me from having to search for an alternate way of doing it using seq. The while loop is a bit bulky for me. I like to keep things short and sweet, and I never could get the C-style for loop to work by giving i the value of 0 or 1. It never returned correctly and was always a little off. I'm sure it could be tweaked to work correctly, but these are definitely helpful solutions, nonetheless.
              – rubynorails
              Nov 20 '15 at 3:19











            • The eval approach is problematic if there is anything non-trivial inside the body of the loop. I imagine it would not be very readable if you needed to nest two such loops.
              – kasperd
              Nov 20 '15 at 10:38















            8














            If you must avoid seq, which as Tom Hunt points out seems to be the usual solution to this, then an eval definitely can do it (though, I wouldn't encourage it):



            eval 'for i in 1..'$numlines'; do echo $i; done'


            You can stay POSIX by avoiding the expansion, and simply do math and integer comparisons on $numlines:



            while [ ! "$numlines" -eq 0 ]; do
            echo "$numlines"
            : $((numlines-=1))
            done


            Outside of POSIX, bash and ksh and zsh also have C-style for loops:



            for((i=0; i<numlines; i++)); do echo $i; done





            share|improve this answer


















            • 1




              I really appreciate this answer as well. While seq worked fine for my scenario and seemed to be the simplest solution, it is good to know that there are other (even POSIX) alternatives. Thanks for this.
              – rubynorails
              Nov 19 '15 at 23:43







            • 2




              There's really no reason to use eval; if you have brace expansion, you have the C-style loop.
              – chepner
              Nov 20 '15 at 2:46










            • @PSkocik - If I could choose 2 answers, I would also choose this one. When I came across the fact that I needed to do this in reverse, your eval example was the simplest and would have saved me from having to search for an alternate way of doing it using seq. The while loop is a bit bulky for me. I like to keep things short and sweet, and I never could get the C-style for loop to work by giving i the value of 0 or 1. It never returned correctly and was always a little off. I'm sure it could be tweaked to work correctly, but these are definitely helpful solutions, nonetheless.
              – rubynorails
              Nov 20 '15 at 3:19











            • The eval approach is problematic if there is anything non-trivial inside the body of the loop. I imagine it would not be very readable if you needed to nest two such loops.
              – kasperd
              Nov 20 '15 at 10:38













            8












            8








            8






            If you must avoid seq, which as Tom Hunt points out seems to be the usual solution to this, then an eval definitely can do it (though, I wouldn't encourage it):



            eval 'for i in 1..'$numlines'; do echo $i; done'


            You can stay POSIX by avoiding the expansion, and simply do math and integer comparisons on $numlines:



            while [ ! "$numlines" -eq 0 ]; do
            echo "$numlines"
            : $((numlines-=1))
            done


            Outside of POSIX, bash and ksh and zsh also have C-style for loops:



            for((i=0; i<numlines; i++)); do echo $i; done





            share|improve this answer














            If you must avoid seq, which as Tom Hunt points out seems to be the usual solution to this, then an eval definitely can do it (though, I wouldn't encourage it):



            eval 'for i in 1..'$numlines'; do echo $i; done'


            You can stay POSIX by avoiding the expansion, and simply do math and integer comparisons on $numlines:



            while [ ! "$numlines" -eq 0 ]; do
            echo "$numlines"
            : $((numlines-=1))
            done


            Outside of POSIX, bash and ksh and zsh also have C-style for loops:



            for((i=0; i<numlines; i++)); do echo $i; done






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 20 '15 at 2:53

























            answered Nov 19 '15 at 23:33









            PSkocik

            17.8k44994




            17.8k44994







            • 1




              I really appreciate this answer as well. While seq worked fine for my scenario and seemed to be the simplest solution, it is good to know that there are other (even POSIX) alternatives. Thanks for this.
              – rubynorails
              Nov 19 '15 at 23:43







            • 2




              There's really no reason to use eval; if you have brace expansion, you have the C-style loop.
              – chepner
              Nov 20 '15 at 2:46










            • @PSkocik - If I could choose 2 answers, I would also choose this one. When I came across the fact that I needed to do this in reverse, your eval example was the simplest and would have saved me from having to search for an alternate way of doing it using seq. The while loop is a bit bulky for me. I like to keep things short and sweet, and I never could get the C-style for loop to work by giving i the value of 0 or 1. It never returned correctly and was always a little off. I'm sure it could be tweaked to work correctly, but these are definitely helpful solutions, nonetheless.
              – rubynorails
              Nov 20 '15 at 3:19











            • The eval approach is problematic if there is anything non-trivial inside the body of the loop. I imagine it would not be very readable if you needed to nest two such loops.
              – kasperd
              Nov 20 '15 at 10:38












            • 1




              I really appreciate this answer as well. While seq worked fine for my scenario and seemed to be the simplest solution, it is good to know that there are other (even POSIX) alternatives. Thanks for this.
              – rubynorails
              Nov 19 '15 at 23:43







            • 2




              There's really no reason to use eval; if you have brace expansion, you have the C-style loop.
              – chepner
              Nov 20 '15 at 2:46










            • @PSkocik - If I could choose 2 answers, I would also choose this one. When I came across the fact that I needed to do this in reverse, your eval example was the simplest and would have saved me from having to search for an alternate way of doing it using seq. The while loop is a bit bulky for me. I like to keep things short and sweet, and I never could get the C-style for loop to work by giving i the value of 0 or 1. It never returned correctly and was always a little off. I'm sure it could be tweaked to work correctly, but these are definitely helpful solutions, nonetheless.
              – rubynorails
              Nov 20 '15 at 3:19











            • The eval approach is problematic if there is anything non-trivial inside the body of the loop. I imagine it would not be very readable if you needed to nest two such loops.
              – kasperd
              Nov 20 '15 at 10:38







            1




            1




            I really appreciate this answer as well. While seq worked fine for my scenario and seemed to be the simplest solution, it is good to know that there are other (even POSIX) alternatives. Thanks for this.
            – rubynorails
            Nov 19 '15 at 23:43





            I really appreciate this answer as well. While seq worked fine for my scenario and seemed to be the simplest solution, it is good to know that there are other (even POSIX) alternatives. Thanks for this.
            – rubynorails
            Nov 19 '15 at 23:43





            2




            2




            There's really no reason to use eval; if you have brace expansion, you have the C-style loop.
            – chepner
            Nov 20 '15 at 2:46




            There's really no reason to use eval; if you have brace expansion, you have the C-style loop.
            – chepner
            Nov 20 '15 at 2:46












            @PSkocik - If I could choose 2 answers, I would also choose this one. When I came across the fact that I needed to do this in reverse, your eval example was the simplest and would have saved me from having to search for an alternate way of doing it using seq. The while loop is a bit bulky for me. I like to keep things short and sweet, and I never could get the C-style for loop to work by giving i the value of 0 or 1. It never returned correctly and was always a little off. I'm sure it could be tweaked to work correctly, but these are definitely helpful solutions, nonetheless.
            – rubynorails
            Nov 20 '15 at 3:19





            @PSkocik - If I could choose 2 answers, I would also choose this one. When I came across the fact that I needed to do this in reverse, your eval example was the simplest and would have saved me from having to search for an alternate way of doing it using seq. The while loop is a bit bulky for me. I like to keep things short and sweet, and I never could get the C-style for loop to work by giving i the value of 0 or 1. It never returned correctly and was always a little off. I'm sure it could be tweaked to work correctly, but these are definitely helpful solutions, nonetheless.
            – rubynorails
            Nov 20 '15 at 3:19













            The eval approach is problematic if there is anything non-trivial inside the body of the loop. I imagine it would not be very readable if you needed to nest two such loops.
            – kasperd
            Nov 20 '15 at 10:38




            The eval approach is problematic if there is anything non-trivial inside the body of the loop. I imagine it would not be very readable if you needed to nest two such loops.
            – kasperd
            Nov 20 '15 at 10:38

















            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%2f244233%2fcan-i-use-a-variable-in-a-bash-brace-expansion%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

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

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay