[英]Unusual behaviour of linux's sort command
On Linux shell the result of echo -e "arrays2 2\narrays 2\narrays3 2" | sort
在 Linux shell 上
echo -e "arrays2 2\narrays 2\narrays3 2" | sort
echo -e "arrays2 2\narrays 2\narrays3 2" | sort
is echo -e "arrays2 2\narrays 2\narrays3 2" | sort
是
arrays 2
arrays2 2
arrays3 2
and the result of echo -e "arrays2 28\narrays 28\narrays3 28" | sort
以及
echo -e "arrays2 28\narrays 28\narrays3 28" | sort
的结果echo -e "arrays2 28\narrays 28\narrays3 28" | sort
is echo -e "arrays2 28\narrays 28\narrays3 28" | sort
是
arrays2 28
arrays 28
arrays3 28
Why in the second case the string arrays2 28
appears on first line.为什么在第二种情况下,字符串
arrays2 28
出现在第一行。 Is this a bug, or I miss something?这是一个错误,还是我错过了什么?
I tried this on RHEL4 and Ubuntu 11.04.我在 RHEL4 和 Ubuntu 11.04 上试过这个。
Thanks.谢谢。
The behaviour is locale-dependent:该行为取决于语言环境:
echo -e "arrays2 28\narrays 28\narrays3 28" | LANG=C sort
prints印刷
arrays 28 arrays2 28 arrays3 28
While尽管
echo -e "arrays2 28\narrays 28\narrays3 28" | LANG=de_DE.UTF-8 sort
prints印刷
arrays2 28 arrays 28 arrays3 28
(Note that the locale must be installed for this to have this effect, if the locale doesn't exist, the behaviour will be the same as with LANG=C
). (请注意,必须安装语言环境才能产生此效果,如果语言环境不存在,则行为将与
LANG=C
相同)。
If you change the locale from en_US.utf8 to the old default, it works the way you expect:如果您将语言环境从 en_US.utf8 更改为旧的默认值,它将按照您的预期工作:
echo -e "aaa\nfoo\narrays2 28\narrays 28\narrays3 28" | LC_ALL=C sort -
aaa
arrays 28
arrays2 28
arrays3 28
foo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.