Apple Event: May 7th at 7 am PT

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

Editing the Bash Profile for Root

I have a bunch of aliases and environment variables set in my bash profile that aren't being recognized by root. I assume it's because I've only edited the ~/.profile file and not the system-wide bash profile, but I've already tried editing /etc/profile and it still doesn't work.

For example, I have an alias set up so I can just type "textedit /path/to/file" instead of "/Applications/TextEdit.app/Contents/MacOS/TextEdit /path/to/file." When I try "sudo textedit" though, I get "command not found." The only way I'm able to get it to work is by logging in as root and sourcing the bash profile from my home folder. Is there a specific profile for the root account that I have to edit or something? How do I get this to work?

15" MacBook Pro, 2.16 GHz Core Duo, Mac OS X (10.5.6)

Posted on Feb 20, 2009 4:29 PM

Reply
Question marked as Best reply

Posted on Feb 20, 2009 4:56 PM

This is a question for the Mac OS X Technologies -> Unix forum.

If you want to edit the root's profile, then

sudo su -

and edit away, as you will be in the root directory's home directory.

But be VERY CAREFUL. If you screw up root, you may find that some system maintenance activities may fail.

You would be smarter to maybe create a very unique alias that just sources your /home/username/.profile

Another approach is to leave the root .profile alone, and create aliases for commands you want to execute with sudo

alias roottextedit='sudo open -a TextEdit '

then you do not need to edit root's .proflie

Future Unix questions should be asked in the Mac OS X Technologies -> Unix forum
10 replies
Question marked as Best reply

Feb 20, 2009 4:56 PM in response to Wes Moberly

This is a question for the Mac OS X Technologies -> Unix forum.

If you want to edit the root's profile, then

sudo su -

and edit away, as you will be in the root directory's home directory.

But be VERY CAREFUL. If you screw up root, you may find that some system maintenance activities may fail.

You would be smarter to maybe create a very unique alias that just sources your /home/username/.profile

Another approach is to leave the root .profile alone, and create aliases for commands you want to execute with sudo

alias roottextedit='sudo open -a TextEdit '

then you do not need to edit root's .proflie

Future Unix questions should be asked in the Mac OS X Technologies -> Unix forum

Feb 20, 2009 9:29 PM in response to BobHarris

@ Bob: Thanks, I actually didn't know there was a Unix forum, but next time I'll make sure I post there. I tried your first suggestion and created a .profile for the root user and it worked until I logged out of the root account. For some reason, using "sudo textedit" still gives me "command not found" and I have to log in with "sudo su -" or "sudo -i" for it to work. I could just create unique aliases for running things as root like you said, but I still wouldn't be able to use any of my environment variables without logging in as root.

For example, I was trying to build something from source yesterday which needed QT3 and looked at $QTDIR to tell it where to find the QT3 folder. After several failed attempts to build I figured out that when I ran "sudo make install" it wasn't finding the QT3 folder, so I had to log in as root and source my .profile, then build it. Obviously I'd rather not have to bother with any of that, I just can't figure out why it doesn't work with sudo.

Feb 20, 2009 9:39 PM in response to iBod

iBod wrote:
You know, I think that's the second time you've reminded me of that fact V.K. Eventually I'll learn...

🙂
I assume that means Bob's won't work either as the full path to the executable within the package isn't specified?

yes, that's right. what will work is the following added to /etc/profile or ~/.profile

alias roottextedit="sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit"

Feb 21, 2009 12:44 AM in response to Wes Moberly

Ok I've actually solved my own problem. After a ton of Googling I finally figured out that aliases and variables aren't passed to sudo and that there's no way to change that directly. I did find out how to create an alias that passes my other aliases and variables to sudo before running it, though. I just had to add this to my .profile:

alias sudo='A=`alias` sudo env '

Now everything works perfectly. Thanks everyone for your responses.

Feb 21, 2009 8:07 AM in response to Wes Moberly

Wes Moberly wrote:
Ok I've actually solved my own problem. After a ton of Googling I finally figured out that aliases and variables aren't passed to sudo and that there's no way to change that directly. I did find out how to create an alias that passes my other aliases and variables to sudo before running it, though. I just had to add this to my .profile:

alias sudo='A=`alias` sudo env '

That's pretty cool. Thanks for sharing!

Editing the Bash Profile for Root

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