bash: how for loop sorts files when iterating [duplicate]

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











up vote
0
down vote

favorite













This question already has an answer here:



  • Does the Bash star * wildcard always produce an (ascending) sorted list?

    4 answers



What is the default behavior of for loop in terms of sorting when listing files in a directory?



e.g.



for sqlfile in *.sql; do mysql -u root -p pass < sqlfile; done


Is this documented somewhere?







share|improve this question











marked as duplicate by Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
May 30 at 9:49


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2




    It does not sort ever. It is the glob (*.sql) that is doing the sorting.
    – ctrl-alt-delor
    May 30 at 9:41














up vote
0
down vote

favorite













This question already has an answer here:



  • Does the Bash star * wildcard always produce an (ascending) sorted list?

    4 answers



What is the default behavior of for loop in terms of sorting when listing files in a directory?



e.g.



for sqlfile in *.sql; do mysql -u root -p pass < sqlfile; done


Is this documented somewhere?







share|improve this question











marked as duplicate by Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
May 30 at 9:49


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2




    It does not sort ever. It is the glob (*.sql) that is doing the sorting.
    – ctrl-alt-delor
    May 30 at 9:41












up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:



  • Does the Bash star * wildcard always produce an (ascending) sorted list?

    4 answers



What is the default behavior of for loop in terms of sorting when listing files in a directory?



e.g.



for sqlfile in *.sql; do mysql -u root -p pass < sqlfile; done


Is this documented somewhere?







share|improve this question












This question already has an answer here:



  • Does the Bash star * wildcard always produce an (ascending) sorted list?

    4 answers



What is the default behavior of for loop in terms of sorting when listing files in a directory?



e.g.



for sqlfile in *.sql; do mysql -u root -p pass < sqlfile; done


Is this documented somewhere?





This question already has an answer here:



  • Does the Bash star * wildcard always produce an (ascending) sorted list?

    4 answers









share|improve this question










share|improve this question




share|improve this question









asked May 30 at 9:36









pkaramol

240112




240112




marked as duplicate by Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
May 30 at 9:49


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Kusalananda bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
May 30 at 9:49


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 2




    It does not sort ever. It is the glob (*.sql) that is doing the sorting.
    – ctrl-alt-delor
    May 30 at 9:41












  • 2




    It does not sort ever. It is the glob (*.sql) that is doing the sorting.
    – ctrl-alt-delor
    May 30 at 9:41







2




2




It does not sort ever. It is the glob (*.sql) that is doing the sorting.
– ctrl-alt-delor
May 30 at 9:41




It does not sort ever. It is the glob (*.sql) that is doing the sorting.
– ctrl-alt-delor
May 30 at 9:41










2 Answers
2






active

oldest

votes

















up vote
3
down vote













According to man bash's section on "Pathname Expansion":




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 […]




This sorting depends on the value of $LC_COLLATE:




This variable determines the collation order used when sorting the results of pathname expansion […]




As you can see from the above, this has nothing to do with the for loop. Globs can be used in many ways, and their expansion is always sorted.






share|improve this answer



















  • 2




    for does not sort ever. It is the glob (*.sql) that is doing the sorting.
    – ctrl-alt-delor
    May 30 at 9:42











  • Sure, I'm just explaining how the sorting that happens in the for loop works.
    – l0b0
    May 30 at 9:43






  • 1




    For me to accept the answer, the comment of ctrl-alt-delor on the glob should be added.
    – pkaramol
    May 30 at 9:44










  • +1 for the LC_COLLATE observation.
    – Rui F Ribeiro
    May 30 at 9:47

















up vote
2
down vote













for loops don't sort, they give you the values in exactly the order they were presented:



$ for x in b a c ; do echo $x ; done
b
a
c


Globs, on the other hand, do sort alphabetically, regardless of if you use them in a for or anywhere else:



$ touch b a c
$ echo *
a b c





