Why does “cat foo” not output foo, but “cat foo,bar” does?

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











up vote
12
down vote

favorite
2












I was trying to concatenate text files in sub-folders and tried:



cat ./mainfolder1,mainfolder2,mainfolder3/subfolder1/book.txt > out$var


However this did not return anything. So, tried adding a non existing 'subfolder2'



cat ./mainfolder1,mainfolder2,mainfolder3/subfolder1,subfolder2/book.txt > out$var


And this time it did work out, concatenating the files successfully.
Why does this happens?







share|improve this question

















  • 2




    Use echo instead of cat to see what command line you got. (Or use set -x for debugging.)
    – Peter Cordes
    May 20 at 6:46














up vote
12
down vote

favorite
2












I was trying to concatenate text files in sub-folders and tried:



cat ./mainfolder1,mainfolder2,mainfolder3/subfolder1/book.txt > out$var


However this did not return anything. So, tried adding a non existing 'subfolder2'



cat ./mainfolder1,mainfolder2,mainfolder3/subfolder1,subfolder2/book.txt > out$var


And this time it did work out, concatenating the files successfully.
Why does this happens?







share|improve this question

















  • 2




    Use echo instead of cat to see what command line you got. (Or use set -x for debugging.)
    – Peter Cordes
    May 20 at 6:46












up vote
12
down vote

favorite
2









up vote
12
down vote

favorite
2






2





I was trying to concatenate text files in sub-folders and tried:



cat ./mainfolder1,mainfolder2,mainfolder3/subfolder1/book.txt > out$var


However this did not return anything. So, tried adding a non existing 'subfolder2'



cat ./mainfolder1,mainfolder2,mainfolder3/subfolder1,subfolder2/book.txt > out$var


And this time it did work out, concatenating the files successfully.
Why does this happens?







share|improve this question













I was trying to concatenate text files in sub-folders and tried:



cat ./mainfolder1,mainfolder2,mainfolder3/subfolder1/book.txt > out$var


However this did not return anything. So, tried adding a non existing 'subfolder2'



cat ./mainfolder1,mainfolder2,mainfolder3/subfolder1,subfolder2/book.txt > out$var


And this time it did work out, concatenating the files successfully.
Why does this happens?









share|improve this question












share|improve this question




share|improve this question








edited May 20 at 3:00









Charles Duffy

734413




734413









asked May 19 at 6:39









CDPF

614




614







  • 2




    Use echo instead of cat to see what command line you got. (Or use set -x for debugging.)
    – Peter Cordes
    May 20 at 6:46












  • 2




    Use echo instead of cat to see what command line you got. (Or use set -x for debugging.)
    – Peter Cordes
    May 20 at 6:46







2




2




Use echo instead of cat to see what command line you got. (Or use set -x for debugging.)
– Peter Cordes
May 20 at 6:46




Use echo instead of cat to see what command line you got. (Or use set -x for debugging.)
– Peter Cordes
May 20 at 6:46










3 Answers
3






active

oldest

votes

















up vote
21
down vote













subfolder1 evaluates to subfolder1, since there are no alternatives. Use subfolder1 instead.






share|improve this answer



















  • 2




    Note that it's different from in csh (where brace expansion comes from), tcsh or fish.
    – Stéphane Chazelas
    May 19 at 9:50

















up vote
20
down vote













By definition, brace expansion in GNU Bash requires either a sequence expression or a series of comma-separated values:




Patterns to be brace expanded take the form of an optional preamble, followed by either a series of comma-separated strings or a sequence expression between a pair of braces, followed by an optional postscript.




You can read the manual for details.



A few simple samples:



echo subfolder1
subfolder1

echo subfolder1,subfolder2
subfolder1 subfolder2

echo subfolder1
subfolder1

echo subfolder1..2
subfolder1 subfolder2





