Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
706 views
in Technique[技术] by (71.8m points)

bash - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...