share|improve this answer




























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    According to man bash's section on "Pathname Expansion":




    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 […]




    This sorting depends on the value of $LC_COLLATE:




    This variable determines the collation order used when sorting the results of pathname expansion […]




    As you can see from the above, this has nothing to do with the for loop. Globs can be used in many ways, and their expansion is always sorted.






    share|improve this answer



















    • 2




      for does not sort ever. It is the glob (*.sql) that is doing the sorting.
      – ctrl-alt-delor
      May 30 at 9:42











    • Sure, I'm just explaining how the sorting that happens in the for loop works.
      – l0b0
      May 30 at 9:43






    • 1




      For me to accept the answer, the comment of ctrl-alt-delor on the glob should be added.
      – pkaramol
      May 30 at 9:44










    • +1 for the LC_COLLATE observation.
      – Rui F Ribeiro
      May 30 at 9:47














    up vote
    3
    down vote













    According to man bash's section on "Pathname Expansion":




    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 […]




    This sorting depends on the value of $LC_COLLATE:




    This variable determines the collation order used when sorting the results of pathname expansion […]




    As you can see from the above, this has nothing to do with the for loop. Globs can be used in many ways, and their expansion is always sorted.






    share|improve this answer



















    • 2




      for does not sort ever. It is the glob (*.sql) that is doing the sorting.
      – ctrl-alt-delor
      May 30 at 9:42











    • Sure, I'm just explaining how the sorting that happens in the for loop works.
      – l0b0
      May 30 at 9:43






    • 1




      For me to accept the answer, the comment of ctrl-alt-delor on the glob should be added.
      – pkaramol
      May 30 at 9:44










    • +1 for the LC_COLLATE observation.
      – Rui F Ribeiro
      May 30 at 9:47












    up vote
    3
    down vote










    up vote
    3
    down vote









    According to man bash's section on "Pathname Expansion":




    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 […]




    This sorting depends on the value of $LC_COLLATE:




    This variable determines the collation order used when sorting the results of pathname expansion […]




    As you can see from the above, this has nothing to do with the for loop. Globs can be used in many ways, and their expansion is always sorted.






    share|improve this answer















    According to man bash's section on "Pathname Expansion":




    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 […]




    This sorting depends on the value of $LC_COLLATE:




    This variable determines the collation order used when sorting the results of pathname expansion […]




    As you can see from the above, this has nothing to do with the for loop. Globs can be used in many ways, and their expansion is always sorted.







    share|improve this answer















    share|improve this answer



    share|improve this answer








    edited May 30 at 20:46


























    answered May 30 at 9:40









    l0b0

    26k17104226




    26k17104226







    • 2




      for does not sort ever. It is the glob (*.sql) that is doing the sorting.
      – ctrl-alt-delor
      May 30 at 9:42











    • Sure, I'm just explaining how the sorting that happens in the for loop works.
      – l0b0
      May 30 at 9:43






    • 1




      For me to accept the answer, the comment of ctrl-alt-delor on the glob should be added.
      – pkaramol
      May 30 at 9:44










    • +1 for the LC_COLLATE observation.
      – Rui F Ribeiro
      May 30 at 9:47












    • 2




      for does not sort ever. It is the glob (*.sql) that is doing the sorting.
      – ctrl-alt-delor
      May 30 at 9:42











    • Sure, I'm just explaining how the sorting that happens in the for loop works.
      – l0b0
      May 30 at 9:43






    • 1




      For me to accept the answer, the comment of ctrl-alt-delor on the glob should be added.
      – pkaramol
      May 30 at 9:44










    • +1 for the LC_COLLATE observation.
      – Rui F Ribeiro
      May 30 at 9:47







    2




    2




    for does not sort ever. It is the glob (*.sql) that is doing the sorting.
    – ctrl-alt-delor
    May 30 at 9:42





    for does not sort ever. It is the glob (*.sql) that is doing the sorting.
    – ctrl-alt-delor
    May 30 at 9:42













    Sure, I'm just explaining how the sorting that happens in the for loop works.
    – l0b0
    May 30 at 9:43




    Sure, I'm just explaining how the sorting that happens in the for loop works.
    – l0b0
    May 30 at 9:43




    1




    1




    For me to accept the answer, the comment of ctrl-alt-delor on the glob should be added.
    – pkaramol
    May 30 at 9:44




    For me to accept the answer, the comment of ctrl-alt-delor on the glob should be added.
    – pkaramol
    May 30 at 9:44












    +1 for the LC_COLLATE observation.
    – Rui F Ribeiro
    May 30 at 9:47




    +1 for the LC_COLLATE observation.
    – Rui F Ribeiro
    May 30 at 9:47












    up vote
    2
    down vote













    for loops don't sort, they give you the values in exactly the order they were presented:



    $ for x in b a c ; do echo $x ; done
    b
    a
    c


    Globs, on the other hand, do sort alphabetically, regardless of if you use them in a for or anywhere else:



    $ touch b a c
    $ echo *
    a b c





    share|improve this answer

























      up vote
      2
      down vote













      for loops don't sort, they give you the values in exactly the order they were presented:



      $ for x in b a c ; do echo $x ; done
      b
      a
      c


      Globs, on the other hand, do sort alphabetically, regardless of if you use them in a for or anywhere else:



      $ touch b a c
      $ echo *
      a b c





      share|improve this answer























        up vote
        2
        down vote










        up vote
        2
        down vote









        for loops don't sort, they give you the values in exactly the order they were presented:



        $ for x in b a c ; do echo $x ; done
        b
        a
        c


        Globs, on the other hand, do sort alphabetically, regardless of if you use them in a for or anywhere else:



        $ touch b a c
        $ echo *
        a b c





        share|improve this answer













        for loops don't sort, they give you the values in exactly the order they were presented:



        $ for x in b a c ; do echo $x ; done
        b
        a
        c


        Globs, on the other hand, do sort alphabetically, regardless of if you use them in a for or anywhere else:



        $ touch b a c
        $ echo *
        a b c






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered May 30 at 9:45









        ilkkachu

        47.8k668131




        47.8k668131












            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)