3

I have a OSX machine where sort runs GNU sort from coreutils 8.26 (installed from Homebrew), and a Linux machine where sort runs GNU sort from coreutils 8.25.

On the Mac:

mac$ echo -e "{1\n2" | sort
2
{1

While on Linux:

linux$ echo -e "{1\n2" | sort
{1
2

I'm aware that sort depends on the locale. I ran locale on the Linux machine, prepended each line of output with export and ran the resulting lines on the OSX machine before running (in the same terminal) the sort command again, which gave the same output as before.

I noticed, however, that running locale on the Mac doesn't show all of the lines which appear on Linux, and I'm not sure if this is related.

The locale on Linux:

linux$ locale
LANG=en_CA.UTF-8
LANGUAGE=en_CA:en
LC_CTYPE="en_CA.UTF-8"
LC_NUMERIC="en_CA.UTF-8"
LC_TIME="en_CA.UTF-8"
LC_COLLATE="en_CA.UTF-8"
LC_MONETARY="en_CA.UTF-8"
LC_MESSAGES="en_CA.UTF-8"
LC_PAPER="en_CA.UTF-8"
LC_NAME="en_CA.UTF-8"
LC_ADDRESS="en_CA.UTF-8"
LC_TELEPHONE="en_CA.UTF-8"
LC_MEASUREMENT="en_CA.UTF-8"
LC_IDENTIFICATION="en_CA.UTF-8"
LC_ALL=en_CA.UTF-8

And locale on OSX:

mac$ locale
LANG="en_CA.UTF-8"
LC_COLLATE="en_CA.UTF-8"
LC_CTYPE="en_CA.UTF-8"
LC_MESSAGES="en_CA.UTF-8"
LC_MONETARY="en_CA.UTF-8"
LC_NUMERIC="en_CA.UTF-8"
LC_TIME="en_CA.UTF-8"
LC_ALL="en_CA.UTF-8"

I've found that if I set LC_ALL=C on both machines, they both sort 2 before {1. But if I set LC_ALL=en_CA.UTF-8 on both machines I have the differing output as above. Same if I set LC_ALL=en_CA.utf8 on both machines. (locale -a lists en_CA.utf8 on the Linux machine but en_CA.UTF-8 on the OSX machine.)

Any idea what is going on here?

  • 2
    Try using only locales that actually exist. Otherwise it defaults to C, which you probably don't want – Fox May 3 '17 at 0:05
  • @Fox, I don't know what you're telling me. I said that locale -a lists the Canadian English UTF-8 locale differently on each machine, and that I'd tried both variants on both machines, and that this made no difference. I only have the Linux machine at my fingertips right now, and have tried the sort operation with every locale installed (a bunch of English variants plus C and POSIX) and {1 always sorts before 2 here. – tremby May 4 '17 at 1:19
  • It's not entirely clear from your Q which combinations you've chosen. Having OS X as en_CA.UTF-8 and Linux as en_CA.utf8 should be consistent, unless the collation rules are specified differently for some reason – Fox May 4 '17 at 1:22
  • I take back what I said at the end of my last comment; my loop was wrong. In fact on Linux it sorts {1 before 2 in all locales except the C variants and POSIX, which sort it the other way around. Further, it doesn't make a difference whether I use .utf8 or .UTF-8, and when running locale-gen the .UTF-8 version is displayed, even though locale -a shows the .utf8 version. This suggests they'll work the same way anyway. I'll test the Mac side of things again tomorrow. Thanks. – tremby May 4 '17 at 1:30
  • 2
    This answer suggests, unfortunately without visible references, that the locales in BSD (and thus macOS) are "somewhat broken compared to those in Linux" – Fox May 6 '17 at 0:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.