7

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

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
user3450548
  • 2,868
  • 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
  • setterm, part of util-linux – peterh Dec 23 '18 at 14:32

3 Answers3

8

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

Panki
  • 6,664
  • 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 https://unix.stackexchange.com/a/474924/308316, the second part (about xterm-like terminals) –  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' –  Dec 22 '18 at 13:30
  • Everything you do with escape sequences you can do more portably with tput. – Chris Davies Dec 22 '18 at 15:13
  • @roaima No you can't. Please read the Note: from my linked answer. –  Dec 22 '18 at 17:44
  • 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
5

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.

JdeBP
  • 68,745
GAD3R
  • 66,769
  • 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. –  Dec 22 '18 at 14:14
4

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.