夏期劇場
夏期劇場

Reputation: 18337

MacOSX how to autorun a script after su - root?

Lets assume i am normal user, the i will switch to root:

user ~ $ su - root
Password: 
root ~ #

So once i logged in as root, i want to run following command automatically:

source .bash_profile

How can i have that above command run automatically please?

Upvotes: 0

Views: 1195

Answers (2)

Suku
Suku

Reputation: 3880

According to the bash man page, .bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.

In your case, you don't need to source .bash_profile like this.

You just need to put source .bash_profile in your root's .bashrc file

if [ -f ~/.bash_profile ]; then
   source ~/.bash_profile
fi

Read me for better understanding of .bash_profile and .bashrc

Update

Example:

[root@sgeorge-ld ~]# cat .bashrc | tail -1
echo "Testing .bashrc for a stack query"
[root@sgeorge-ld ~]# exit
logout
[sgeorge@sgeorge-ld ~]$ su - root
Password: 
Testing .bashrc for a stack query
[root@sgeorge-ld ~]# 

Upvotes: 1

Yaşar Şentürk
Yaşar Şentürk

Reputation: 515

First of all, when you switch to root user, you will be still your regular user's home directory. Which .bash_profile you want to execute? /Users/myuser/.bash_profile or root's /var/root/.bash_profile?

Regardless of what you would like to execute, you can edit /var/root/.bashrc (if you don't have it, create one) and add your command there.

Upvotes: 0

Related Questions