How to enable colored output from shell command wrapped in a script?
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?
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.
@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 🙂
2 Answers 2
To force color output from 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.
just use Bash functions, like, say this one:
make() < pathpat="(/[^/]*)+:7+" 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 $ >
How to turn on or off colors in bash
by NIX CRAFT on NOVEMBER 4, 2006 · 1 COMMENT· LAST UPDATED NOVEMBER 4, 2006
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.
Type the following command
Or just remove alias with unalias command:
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 - Set foreground color, see color below tput setab - Set background color, see color below Colors code for tput command Color Color 0 Black 1 Red 2 Green 3 Yellow 4 Blue 5 Magenta 6 Cyan 7 White
Disable color in shell/terminal
I want to disable all color in my shell. Not ls, not nano, not vi, nothing. What's the best way to achieve this?
6 Answers 6
if you are using PUTTY to remotely access the shell, then:
- on the left panel, click Colors
- uncheck the three boxes on the right panel (they are checked by default)
I'm using iTerm (OS X) and still getting color if I export TERM=vt220, and in 'ls' even after unsetting LS_COLORS
unset LS_COLORS; export TERM=xterm should do it, or at least get you most of the way there. You may need to change that to say TERM=vt220 for some overly "smart" programs.
This will start an xterm with no colors.
This is the best answer. Also, @lzap appears to be incorrect, at least on Ubuntu. xterm -cm give me a no-color terminal on Ubuntu regardless of LS_COLORS.
I encountered the same problem while writing an SSH robot in Python (colors came out as jibberish when run through Visual Studio). The simplest solution was to open a new shell inside the other shell that was running.
This opened a fresh shell without any of my settings and all printouts was monochrome. It also reset the prompt which was a bonus for my intended purpose.
The best way is probably to tell your terminal emulator and environment that it cannot support colors in the first place:
cat > ~/.bashrc unset LS_COLORS TERM=xterm-mono export TERM EOF
make a backup of .bashrc and then open .bashrc and remove all of these lines. this has the added benefit of disabling text colors in gedit!
# set a fancy prompt (non-color, unless we know we "want" color) case "$TERM" in xterm|xterm-color|*-256color) color_prompt=yes;; esac # uncomment for a colored prompt, if the terminal has the capability; turned # off by default to not distract the user: the focus in a terminal window # should be on the output of commands, not on the prompt #force_color_prompt=yes if [ -n "$force_color_prompt" ]; then if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then # We have color support; assume it's compliant with Ecma-48 # (ISO/IEC-6429). (Lack of such support is extremely rare, and such # a case would tend to support setf rather than setaf.) color_prompt=yes else color_prompt= fi fi if [ "$color_prompt" = yes ]; then if [[ $ == 0 ]] ; then PS1='$\[\033[01;31m\]\h\[\033[01;34m\] \W \$\[\033[00m\] ' else PS1='$\[\033[01;32m\]\u@\h\[\033[00m\] \[\033[01;34m\]\w \$\[\033[00m\] ' fi else PS1='$\u@\h \w \$ ' fi unset color_prompt force_color_prompt # If this is an xterm set the title to user@host:dir case "$TERM" in xterm*|rxvt*) PS1="\[\e]0;$\u@\h \w\a\]$PS1" ;; *) ;; esac # enable color support of ls and also add handy aliases if [ -x /usr/bin/dircolors ]; then test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)" alias ls='ls --color=auto' #alias dir='dir --color=auto' #alias vdir='vdir --color=auto' alias grep='grep --color=auto' alias fgrep='fgrep --color=auto' alias egrep='egrep --color=auto' fi
How do I get a colored bash?
How can I get the bash to look colored like this?
@kva Answering your own question at the same time as posting is encouraged across the Stack Exchange network.
Related: askubuntu.com/questions/123268/…. It explains how individual parts of the prompt can be coloured differently.
5 Answers 5
Open ~/.bashrc in text editor and uncomment line:
save then execute source ~/.bashrc
Here is what mine looks like after your method: !2016-10-25 16:12:15.png Not exactly as in the question.
Is force_color_prompt=yes the intended way of enabling colors? To me forcing sounds like a workaround.
I hope I didn't sound disrespectful with my comments, I'm just trying to understand how it was meant to work. For example, above those lines you mention, there's a different way of enabling colors, xterm-color|*-256color) color_prompt=yes;; Which makes me think that the colors could automatically enable if you had correct value in TERM variable.
I came up with this solution:
- open ~/.bashrc in an editor
- copy this and add it at the end of .bashrc file:
PS1='\[\033[1;36m\]\u\[\033[1;31m\]@\[\033[1;32m\]\h:\[\033[1;35m\]\w\[\033[1;31m\]\$\[\033[0m\] '
For a full list of available colors and further options look up these links:
A version that is a bit more 'general' - should work with a varied environment:
(depends on terminfo)
Insert this in your $HOME/.bashrc :
function fgtab < echo "tput setf/setb - Foreground/Background table" for f in ; do for b in ; do echo -en "$(tput setf $f)$(tput setb $b) $f/$b " done echo -e "$(tput sgr 0)" done > # The prompt in a somewhat Terminal -type independent manner: cname="$(tput setf 3)" csgn="$(tput setf 4)" chost="$(tput setf 2)" cw="$(tput setf 6)" crst="$(tput sgr 0)" PS1="\[$\]\u\[$\]@\[$\]\h:\[$\]\w\[$\]\$\[$\] "
Then execute source ~/.bashrc .
After that, fgtab will display a color table with numbers. Those numbers are for tput setf n and tput setb n where 'n' is the number, 'f' stands for 'foreground' and 'b' stands for 'background' color.
tput sgr 0 will reset foreground and background colors to default.
And as you can see, changing the colors used for the prompt becomes really easy (just edit the same number in $HOME/.bashrc as you wish).
Add an $(tput setb n) in $cname if you wish to have ALL of the prompt with background n.