How to Change the Output Color of Echo in Linux

How to change color of bash output?

In order to print a single line, you need to unset the color that has been set. Using tput

$ echo "$(tput setaf 4)Print Blue$(tput sgr 0)"

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

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

How to get bash to output color when run in a script?

You don't need export here, and it's simpler to make sure the correct escape character is added to each variable, rather than making echo or printf do the replacement.

GREEN=$'\e[0;32m'
RED=$'\e[0;31m'
NC=$'\e[0m'

echo "I ${RED}love${NC} ${GREEN}Stack Overflow${NC}"

Better yet, use tput to get the correct sequence for your terminal instead of assuming ANSI escape sequences.

GREEN=$(tput setaf 2)
RED=$(tput setaf 1)
NC=$(tput sgr0)

Bash is it possible to echo in color output of a command stored in a variable?

You are confusing yourself by not distinguishing between normal program output to stdout and error messages output to stderr.

Executing

str=$(command)

causes command to be executed with its stdout redirected, but without changing stderr. The consequence is that error messages sent to stderr will just appear immediately on stderr, which is presumably still your terminal.

The redirection is to a bash process which captures the output (that is, to stdout) and, when the command is finished, assigns the collected output to the shell variable. If there was an error and nothing was sent to stdout, the shell variable will end up set to an empty string.

So

str=$(command)
printf '\033[31m%s\033[0m\n' "$str"

will directly output error messages, and will capture and later output regular output. (I changed the echo to printf and \e to \033 in order to make the command portable. Also see note 1, below.)

If you just want to colour output, there is no need to capture the output in a variable at all. Just send the appropriate colour sequences before and after executing the command:

printf '\033[31m'
command
printf '\033[0m'

That will colour all output from command. (Of course, the output itself could include colour sequences, but there is no simple way to get around that.)

If you have some other reason to capture the output (which seems unlikely), you could run the command with stderr redirected to stdout. In that case, you will get both stdout and stderr output (intermingled) in the shell variable:

str=$(command 2>&1)

Notes

  1. Although using ANSI colour sequences is probably going to work in any modern platform on which bash is running, some people will recommend the use of the tput command in order to access the terminfo database. For example,

    tput setaf 1   # Set terminal to ANSI colour 1 (red)
    command
    tput sgr0 # Reset all terminal attributes

    tput setaf 0 is not the same as the control sequence ESC [ 0 m. setaf 0 sets the foreground colour to black, which will make output invisible if you use a white-on-black console, while ESC [ 0 m resets all character display attributes to their defaults. That's also what tput sgr0 is defined as doing, although it may also reset other terminal attributes. (With my terminfo setting, tput sgr0 outputs ESC ( B followed by ESC [ m; the former resets the terminal font mapping, which I think is redundant in my case. But YMMV.)

    It is possible (but unlikely) that there is no setaf entry for your terminal, in which case you are supposed to fall back to setf; however, the colour numbering is different so that setf 1 is blue and setf 4 is red, while setaf 1 is red and setaf 4 is blue. All of these convoluted details are documented in man 5 terminfo. Search for "Color Handling" and enjoy. If you are using Linux, you might also find man 4 console_codes interesting reading.

How to wrap color coded text to fixed line length in terminal?

When determining the (printable) width of a prompt (eg, PS1) the special characters - \[ and \] - are used to designate a series of non-printing characters (see this, this, this and this).

So far I've been unable to find a way to use \[ and \] outside the scope of a prompt hence this awk hack ...


Assumptions:

  • we don't know the color codes in advance
  • for this exercise it is sufficient to deal with color codes of the format \e[...m (\e[m turns off color)
  • may have to deal with multiple color codes in the input

We'll wrap one awk idea in a bash function (for easier use):

myfold() {

awk -v n="${1:-10}" ' # default wrap is 10 (printable) characters
BEGIN { regex="[[:cntrl:]][[][^m]*m" # regex == "\e[*m"
#regex="\x1b[[][^m]*m" # alternatives
#regex="\033[[][^m]*m"
}
{ input=$0

while (input != "" ) { # repeatedly strip off "n" characters until we have processed the entire line
count=n
output=""

while ( count > 0 ) { # repeatedly strip off color control codes and characters until we have stripped of "n" characters
match(input,regex)

if (RSTART && RSTART <= count) {
output=output substr(input,1,RSTART+RLENGTH-1)
input=substr(input,RSTART+RLENGTH)
count=count - (RSTART > 1 ? RSTART-1 : 0)
}
else {
output=output substr(input,1,count)
input=substr(input,count+1)
count=0
}
}
print output
}
}
'
}

NOTES:

  • other non-color, non-printing characters will throw off the count
  • the regex could be expanded to address other non-printing color and/or character codes

Test run:

$ echo -e "\e[31m123456789012345\e[m67890\e[32mABCD\e[m"
12345678901234567890ABCD


$ echo -e "\e[31m123456789012345\e[m67890\e[32mABCD\e[m" | myfold 10
1234567890
1234567890
ABCD


$ echo -e "\e[31m123456789012345\e[m67890\e[32mABCD\e[m" | myfold 7
1234567
8901234
567890A
BCD

Displaying colors:

Sample Image



Related Topics



Leave a reply



Submit