3

When I'm using a POSIX compliant shell (es: dash, bash, zsh, ...) can I be sure that * will always expand in alphabetical order (dictated by LC_COLLATE)?

example:

$ echo 1 > file_a
$ echo 2 > file_b
$ echo 3 > file_c
$ cat *
1
2
3
S1cK94
  • 35
  • 5

2 Answers2

2

That behavior is required by POSIX, and you're safe to rely on it.

Another note that you want to set your locale to C to get consistent behavior. In locale with collation elements have the same sorting order, you will have strange result.

On GNU system with UTF-8 locale:

$ printf '%b\n' '\U2461' '\U2460' | sort
②
①

or:

$ printf '%s\n' A B a b | sort
a
A
b
B

Setting to C locale:

$ printf '%b\n' '\U2461' '\U2460' | LC_ALL=C sort
①
②

$ printf '%s\n' A B a b | LC_ALL=C sort
A
B
a
b

Some shells even do not support multibyte characters like dash, mksh or support but will choke on invalid sequences of bytes like yash.

cuonglm
  • 153,898
1

Yes. The normative answer can be found here:

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.

user3188445
  • 5,257