How does curly brace expansion work in the shell?

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











up vote
0
down vote

favorite












The command echo 1..3-1,2 prints 1-1 1-2 2-1 2-2 3-1 3-2. I understand the way those curly braces can be used. But what actually are they?



Is it the job of sh / bash to parse/expand them and deliver the expanded version to the executed program?



If so, what other tricks can it do and is there a specification?



Also, is there a name for it?



Is ls *.txt handled in a similar way internally?



Is there a way to achieve an n-times repetition of an argument? Like (not working, of course, only a concept): cat test.pdf*3 ⇒ cat test.pdf test.pdf test.pdf ?







share|improve this question


















  • 2




    They are called brace expansion, obviously. It's one of several expansions done by bash. You can read on it in manual.
    – PesaThe
    Dec 6 '17 at 1:02











  • Thanks! Posted as answer, this could get a check mark :)
    – Matmarbon
    Dec 6 '17 at 1:12














up vote
0
down vote

favorite












The command echo 1..3-1,2 prints 1-1 1-2 2-1 2-2 3-1 3-2. I understand the way those curly braces can be used. But what actually are they?



Is it the job of sh / bash to parse/expand them and deliver the expanded version to the executed program?



If so, what other tricks can it do and is there a specification?



Also, is there a name for it?



Is ls *.txt handled in a similar way internally?



Is there a way to achieve an n-times repetition of an argument? Like (not working, of course, only a concept): cat test.pdf*3 ⇒ cat test.pdf test.pdf test.pdf ?







share|improve this question


















  • 2




    They are called brace expansion, obviously. It's one of several expansions done by bash. You can read on it in manual.
    – PesaThe
    Dec 6 '17 at 1:02











  • Thanks! Posted as answer, this could get a check mark :)
    – Matmarbon
    Dec 6 '17 at 1:12












up vote
0
down vote

favorite









up vote
0
down vote

favorite











The command echo 1..3-1,2 prints 1-1 1-2 2-1 2-2 3-1 3-2. I understand the way those curly braces can be used. But what actually are they?



Is it the job of sh / bash to parse/expand them and deliver the expanded version to the executed program?



If so, what other tricks can it do and is there a specification?



Also, is there a name for it?



Is ls *.txt handled in a similar way internally?



Is there a way to achieve an n-times repetition of an argument? Like (not working, of course, only a concept): cat test.pdf*3 ⇒ cat test.pdf test.pdf test.pdf ?







share|improve this question














The command echo 1..3-1,2 prints 1-1 1-2 2-1 2-2 3-1 3-2. I understand the way those curly braces can be used. But what actually are they?



Is it the job of sh / bash to parse/expand them and deliver the expanded version to the executed program?



If so, what other tricks can it do and is there a specification?



Also, is there a name for it?



Is ls *.txt handled in a similar way internally?



Is there a way to achieve an n-times repetition of an argument? Like (not working, of course, only a concept): cat test.pdf*3 ⇒ cat test.pdf test.pdf test.pdf ?









share|improve this question













share|improve this question




share|improve this question








edited Dec 6 '17 at 0:49









Jeff Schaller

32k848109




32k848109










asked Dec 6 '17 at 0:42









Matmarbon

1407




1407







  • 2




    They are called brace expansion, obviously. It's one of several expansions done by bash. You can read on it in manual.
    – PesaThe
    Dec 6 '17 at 1:02











  • Thanks! Posted as answer, this could get a check mark :)
    – Matmarbon
    Dec 6 '17 at 1:12












  • 2




    They are called brace expansion, obviously. It's one of several expansions done by bash. You can read on it in manual.
    – PesaThe
    Dec 6 '17 at 1:02











  • Thanks! Posted as answer, this could get a check mark :)
    – Matmarbon
    Dec 6 '17 at 1:12







2




2




They are called brace expansion, obviously. It's one of several expansions done by bash. You can read on it in manual.
– PesaThe
Dec 6 '17 at 1:02





They are called brace expansion, obviously. It's one of several expansions done by bash. You can read on it in manual.
– PesaThe
Dec 6 '17 at 1:02













Thanks! Posted as answer, this could get a check mark :)
– Matmarbon
Dec 6 '17 at 1:12




Thanks! Posted as answer, this could get a check mark :)
– Matmarbon
Dec 6 '17 at 1:12










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










They are called brace expansion.



It is one of several expansions done by bash, zsh and ksh, filename expansion *.txt being another one of them. Brace expansion is not covered by the POSIX standard and is thus not portable.
You can read on this in bash manual.



