I can change the colour through preference , but how can I change the background colour and text colour of terminal through the command line?
Ubuntu – Changing colour of text and background of terminal
color-managementcommand line
color-managementcommand line
I can change the colour through preference , but how can I change the background colour and text colour of terminal through the command line?
Best Answer
On certain XTerm/ANSI-compatible terminals (like
xterm
andgnome-terminal
), you can set colors using a palette bigger then the default 8/16-colors palette (for example using an 88-colors, 256-colors or 16777216-colors (RGB) palette; bothxterm
andgnome-terminal
support both the 256-colors and the 16777216-colors (RGB) palette); mind that the shell might override this (this is the case e.g. forzsh
).Here's a script to list the colors in the 256-color palette along with their ANSI color code in XTerm/ANSI-compatible terminals with a 256-color palette support:
Depending on whether you want to apply the color to the foreground or to the background, use an
<fg_bg>
value of38
or48
(respectively) in the following command:For example, to set the foreground color (
<fg_bg>
=38
) to red (<ANSI_color_code>
=196
) and the background color (<fg_bg>
=48
) to black (<ANSI_color_code>
=0
):It's necessary to redraw the prompt using
printf '\e[K'
in order for the background color to apply to the whole line and in order for the foreground color to apply to the cursor:The same thing can be accomplished using RGB values instead of ANSI color codes in a compatible terminal; depending on whether you want to apply the color to the foreground or to the background, use an
<fg_bg>
value of38
or48
(respectively) in the following command:For example, to set the foreground color (
<fg_bg>
=38
) to red (<R>
=255
,<G>
=0
,<B>
=0
) and the background color (<fg_bg>
=48
) to black (<R>
=0
,<G>
=0
,<B>
=0
):Again, it's necessary to redraw the prompt using
printf '\e[K'
in order for the background color to apply to the whole line and in order for the foreground color to apply to the cursor:Using either methods, you can use
printf '\e[0m'
to reset all the attributes: