6

This question already has an answer here:

There is a command in Microsoft's cmd, called color. I know that, in bash, there are special characters that allows you, during the echos, to change the text colors. As well I do know that in ubuntu you can edit the parameters of the terminal setting a "style" going inside the config, editing it and applying it with mouse under the menus.

What I ask is, if there exists under debian, ubuntu and centOS something very simple like:

color 1b

so that the console turns from:

enter image description here

to

enter image description here

marked as duplicate by Thomas Dickey, RalfFriedl, icarus, JigglyNaga, Jeff Schaller Dec 23 '18 at 20:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • It is unclear as to if you are asking how to do it in Debian etc, or in Microsoft's Windows' CMD. – ctrl-alt-delor Dec 22 '18 at 12:00
  • 1
    @ctrl-alt-delor I posted the screenshot of it being done in windows because I know how to do it in windows but not in linux. Also this is unix stack exchange otherwise I would have gone to a windows forum / stack exchange.. – user3450548 Dec 22 '18 at 12:12
  • Sorry I thought you were asking about bash in MS-Windows. I will edit you question to make it clear, for you. – ctrl-alt-delor Dec 22 '18 at 12:13
  • @ctrl-alt-delor ok but I have edited the title in a clearer way.. in case leave it as it :) – user3450548 Dec 22 '18 at 12:14
  • Bash has no notion as such of colors etc - this is the realm of the terminal (which used to be a real device, but these days usually is emulated in software). So it is just a question of finding a program that will do what you want that can be started from bash. – Thorbjørn Ravn Andersen Dec 22 '18 at 18:21
6

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

  • It actually does something however just the written text changes, what about the full background like in windows cmd? – user3450548 Dec 22 '18 at 12:17
  • I believe this requires clearing the screen with clear or CTRL + L – Panki Dec 22 '18 at 13:16
  • @user3450548 unix.stackexchange.com/a/474924/308316, the second part (about xterm-like terminals) – mosvy Dec 22 '18 at 13:21
  • @user3450548 to answer your question directly this should set the background as in the 2nd picture: printf '\e]11;#000080\a' – mosvy Dec 22 '18 at 13:30
  • 2
    Perhaps worth noting that none of this really has anything to do with bash. Escape sequences, tput, and so on will work from any shell, while none of them will work if the terminal emulator you're using doesn't support color changing. – jamesqf Dec 22 '18 at 18:32
4

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.

  • Just like with the tput setab from the accepted answer, calling either reset or temporarily setting the background with printf 'foo\e[41mB\e[mar\n' will wipe that setting. – mosvy Dec 22 '18 at 14:14
3

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.