I was expecting @'s to be sorted together, but they interleaved with the non-@ versions. Does sort have an option for ASCII sorting? I didn't see something obvious in the man page.

$ echo '@x
> @y
> @z
> x
> y
> z' | sort

This is locale-dependent. man sort includes:

*** 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.

You can set that just for the one command, like:

... | LC_ALL=C sort ...
  • 2
    Or better use just LC_COLLATE=C which would work just as well. No need to throw the baby out with the bathwater -- an UTF-8 LC_CTYPE is perfectly fine, it's LC_COLLATE which is unasked for trouble. – user414777 Jan 29 at 23:14
  • @user414777 True here. I tend to globally export LC_ALL at the top of my scripts, because I rely on awk, sort, uniq, ls, tr etc to play nicely together. – Paul_Pedant Jan 30 at 10:37

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.