How to change color of text on LInux

Hey guys. I'm using CodeBlocks on my Ubuntu. Trying to figure out how to change console text color with C++. Every place I look has windows.h and I am not using microsoft. Thank you for any help that you can give.
On Linux, you can just print ANSI color codes, see https://en.wikipedia.org/wiki/ANSI_escape_code#Colors

1
2
3
4
5
#include <stdio.h>
int main()
{
    puts("\x1b[31mthis is red\x1b[0m");
}


or, I suppose, you could be extra portable and use
1
2
3
4
5
6
7
#include <stdio.h>
#include <term.h>
int main()
{
    setupterm(NULL, 1, NULL);
    printf("%sthis is red%s\n", tparm(tigetstr("setaf"), 1), tigetstr("sgr0"));
}

(link with -lcurses and check all those functions for errors in real code)
Last edited on
Nice. Thank you. At first I didn't understand it. 30-37 foreground colors. 40-47 background colors. 0 for reset.
1
2
3
4
5
#include <stdio.h>
int main()
{
    puts("\x1b[5m\x1b[32mThank you\x1b[0m");
}
Topic archived. No new replies allowed.