2

Basically, say I have a command like grep which outputs color-coded results with a flag like -r. If I were to wrap this command within a shell script, upon running the script, the output won't have the same color-coded effect.

Is there any other way of making the shell script spit out results (on terminal) in the same color-coding without resorting to using tput or manual color coding?

5
  • great! thanks. add an answer and I will choose it as the best answer. Commented Feb 15, 2014 at 6:22
  • my answer didn't help? Commented Feb 15, 2014 at 6:33
  • you can use grep rather than ls Commented Feb 15, 2014 at 6:34
  • Hi, your answer is useful but it isn't directly related to my question. my question was more about why grep doesn't output color-coded results when I use it in a shell script. Commented Feb 15, 2014 at 6:39
  • 1
    @R. L. ok no problem...I said about tput and I also referred in my answer ls --color=auto but not as accurate as what John1024 said...have a nice day :) Commented Feb 15, 2014 at 6:50

2 Answers 2

2

To force color output from grep:

grep --color=always

From man grep:

   --color[=WHEN], --colour[=WHEN]
          Surround   the   matched   (non-empty)   strings,
          matching lines, context lines, file  names,  line
          numbers, byte offsets, and separators (for fields
          and  groups  of  context   lines)   with   escape
          sequences   to  display  them  in  color  on  the
          terminal.   The  colors  are   defined   by   the
          environment variable GREP_COLORS.  The deprecated
          environment   variable   GREP_COLOR   is    still
          supported,   but   its   setting  does  not  have
          priority.  WHEN is never, always, or auto.
Sign up to request clarification or add additional context in comments.

Comments

1

just use Bash functions, like, say this one:

make()
{
  pathpat="(/[^/]*)+:[0-9]+"
  ccred=$(echo -e "\033[0;31m")
  ccyellow=$(echo -e "\033[0;33m")
  ccend=$(echo -e "\033[0m")
  /usr/bin/make "$@" 2>&1 | sed -E -e "/[Ee]rror[: ]/ s%$pathpat%$ccred&$ccend%g" -e "/[Ww]arning[: ]/ s%$pathpat%$ccyellow&$ccend%g"
  return ${PIPESTATUS[0]}
}

How to turn on or off colors in bash

by NIX CRAFT on NOVEMBER 4, 2006 · 1 COMMENT· LAST UPDATED NOVEMBER 4, 2006

in BASH SHELL

Q. How do I turn on or off file name colors in bash shell?

A. Most modern Linux distro comes with alias that defines colors for your file. However ls command is responsible for displaying color on screen for files, directories and other object.

By default, color is not used to distinguish types of files. You need to pass --color option to ls command.

Task: Turn off colors

Type the following command

$ ls --color=none

Or just remove alias with unalias command:

$ unalias ls

Task: Turn on colors

Use any of the following command:

$ ls --color=auto
$ ls --color=tty

You can add or remove ls alias from ~/.bash_profile or ~/.bashrc file.

this commands for bash that are working well

handy tput commands

tput bold - Bold effect
tput rev - Display inverse colors
tput sgr0 - Reset everything
tput setaf {CODE}- Set foreground color, see color {CODE} below
tput setab {CODE}- Set background color, see color {CODE} below
Colors {code} code for tput command

Color {code}    Color
0    Black
1    Red
2    Green
3    Yellow
4    Blue
5    Magenta
6    Cyan
7    White

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.