3

I'm trying to sort a list strings case-sensitively, but the list is coming out in case-insensitive order. The man or --help info for sort gives me a -f or --ignore-case option, but doesn't list an option for sorting insensitively to override what seems to be the default order. I checked to make sure there is no alias defined for sort.

Why is sort defaulting operation to the thing that an option lets me select, and why is there no option documented to override that default?

Further checking makes it look to be doing a dictionary sort because it seems to be ignoring punctuation. In any case I want it to behave like it's supposed to default to, or at least give me CLI overrides to make it behave like it's supposed to.

sort --version sort (GNU coreutils) 8.4

Centos 6 I think.

6
  • Could you provide your sort version (and provenance -- what OS/distribution) and some sample data for us to test against?
    – Jeff Schaller
    Commented Nov 20, 2019 at 14:15
  • sort's man page(s) refer to a LC_CTYPE variable, so the contents of that might be useful as well.
    – Jeff Schaller
    Commented Nov 20, 2019 at 14:16
  • @JeffSchaller how do I find out the LC_CTYPE? I've never been able to figure that out in Linux. Can I get it from env or printenv or echo it or what? Nothing I do to look for those things ever seems to work like it did when I started in Unix 40 years ago. And if the problem is a system variable setting, why aren't there CLI options to override those settings on the fly?
    – Sinc
    Commented Nov 20, 2019 at 14:26
  • echo "$LC_CTYPE"
    – Jeff Schaller
    Commented Nov 20, 2019 at 14:36
  • 2
    locale
    – ilkkachu
    Commented Nov 20, 2019 at 14:38

1 Answer 1

8

It depends on your locale (this on a Debian/GNU system):

$ printf '%s\n' B A b a | LC_COLLATE=en_US.UTF-8 sort 
a
A
b
B
$ printf '%s\n' B A b a | LC_COLLATE=C sort 
A
B
a
b

The man page for GNU sort mentions this:

* WARNING * The locale specified by the environment affects sort order. Set LC_ALL=C to get the traditional sort order that uses native byte values.

LC_COLLATE is the option that affects sorting, whileLC_ALL is the overkill switch, it overrides all the other LC_* options.

Note that at least in the en_US.UTF-8 that's not a case-insensitive sort, since it sorts a before A regardless of the original order.

The locale command should show the effective locales.

4
  • Thanks, that worked. The question remains though: "Why aren't there CLI options to the sort command itself to override the LC options on the fly?" If my system has locale settings that affect sort why doesn't sort have built in options like -nd (non-dictionary) or -uf/--case (for unfold or case-sensitive)?
    – Sinc
    Commented Nov 20, 2019 at 14:43
  • 1
    Isn't LC_COLLATE=C sort ... a CLI solution?
    – Jeff Schaller
    Commented Nov 20, 2019 at 14:50
  • Yes, but I expect an option of sort, preferably one that is documented in the help/man page. The only thing hinting at the locale effects is the warning that ilkkachu quoted, which doesn't tell you anything about how to counteract it.
    – Sinc
    Commented Nov 20, 2019 at 17:41
  • @Sinc, well it does tell you to 'set LC_ALL=C'. As for why there's no switch for doing a bytewise sort, or to sort in the C locale, I don't know. Possibly because they think that the possibility of setting the locale environment vars is enough.
    – ilkkachu
    Commented Nov 20, 2019 at 19:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .