Does bash have a color command, as seen in MS-Windows CMD?

There are multiple ways you can do this.

One way is by using tput:

tput setab 4 sets the background color to blue. To set the foreground color, use tput setaf.

Another way is by using raw ANSI escapes, here is a good documentation: https://misc.flogisoft.com/bash/tip_colors_and_formatting


The command setterm can be used:

setterm -background blue

or

setterm -ba blue

This uses standard ECMA-48 control sequences and will actually work with many (but not all) terminal emulators. (Contrary to the manual, it does not in fact use terminfo for this option.) ECMA-48 includes the notion of a default colour for both background and foreground which one can change to with default:

setterm --background default

To change the default colour, add the --store option (which emits a control sequence that only works with the Linux kernel's built-in terminal emulator, however):

setterm --background red --store

See man setterm and setterm --help for more details.


With xterm-like terminal emulators, you can use:

xtermcontrol --bg blue

(blue or any color specification supported by XParseColor(3x)).

That actually sends a \33]11;blue\7 sequence, so you can do the same with:

printf '\33]11;%s\a' blue

See Operating System Commands, in the XTerm Control Sequences document for details.

Tags:

Linux

Bash