Colored Shell Script Output Library

Colored shell script output library

Here is an modified snippet from my dotfiles that should do what you want

RCol='\e[0m'    # Text Reset

# Regular Bold Underline High Intensity BoldHigh Intens Background High Intensity Backgrounds
Bla='\e[0;30m'; BBla='\e[1;30m'; UBla='\e[4;30m'; IBla='\e[0;90m'; BIBla='\e[1;90m'; On_Bla='\e[40m'; On_IBla='\e[0;100m';
Red='\e[0;31m'; BRed='\e[1;31m'; URed='\e[4;31m'; IRed='\e[0;91m'; BIRed='\e[1;91m'; On_Red='\e[41m'; On_IRed='\e[0;101m';
Gre='\e[0;32m'; BGre='\e[1;32m'; UGre='\e[4;32m'; IGre='\e[0;92m'; BIGre='\e[1;92m'; On_Gre='\e[42m'; On_IGre='\e[0;102m';
Yel='\e[0;33m'; BYel='\e[1;33m'; UYel='\e[4;33m'; IYel='\e[0;93m'; BIYel='\e[1;93m'; On_Yel='\e[43m'; On_IYel='\e[0;103m';
Blu='\e[0;34m'; BBlu='\e[1;34m'; UBlu='\e[4;34m'; IBlu='\e[0;94m'; BIBlu='\e[1;94m'; On_Blu='\e[44m'; On_IBlu='\e[0;104m';
Pur='\e[0;35m'; BPur='\e[1;35m'; UPur='\e[4;35m'; IPur='\e[0;95m'; BIPur='\e[1;95m'; On_Pur='\e[45m'; On_IPur='\e[0;105m';
Cya='\e[0;36m'; BCya='\e[1;36m'; UCya='\e[4;36m'; ICya='\e[0;96m'; BICya='\e[1;96m'; On_Cya='\e[46m'; On_ICya='\e[0;106m';
Whi='\e[0;37m'; BWhi='\e[1;37m'; UWhi='\e[4;37m'; IWhi='\e[0;97m'; BIWhi='\e[1;97m'; On_Whi='\e[47m'; On_IWhi='\e[0;107m';

Then you can just echo -e "${Blu}blue ${Red}red ${RCol}etc...."

Colored text printing spaces in shell script

You need to put double quotes around the calls to print_bars to prevent word splitting, which removes excess spaces.

echo "CPU $pcpu [$(print_bars $pcpu 100)] - MEM $(human_print $mem) / $(human_print $tmem) [$(print_bars $mem $tmem)]"

The spaces around the color codes are because commands like

echo $GREEN

add a newline after the output, which turns into a space by word splitting. You should use

echo -n $GREEN

or

printf "%s" "$GREEN"

Linux: terminal colors shell script

A library called ncurses, you can build that kind of menu.

You can get more info in:
https://en.m.wikipedia.org/wiki/Ncurses

The colored ouput I know that only can do by ANSI codes.
You should look this question.

PolyML colored output to terminal in Linux

\033 is a character escape sequence which in bash and many other languages is interpreted as the character with ASCII code corresponding to the octal number 33.

In OCaml however, this escape sequence is interpreted as decimal. We can either convert the number from octal (33) to decimal (27) and continue to use this syntax, or use the correct syntax for octal escape sequences (\o027). Or we could even use hex (\x1b) if we want to be a bit more adventurous.

All of these will work in OCaml, and possibly also PolyML (if you replace Printf.printf with print of course):

Printf.printf "\027[31m RED \027[0m NORMAL \n";;
Printf.printf "\o033[31m RED \o033[0m NORMAL \n";;
Printf.printf "\x1b[31m RED \x1b[0m NORMAL \n";;

Source: The OCaml manual Chapter 9.1: Lexical conventions

stdlib and colored output in C

All modern terminal emulators use ANSI escape codes to show colours and other things.

Don't bother with libraries, the code is really simple.

More info is here.

Example in C:

#include <stdio.h>

#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"

int main (int argc, char const *argv[]) {

printf(ANSI_COLOR_RED "This text is RED!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_GREEN "This text is GREEN!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_YELLOW "This text is YELLOW!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_BLUE "This text is BLUE!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_MAGENTA "This text is MAGENTA!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_CYAN "This text is CYAN!" ANSI_COLOR_RESET "\n");

return 0;
}

Getting value from function, use that value to get colored output in shell

When you define your array, you are using strings instead of variables:

colr=(red blue green cyan purple yellow lgreen lblue lred lcyan)

Should be:

colr=($red $blue $green $cyan $purple $yellow $lgreen $lblue $lred $lcyan)

How do I output coloured text to a Linux terminal?

You need to output ANSI colour codes. Note that not all terminals support this; if colour sequences are not supported, garbage will show up.

Example:

 cout << "\033[1;31mbold red text\033[0m\n";

Here, \033 is the ESC character, ASCII 27. It is followed by [, then zero or more numbers separated by ;, and finally the letter m. The numbers describe the colour and format to switch to from that point onwards.

The codes for foreground and background colours are:

         foreground background
black 30 40
red 31 41
green 32 42
yellow 33 43
blue 34 44
magenta 35 45
cyan 36 46
white 37 47

Additionally, you can use these:

reset             0  (everything back to normal)
bold/bright 1 (often a brighter shade of the same colour)
underline 4
inverse 7 (swap foreground and background colours)
bold/bright off 21
underline off 24
inverse off 27

See the table on Wikipedia for other, less widely supported codes.


To determine whether your terminal supports colour sequences, read the value of the TERM environment variable. It should specify the particular terminal type used (e.g. vt100, gnome-terminal, xterm, screen, ...). Then look that up in the terminfo database; check the colors capability.



Related Topics



Leave a reply



Submit