Shell Is result of globbing in sh sorted?

POSIX says in https://pubs.opengroup.org/onlinepubs/9799919799/index.html

2.14.3 Patterns Used for Filename Expansion​

[...]
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 this collating sequence does not have a total ordering of all characters (see XBD 7.3.2 LC_COLLATE), any filenames or pathnames that collate equally shall be further compared byte-by-byte using the collating sequence for the POSIX locale.

I've never seen any shell where this is not the case, so for all intents and purposes you can rely on it.
 
Are the $f in an expression like

for f in file[0-9][0-9].c; do echo "doing something with $f"; done

taken in lexicographic order?
Yes, it is, you can verify it:
Code:
$ touch b c 4 ab k_l
$ for f in *; do echo ${f}; done
4
ab
b
c
k_l

It seems that POSIX demands it, but it is always implemented? In FreeBSD and elsewhere?
Is is implemented in FreeBSD sh(1), here some source code references:
- 1. qsort(3) in expandmeta().
- 2. strcoll(3), which is used in the abovementioned function. This part is mentioned in schweikh's answer.

I don't know about other shell implementations.
 
Is is implemented in FreeBSD sh(1), here some source code references:
- 1. qsort(3) in expandmeta().
- 2. strcoll(3), which is used in the abovementioned function. This part is mentioned in schweikh's answer.
Of course sorting is implemented, but the question was if it is really used in sh?
Otherwise I could use something like:

`ls -1 pattern | sort | tr \n ' '`

I've never seen any shell where this is not the case, so for all intents and purposes you can rely on it.

I also do not remember seeing unsorted output. But you know, the few systems call themselves POSIX compliant. In the man page of sh we read

The current version of sh is close to the IEEE Std 1003.1 ("POSIX.1") specification for the shell.

It was also a question about portability. I do not know what Linux distros, Mac, Cygwin do.
 
It was also a question about portability. I do not know what Linux distros, Mac, Cygwin do.
bash, ksh, busybox sh, zsh, mksh all adhere to POSIX for collating glob expansions.
The one thing I was ever surprised about was a Linux system that would do "lexicographical" sorting in ls(1) by default, so A then a, then B then b, etc, instead of "ASCII" sorting, where uppercase sorts first, then lowercase. This could be corrected by setting LC_ALL or LC_COLLATE to any of the "POSIX" or "C" locales. For consistent and sane behavior, all your startup and other scripts should always set the desired locale.
 
Back
Top