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"

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)

  • 2
    I recommend you use tput to change colours instead. Look it up. – Ram Jul 31 '11 at 16:14
  • What colors are you setting and how? Do you mean your prompt (PS1)? – terdon Jan 5 '14 at 11:15

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"  
$ echo -e "$(tput rev) TEST $(tput sgr0)"  

(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<

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

$ set | grep TERM

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,
    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,
    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.

| improve this answer | |
  • echo -e "\e \033 \x1b " | od -t x1z - which is most readable? – Hannu May 14 '17 at 16:55
  • 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 ? – SebMa Apr 26 '19 at 13:22
  • 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. – Hannu Apr 26 '19 at 18:22
  • TERM=ansi a=$(tput sgr0); set | grep ^a= and the result is a=$'\E[0;10m'. This looks different from \e[0m – SebMa Apr 27 '19 at 0:09
  • 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 Apr 27 '19 at 16:28

I believe you are looking for "reset" console command

| improve this answer | |
  • 3
    The problem with reset is that it erases all console content, and sometimes that's not what you want. – niglesias Apr 4 '18 at 19:09

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)

| improve this answer | |

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.

| improve this answer | |
  • 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? – dgo.a Jul 31 '11 at 18:24
  • 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. – Yitzchak Jul 31 '11 at 19:16
  • 1
    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. – lornix Jul 27 '13 at 3:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.