0

Observe the following example commands in bash:

jason@MacBook:~ mkdir temp
jason@MacBook:~ cd temp
jason@MacBook:~/temp touch file.txt
jason@MacBook:~/temp touch file1.txt
jason@MacBook:~/temp touch file2.txt
jason@MacBook:~/temp ls file*
file.txt  file1.txt file2.txt
jason@MacBook:~/temp touch file0.txt
jason@MacBook:~/temp ls file*
file.txt  file0.txt file1.txt file2.txt
jason@MacBook:~/temp touch fil.txt
jason@MacBook:~/temp ls fil*
fil.txt   file.txt  file0.txt file1.txt file2.txt
jason@MacBook:~/temp 

As you can see from the output, all the file names are sorted in ascending lexicographic order.

I was wondering if this is a requirement by bash or whether it's implementation-specific, and it just so happened to work this way in my Mac's bash shell. The reason for which I'm asking is because if the file names are stored in a trie and it turns out that bash does indeed require that strings are printed in lexicographic order, a tiny amount of tweaking would be required in the code to traverse the trie correctly to print out the file names in this ascending order.

Jason
  • 2,495
  • 4
  • 26
  • 37
  • Collation order -- specified by `LC_COLLATE` -- is followed. – Charles Duffy Jun 21 '17 at 00:28
  • By the way, the word for this is a "glob expression" (to allow more precise tagging/searching/title). – Charles Duffy Jun 21 '17 at 00:28
  • BTW, quoting from [the relevant specification](http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_13_03): "If the pattern matches any existing filenames or pathnames, the pattern shall be replaced with those filenames and pathnames, sorted according to the collating sequence in effect in the current locale. If the pattern contains an invalid bracket expression or does not match any existing filenames or pathnames, the pattern string shall be left unchanged." As this is POSIX-specified behavior, it's guaranteed across other compliant shells (dash, ksh) as well. – Charles Duffy Jun 21 '17 at 00:35
  • See also http://wiki.bash-hackers.org/syntax/expansion/globs – Charles Duffy Jun 21 '17 at 00:37
  • Thanks! For people that might be interested, this SO thread has some info on LC_COLLATE: https://unix.stackexchange.com/questions/15980/does-should-lc-collate-affect-character-ranges – Jason Jun 21 '17 at 00:46

0 Answers0