share|improve this answer






























    up vote
    1
    down vote













    Braces will only expand if they have coma separated strings, for e.g. abc,def or range, for e.g. a..e specified between them.



    In your case you can just write subfolder1 without enclosing it in braces as there is no need for that



    cat ./mainfolder1,mainfolder2,mainfolder3/subfolder1/book.txt > out$var





    share|improve this answer























    • Unfortunately, /path/a,/filename expands to the two strings /path/a/filename and /path//filename, which may be unwanted.
      – Kusalananda
      May 19 at 17:02










    • thanks @Kusalananda for rectifying me, yes bash will provide a warning saing "ambiguous redirect"
      – Neo_Returns
      May 19 at 17:10











    • No, you get ambiguous redirect if you try to redirect into a file given by an unquoted variable that has no value, e.g. echo 'hello' >$idontexist.
      – Kusalananda
      May 19 at 17:14






    • 1




      ...or if the filename in the redirection gets expanded to multiple words. Like > *.txt with multiple .txt files, or > $file if $file contains whitespace. But of course there's nothing ambiguous in giving cat multiple arguments
      – ilkkachu
      May 20 at 8:33











    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%2f444754%2fwhy-does-cat-foo-not-output-foo-but-cat-foo-bar-does%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    21
    down vote













    subfolder1 evaluates to subfolder1, since there are no alternatives. Use subfolder1 instead.






    share|improve this answer



















    • 2




      Note that it's different from in csh (where brace expansion comes from), tcsh or fish.
      – Stéphane Chazelas
      May 19 at 9:50














    up vote
    21
    down vote













    subfolder1 evaluates to subfolder1, since there are no alternatives. Use subfolder1 instead.






    share|improve this answer



















    • 2




      Note that it's different from in csh (where brace expansion comes from), tcsh or fish.
      – Stéphane Chazelas
      May 19 at 9:50












    up vote
    21
    down vote










    up vote
    21
    down vote









    subfolder1 evaluates to subfolder1, since there are no alternatives. Use subfolder1 instead.






    share|improve this answer















    subfolder1 evaluates to subfolder1, since there are no alternatives. Use subfolder1 instead.







    share|improve this answer















    share|improve this answer



    share|improve this answer








    edited May 19 at 9:34









    David Foerster

    915616




    915616











    answered May 19 at 6:45









    Ignacio Vazquez-Abrams

    32k66780




    32k66780







    • 2




      Note that it's different from in csh (where brace expansion comes from), tcsh or fish.
      – Stéphane Chazelas
      May 19 at 9:50












    • 2




      Note that it's different from in csh (where brace expansion comes from), tcsh or fish.
      – Stéphane Chazelas
      May 19 at 9:50







    2




    2




    Note that it's different from in csh (where brace expansion comes from), tcsh or fish.
    – Stéphane Chazelas
    May 19 at 9:50




    Note that it's different from in csh (where brace expansion comes from), tcsh or fish.
    – Stéphane Chazelas
    May 19 at 9:50












    up vote
    20
    down vote













    By definition, brace expansion in GNU Bash requires either a sequence expression or a series of comma-separated values:




    Patterns to be brace expanded take the form of an optional preamble, followed by either a series of comma-separated strings or a sequence expression between a pair of braces, followed by an optional postscript.




    You can read the manual for details.



    A few simple samples:



    echo subfolder1
    subfolder1

    echo subfolder1,subfolder2
    subfolder1 subfolder2

    echo subfolder1
    subfolder1

    echo subfolder1..2
    subfolder1 subfolder2





    share|improve this answer



























      up vote
      20
      down vote













      By definition, brace expansion in GNU Bash requires either a sequence expression or a series of comma-separated values:




      Patterns to be brace expanded take the form of an optional preamble, followed by either a series of comma-separated strings or a sequence expression between a pair of braces, followed by an optional postscript.




      You can read the manual for details.



      A few simple samples:



      echo subfolder1
      subfolder1

      echo subfolder1,subfolder2
      subfolder1 subfolder2

      echo subfolder1
      subfolder1

      echo subfolder1..2
      subfolder1 subfolder2





      share|improve this answer

























        up vote
        20
        down vote










        up vote
        20
        down vote









        By definition, brace expansion in GNU Bash requires either a sequence expression or a series of comma-separated values:




        Patterns to be brace expanded take the form of an optional preamble, followed by either a series of comma-separated strings or a sequence expression between a pair of braces, followed by an optional postscript.




        You can read the manual for details.



        A few simple samples:



        echo subfolder1
        subfolder1

        echo subfolder1,subfolder2
        subfolder1 subfolder2

        echo subfolder1
        subfolder1

        echo subfolder1..2
        subfolder1 subfolder2





        share|improve this answer















        By definition, brace expansion in GNU Bash requires either a sequence expression or a series of comma-separated values:




        Patterns to be brace expanded take the form of an optional preamble, followed by either a series of comma-separated strings or a sequence expression between a pair of braces, followed by an optional postscript.




        You can read the manual for details.



        A few simple samples:



        echo subfolder1
        subfolder1

        echo subfolder1,subfolder2
        subfolder1 subfolder2

        echo subfolder1
        subfolder1

        echo subfolder1..2
        subfolder1 subfolder2






        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited May 19 at 9:44









        αғsнιη

        14.7k82361




        14.7k82361











        answered May 19 at 6:56









        Arthur Hess

        3385




        3385




















            up vote
            1
            down vote













            Braces will only expand if they have coma separated strings, for e.g. abc,def or range, for e.g. a..e specified between them.



            In your case you can just write subfolder1 without enclosing it in braces as there is no need for that



            cat ./mainfolder1,mainfolder2,mainfolder3/subfolder1/book.txt > out$var





            share|improve this answer























            • Unfortunately, /path/a,/filename expands to the two strings /path/a/filename and /path//filename, which may be unwanted.
              – Kusalananda
              May 19 at 17:02










            • thanks @Kusalananda for rectifying me, yes bash will provide a warning saing "ambiguous redirect"
              – Neo_Returns
              May 19 at 17:10











            • No, you get ambiguous redirect if you try to redirect into a file given by an unquoted variable that has no value, e.g. echo 'hello' >$idontexist.
              – Kusalananda
              May 19 at 17:14






            • 1




              ...or if the filename in the redirection gets expanded to multiple words. Like > *.txt with multiple .txt files, or > $file if $file contains whitespace. But of course there's nothing ambiguous in giving cat multiple arguments
              – ilkkachu
              May 20 at 8:33















            up vote
            1
            down vote













            Braces will only expand if they have coma separated strings, for e.g. abc,def or range, for e.g. a..e specified between them.



            In your case you can just write subfolder1 without enclosing it in braces as there is no need for that



            cat ./mainfolder1,mainfolder2,mainfolder3/subfolder1/book.txt > out$var





            share|improve this answer























            • Unfortunately, /path/a,/filename expands to the two strings /path/a/filename and /path//filename, which may be unwanted.
              – Kusalananda
              May 19 at 17:02










            • thanks @Kusalananda for rectifying me, yes bash will provide a warning saing "ambiguous redirect"
              – Neo_Returns
              May 19 at 17:10











            • No, you get ambiguous redirect if you try to redirect into a file given by an unquoted variable that has no value, e.g. echo 'hello' >$idontexist.
              – Kusalananda
              May 19 at 17:14






            • 1




              ...or if the filename in the redirection gets expanded to multiple words. Like > *.txt with multiple .txt files, or > $file if $file contains whitespace. But of course there's nothing ambiguous in giving cat multiple arguments
              – ilkkachu
              May 20 at 8:33













            up vote
            1
            down vote










            up vote
            1
            down vote









            Braces will only expand if they have coma separated strings, for e.g. abc,def or range, for e.g. a..e specified between them.



            In your case you can just write subfolder1 without enclosing it in braces as there is no need for that



            cat ./mainfolder1,mainfolder2,mainfolder3/subfolder1/book.txt > out$var





            share|improve this answer















            Braces will only expand if they have coma separated strings, for e.g. abc,def or range, for e.g. a..e specified between them.



            In your case you can just write subfolder1 without enclosing it in braces as there is no need for that



            cat ./mainfolder1,mainfolder2,mainfolder3/subfolder1/book.txt > out$var






            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited May 19 at 17:11


























            answered May 19 at 16:54









            Neo_Returns

            17311




            17311











            • Unfortunately, /path/a,/filename expands to the two strings /path/a/filename and /path//filename, which may be unwanted.
              – Kusalananda
              May 19 at 17:02










            • thanks @Kusalananda for rectifying me, yes bash will provide a warning saing "ambiguous redirect"
              – Neo_Returns
              May 19 at 17:10











            • No, you get ambiguous redirect if you try to redirect into a file given by an unquoted variable that has no value, e.g. echo 'hello' >$idontexist.
              – Kusalananda
              May 19 at 17:14






            • 1




              ...or if the filename in the redirection gets expanded to multiple words. Like > *.txt with multiple .txt files, or > $file if $file contains whitespace. But of course there's nothing ambiguous in giving cat multiple arguments
              – ilkkachu
              May 20 at 8:33

















            • Unfortunately, /path/a,/filename expands to the two strings /path/a/filename and /path//filename, which may be unwanted.
              – Kusalananda
              May 19 at 17:02










            • thanks @Kusalananda for rectifying me, yes bash will provide a warning saing "ambiguous redirect"
              – Neo_Returns
              May 19 at 17:10











            • No, you get ambiguous redirect if you try to redirect into a file given by an unquoted variable that has no value, e.g. echo 'hello' >$idontexist.
              – Kusalananda
              May 19 at 17:14






            • 1




              ...or if the filename in the redirection gets expanded to multiple words. Like > *.txt with multiple .txt files, or > $file if $file contains whitespace. But of course there's nothing ambiguous in giving cat multiple arguments
              – ilkkachu
              May 20 at 8:33
















            Unfortunately, /path/a,/filename expands to the two strings /path/a/filename and /path//filename, which may be unwanted.
            – Kusalananda
            May 19 at 17:02




            Unfortunately, /path/a,/filename expands to the two strings /path/a/filename and /path//filename, which may be unwanted.
            – Kusalananda
            May 19 at 17:02












            thanks @Kusalananda for rectifying me, yes bash will provide a warning saing "ambiguous redirect"
            – Neo_Returns
            May 19 at 17:10





            thanks @Kusalananda for rectifying me, yes bash will provide a warning saing "ambiguous redirect"
            – Neo_Returns
            May 19 at 17:10













            No, you get ambiguous redirect if you try to redirect into a file given by an unquoted variable that has no value, e.g. echo 'hello' >$idontexist.
            – Kusalananda
            May 19 at 17:14




            No, you get ambiguous redirect if you try to redirect into a file given by an unquoted variable that has no value, e.g. echo 'hello' >$idontexist.
            – Kusalananda
            May 19 at 17:14




            1




            1




            ...or if the filename in the redirection gets expanded to multiple words. Like > *.txt with multiple .txt files, or > $file if $file contains whitespace. But of course there's nothing ambiguous in giving cat multiple arguments
            – ilkkachu
            May 20 at 8:33





            ...or if the filename in the redirection gets expanded to multiple words. Like > *.txt with multiple .txt files, or > $file if $file contains whitespace. But of course there's nothing ambiguous in giving cat multiple arguments
            – ilkkachu
            May 20 at 8:33













             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f444754%2fwhy-does-cat-foo-not-output-foo-but-cat-foo-bar-does%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)