"Reset" colors of terminal after ssh exit/logout

32,052

Solution 1

Better:
tput sgr0 is normally the equivalent of echo -en "\e[0m"

The difference is that using tput will adapt if the terminal type is other than ANSI - and it has been set up properly.

man 5 terminfo for more codes to use.

In there you will find 'reset' so tput reset should work.


$ echo -e "\e[7m TEST \e[0m"  
 TEST   
$ echo -e "$(tput rev) TEST $(tput sgr0)"  
 TEST   
$   

(The TEST prints should have swapped background/foreground colors, cannot be shown here - only "emulated")


Found another option for this, a less geeky way:

the util-linux (on ubuntu) package contains setterm (shell utility).

type setterm --help and you'll find the available options.


The ACTUAL output of tput sgr0
$ tput sgr0 | od -t x1z
0000000 1b 28 42 1b 5b 6d                                >.(B.[m<
0000006

... depends on how your terminal is set up, e.g.

$ set | grep TERM
TERM=xterm-256color

for the above.


$ infocmp ansi
    Reconstructed via infocmp from file: /lib/terminfo/a/ansi
ansi|ansi/pc-term compatible with color,
    am, mc5i, mir, msgr,
    colors#8, cols#80, it#8, lines#24, ncv#3, pairs#64,
    acsc=+\020\,\021-\030.^Y0\333`\004a\261f\370g\361h\260j\331k\277l\332m\300n\305o~p\304q\304r\304s_t\303u\264v\301w\302x\263y\363z\362{\343|\330}\234~\376,
    bel=^G, blink=\E[5m, bold=\E[1m, cbt=\E[Z, clear=\E[H\E[J,
    cr=^M, cub=\E[%p1%dD, cub1=\E[D, cud=\E[%p1%dB, cud1=\E[B,
    cuf=\E[%p1%dC, cuf1=\E[C, cup=\E[%i%p1%d;%p2%dH,
    cuu=\E[%p1%dA, cuu1=\E[A, dch=\E[%p1%dP, dch1=\E[P,
    dl=\E[%p1%dM, dl1=\E[M, ech=\E[%p1%dX, ed=\E[J, el=\E[K,
    el1=\E[1K, home=\E[H, hpa=\E[%i%p1%dG, ht=\E[I, hts=\EH,
    ich=\E[%p1%d@, il=\E[%p1%dL, il1=\E[L, ind=^J,
    indn=\E[%p1%dS, invis=\E[8m, kbs=^H, kcbt=\E[Z, kcub1=\E[D,
    kcud1=\E[B, kcuf1=\E[C, kcuu1=\E[A, khome=\E[H, kich1=\E[L,
    mc4=\E[4i, mc5=\E[5i, nel=\r\E[S, op=\E[39;49m,
    rep=%p1%c\E[%p2%{1}%-%db, rev=\E[7m, rin=\E[%p1%dT,
    rmacs=\E[10m, rmpch=\E[10m, rmso=\E[m, rmul=\E[m,
    s0ds=\E(B, s1ds=\E)B, s2ds=\E*B, s3ds=\E+B,
    setab=\E[4%p1%dm, setaf=\E[3%p1%dm,
    sgr=\E[0;10%?%p1%t;7%;%?%p2%t;4%;%?%p3%t;7%;%?%p4%t;5%;%?%p6%t;1%;%?%p7%t;8%;%?%p9%t;11%;m,
    sgr0=\E[0;10m, smacs=\E[11m, smpch=\E[11m, smso=\E[7m,
    smul=\E[4m, tbc=\E[3g, u6=\E[%i%d;%dR, u7=\E[6n,
    u8=\E[?%[;0123456789]c, u9=\E[c, vpa=\E[%i%p1%dd,
SEE ALSO (man pages):
       tic(1) infocmp(1), captoinfo(1), infotocap(1), toe(1),  
       ncurses(3NCURSES), term(5).  terminfo(5).

Explore more, begin here maybe https://en.wikipedia.org/wiki/Terminfo
Programmer's guide to ncurses - google books

note: ncurses provides interfaces for termcap, terminfo and of course curses.

Solution 2

I believe you are looking for "reset" console command

Solution 3

You might find using this in the remote .bash_logout more appropiate:

tput init

It might be good to have this on every .bash_logout as a general good practice (and perhaps even on ~/.profile)

Solution 4

If you put the colors you want in the remote account's ~/.bashrc it should take effect on login and revert to your local settings on exit.

Share:
32,052

Related videos on Youtube

dgo.a
Author by

dgo.a

Updated on September 18, 2022

Comments

  • dgo.a
    dgo.a almost 2 years

    When I ssh into a remote server, I like the colors of the terminal to change. I use setterm on my remote ~/.bashrc file to get this done. However, when I exit, the terminal colors are not reset to the local ones.

    I solved the problem, but I am not sure if it is the best solution. This is what I could come up with.

    On the ~/.bash_logout on the remote server, I put:

    echo -e "\033[0m"
    /usr/bin/clear
    

    Just out of curiousity: Does anyone know of a better way? (I got the echo -e "\033[0m" line from http://edoceo.com/liber/linux-bash-shell)

    • Ram
      Ram almost 13 years
      I recommend you use tput to change colours instead. Look it up.
    • terdon
      terdon over 10 years
      What colors are you setting and how? Do you mean your prompt (PS1)?
  • dgo.a
    dgo.a almost 13 years
    I thought so too. But, they did not revert back when I exited ssh. Maybe it's because I was using setterm in .bashrc (using GNOME terminal). Do you use a different method of setting the color scheme in .bashrc?
  • Yitzchak
    Yitzchak almost 13 years
    if you're using GNOME, I'm assuming you run linux. I use Ubuntu, but I'm guessing the process is pretty similar across the distros. You can backup the file then open it in a text editor and edit the contents starting at the line that says PS1=. You can find information at novell.com/coolsolutions/tools/17142.html. It tells you more than you ever wanted to know about the .bashrc file.
  • alpha
    alpha almost 11 years
    Uh, no. Terminal settings are not based on what you're doing in the terminal. They (anthromorphizing!) don't care if you're local, remote, working in Swahili or anything. It's a simple state machine. Since you used a command to change it to something... you have to use a command to change it to something else. The .bash_logout solution is the best way, unless you want to write an alias/function to wrap your ssh commands. Blech. Easier, make your settings (whatever they are) part of your PS1 prompt output. When you log into a machine, it'll change, log out, old prompt will reinstate.
  • Hannu
    Hannu about 7 years
    echo -e "\e \033 \x1b " | od -t x1z - which is most readable?
  • niglesias
    niglesias about 6 years
    The problem with reset is that it erases all console content, and sometimes that's not what you want.
  • SebMa
    SebMa about 5 years
    tput sgr0 returns something different from \e[0m, look at this : a=$(tput sgr0); set | grep ^a= and see the result. Can you also please tell what is the equivalent setterm option for \e[0m ?
  • Hannu
    Hannu about 5 years
    Note that \e[0m or the equal \e[m is for ANSI type terminals, tput sgr0 might give you something entirely different if your terminal is not ANSI. You cannot be sure even for ANSI terminals, as the one you're actually using might have been set up differently in the terminfo database.
  • SebMa
    SebMa about 5 years
    TERM=ansi a=$(tput sgr0); set | grep ^a= and the result is a=$'\E[0;10m'. This looks different from \e[0m
  • Hannu
    Hannu about 5 years
    Yes? "You cannot be sure even for ANSI terminals, as the one you're actually using might have been set up differently in the terminfo database." As you can see above, that variant has \e(B\e[m - expect differences!
  • Hannu
    Hannu about 5 years
    As you can see from my recent addition rmacs=\E[10m - where "rm" most likely is "remove". Above it you have a longish definition starting with acsc= - which seems to be something enabling(?) an "Alternate Character Set" - I'd guess that is something supported by a certain type of terminal, maybe something similar to "petsci" (for old Commodore PET, Vic20, and 64 computers)
  • Hannu
    Hannu over 2 years
    WIndows user? - Some reading; docs.microsoft.com/en-us/windows/console/…