How to use linux bash "sort" command in a portable script?

1

I am writing a bash script and I want to make it portable, to have the same results in the other computers.

From this question I learned the system settings, like language settings and possibly other variables, can affect the results of exactly the same sort command on exactly the same input file and arguments.

So my question is how to use sort and to ensure the expected result in the other systems? Which system variables should I consider? Does putting LC_ALL=C b solve all the problem?

Ali

Posted 2014-10-22T21:47:06.447

Reputation: 391

i had a similar problem, maybe this could help you. http://stackoverflow.com/questions/10486736/how-do-i-sort-these-values-using-bash-script-in-linux

– maths – 2014-10-22T23:14:44.583

1Read the sort man page for each intended system. For GNU systems (i.e. Linux), more detail can be found with info sort, especially see the footnote. – glenn jackman – 2014-10-22T23:33:25.683

Answers

1

Yes, LC_ALL=C will solve your portability problems (it will behave on all systems the same)

Note however, that does not necessarily mean that it will do what you want. "C" locale will sort on byte value. Which is fine if all your data is pure ASCII. However, if your data may contain some other character sets (eg. other languages in ISO-8859-* series, KOI-R, or heavens forbid multibyte charsets like UTF-16, UTF-8 etc.), it will break horribly.

But it would still be portable, as it would break horribly in the same way on all platforms.

Matija Nalis

Posted 2014-10-22T21:47:06.447

Reputation: 2 107