1

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

Sachin
  • 1,217
  • 2
  • 11
  • 31
  • 1
    You code display correctly magenta color on my terminal. `my internal Terminal color sceheme is conflicting with these ANSI color codes` The color scheme is literally there to interpret asci escape codes. It's the same thing, not a conflict. The 16colors terminla color scheme is the interpretation of escape sequences to colors. If you setup your terminal to interpret `1;35m` as yellow, well, then it's going to be yellow. – KamilCuk Sep 28 '20 at 10:40
  • actually i suspected something wrong with my color scheme because if i remove the color scheme , the issue gets resolved , it is only happening when that particular color scheme is active . heres that color scheme if that helps - https://del.dog/raw/aryfuvicun – Sachin Sep 28 '20 at 10:47
  • Is [this](https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux/60221764#60221764) answer your question? – Ivan Sep 28 '20 at 10:48
  • @Sachin : Colour schemes usually allow to re-assign the RGB value for a certain colour such as 'green'. If your terminal program allows it, the solution would be that you are specifying RGB directly. Unfortunately, I don't know to what extent the ANSI color codes, respectively bash is supporting this. I'm doing it with zsh, but never tried with bash. In any case, I think you should also post, what terminal you are using. – user1934428 Sep 28 '20 at 11:20

1 Answers1

1

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

Sachin
  • 1,217
  • 2
  • 11
  • 31