Brackets, Braces, Curly Brackets in Bash












9















Here goes the riddle:



If I do:



touch file{1,2,3}


It creates file1, file2, file3



And if I do



rm file[1-3]


It deletes them.



but if I do



touch file[1-3] 


it creates:



file[1-3]


Why?










share|improve this question




















  • 1





    Try to repeat it doing shopt -s nullglob before - you'll understand it. See mywiki.wooledge.org/glob

    – Rmano
    Oct 14 '15 at 21:37
















9















Here goes the riddle:



If I do:



touch file{1,2,3}


It creates file1, file2, file3



And if I do



rm file[1-3]


It deletes them.



but if I do



touch file[1-3] 


it creates:



file[1-3]


Why?










share|improve this question




















  • 1





    Try to repeat it doing shopt -s nullglob before - you'll understand it. See mywiki.wooledge.org/glob

    – Rmano
    Oct 14 '15 at 21:37














9












9








9


2






Here goes the riddle:



If I do:



touch file{1,2,3}


It creates file1, file2, file3



And if I do



rm file[1-3]


It deletes them.



but if I do



touch file[1-3] 


it creates:



file[1-3]


Why?










share|improve this question
















Here goes the riddle:



If I do:



touch file{1,2,3}


It creates file1, file2, file3



And if I do



rm file[1-3]


It deletes them.



but if I do



touch file[1-3] 


it creates:



file[1-3]


Why?







bash syntax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 14 '15 at 21:37









muru

1




1










asked Oct 14 '15 at 21:24









UlukaiUlukai

220110




220110








  • 1





    Try to repeat it doing shopt -s nullglob before - you'll understand it. See mywiki.wooledge.org/glob

    – Rmano
    Oct 14 '15 at 21:37














  • 1





    Try to repeat it doing shopt -s nullglob before - you'll understand it. See mywiki.wooledge.org/glob

    – Rmano
    Oct 14 '15 at 21:37








1




1





Try to repeat it doing shopt -s nullglob before - you'll understand it. See mywiki.wooledge.org/glob

– Rmano
Oct 14 '15 at 21:37





Try to repeat it doing shopt -s nullglob before - you'll understand it. See mywiki.wooledge.org/glob

– Rmano
Oct 14 '15 at 21:37










1 Answer
1






active

oldest

votes


















12














If you took the trouble of reading the manpage instead of making riddles:



