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
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
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
.
LC_COLLATE=C
is that some shells don't support multibyte characters in pattern matching (e.g. dash, mksh).
– Gilles 'SO- stop being evil'
Nov 18 '15 at 22:26
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.