7

I would like an alias that gives a directory listing in dictionary order with "." files first. It seems that one few ways to accomplish this devoid of writing my own script (which isn't a real problem) is to set the locale with LC_ALL="C". (This is per the sorting man page, and multiple other sites).

However setting the locale to "C" can (and does) cause some software installations to break. So my short term solution is akin to this (This is without the additional ls options, for brevity):

alias ls='(LC_ALL="C"; /bin/ls)'

But this does not allow arguments to be "passed" into the subshell.

This of course does not work:

alias ls='(LC_ALL="C"; /bin/ls $*)'

Is there a way to handle this with a simple alias? (As opposed to writing a shell script/function, which I can do).

10

You don't need a subshell. You can change the environment just for the following command by prepending the command with the variable assignment without semicolon. Compare for example the output of

LC_ALL=C env | grep LC_ALL

with the output of

env | grep LC_ALL

So, to cut a long story short, the following alias should work for you:

alias ls='LC_ALL=C /bin/ls'

I used /bin/ls as in your example, but as pointed out by Kamil Maciorowski in a comment to another answer, your typed in ls command might be an alias, too, so maybe you should also consider that alternative:

alias ls='LC_ALL=C ls'
  • Holy Cripes! That's what I get for spending so much time in csh/tcsh. I must have read through the bash and zsh documentation a dozen times looking for that and not found it. Incredible. Thanks! – tgm1024 Jun 3 '18 at 23:17
  • As a quick aside to the using ls instead of fully qualified /bin/ls direct executable reference. This is something I usually suggest folks do anyway. In most cases, I simply don't want someone else's rendition of what a utility is supposed to do. It presents an open-ended question of what might go wrong in the future. The reference to /bin/ls is intended and what I suggest to others. – tgm1024 Jun 6 '18 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.