bash 为什么在每台机器上**排序** 排序不一样?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why doesn't **sort** sort the same on every machine?
提问by Jon Ericson
Using the same sortcommand with the same input produces different results on different machines. How do I fix that?
对相同的输入使用相同的排序命令在不同的机器上会产生不同的结果。我该如何解决?
回答by Henrik Gustafsson
The man-pageon OS X says:
OS X 上的手册页说:
******* 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_ALL=C 以获取使用本机字节值的传统排序顺序。
which might explain things.
这可能会解释一些事情。
If some of your systems have no locale support, they would default to that locale (C), so you wouldn't have to set it on those. If you have some that supports locales and want the same behavior, set LC_ALL=Con those systems. That would be the way to have as many systems as I know do it the same way.
如果您的某些系统不支持区域设置,它们将默认为该区域设置 (C),因此您不必在这些系统上进行设置。如果您有一些支持语言环境并想要相同的行为,请LC_ALL=C在这些系统上进行设置。这将是让我所知道的尽可能多的系统以同样的方式来做的方式。
If you don't have any locale-less systems, just making sure they share locale would probably be enough.
如果您没有任何无语言环境的系统,只需确保它们共享语言环境就足够了。
For more canonical information, see the The Single UNIX ? Specification, Version 2 description of locale, environment variables, setlocale()and the description of the sort(1)utility.
有关更多规范信息,请参阅 The Single UNIX ? 规范,第 2 版语言环境描述、环境变量、setlocale()和sort(1)实用程序的描述。
回答by Jon Ericson
This can be the result of locale differences:
这可能是区域设置差异的结果:
$ echo 'CO2_
CO_' | env LC_ALL=C sort
CO2_
CO_
$ echo 'CO2_
CO_' | env LC_ALL=en_US sort
CO_
CO2_
Setting the LC_ALL environment variable to the same value should correct the problem.
将 LC_ALL 环境变量设置为相同的值应该可以解决问题。
回答by Greg Hewgill
This is probably due to different settings of the locale environment variables. sortwill use these settings to determine how to compare strings. By setting these environment variables the way you want before calling sort, you should be able to force it to behave in one specific way.
这可能是由于locale 环境变量的不同设置。sort将使用这些设置来确定如何比较字符串。通过在调用之前按照您想要的方式设置这些环境变量sort,您应该能够强制它以一种特定的方式运行。
回答by J?rg W Mittag
For more than you ever wanted to know about sort, read the specification of sortin the Single Unix Specification v3. It states
欲了解更多比你想要知道的sort,阅读的规格sort在单一Unix规格V3。它指出
Comparisons [...] shall be performed using the collating sequence of the current locale.
比较 [...] 应使用当前语言环境的整理顺序进行。
IOW, how sortsorts is dependent on the locale (language) settings of the environment that the script is running under.
IOW,如何sort排序取决于脚本运行环境的区域设置(语言)设置。

