Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Change bash prompt for root user (Leopard)

Hi all

This is probably a stupid question but...
I don't like the new shell prompt in Leopard (I'm not really interested in which bash version I'm using, I'd rather see my working directory), so I changed it for my "normal" user in ~/.bash_profile. I'd like to change the prompt for the root user, too, but where can I do that? I added a .bash_profile and a .bashrc file (with my prompt definition in them of course) to /var/root, but this didn't have any effect.

Thanks, Tina

G5 Dual 1.8, 23'' Cinema Display, Mac OS X (10.4.5), iMac G4/800, iBook G3/700, G4/400, iBook G3/366, iMac G3/233, PM 7200, Mac SE

Posted on Nov 28, 2007 7:06 AM

Reply
Question marked as Best reply

Posted on Nov 28, 2007 7:25 AM

Do you login as root, or do you sudo bash? For me, when I sudo bash my PS1 gets inherited to the new shell.

"sudo -H bash" changes HOME to /var/root (reads .bashrc)
"sudo -i" simulates login (reads .bash_login)
10 replies

Nov 28, 2007 12:38 PM in response to jarik

I do a sudo bash. Your method of using sudo -H bash instead and creating /var/root/.bashrc worked, at least partially, as I'm still not able to get a # at the end of the prompt. I can get a $ when I set PS1 to
export PS1="\h:\w \u\$ "

but if I use
export PS1="\h:\w \u\# "

I get get instead of the # the number of the commands I issued.

The other problem with this that when you then do "cd ~", you'll be taken to /var/root and not to the home of the user as who you invoked the sudo. But that's not that big an issue to me, but it would be nice to have it just as it was in Tiger.
I also tried adding some backslashes in front of the #, but that doesn't help.

Tina

Nov 28, 2007 12:57 PM in response to Tina Siegenthaler

Try the line
PS1="\ \h \ \W \ \$ "

except that you put the two backslashes together without the space in between. I had to add those to fool the automatic formatting system.

This should give you a hash when effective uid is 0.

I'm curious why it's not getting inherited as it is with my setup? Could you mv the new .bashrc and .bash_login out of the way and just do a plain sudo bash, then

<pre>echo $PS1</pre>

Also, how is PS1 set in your own .bashrc and .bash_login?

Message was edited by: jarik

Actually, it's not being inherited: instead my own .bashrc gets reread when I sudo bash (bash reads .bashrc from $HOME when starting unless it's a login shell, and $HOME is not being changed by default with sudo). So, I am suspecting that your .bashrc has some control structure to it and the PS1 setting line is not being executed. You could add some debugging to it, maybe a set -x in the beginning.

Message was edited by: jarik

Nov 28, 2007 1:24 PM in response to jarik

jarik wrote:
Try the line
PS1="\ \h \ \W \ \$ "


Yes, that works. Great!

I'm curious why it's not getting inherited as it is with my setup? Could you mv the new .bashrc and .bash_login out of the way and just do a plain sudo bash, then

<pre>echo $PS1</pre>


\s-\v\$

Also, how is PS1 set in your own .bashrc and .bash_login?


export PS1="\h:\w \u\$ "


Actually, it's not being inherited: instead my own .bashrc gets reread when I sudo bash (bash reads .bashrc from $HOME when starting unless it's a login shell, and $HOME is not being changed by default with sudo). So, I am suspecting that your .bashrc has some control structure to it and the PS1 setting line is not being executed. You could add some debugging to it, maybe a set -x in the beginning.


Hmm. I don't have a .bashrc, I use .bash_profile. I used to use .bashrc, but at some time I changed to .bash_profile, don't remember why, must have been some sort of problem??
So I'll add a .bashrc file to my home. BTW, my entire .bash_profile reads

# additional search paths for executables
PATH=$PATH:/usr/local/sbin:/usr/local/scripts:/usr/X11R6/bin:/sw/bin:/sw/sbin:/D eveloper/Tools
export PATH

# Define prompt
export PS1="\h:\w \u\$ "

$PATH works, so I don't think that $PS1 isn't read. I'd rather say that .bash_profile isn't reread when sudoing? I'll try it out.

Tina

That's odd. I get the correct $PATH when doing sudo bash (as it is set inmy user's .bash_profile), but not the prompt. Still, I don't think that $PS1 is not read correctly when sudoing, since it is correct for my normal user's shell.

Message was edited by: Tina Siegenthaler

Nov 28, 2007 1:31 PM in response to Tina Siegenthaler

.bash_profile (or .bash_login) is only read if the shell is a login shell. Do a shopt | grep login_shell to find out if it is. So when the new bash for sudo starts it won't find any bashrc to read, and it won't touch .bash_profile or .bash_login. You can do a simple

<pre class=command>. ~/.bash_profile</pre>

from your .bashrc so you don't have to keep two files up-to-date. PATH is probably inherited from your original shell. BTW, there is no need to export PS1 that I know of.

Let's see if you can kick me up into Level 3 :-D

Message was edited by: jarik

Message was edited by: jarik

Nov 28, 2007 1:34 PM in response to jarik

You can do a simple . .bash_profile from your .bashrc so you don't have to keep two files up-to-date.


Good idea, since I dont' remember why and when I started using .bash_profile, I should probably keep it. There must have been some reason why I switched (I guess...).

BTW, there is no need to export PS1 that I know of.


Errm, I have to confess I got that line out of some book (well, edited it a bit, but left the "export"), and it worked...

Let's see if you can kick me up into Level 3 :-D

May well be possible ;-))

Tina

Change bash prompt for root user (Leopard)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.