I'm currently trying to modify my terminal to have the prompt displayed in a different colour than the whole rest of the shell, and I would like to make it white for normal users and red for root.
I would like to do so on both, an OS X 10.10 Desktop and an Ubuntu 14.10 Server.
The only way I know of doing this is to modify $PS1.
And to do so, I would have to put it in a script that the shell sources on login, right?

However, on OS X the root user's shell is /bin/sh, and although that is actually bash, it does not source any login script unless a full login is invoked.
Now I know about su - <user>, but I need it to work with just su <user>.
I could change the root user's login shell, but I'd rather not.

Here's what I found out so far:

bash invoked as bash and logged in as su - sources:


bash invoked as bash and logged in as su sources:


bash invoked as sh and logged in as su - sources:


bash invoked as sh and logged in as su sources nothing at all.

I also tried hijacking $PROMPT_COMMAND, but that gets emptied on invoking su.

On the bash manual it says:

Invoked with name sh

If Bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well.

When invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first attempts to read and execute commands from /etc/profile and ~/.profile, in that order. The --noprofile option may be used to inhibit this behavior. When invoked as an interactive shell with the name sh, Bash looks for the variable ENV, expands its value if it is defined, and uses the expanded value as the name of a file to read and execute. Since a shell invoked as sh does not attempt to read and execute commands from any other startup files, the --rcfile option has no effect. A non-interactive shell invoked with the name sh does not attempt to read any other startup files.

When invoked as sh, Bash enters POSIX mode after the startup files are read.

So... is there any way me to set $ENV?
Simply setting it before invoking su has no effect.

Or is there any other way for me to modify $PS1 when the UID changes (full login or not), without having to use su - ..., changing the root user's login shell or messing with su/bash?

  • 1
    I presume you meant to write “bash invoked as sh and logged in as su [not su -] sources nothing at all”? – Gilles Apr 6 '15 at 23:34
su -c bash

Make an alias if that's too much typing for you.

You've correctly analyzed that a plain su doesn't load any initialization file, so either you need to change the configuration or you need to change the command.

  • Well, in that case... looks like root is gonna get a new login shell. – Siguza Apr 7 '15 at 16:43

I personally use that on my Mac and it's also working on my Ubuntu Server. But I edited two files.

In the .profile of root I put

PS1="\[\033[1;31m\]\u\[\e[m\]@\h \[\e[0;32m\]\w\[\e[m\] \$ " # Root

and in the .bash_profile of my personal user I put

PS1="\[\033[1;32m\]\u\[\e[m\]@\h \[\e[0;32m\]\w\[\e[m\] \$ " # User

The effect is the following :

My Terminal You can find all colors here http://blog.taylormcgann.com/tag/prompt-color/

But in your case, I think you should use this solution https://unix.stackexchange.com/a/174/50145

  • 1
    .profile isn't read by a plain su, that's the whole point of the question. – Gilles Apr 6 '15 at 23: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.