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

6
  • It is unclear as to if you are asking how to do it in Debian etc, or in Microsoft's Windows' CMD. Commented Dec 22, 2018 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.. Commented Dec 22, 2018 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. Commented Dec 22, 2018 at 12:13
  • @ctrl-alt-delor ok but I have edited the title in a clearer way.. in case leave it as it :) Commented Dec 22, 2018 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. Commented Dec 22, 2018 at 18:21

3 Answers 3

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

7
  • It actually does something however just the written text changes, what about the full background like in windows cmd? Commented Dec 22, 2018 at 12:17
  • I believe this requires clearing the screen with clear or CTRL + L
    – Panki
    Commented Dec 22, 2018 at 13:16
  • @user3450548 unix.stackexchange.com/a/474924/308316, the second part (about xterm-like terminals)
    – user313992
    Commented Dec 22, 2018 at 13:21
  • @user3450548 to answer your question directly this should set the background as in the 2nd picture: printf '\e]11;#000080\a'
    – user313992
    Commented Dec 22, 2018 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
    Commented Dec 22, 2018 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.

1
  • 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.
    – user313992
    Commented Dec 22, 2018 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.

Not the answer you're looking for? Browse other questions tagged .