GNU sort by case-sensitive

Override the collation order.

echo -e "c\nb\nB\na" | LC_COLLATE=C sort

Interestingly, yet another sort order is available like this:

echo -e "c\nb\nB\na" | LC_COLLATE=C sort --ignore-case

which puts the uppercase letter before its corresponding lowercase letter.

Here is a comparison of their outputs (I added "d" and "D") in the en_US.UTF-8 locale (except where overridden):

  1. echo -e "d\nD\nc\nb\nB\na" | sort
  2. echo -e "d\nD\nc\nb\nB\na" | sort --ignore-case
  3. echo -e "d\nD\nc\nb\nB\na" | LC_COLLATE=C sort
  4. echo -e "d\nD\nc\nb\nB\na" | LC_COLLATE=C sort --ignore-case

Output:

1   2   3   4
-   -   -   -
a   a   B   a
b   b   D   B
B   B   a   b
c   c   b   c
d   d   c   D
D   D   d   d

Tags:

Linux