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 
2

2 Answers 2

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.

2
  • Another reason to stick to LC_COLLATE=C is that some shells don't support multibyte characters in pattern matching (e.g. dash, mksh). Commented Nov 18, 2015 at 22:26
  • @Gilles: Ah, right, it's fair enough. I added it to my answer, thanks. Commented Nov 19, 2015 at 1:34
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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.