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?
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.
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
fileRead me for better understanding of
.bash_profile
and.bashrc
Update
Example: