Paolo
Paolo

Reputation: 2249

What sort order does Linux use?

My Bash script is running things in a folder, in alphanumerical order. But it turns out it's not the same order as the one I have in my Mac OS folder. So now I'm wondering what sorting order Linux uses, and can it be changed? Can I change it for my Bash script only? Say I want to run a video player in a Bash script, running all videos in a folder in alphanumerical order, can I specify in the script what alphanumerical order it should be?

Upvotes: 5

Views: 833

Answers (1)

Sylvain Leroux
Sylvain Leroux

Reputation: 52030

The sort order for many commands (incl. bash glob, ls, sort) is based on your current locale settings.

You can force the collation by setting the LC_COLLATE environment variable. Setting it to C will perform a comparison on byte values.

On my system (en_US.utf8):

sh$ touch eleve
sh$ touch élève
sh$ touch Eleve 
sh$ touch Élève
sh$ touch äkta
sh$ touch österreich

sh$ ls
äkta  eleve  Eleve  élève  Élève  österreich  pommes

sh$ LC_COLLATE=fr_FR.utf8 ls
äkta  eleve  Eleve  élève  Élève  österreich  pommes

sh$ LC_COLLATE=sv_SE.utf8 ls
eleve  Eleve  élève  Élève  pommes  äkta  österreich

sh$ LC_COLLATE=C ls
Eleve  eleve  pommes  Élève  äkta  élève  österreich

Upvotes: 6

Related Questions