Yes, it is, you can verify 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?
$ touch b c 4 ab k_l
$ for f in *; do echo ${f}; done
4
ab
b
c
k_l
Is is implemented in FreeBSD sh(1), here some source code references:It seems that POSIX demands it, but it is always implemented? In FreeBSD and elsewhere?
Of course sorting is implemented, but the question was if it is really used in sh?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've never seen any shell where this is not the case, so for all intents and purposes you can rely on it.
The current version of sh is close to the IEEE Std 1003.1 ("POSIX.1") specification for the shell.
bash, ksh, busybox sh, zsh, mksh all adhere to POSIX for collating glob expansions.It was also a question about portability. I do not know what Linux distros, Mac, Cygwin do.
do not do this! ls is not meant to be parsed, and this will cause issues with funny characters in file names. see https://mywiki.wooledge.org/ParsingLs and https://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29 for further details`ls -1 pattern | sort | tr \n ' '`