Brace Expansion
Brace expansion is a mechanism by which arbitrary strings may be
generated. This mechanism is similar to pathname expansion, but the
filenames generated need not exist.
...
Pathname Expansion
After word splitting, unless the -f option has been set, bash scans
each word for the characters *, ?, and [. If one of these characters
appears, then the word is regarded as a pattern, and replaced with an
alphabetically sorted list of filenames matching the pattern (see
Pattern Matching below). If no matching filenames are found, and the
shell option nullglob is not enabled, the word is left unchanged.
...
Pattern Matching

Any character that appears in a pattern, other than the special pattern
characters described below, matches itself. ...

The special pattern characters have the following meanings:

...
[...] Matches any one of the enclosed characters. A pair of
characters separated by a hyphen denotes a range
expression
; any character that falls between those two
characters, inclusive, using the current locale's
collating sequence and character set, is matched.


file[1-3] expands into files named file1, file2, file3. Filename expansion happens only if matching files exist. If not, the pattern is left as-is. Therefore, with files named file1, file2, file3, file[1-3] expands to file1 file2 file3. Without these files, it doesn't expand, and remains as file[1-3]. With {...}, the filenames don't have to exist, so file{1..3} expands to file1 file2 file3 irrespective of the files being present or absent.






share|improve this answer

























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "89"
    };
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2faskubuntu.com%2fquestions%2f685620%2fbrackets-braces-curly-brackets-in-bash%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    12














    If you took the trouble of reading the manpage instead of making riddles:



    Brace Expansion
    Brace expansion is a mechanism by which arbitrary strings may be
    generated. This mechanism is similar to pathname expansion, but the
    filenames generated need not exist.
    ...
    Pathname Expansion
    After word splitting, unless the -f option has been set, bash scans
    each word for the characters *, ?, and [. If one of these characters
    appears, then the word is regarded as a pattern, and replaced with an
    alphabetically sorted list of filenames matching the pattern (see
    Pattern Matching below). If no matching filenames are found, and the
    shell option nullglob is not enabled, the word is left unchanged.
    ...
    Pattern Matching

    Any character that appears in a pattern, other than the special pattern
    characters described below, matches itself. ...

    The special pattern characters have the following meanings:

    ...
    [...] Matches any one of the enclosed characters. A pair of
    characters separated by a hyphen denotes a range
    expression
    ; any character that falls between those two
    characters, inclusive, using the current locale's
    collating sequence and character set, is matched.


    file[1-3] expands into files named file1, file2, file3. Filename expansion happens only if matching files exist. If not, the pattern is left as-is. Therefore, with files named file1, file2, file3, file[1-3] expands to file1 file2 file3. Without these files, it doesn't expand, and remains as file[1-3]. With {...}, the filenames don't have to exist, so file{1..3} expands to file1 file2 file3 irrespective of the files being present or absent.






    share|improve this answer






























      12














      If you took the trouble of reading the manpage instead of making riddles:



      Brace Expansion
      Brace expansion is a mechanism by which arbitrary strings may be
      generated. This mechanism is similar to pathname expansion, but the
      filenames generated need not exist.
      ...
      Pathname Expansion
      After word splitting, unless the -f option has been set, bash scans
      each word for the characters *, ?, and [. If one of these characters
      appears, then the word is regarded as a pattern, and replaced with an
      alphabetically sorted list of filenames matching the pattern (see
      Pattern Matching below). If no matching filenames are found, and the
      shell option nullglob is not enabled, the word is left unchanged.
      ...
      Pattern Matching

      Any character that appears in a pattern, other than the special pattern
      characters described below, matches itself. ...

      The special pattern characters have the following meanings:

      ...
      [...] Matches any one of the enclosed characters. A pair of
      characters separated by a hyphen denotes a range
      expression
      ; any character that falls between those two
      characters, inclusive, using the current locale's
      collating sequence and character set, is matched.


      file[1-3] expands into files named file1, file2, file3. Filename expansion happens only if matching files exist. If not, the pattern is left as-is. Therefore, with files named file1, file2, file3, file[1-3] expands to file1 file2 file3. Without these files, it doesn't expand, and remains as file[1-3]. With {...}, the filenames don't have to exist, so file{1..3} expands to file1 file2 file3 irrespective of the files being present or absent.






      share|improve this answer




























        12












        12








        12







        If you took the trouble of reading the manpage instead of making riddles:



        Brace Expansion
        Brace expansion is a mechanism by which arbitrary strings may be
        generated. This mechanism is similar to pathname expansion, but the
        filenames generated need not exist.
        ...
        Pathname Expansion
        After word splitting, unless the -f option has been set, bash scans
        each word for the characters *, ?, and [. If one of these characters
        appears, then the word is regarded as a pattern, and replaced with an
        alphabetically sorted list of filenames matching the pattern (see
        Pattern Matching below). If no matching filenames are found, and the
        shell option nullglob is not enabled, the word is left unchanged.
        ...
        Pattern Matching

        Any character that appears in a pattern, other than the special pattern
        characters described below, matches itself. ...

        The special pattern characters have the following meanings:

        ...
        [...] Matches any one of the enclosed characters. A pair of
        characters separated by a hyphen denotes a range
        expression
        ; any character that falls between those two
        characters, inclusive, using the current locale's
        collating sequence and character set, is matched.


        file[1-3] expands into files named file1, file2, file3. Filename expansion happens only if matching files exist. If not, the pattern is left as-is. Therefore, with files named file1, file2, file3, file[1-3] expands to file1 file2 file3. Without these files, it doesn't expand, and remains as file[1-3]. With {...}, the filenames don't have to exist, so file{1..3} expands to file1 file2 file3 irrespective of the files being present or absent.






        share|improve this answer















        If you took the trouble of reading the manpage instead of making riddles:



        Brace Expansion
        Brace expansion is a mechanism by which arbitrary strings may be
        generated. This mechanism is similar to pathname expansion, but the
        filenames generated need not exist.
        ...
        Pathname Expansion
        After word splitting, unless the -f option has been set, bash scans
        each word for the characters *, ?, and [. If one of these characters
        appears, then the word is regarded as a pattern, and replaced with an
        alphabetically sorted list of filenames matching the pattern (see
        Pattern Matching below). If no matching filenames are found, and the
        shell option nullglob is not enabled, the word is left unchanged.
        ...
        Pattern Matching

        Any character that appears in a pattern, other than the special pattern
        characters described below, matches itself. ...

        The special pattern characters have the following meanings:

        ...
        [...] Matches any one of the enclosed characters. A pair of
        characters separated by a hyphen denotes a range
        expression
        ; any character that falls between those two
        characters, inclusive, using the current locale's
        collating sequence and character set, is matched.


        file[1-3] expands into files named file1, file2, file3. Filename expansion happens only if matching files exist. If not, the pattern is left as-is. Therefore, with files named file1, file2, file3, file[1-3] expands to file1 file2 file3. Without these files, it doesn't expand, and remains as file[1-3]. With {...}, the filenames don't have to exist, so file{1..3} expands to file1 file2 file3 irrespective of the files being present or absent.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Oct 14 '15 at 21:42

























        answered Oct 14 '15 at 21:36









        murumuru

        1




        1






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Ask Ubuntu!


            • 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%2faskubuntu.com%2fquestions%2f685620%2fbrackets-braces-curly-brackets-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

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?