Temporarily setting colours in terminal

Hi all, :wave:

lately I encountered a “problem” which I couldn´t really solve.

Well, it´s not a real problem, rather a phenomenon I´d like to know the solution to.

This is the scenario:

I open a terminal (Linux Lite or Archlinux, it doesn´t matter). It may be xfce4-terminal, lxterminal, or xterm.
They all behave the same.

The colour settings I use for e.g. xfce4-terminal are aubergine for the background and white for the characters.

I wanted to change the settings, but not on a permanent basis, so the settings options of the terminal emulator wouldn´t be the right way of doing it for me.

So I tried this:

printf '\033]11;#FFFFFF\007'  # Set terminal background colour to white
printf '\033]10;#000000\007'  # Set terminal foreground colour to black

This worked well.
And it´s for the current tab only. If I open a new tab in the terminal it´s back to the defaults.
This is the way I like it.

As I said, the commands from above worked well, but I might want to use them within a script and after running a command within the script I want to get back to the defaults.

So the question is: what commands are needed to get back to them :question:

I tried:

printf '\033]11;#XXXXXX\007'  # Set terminal background colour back
printf '\033]10;#YYYYYY\007'  # Set terminal foreground colour back

but it didn´t work. The terminal stayed black on white. :slightly_frowning_face:

Do you have any ideas why that is?

I even tried:
tput init # Reset terminal colors using tput
This didn´t change it either.

Thanks a lot for yor help.
Many greetings from Rosika :slightly_smiling_face:

2 Likes

Looks like you used printf to change the colors back to default, but did you print any text after that?

Maybe if you printed some text (even spaces?) after changing the colors back to default the colors would stay default. Just a shot in the dark. At least with the text you printed would confirm the color change was temporary if not permanent.

2 Likes

Bi @Rosika ,
This has a slightly different approach to yours

The codes are different?
They do have a nice demo script

2 Likes

Hii @pdecker and @nevj , :wave:

thank you very much for your replies. :heart:

I just got it sorted out (see below).

@pdecker :

Printing some text works fine, but still black on white.
The change is only permanent for the current tab. Any other tab I open displays my default settings (white on aubergine).
Thanks for your suggestion.

@nevj :

Thanks for the link. Although I solved the problem I´ll look it up. Seems to provide some interesting infos.

Here´s the solution:

The first part of my pair of commands was correct:

printf '\033]11;#FFFFFF\007'
printf '\033]10;#000000\007'

It changed the terminal colours as intended:

But the second part was incorrect:

printf '\033]11;#XXXXXX\007'
printf '\033]10;#YYYYYY\007'

According to colorhexa these definitions are invalid.
I should´ve used

printf '\033]11;#2E0226\007'
printf '\033]10;#FFFFFF\007'

to get things back to normal again. I tried it out and it worked:

O.K., seems ChatGPT led me astray in this respect. :blush:

Well, everything works as intended now.
I still have to do some research on the syntax:

printf '\033]11;#2E0226\007'

Seems the “11” part defines the background whereas the “10” part defines the foreground.
But I need to understand the whole of the syntax.
Does anyone have an idea where to look it up :question:

Thanks again for your kind help.

Many greetings from Rosika :slightly_smiling_face:

P.S.:

Taking a cursory look at @nevj ´s page he provided the link to, I realized they use something like \033[37m there.

I.e. square bracket open. My examples use square brackets closed, but still work… :thinking: .

3 Likes

Those codes are completely different to yours
What is going on here?

2 Likes

Hi Neville, :wave:

I wish I knew. I´m a bit confused, too. I need to do some more research on the topic.

Cheers from Rosika. :slightly_smiling_face:

2 Likes

Hi @nevj and all, :wave:

Seems I got it now. I have to admit consulted ChatGPT for that. :wink:

The syntax of printf '\033]10;#FFFFFF\007' :

Let’s break down the syntax of the printf '\033]10;#FFFFFF\007' command:

  1. printf:

printf is a command-line utility in Unix-like operating systems that allows you to format and print text. It is similar to the echo command but provides more control over the formatting of the output.

  1. '\033':

This is an escape sequence that represents the ASCII escape character. In octal notation, \033 represents the ASCII control character for escape. It is followed by a semicolon (;) to separate it from the rest of the escape sequence.

  1. ']10;#FFFFFF':

This is the ANSI escape sequence for setting the foreground color in the terminal. The ]10; part indicates that this sequence is for setting the foreground color. The #FFFFFF part is a hexadecimal color code (in this case, white). The ANSI escape sequence format for setting colors follows the pattern ]N;#RRGGBB or ]N;#RRGGBBAA, where N is the color index (10 for foreground, 11 for background), RR, GG, BB, and AA are hexadecimal values representing the red, green, blue, and alpha components of the color, respectively.

  1. '\007':

This is another escape sequence that represents the ASCII bell character. In octal notation, \007 represents the ASCII control character for bell or alert. It is used here to terminate the ANSI escape sequence.

So, when you execute the printf '\033]10;#FFFFFF\007' command, it sends the ANSI escape sequence to the terminal, instructing it to set the foreground color to white.

Great. :smiley:

Now I wanted to know:

Looking around on the net I always found examples like \033[91m (defining red colour). So they use square bracket open. My examples (and yours) use square brackets closed, but still work… Can you explain that to me?

Here´s the solution to that:

The difference you’re noticing is related to two different standards for ANSI escape sequences: CSI (Control Sequence Introducer) and OSC (Operating System Command).

  1. CSI (Control Sequence Introducer):

This standard uses square brackets [ to introduce control sequences.
The typical format is \033[ followed by the sequence parameters and the final command character.
For example, \033[91m is an example of a CSI sequence that sets the text color to red. The CSI standard is commonly used for many types of control sequences, including color changes, cursor movement, and text formatting.

  1. OSC (Operating System Command):

This standard uses square brackets ] to introduce control sequences.
The typical format is \033] followed by the sequence parameters and the final command character.
OSC sequences are generally used for operating system commands or functions related to terminal configuration, such as setting terminal window titles or changing terminal colors. The sequence \033]10;#FFFFFF\007 that we discussed earlier is an example of an OSC sequence used to set the foreground color to white.

So, in summary, both CSI and OSC standards are valid for ANSI escape sequences, but they serve different purposes.
CSI sequences are more commonly used for general control sequences, while OSC sequences are specifically used for operating system commands related to the terminal.

Well, that is interesting. A lot of information. :wink:

Thanks a lot @pdecker and @nevj (and dare I say ChatGPT) for your help. :heart:

Many greetings fro Rosika :slightly_smiling_face:

2 Likes

@Rosika ,
Thank you, that is illuminating.
I think you will find the CSI codes used in defining colours for your shell prompt.

2 Likes