8

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).

0

1 Answer 1

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'
2
  • Is it possible to make it work with a subshell though? Reached this question while searching for that use case. I have a case where I use a subshell in my alias to change directory before calling the program
    – Wadih M.
    Feb 3, 2022 at 22:08
  • @WadihM. You can use a function instead of an alias, if you need to handle the arguments.
    – mpy
    Feb 4, 2022 at 18:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .