-1

When I do tput sgr0 it changes my terminal font and colour to white (should be black) and looks blocky.

Please see screenshot:

enter image description here

How do I stop this?

To be more concise, any echo after tput sgr 0 seems to output to bold white, my terminal text colour is black.

Update:

My ~.bashrc:

export LC_ALL="en_GB.UTF-8"
export BASH_SILENCE_DEPRECATION_WARNING=1

branch() {
  git branch 2> /dev/null | \ 
  grep \*                 | \ 
  awk '{print$2}'         | \ 
  sed '
    s#^#(#g;
    s#$#)#g
  '
 }

colo() { tput setaf $1; }


PS1='`tput bold``colo 5`\W: `tput sgr0``colo 0``branch``colo 0` \n λ '
2
  • Hmm. I notice your question refers to sgr0 but your example code in the screenshot uses sgr 0. Different! Commented Nov 22, 2023 at 10:20
  • as you can see in the screenshot, they do the same thing. Commented Nov 22, 2023 at 14:47

1 Answer 1

3

The sgr0 code is described in man terminfo as "turn off all attributes".

The sgr code is described as "define video attributes #1-#9 (PG9)" and takes a value 1..9; the sequence sgr 0 is undefined. You have used both sgr0 and sgr 0 in your question.

The remainder of the answer assumes sgr0, since the other sequence isn't valid.


If using tput sgr0 resets your terminal to black and white then those colours are part of the default (reset) mode. I assume therefore that rather then configuring your terminal application to use your preferred colour scheme as its default, you have put instructions in one of your shell initialisation files to set specific foreground and background colours. These are (correctly) being reset by the sgr0 code.

Two possible solutions come to mind,

  1. Configure the terminal application directly to use your preferred colours rather then setting them in a shell configuration file, so they become the default reset values
  2. Modify the shell prompt command to set your preferred foreground and background attributes each time it's printed
6
  • On many xterm-like terminal emulators, you can also change the default background and foreground colours with escape sequences. Something like printf '\e]10;black\a\e]11;white\a' Commented Nov 22, 2023 at 13:54
  • as you can see in the screenshot tput sgr0 and tput sgr 0 both do the same thing, it's the text that gets turned to white (white bold I think) I'll post my ~/.bashrc @chrisDavies please see the update. This is the only file where I have colours set. Commented Nov 22, 2023 at 14:29
  • maybe it's my mac terminal GUI settings, if it's not the file I added in the update. One more thing the background stays constant, only the text goes bold white after tput sgr0 please see screenshot, original text is black. Commented Nov 22, 2023 at 14:42
  • you are right, I have 7 for white in my ~/.bashrc thanks a lot Commented Nov 22, 2023 at 14:51
  • Sorry that was a ~/.bashrc for my other machine, please see the updated one. Commented Nov 22, 2023 at 14:56

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.