Say I have a below for in loop:

for i in /apps/textfiles/*.txt
do something

Now say I have 50 files inside /apps/textfiles/

In what order will the files be picked?

1 Answer 1


Filename expansion in Bash is sorting alphabetically.

From the Bash Manual:

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

It doesn't make a difference here that your globbing context is part of the for loop.

Note that alphabetical sorting still obeys the collation order defined by the LC_COLLATE variable:


This variable determines the collation order used when sorting the results of filename expansion, and determines the behavior of range expressions, equivalence classes, and collating sequences within filename expansion and pattern matching (see Filename Expansion).

  • And by alphabetically, they mean according to the order defined by your locale LC_COLLATE. Commented Apr 13, 2017 at 15:42
  • 2
    LC_COLLATE=C is ASCIIbetical, it's instructive to experiment with something like this seq 32 127|awk '{printf("%c\n",$1)}' | LC_COLLATE=en_US sort Commented Apr 13, 2017 at 16:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .