shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

New rule: suggest setting `LC_ALL` when calling `sort`

Open raphael-proust opened this issue 1 year ago • 2 comments

For bugs

  • Rule Id (if any, e.g. SC1000): None (yet?)
  • My shellcheck version (shellcheck --version or 'online'): online
  • [x] I tried on shellcheck.net and verified that this is still a problem on the latest commit
  • [ ] It's not reproducible on shellcheck.net, but I think that's because it's an OS, configuration or encoding issue

For new checks and feature suggestions

  • [x] shellcheck.net (i.e. the latest commit) currently gives no useful warnings about this
  • [x] I searched through https://github.com/koalaman/shellcheck/issues and didn't find anything related

Here's a snippet or screenshot that shows the problem:

#!/bin/sh
cat /tmp/some_file | sort

Here's what shellcheck currently says:

No issues detected!

Here's what I wanted or expected to see:

It should suggest to set LC_ALL=C locally in the script:

Did you mean cat /tmp/some_file | LC_ALL=C sort

Setting LC_ALL ensures it's all sorted the same on all machines.

raphael-proust avatar Dec 12 '24 15:12 raphael-proust

I agree with this suggestion for a rule. I think the new rule should trigger if LC_ALL is not explicitly set, but it should allow setting LC_ALL to any value, not necessarily C. There are cases where locale-dependent sorting is wanted. It should even allow an empty value. But it should warn when it seems that the author of the script is unaware that LC_ALL can affect the behaviour of sort.

The manpage of sort includes this warning:

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

This is a good indicator that this is a common gotcha that shellcheck should check for.

Flimm avatar Apr 12 '25 10:04 Flimm

I agree that the rule shouldn't require the value C. I think it's a good default suggestion in the documentation page of the warning. But yes, other sortings might be preferred.

raphael-proust avatar Apr 24 '25 14:04 raphael-proust