0

How do I customize bash colors for command output?

For example; I run ls and want directories and files to be a different color. How do I accomplish this? I see that distro's are doing this but I cannot find any documentation on colorizing OUTPUT from other commands.

0

3 Answers 3

4

That's not something your shell does. Color is generated through special control characters. Your terminal emulator needs to translate them into actual color.

For ls, you need to set

  • LSCOLORS (FreeBSD, OS X, ..) or
  • LS_COLORS (Linux).

See here for an LS_COLORS generator. You can then call

  • ls --color (Linux)
  • ls -G or set the CLICOLOR environment variable (BSD variants).

The rest is up to your terminal emulator. For example, my iTerm2 is configured like this:

5
  • You really can't. Most builtin commands won't colorize output unless explicitly called (like in ls). Most newer command-line tools have some sort of colorization, but it really depends. You can't find that out by just looking at the program. See uzsolt's answer below for a wrapper that would colorize anything.
    – slhck
    Jun 13, 2012 at 7:56
  • Can you provide the exact LSCOLORS settings for the colors you've shown here?
    – einpoklum
    Oct 20, 2013 at 13:25
  • @einpoklum Unfortunately not, sorry. I don't have this particular profile anymore, and you cannot directly convert iTerm2 colors to LS_COLORS as far as I know.
    – slhck
    Oct 20, 2013 at 13:35
  • How about a reference to some similar, pastel-ish color scheme?
    – einpoklum
    Oct 20, 2013 at 14:50
  • @josten Any specific reason for unaccepting this answer? Do you need any additional information?
    – slhck
    Jan 2, 2014 at 11:36
2

I have finally been able to get an answer to this question that does exactly what I expect it to.

For those interested the details; see the man terminfo and man termcap man pages. Those man pages layout terminal capabilities and environment variables that you can set allowing you to customize colors for any commands output.

One such example is having colored man pages:

man() {
    env LESS_TERMCAP_mb=$'\E[01;31m' \
    LESS_TERMCAP_md=$'\E[01;38;5;202m' \
    LESS_TERMCAP_me=$'\E[0m' \
    LESS_TERMCAP_se=$'\E[0m' \
    LESS_TERMCAP_so=$'\E[0;1;4;38;5;82m' \
    LESS_TERMCAP_ue=$'\E[0m' \
    LESS_TERMCAP_us=$'\E[04;38;5;51m' \
    man "$@"
}

The above variables will get applied to the environment of man when it's executed giving colors within a man page. These can also be applied to other commands (assuming it's using the terminfo output fields properly).

1

Maybe you can check a wrapper, eg. cw:

cw is a non-intrusive real-time ANSI color wrapper for common unix-based commands on GNU/linux. cw is designed to simulate the environment of the commands being executed, so that if a person types 'du', 'df', 'ping', etc. in their shell it will automatically color the output in real-time according to a definition file containing the color format desired. cw has support for wildcard match coloring, tokenized coloring, headers/footers, case scenario coloring, command line dependent definition coloring, and includes over 50 pre-made definition files.

You must log in to answer this question.