On @Arrow's suggestion: in order to get cat test.pdf test.pdf test.pdf with brace expansion alone, you would have to use this "hack":



#cat test.pdf test.pdf
cat test.pdf,

#cat test.pdf test.pdf test.pdf
cat test.pdf,,

#cat test.pdf test.pdf test.pdf test.pdf
cat test.pdf,,,


Some common uses:



for index in 1..10; do
echo "$index"
done

touch test_file_a..e.txt


Or another "hack" to print a string 10 times:



printf -- "mystringn%0.s" 1..10



Be aware that brace expansion in bash is done before parameter expansion, therefore a common mistake is:



num=10
for index in 1..$num; do
echo "$index"
done


(the ksh93 shell copes with this though)






share|improve this answer






















  • @Kusalananda Thanks for the edit. For some reason I thought the question was bash specific.
    – PesaThe
    Dec 6 '17 at 10:07

















up vote
1
down vote













PesaThe's answer answer covers important aspects of the question. There are several things I want to add.



The asterisk in ls *.txt is handled by the shell and is therefore controlled by shell options which can be changed by shell built-ins. In this case, one can disable asterisk expansion by running set -f and enable it again by set +f.



Another thing is that anyone who wants to make the script portable should check the POSIX standard. For example 1..9..2 expands to 1 3 5 7 9 in bash 4 but does not expand in lower bash versions or in sh.



Hope this helps.






