Using colors with printf

When writing such text, it displays the text in blue:

printf "\e[1;34mThis is a blue text.\e[0m" 

But I want to have the format defined in printf:

 printf '%-6s' "This is text" 

Now I tried a few options on how to add color, without success:

 printf '%-6s' "\e[1;34mThis is text\e[0m" 

I even tried to add the attribute code to the format without success. This does not work, and I can not find an example anywhere where colors are added to printf, which has a format like in my case.

+64
linux bash colors printf
Mar 23 2018-11-23T00:
source share
8 answers

You mix the parts together and not separate them cleanly.

 printf '\e[1;34m%-6s\e[m' "This is text" 

Basically, put fixed data in a format and a variable in the parameters.

+55
Mar 23 2018-11-23T00:
source share

Instead of using archaic terminal codes, I can offer the following alternative. It not only provides a more readable code, but also allows you to store color information separately from the format specifiers as you originally intended.

 blue=$(tput setaf 4) normal=$(tput sgr0) printf "%40s\n" "${blue}This text is blue${normal}" 

See my answer HERE for additional colors

+138
Mar 23 2018-11-23T00:
source share

This works for me:

 printf "%b" "\e[1;34mThis is a blue text.\e[0m" 

From printf(1) :

 %b ARGUMENT as a string with '\' escapes interpreted, except that octal escapes are of the form \0 or \0NNN 
+21
Mar 23 '11 at 23:10
source share

This is a small program to get a different color on the terminal.

 #include <stdio.h> #define KNRM "\x1B[0m" #define KRED "\x1B[31m" #define KGRN "\x1B[32m" #define KYEL "\x1B[33m" #define KBLU "\x1B[34m" #define KMAG "\x1B[35m" #define KCYN "\x1B[36m" #define KWHT "\x1B[37m" int main() { printf("%sred\n", KRED); printf("%sgreen\n", KGRN); printf("%syellow\n", KYEL); printf("%sblue\n", KBLU); printf("%smagenta\n", KMAG); printf("%scyan\n", KCYN); printf("%swhite\n", KWHT); printf("%snormal\n", KNRM); return 0; } 
+17
Nov 25 '14 at 11:21
source share

This is a small function that prints colored text using bash scripts. You can add as many styles as you want, and even print tabs and new lines:

 #!/bin/bash # prints colored text print_style () { if [ "$2" == "info" ] ; then COLOR="96m"; elif [ "$2" == "success" ] ; then COLOR="92m"; elif [ "$2" == "warning" ] ; then COLOR="93m"; elif [ "$2" == "danger" ] ; then COLOR="91m"; else #default color COLOR="0m"; fi STARTCOLOR="\e[$COLOR"; ENDCOLOR="\e[0m"; printf "$STARTCOLOR%b$ENDCOLOR" "$1"; } print_style "This is a green text " "success"; print_style "This is a yellow text " "warning"; print_style "This is a light blue with a \t tab " "info"; print_style "This is a red text with a \n new line " "danger"; print_style "This has no color"; 
+9
Jul 15 '17 at 19:29
source share

I use this c code to print the color output of a shell. The code is based on this post.

 //General Formatting #define GEN_FORMAT_RESET "0" #define GEN_FORMAT_BRIGHT "1" #define GEN_FORMAT_DIM "2" #define GEN_FORMAT_UNDERSCORE "3" #define GEN_FORMAT_BLINK "4" #define GEN_FORMAT_REVERSE "5" #define GEN_FORMAT_HIDDEN "6" //Foreground Colors #define FOREGROUND_COL_BLACK "30" #define FOREGROUND_COL_RED "31" #define FOREGROUND_COL_GREEN "32" #define FOREGROUND_COL_YELLOW "33" #define FOREGROUND_COL_BLUE "34" #define FOREGROUND_COL_MAGENTA "35" #define FOREGROUND_COL_CYAN "36" #define FOREGROUND_COL_WHITE "37" //Background Colors #define BACKGROUND_COL_BLACK "40" #define BACKGROUND_COL_RED "41" #define BACKGROUND_COL_GREEN "42" #define BACKGROUND_COL_YELLOW "43" #define BACKGROUND_COL_BLUE "44" #define BACKGROUND_COL_MAGENTA "45" #define BACKGROUND_COL_CYAN "46" #define BACKGROUND_COL_WHITE "47" #define SHELL_COLOR_ESCAPE_SEQ(X) "\x1b["X"m" #define SHELL_FORMAT_RESET ANSI_COLOR_ESCAPE_SEQ(GEN_FORMAT_RESET) int main(int argc, char* argv[]) { //The long way fputs(SHELL_COLOR_ESCAPE_SEQ(GEN_FORMAT_DIM";"FOREGROUND_COL_YELLOW), stdout); fputs("Text in gold\n", stdout); fputs(SHELL_FORMAT_RESET, stdout); fputs("Text in default color\n", stdout); //The short way fputs(SHELL_COLOR_ESCAPE_SEQ(GEN_FORMAT_DIM";"FOREGROUND_COL_YELLOW)"Text in gold\n"SHELL_FORMAT_RESET"Text in default color\n", stdout); return 0; } 
+2
Mar 14 '18 at 8:36
source share

man printf.1 has a note: "... your shell may have its own version of printf ...". This question is tagged for bash , but, if at all possible, I'm trying to write scripts portable to any shell. dash is generally a good minimal base for portability, so the answer here works in bash , dash and & zsh . If the script works in these 3, it is most likely portable anywhere.

The last printf implementation in dash [1] does not colorize the output, given the %s format specifier with the ANSI \e escape character, but the %b format specifier combined with the octal \033 (equivalent to ASCII ESC ) will do the job. Please comment on any outliers, but, AFAIK, printf is implemented in all shells to use the octal subset of ASCII at a minimal level.

To the title of the question "Using colors with printf", the most portable way to specify formatting is to combine the %b format specifier for printf (as indicated in the previous answer by @Vlad) with the octal escape \033 .




portable-color.sh

 #/bin/sh P="\033[" BLUE=34 printf "-> This is %s %-6s %s text \n" $P"1;"$BLUE"m" "blue" $P"0m" printf "-> This is %b %-6s %b text \n" $P"1;"$BLUE"m" "blue" $P"0m" 



Outputs:

 $ ./portable-color.sh -> This is \033[1;34m blue \033[0m text -> This is blue text 

... and "blue" is blue in the second line.

%-6s format %-6s of the OP is in the middle of the format string between the sequences of control characters for opening and closing.




[1] Link: man dash Section "Builtins" :: "printf" :: "Format"

+1
Jul 13 '18 at 17:46
source share
 #include <stdio.h> //fonts color #define FBLACK "\033[30;" #define FRED "\033[31;" #define FGREEN "\033[32;" #define FYELLOW "\033[33;" #define FBLUE "\033[34;" #define FPURPLE "\033[35;" #define D_FGREEN "\033[6;" #define FWHITE "\033[7;" #define FCYAN "\x1b[36m" //background color #define BBLACK "40m" #define BRED "41m" #define BGREEN "42m" #define BYELLOW "43m" #define BBLUE "44m" #define BPURPLE "45m" #define D_BGREEN "46m" #define BWHITE "47m" //end color #define NONE "\033[0m" int main(int argc, char *argv[]) { printf(D_FGREEN BBLUE"Change color!\n"NONE); return 0; } 
-one
Jul 20 '16 at 14:12
source share



All Articles