0

What does dircolors affect. Is it for the ls command only ? Should dircolors be called before defining alias ls='ls --color'?

3
  • From man 1 dircolors: dircolors - color setup for ls May 16 at 22:16
  • There's a good explanation here May 16 at 22:27
  • I am still unsure about what the order of the calls should be. Have also defined LS_COLORS but without exporting the variable, before evaluating the dircolours file.
    – Vera
    May 16 at 22:33

1 Answer 1

0

dircolors outputs code meant to be interpreted by a shell. Without option or with the -b option, that's meant for Bourne-like shells such as ksh, zsh or bash and all modern sh, with -c, that's for csh or tcsh, it doesn't support any other shell².

The output code instructs the shell to set the $LS_COLORS environment variable, that variable is queried by the GNU implementation of ls to customize its colouring output when invoked with the --color option.

In Bourne²-like shells, you'd do:

eval "$(dircolors -b)"

To evaluate the output of dircolors

In (t)csh:

eval "`dircolors -c`"

The $LS_COLORS variable must be set before ls --color is invoked but has no incidence on the definition of the aliases or functions that use ls. So the:

alias ls 'ls --color' # (t)csh
alias ls='ls --color' # Korn-like shells
ls() command ls --color "$@" # most Bourne-like shells
ls() { command ls --color "$@"; } # all Bourne-like shells

May be put before or after that call to eval, it makes no difference.

Since it updates the environment, it's enough to put the eval line in your ~/.profile / ~/.login (or equivalent login session customisation file for your login shell) so it's set only once per login session, while the aliases / functions should be set via the shell customisation file (~/.zshrc, ~/.cshrc, ~/.bashrc...).

In any case, if you don't use dircolors, you'll still get colours, just the default ones. When using dircolors without a customization file, you'll get different defaults.

You can edit those defaults by doing:

dircolors -p > ~/.config/dircolors

Edit that file and change the line in your shell startup file with:

eval "$(dircolors ~/.config/dircolors)"

¹ There are ways around it for other shells, but you're on your own then. For instance, with recent fish versions, you could do set -x LS_COLORS "$(sh -c "$(dircolors -b)"'; printf %s "$LS_COLORS"')"

² except in the Bourne-shell itself that didn't have $(...) but the Bourne shell is not in use any longer and its `...` equivalent is deprecated.

2
  • Yes, I am using the -b option. Is it contradictory to set LS_COLORS and then executing dircolors with a customization file ?
    – Vera
    May 17 at 7:57
  • @Hovlar, eval "$(dircolors -b)" sets $LS_COLORS. dircolors outputs LS_COLORS='...'; export LS_COLORS, so if you also have a LS_COLORS=... that will indeed conflict (override or be overridden depending on whether it's after or before) with that. May 17 at 8:10

You must log in to answer this question.

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