Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > macOS > Leopard: .bash_profile and .bashrc are not sourced when sudo su -?

Leopard: .bash_profile and .bashrc are not sourced when sudo su -?
Thread Tools
fortepianissimo
Senior User
Join Date: Nov 2002
Location: US
Status: Offline
Reply With Quote
Oct 28, 2007, 08:34 PM
 
Can anyone confirm .bash_profile/.bashrc are not sourced when sudo su - into the root account? Strangely there's no such problem for a normal user account. Thanks!
     
besson3c
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Oct 28, 2007, 08:48 PM
 
What is the difference between sudo su -, su, or su - root?
     
rehoot
Dedicated MacNNer
Join Date: Nov 2005
Status: Offline
Reply With Quote
Oct 28, 2007, 08:50 PM
 
I don't use "sudo su". Are you typing "sudo su root" ?

The home directory for my root user ID (when I use "login root") is /var/root. Try putting any profile files there.
Mac Pro Quad: 2.66GHz; 4 GB Ram; 4x500GB drives; Radeon X1900, 23" Cinema Screen, APC UPS
PowerBook G4: 1.33GHz; 768MB Ram; 60GB drive
     
Simon
Posting Junkie
Join Date: Nov 2000
Location: in front of my Mac
Status: Offline
Reply With Quote
Oct 29, 2007, 04:59 AM
 
.bash_profile gets sourced on login sessions, but not otherwise - that's where .bashrc comes in.

Try putting your .bash_profile in /var/root/ and using su. Then do a xterm and see if the .bashrc in /var/root/ gets sourced. Use echo commands to notify you of what is being sourced when.
     
fortepianissimo  (op)
Senior User
Join Date: Nov 2002
Location: US
Status: Offline
Reply With Quote
Oct 29, 2007, 09:13 PM
 
Originally Posted by Simon View Post
.bash_profile gets sourced on login sessions, but not otherwise - that's where .bashrc comes in.

Try putting your .bash_profile in /var/root/ and using su. Then do a xterm and see if the .bashrc in /var/root/ gets sourced. Use echo commands to notify you of what is being sourced when.
My .bash_profile is indeed in /var/root, and by "su -" I should get a login session. I placed an echo inside .bash_profile and confirmed it's not sourced.

Weird thing is this same .bash_profile worked fine under Tiger, and it works fine with a non-root user in Leopard. Anything changed under the hood that I missed?

Thanks!
     
Hal Itosis
Grizzled Veteran
Join Date: Mar 2004
Status: Offline
Reply With Quote
Oct 29, 2007, 09:26 PM
 
Originally Posted by fortepianissimo View Post
My .bash_profile is indeed in /var/root, and by "su -" I should get a login session. I placed an echo inside .bash_profile and confirmed it's not sourced.

Weird thing is this same .bash_profile worked fine under Tiger, and it works fine with a non-root user in Leopard. Anything changed under the hood that I missed?
 
In /var/root, try .profile instead of .bash_profile

BTW, re: post #1... I'm lost:
If you have root, why are you using "sudo"?
What is the need? What is the ultimate goal?
-HI-
     
fortepianissimo  (op)
Senior User
Join Date: Nov 2002
Location: US
Status: Offline
Reply With Quote
Oct 30, 2007, 12:38 AM
 
Originally Posted by Hal Itosis View Post
 
In /var/root, try .profile instead of .bash_profile

BTW, re: post #1... I'm lost:
If you have root, why are you using "sudo"?
What is the need? What is the ultimate goal?
Thanks - indeed .profile works. But "man bash" didn't mention this? Where is it described?

About the goal for doing sudo su -. It's just for me to occasionally fire off fink command or do some other system-wide installation etc.

Thanks!
     
rehoot
Dedicated MacNNer
Join Date: Nov 2005
Status: Offline
Reply With Quote
Oct 30, 2007, 01:29 AM
 
Originally Posted by fortepianissimo View Post
Thanks - indeed .profile works. But "man bash" didn't mention this? Where is it described?

Thanks!
At the moment, I'm on my G4 with OS 10.3.9, and ".profile" is in the man page. To find it, type this:

/\.profile

The first slash is for the find command, and the second is the prefix so the period isn't considered a wild card.

Here is what is says in 1.3.9:

When bash is invoked as an interactive login shell, or as a non-inter-
active shell with the --login option, it first reads and executes com-
mands from the file /etc/profile, if that file exists. After reading
that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile,
in that order, and reads and executes commands from the first one that
exists and is readable. The --noprofile option may be used when the
shell is started to inhibit this behavior.
Mac Pro Quad: 2.66GHz; 4 GB Ram; 4x500GB drives; Radeon X1900, 23" Cinema Screen, APC UPS
PowerBook G4: 1.33GHz; 768MB Ram; 60GB drive
     
besson3c
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Oct 30, 2007, 01:56 AM
 
AFAIK,

su = switch user
su - username = switch user to that username and transfer current environment variables
sudo <command> = execute this command as root (requires you to be set as an admin in OS X)
sudo su = execute the switch user command as root, this is a little weird - you don't need to be root to switch users.

As for .profile vs .bash_profile, I've never really taken the time to understand this. It's a little bit unusual, and to add insult to injury for some reason my experiences always seem a little different on different Unix/Linux OSes... Maybe this is due to OS X Terminal environment variables? I'm not sure if iTerm would use these same variables...

I bet you could symlink .profile to .bash_profile and it would work fine...
     
fortepianissimo  (op)
Senior User
Join Date: Nov 2002
Location: US
Status: Offline
Reply With Quote
Oct 30, 2007, 08:36 AM
 
Originally Posted by rehoot View Post
At the moment, I'm on my G4 with OS 10.3.9, and ".profile" is in the man page. To find it, type this:

/\.profile

The first slash is for the find command, and the second is the prefix so the period isn't considered a wild card.

Here is what is says in 1.3.9:

When bash is invoked as an interactive login shell, or as a non-inter-
active shell with the --login option, it first reads and executes com-
mands from the file /etc/profile, if that file exists. After reading
that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile,
in that order, and reads and executes commands from the first one that
exists and is readable. The --noprofile option may be used when the
shell is started to inhibit this behavior.
Yes I did read that - but it didn't explain why it bypassed ~/.bash_profile for root, and only takes ~/.profile.
     
fortepianissimo  (op)
Senior User
Join Date: Nov 2002
Location: US
Status: Offline
Reply With Quote
Oct 30, 2007, 08:37 AM
 
Originally Posted by besson3c View Post
AFAIK,

su = switch user
su - username = switch user to that username and transfer current environment variables
sudo <command> = execute this command as root (requires you to be set as an admin in OS X)
sudo su = execute the switch user command as root, this is a little weird - you don't need to be root to switch users.

As for .profile vs .bash_profile, I've never really taken the time to understand this. It's a little bit unusual, and to add insult to injury for some reason my experiences always seem a little different on different Unix/Linux OSes... Maybe this is due to OS X Terminal environment variables? I'm not sure if iTerm would use these same variables...

I bet you could symlink .profile to .bash_profile and it would work fine...
Indeed I ended up "ln -s .bash_profile .profile" and it works fine.
     
fortepianissimo  (op)
Senior User
Join Date: Nov 2002
Location: US
Status: Offline
Reply With Quote
Nov 10, 2007, 11:14 AM
 
An update and conclusion:

1. the shell for root is not bash, it's sh.

2. From man bash:

"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 a non-interactive shell with the --login option, it first attempts to read and execute commands from /etc/profile and ~/.profile, in that order."
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 10:26 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,