Colour Output of Program Run Under Bash

Colour output of program run under BASH

Most terminals respect the ASCII color sequences. They work by outputting ESC, followed by [, then a semicolon-separated list of color values, then m. These are common values:

Special
0 Reset all attributes
1 Bright
2 Dim
4 Underscore
5 Blink
7 Reverse
8 Hidden

Foreground colors
30 Black
31 Red
32 Green
33 Yellow
34 Blue
35 Magenta
36 Cyan
37 White

Background colors
40 Black
41 Red
42 Green
43 Yellow
44 Blue
45 Magenta
46 Cyan
47 White

So outputting "\033[31;47m" should make the terminal front (text) color red and the background color white.

You can wrap it nicely in a C++ form:

enum Color {
NONE = 0,
BLACK, RED, GREEN,
YELLOW, BLUE, MAGENTA,
CYAN, WHITE
}

std::string set_color(Color foreground = 0, Color background = 0) {
char num_s[3];
std::string s = "\033[";

if (!foreground && ! background) s += "0"; // reset colors if no params

if (foreground) {
itoa(29 + foreground, num_s, 10);
s += num_s;

if (background) s += ";";
}

if (background) {
itoa(39 + background, num_s, 10);
s += num_s;
}

return s + "m";
}

Colour output of program run under BASH

Most terminals respect the ASCII color sequences. They work by outputting ESC, followed by [, then a semicolon-separated list of color values, then m. These are common values:

Special
0 Reset all attributes
1 Bright
2 Dim
4 Underscore
5 Blink
7 Reverse
8 Hidden

Foreground colors
30 Black
31 Red
32 Green
33 Yellow
34 Blue
35 Magenta
36 Cyan
37 White

Background colors
40 Black
41 Red
42 Green
43 Yellow
44 Blue
45 Magenta
46 Cyan
47 White

So outputting "\033[31;47m" should make the terminal front (text) color red and the background color white.

You can wrap it nicely in a C++ form:

enum Color {
NONE = 0,
BLACK, RED, GREEN,
YELLOW, BLUE, MAGENTA,
CYAN, WHITE
}

std::string set_color(Color foreground = 0, Color background = 0) {
char num_s[3];
std::string s = "\033[";

if (!foreground && ! background) s += "0"; // reset colors if no params

if (foreground) {
itoa(29 + foreground, num_s, 10);
s += num_s;

if (background) s += ";";
}

if (background) {
itoa(39 + background, num_s, 10);
s += num_s;
}

return s + "m";
}

Can colorized output be captured via shell redirect?

One way to capture colorized output is with the script command. Running script will start a bash session where all of the raw output is captured to a file (named typescript by default).

How to enable colored output from shell command wrapped in a script?

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.

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 detect colored text output in bash

The literal characters are available for matching using the regular tools; it's just the terminal when actually displaying the characters that removes them and changes the display color instead.

output=$(javaprog)
if [[ $output == $'\e[32mThis prints in the color green.\e[0m' ]]; then
echo "Detected green output"
fi

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)"

Show colors in output of bash script or bash -c

You have to enable colors:

 bash -c "ls --color=auto"

Try also:

bash -i -c ls


Related Topics



Leave a reply



Submit