POSIX “sort -n” alpha characters between zero and 1?

I'm having trouble getting POSIX "sort" to behave the way I want it to. When sorting numeric file names, those that start with letters show up between 0 and 1.

Why does this happen?

Is there a different sort option I can use to achieve the desired result?

$touch 0 1 a
$ls | sort -n

Desired Output: 0 1 a

Actual Output: 0 a 1

In the gnu implementation, "-g" does roughly what I want, but that's not POSIX. Which unfortunately, means I can't use it.


If you want explicitly sort output as

  • first numerically sorted files so 0, 1, 2, 11, 222, and after
  • all other files sorted

you can do it for example with

cat <(ls -1 | grep '^[0-9]' |sort -n) <(ls -1 | grep -v '^[0-9]' | sort)

so catenate output of two commands

  • sort numerically files what begins with numbers, and
  • sort other files (what aren't begins with numbers)

but, this is works with bash only and don't know how much bash is POSIX, if bash is not ok you can use temporary files...

Its because you have the -n. That means numeric so a is being treated as 0. Get rid of the -n and you should be right (at the cost of 11 now coming before 2)

Need Your Help

PHP forcing browser to download files (two ways)

php file download

I want a php script to download files of any type without opening it. I have found the following functions, which seem to me a bit different, but I don`t know which is better. Please let me know wh...

How do I submit another pull request once the first one was merged?

git github pull-request

I forked a public Github repository, cloned the fork to a local repo, made some changes, pushed them to the master branch of my fork. Then I opened a pull request and it was merged into the original