linuxbashshellechoterminal-color

How to force the output color of echo to follow colors defined in bash script?


I am using ANSI escape codes in my bash script to change the color of echo commands

#/bin/bash

cecho() {
        local code="\033["
        case "$1" in
                black  | bk) color="${code}0;30m";;
                red    |  r) color="${code}1;31m";;
                green  |  g) color="${code}1;32m";;
                yellow |  y) color="${code}1;33m";;
                blue   |  b) color="${code}1;34m";;
                purple |  p) color="${code}1;35m";;
                cyan   |  c) color="${code}1;36m";;
                gray   | gr) color="${code}0;37m";;
                *) local text="$1"
        esac
        [ -z "$text" ] && local text="$color$2${code}0m"
        echo -e "$text"
}

cecho b "This is blue"
cecho r "This is red"

The issue is i am using a Terminal color scheme and it causes the cecho command to display wrong colors , for eg. If i try to output cecho command for purple color , it gets displayed in yellow color. I think my internal Terminal color sceheme is conflicting with these ANSI color codes. Is it somehow possible to override any internal color schemes and force display the colors defined in bash script ? Wrong colors being displayed in echo might be due to other reasons as well , conflict with internal color scheme is just my guess .

enter image description here


Solution

  • In my color scheme,there were some variables pre defined for different colors.So while using ANSI color codes ,it was getting overrided by these gloablly defined colors. Removing all those internally defined colors has apparently fixed this issue