[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: The ls command



On Thu, Oct 30, 2025 at 21:51:16 -0700, Michael Paoli wrote:
> $ ls ... | sort
> may give results different than, e.g. ls -1 or what one might
> otherwise be expecting/anticipating,

You'd have to work pretty hard for that to be true.  Can you come up
with an actual example?  Remember, POSIX says

    If more than one operand is specified, ls shall write non-directory
    operands first; it shall  sort  directory  and non-directory operands
    separately according to the collating sequence in the current locale.

The only way I can contrive to make such an example would be to create
an alias or function for "sort", where the LC_COLLATE variable (or LC_ALL
or LANG) is overridden.

hobbit:~$ mkdir /tmp/x && cd "$_"
hobbit:/tmp/x$ touch readme README2
hobbit:/tmp/x$ ls
readme  README2
hobbit:/tmp/x$ ls | sort
readme
README2
hobbit:/tmp/x$ ls | LC_COLLATE=C sort
README2
readme
hobbit:/tmp/x$ sort() { LC_COLLATE=C command sort "$@"; }
hobbit:/tmp/x$ ls | sort
README2
readme

I suppose it could also be done by creating a function or alias that
wraps "ls" in such a way that it sorts by something other than names
(e.g. alias ls='ls -t').  This would be a bad idea.


Reply to: