1

I'm trying to change the color of my shell which I connect to using Putty.

When I type this

export PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h:\w\$ '

I get exactly what I want. Everything is green and it's only applied for the root user so I can distinguish root from other users.

However, when I put this code (without "export") in /root/.bashrc, I get this effect when I change the current shell to bash using the command bash.

Example:

example

How can I make my shell green, without switching to "bash" everytime I connect, i.e. how can I apply this to the default shell aswell?

Debian 8.3 (Jessie) is used here

  • ~/.bashrc is read by Bash, what shell are you using? – kos Feb 13 '16 at 1:04
  • echo $SHELL returns /bin/bash, so I'm obviously using bash. Why does this not work then? – bytecode77 Feb 13 '16 at 1:11
  • Okay, I came closer now. When I create a file named .profile with the content . ~/.bashrc, I get the green shell, but the text above the first input prompt is still gray. Is there a way to solve this? – bytecode77 Feb 13 '16 at 1:17
  • 1
    Yeah, ~/.bashrc is read only by non-login shells, ~/.profile is read by login shells. Putting it in /etc/profile instead should change the color of the MOTD as well. – kos Feb 13 '16 at 1:29
  • Can you please put an answer with a description where exactly to put what and make everything (including MOTD) green, but only for root user? – bytecode77 Feb 13 '16 at 1:36
2

The problem is ~/.bashrc is not read by login shells, and hence it's not read by the shell you get by logging in via SSH.

As you've noticed already, ~/.profile is read by login shells, so that's a way to set the color, but it doesn't affect the MOTD.

On Debian the MOTD is stored in /etc/motd; here's a command which will append the correct escape sequences at the start and at the end of the file:

printf '\e[0;32m' | sudo perl -i -pe 'if($. == 1) { $_ = <STDIN>.$_ }' /etc/motd; printf '\e[0m' | sudo tee -a /etc/motd

screenshot

The \e[0;32m will set the color to green at the start, the \e[0m will reset the all the attributes at the end; this way the MOTD is changed without affecting what's printed after; if you don't want to reset the attributes at the end (affecting what's printed after the MOTD), just drop the last command:

printf '\e[0;32m' | sudo perl -i -pe 'if($. == 1) { $_ = <STDIN>.$_ }' /etc/motd
|improve this answer|||||
  • ~/.profile was very helpful. I won't set the MOTD color, though, because I want green color only for the root user. But with the profile set to green, this is the closest to what I wanted to achieve. +1 – bytecode77 Feb 13 '16 at 10:49

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.