Tell me more ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I've been updating some of the default profile for bash, and saw from the tutorials I was following that I could reload the new profile with the new environment settings by using:

source /etc/bash.bashrc

The only thing is - the new environment variables were only available to my current user - and were ignored when I used sudo. They only became available to sudo when I closed my terminal session and rejoined.

When I try to use:

sudo source /etc/bash.bashrc

I get the error:

sudo: source: command not found

Is there a simple way to load in the new bash profile settings for sudo without having to close the terminal and restart?

-- Initially, I was using some installer scripts which referenced the variables. I found that while they could access the variables when I called the scripts directly (although, this would cause a later problem with creating directories as I needed to be root), calling the install scripts using sudo wouldn't.

I proved this by testing with these simple commands:

echo $ENV_VARIABLE
sudo echo $ENV_VARIABLE

The first would output the variable's value, but the second wouldn't output anything.

share|improve this question
How did you try to use the variables from sudo ? Please note that if you use "sudo command $variable" it will replace the variable from your shell, not from sudo's environment. – João Pinto Jan 10 '11 at 22:57

3 Answers

up vote 6 down vote accepted

The problem is that source is a bash build-in command (not a program - like ls or grep). I think one approach is to login as root and then execute the source command.

sudo -s
source /etc/bash.bashrc
share|improve this answer
2  
You're right that the problem is that source is a shell builtin. sudo su is a kind of weird way to say it -- better to just say sudo -s which is sudo's own way of saying "start a shell as this user." Your one-line version won't work because each of the commands in it is run by the main user's shell in a separate subprocess. – poolie Jan 10 '11 at 22:58
1  
Right. Plus BASH reads /etc/bashrc at login time. So you may as well use 'su' with -, -l, or --login switch in order to get the environment of that user: 'sudo su -' to become root or 'su - $username' to become another user. – user8290 Jan 10 '11 at 23:21
The "one line" example won't work because su starts a new shell and "source" is run only after it terminates. The first example only works if the second line used inside the root shell. – loevborg Jan 11 '11 at 9:41
Thanks for the comments guys! – Marcos Roriz Jan 11 '11 at 13:53
sudo -s is no better than sudo su. It won't have any effect either way. – loevborg Jan 11 '11 at 13:58
show 1 more comment

As Marcos says, your main problem here is that source is a shell builtin command that affects only the shell process in which it's run.

The easy solution is to just start a new shell as root, and bash will automatically read /etc/bash.bashrc when it starts. That's as simple as just saying

sudo bash
share|improve this answer

Closing and reopening the terminal should not change things. By default, sudo strips the environment. To disable that, add -E to sudo.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.