3

When I have the files a.txt, b.txt and c.txt is it guaranteed that

cat *.txt > all_files.txt

or

cat ?.txt > all_files.txt

will combine the files in alphabetical order?

(In all my tests, the alphabetical order was preserved, but I'm not sure because for example with ls the order is undefined and need not be alphabetic - but it often is, because the files have often been written to the directory alphabetically)

Vitalii Elenhaupt
  • 7,146
  • 3
  • 27
  • 43
Roland Seuhs
  • 1,878
  • 4
  • 27
  • 51
  • 5
    More or less dupe from serverfault: http://serverfault.com/q/122737/240915 (the short answer is "yes") – Tom Fenech May 26 '15 at 14:26
  • I don't think it can be a duplicate if it's on another stack-exchange site. When I posed the question I did not see any duplicates in the list (because it only shows entries from stack-overlow of course), and it is clearly on-topic for programming. – Roland Seuhs May 26 '15 at 14:29
  • 1
    Don't take it the wrong way - there's just an answer to your question there :) – Tom Fenech May 26 '15 at 14:31
  • 5
    "The pathnames are in sort order as defined by the current setting of the LC_COLLATE category, see the XBD specification, LC_COLLATE". http://pubs.opengroup.org/onlinepubs/007908775/xsh/glob.html – Vitalii Elenhaupt May 26 '15 at 14:32
  • Shell expansion is alphabetical; `ls` can sort chronologically or alphabetically, up or down, depending on options. – mpez0 May 26 '15 at 14:35

1 Answers1

5

No, it depends on the locale. The order is dictated by the collation sequence in the locale, which can be changed using the LC_COLLATE or LC_ALL environment variables. Note that bash behaves differently in this respect to some other shells (e.g. Korn shell).

If you have a locale setting of C or POSIX then it will be in character set order. Otherwise you will probably only notice a difference with mixed case letters, e.g. the sequence for en_ locales is aAbBcC ... xXyYzZ. For example see http://collation-charts.org/fc6/fc6.en_GB.iso885915.html.

Available locales may be listed using locale -a.

Edit: another variable LANG is available, but it is generally not used much nowadays. According to the Single UNIX Specification it is used: in the absence of the LC_ALL and other LC_* ... environment variables.

cdarke
  • 42,728
  • 8
  • 80
  • 84