1

I was checking .bashrc to set colors for ls comand and found this

export SHELL='/bin/bash'
 export LS_OPTIONS='--color=auto'
 eval "`dircolors`"
 alias ls='ls $LS_OPTIONS'

Will there be any problem if i use dircolors instead of using it with eval? What's the difference?

3

Neither eval dircolors nor dircolors will work.

What you need is:

eval "$(dircolors)"

(or the ancient form eval "`dircolors`")

That is, you need to evaluate the output of dircolors. dircolors outputs code to be evaluated by the shell like:

LS_COLORS='...'
export LS_COLORS

That's the code you want to evaluate. eval dircolors is just like dircolors, so it will just run dircolors with its output not redirected, so that shell code above will just end up being displayed and not evaluated by any shell.

Also if $LS_OPTIONS is meant to contain a list of ls options in shell syntax for instance accepting things like --exclude='*~', then you'd need to define it as:

ls() {
  eval 'command ls '"$LS_OPTIONS"' "$@"'
}

Or with zsh:

alias ls='ls "${(Q@)${(z)LS_OPTIONS}}"'

If it's meant to contain a space-separated list of options instead, with bash 4.4+

ls() {
  local IFS=' '
  local -
  set -o noglob
  command ls $LS_OPTIONS "$@"
}

Or with zsh:

alias ls='ls ${(s: :)LS_OPTIONS}'
4
  • Can you plz explain why that is so? Mar 1 '18 at 11:46
  • 1
    @AbinashDash, see edit. Mar 1 '18 at 11:48
  • Okay. Now it looks like a long answer... :D Mar 1 '18 at 11:49
  • I misunderstood things. As executing dircolors in shell gives the output LS_COLORS='...' and export LS_COLORS , I thought those are evaluated by themselves(after executing dircolors) and it's only in printing what commands are executed. Thanks for clearing my doubts. Great answer. Mar 1 '18 at 12:02

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.