Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
Although this is not what I was looking for, using VI(m) as a root always had no colors; I have found this to enable coloring of VI(m) (incase someone else comes to this question and wants to fix VI(m) coloring) kb.mediatemple.net/questions/1565/Enabling+vi+syntax+colors#gs –  josten Jul 1 '12 at 23:32
add comment

3 Answers 3

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:

share|improve this answer
    
How do I find out which commands support colorized output? –  josten Jun 13 '12 at 2:15
    
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 '12 at 7:56
    
Can you provide the exact LSCOLORS settings for the colors you've shown here? –  einpoklum Oct 20 '13 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 '13 at 13:35
    
How about a reference to some similar, pastel-ish color scheme? –  einpoklum Oct 20 '13 at 14:50
show 2 more comments

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.

share|improve this answer
add comment
up vote 1 down vote accepted

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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