Changing sh color in .bashrc

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

bytecode77

Posted 2016-02-13T00:41:48.167

Reputation: 971

~/.bashrc is read by Bash, what shell are you using? – kos – 2016-02-13T01:04:27.503

echo $SHELL returns /bin/bash, so I'm obviously using bash. Why does this not work then? – bytecode77 – 2016-02-13T01:11:51.803

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 – 2016-02-13T01:17:10.503

1Yeah, ~/.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 – 2016-02-13T01:29:09.153

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 – 2016-02-13T01:36:06.153

No, apparently it doesn't. I can't test this reliably right now; I'll boot my Debian VM later to check directly on Debian, but in the meantime you can try Googling for "change motd color". I'll try to post an answer later. – kos – 2016-02-13T01:46:43.563

Answers

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

kos

Posted 2016-02-13T00:41:48.167

Reputation: 201

~/.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 – 2016-02-13T10:49:48.397