share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f409065%2fhow-does-curly-brace-expansion-work-in-the-shell%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    They are called brace expansion.



    It is one of several expansions done by bash, zsh and ksh, filename expansion *.txt being another one of them. Brace expansion is not covered by the POSIX standard and is thus not portable.
    You can read on this in bash manual.



    On @Arrow's suggestion: in order to get cat test.pdf test.pdf test.pdf with brace expansion alone, you would have to use this "hack":



    #cat test.pdf test.pdf
    cat test.pdf,

    #cat test.pdf test.pdf test.pdf
    cat test.pdf,,

    #cat test.pdf test.pdf test.pdf test.pdf
    cat test.pdf,,,


    Some common uses:



    for index in 1..10; do
    echo "$index"
    done

    touch test_file_a..e.txt


    Or another "hack" to print a string 10 times:



    printf -- "mystringn%0.s" 1..10



    Be aware that brace expansion in bash is done before parameter expansion, therefore a common mistake is:



    num=10
    for index in 1..$num; do
    echo "$index"
    done


    (the ksh93 shell copes with this though)






    share|improve this answer






















    • @Kusalananda Thanks for the edit. For some reason I thought the question was bash specific.
      – PesaThe
      Dec 6 '17 at 10:07














    up vote
    3
    down vote



    accepted










    They are called brace expansion.



    It is one of several expansions done by bash, zsh and ksh, filename expansion *.txt being another one of them. Brace expansion is not covered by the POSIX standard and is thus not portable.
    You can read on this in bash manual.



    On @Arrow's suggestion: in order to get cat test.pdf test.pdf test.pdf with brace expansion alone, you would have to use this "hack":



    #cat test.pdf test.pdf
    cat test.pdf,

    #cat test.pdf test.pdf test.pdf
    cat test.pdf,,

    #cat test.pdf test.pdf test.pdf test.pdf
    cat test.pdf,,,


    Some common uses:



    for index in 1..10; do
    echo "$index"
    done

    touch test_file_a..e.txt


    Or another "hack" to print a string 10 times:



    printf -- "mystringn%0.s" 1..10



    Be aware that brace expansion in bash is done before parameter expansion, therefore a common mistake is:



    num=10
    for index in 1..$num; do
    echo "$index"
    done


    (the ksh93 shell copes with this though)






    share|improve this answer






















    • @Kusalananda Thanks for the edit. For some reason I thought the question was bash specific.
      – PesaThe
      Dec 6 '17 at 10:07












    up vote
    3
    down vote



    accepted







    up vote
    3
    down vote



    accepted






    They are called brace expansion.



    It is one of several expansions done by bash, zsh and ksh, filename expansion *.txt being another one of them. Brace expansion is not covered by the POSIX standard and is thus not portable.
    You can read on this in bash manual.



    On @Arrow's suggestion: in order to get cat test.pdf test.pdf test.pdf with brace expansion alone, you would have to use this "hack":



    #cat test.pdf test.pdf
    cat test.pdf,

    #cat test.pdf test.pdf test.pdf
    cat test.pdf,,

    #cat test.pdf test.pdf test.pdf test.pdf
    cat test.pdf,,,


    Some common uses:



    for index in 1..10; do
    echo "$index"
    done

    touch test_file_a..e.txt


    Or another "hack" to print a string 10 times:



    printf -- "mystringn%0.s" 1..10



    Be aware that brace expansion in bash is done before parameter expansion, therefore a common mistake is:



    num=10
    for index in 1..$num; do
    echo "$index"
    done


    (the ksh93 shell copes with this though)






    share|improve this answer














    They are called brace expansion.



    It is one of several expansions done by bash, zsh and ksh, filename expansion *.txt being another one of them. Brace expansion is not covered by the POSIX standard and is thus not portable.
    You can read on this in bash manual.



    On @Arrow's suggestion: in order to get cat test.pdf test.pdf test.pdf with brace expansion alone, you would have to use this "hack":



    #cat test.pdf test.pdf
    cat test.pdf,

    #cat test.pdf test.pdf test.pdf
    cat test.pdf,,

    #cat test.pdf test.pdf test.pdf test.pdf
    cat test.pdf,,,


    Some common uses:



    for index in 1..10; do
    echo "$index"
    done

    touch test_file_a..e.txt


    Or another "hack" to print a string 10 times:



    printf -- "mystringn%0.s" 1..10



    Be aware that brace expansion in bash is done before parameter expansion, therefore a common mistake is:



    num=10
    for index in 1..$num; do
    echo "$index"
    done


    (the ksh93 shell copes with this though)







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 6 '17 at 6:39









    Kusalananda

    104k14206324




    104k14206324










    answered Dec 6 '17 at 1:22









    PesaThe

    47337




    47337











    • @Kusalananda Thanks for the edit. For some reason I thought the question was bash specific.
      – PesaThe
      Dec 6 '17 at 10:07
















    • @Kusalananda Thanks for the edit. For some reason I thought the question was bash specific.
      – PesaThe
      Dec 6 '17 at 10:07















    @Kusalananda Thanks for the edit. For some reason I thought the question was bash specific.
    – PesaThe
    Dec 6 '17 at 10:07




    @Kusalananda Thanks for the edit. For some reason I thought the question was bash specific.
    – PesaThe
    Dec 6 '17 at 10:07












    up vote
    1
    down vote













    PesaThe's answer answer covers important aspects of the question. There are several things I want to add.



    The asterisk in ls *.txt is handled by the shell and is therefore controlled by shell options which can be changed by shell built-ins. In this case, one can disable asterisk expansion by running set -f and enable it again by set +f.



    Another thing is that anyone who wants to make the script portable should check the POSIX standard. For example 1..9..2 expands to 1 3 5 7 9 in bash 4 but does not expand in lower bash versions or in sh.



    Hope this helps.






    share|improve this answer


























      up vote
      1
      down vote













      PesaThe's answer answer covers important aspects of the question. There are several things I want to add.



      The asterisk in ls *.txt is handled by the shell and is therefore controlled by shell options which can be changed by shell built-ins. In this case, one can disable asterisk expansion by running set -f and enable it again by set +f.



      Another thing is that anyone who wants to make the script portable should check the POSIX standard. For example 1..9..2 expands to 1 3 5 7 9 in bash 4 but does not expand in lower bash versions or in sh.



      Hope this helps.






      share|improve this answer
























        up vote
        1
        down vote










        up vote
        1
        down vote









        PesaThe's answer answer covers important aspects of the question. There are several things I want to add.



        The asterisk in ls *.txt is handled by the shell and is therefore controlled by shell options which can be changed by shell built-ins. In this case, one can disable asterisk expansion by running set -f and enable it again by set +f.



        Another thing is that anyone who wants to make the script portable should check the POSIX standard. For example 1..9..2 expands to 1 3 5 7 9 in bash 4 but does not expand in lower bash versions or in sh.



        Hope this helps.






        share|improve this answer














        PesaThe's answer answer covers important aspects of the question. There are several things I want to add.



        The asterisk in ls *.txt is handled by the shell and is therefore controlled by shell options which can be changed by shell built-ins. In this case, one can disable asterisk expansion by running set -f and enable it again by set +f.



        Another thing is that anyone who wants to make the script portable should check the POSIX standard. For example 1..9..2 expands to 1 3 5 7 9 in bash 4 but does not expand in lower bash versions or in sh.



        Hope this helps.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 6 '17 at 6:34









        Kusalananda

        104k14206324




        104k14206324










        answered Dec 6 '17 at 6:31









        Weijun Zhou

        1,434119




        1,434119



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f409065%2fhow-does-curly-brace-expansion-work-in-the-shell%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

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

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay