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.bashrcThe 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.bashrcI get the error:
sudo: source: command not foundIs 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_VARIABLEThe first would output the variable's value, but the second wouldn't output anything.
18 Answers
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 5 The problem is not that source is a shell builtin command. The fact that it is is what's actually throwing you the command not found error, but it doesn't mean it would work if it were.
The actual problem is how environment variables work. And they work like this:
every time a new process is started, if nothing happens, it inherits the environment of its parent. Due to this, using a subshell (e.g. typing bash inside a bash instance) and looking at the output of env should give similar results than its parent.
However, due to how sudo works (as stated in its manpage), sudo tries to strip the environment of the user and create a "default" environment for the supplanting user, so that the command run is run as if the user who invoked it had been the calling user (which is the expected behaviour), and thus running nautilus as sudo nautilus should open a folder at the /root folder, and not /home/yourusername.
So:
Doing something like sudo source script.sh and then sudo command, even if it worked, it wouldn't be successful at setting any variable to the later sudo command.
In order to pass environment variables, you can either tell sudo to preserve the environment (via the -E switch; and having appropriate permissions in your sudoers file) and/or setting it for the command as sudo VAR1=VALUE1 VAR2=VALUE2 command.
Using bash process substitution you can do:
source <(sudo cat /etc/bash.bashrc) 5 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 Closing and reopening the terminal should not change things. By default, sudo strips the environment. To disable that, add -E to sudo.
The error happens because the binary you are trying to call from command line is only part of the current user's PATH variable, but not a part of root user's PATH.
You can verify this by locating the path of the binary you are trying to access. In my case I was trying to call "bettercap-ng". So I ran,
$ which bettercap-ng
/home/user/work/bin/bettercap`I checked whether this location is part of my root user's PATH.
$ sudo env | grep ^PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/binSo sudo cannot find the binary that I am trying to call from commandline. Hence returns the error command not found.
You can direct sudo to use the current user's PATH when calling a binary like below.
sudo -E env "PATH=$PATH" [command] [arguments]In fact, one can make an alias out of it:
alias mysudo='sudo -E env "PATH=$PATH"'It's also possible to name the alias itself sudo, replacing the original sudo.
Some UNIX shell does not support source. instead, they support . So try this
. /etc/bash.bashrcHope it works
1It doesn't work because source is a built in command, not a program. I wrote a bash script to force sudo on built in commands:
#!/bin/bash
function forceSudo()
{ command="${@}" file="${@: -1}" if ! $command 2>/dev/null then permission=$(stat -c '%a' $file) sudo chmod o+rx $file result=$command 2>/dev/null sudo chmod $permission $file if ! $result then echo $result fi fi
}Save the file as forceSudo and save it in your scripts location, possibly ~/.local/bin. To avoid having to source the file before using the function, add alias forceSudo='unalias forceSudo && . forceSudo && forceSudo "$@"' to ~/.bashrc.
Now you can use forceSudo source /etc/bash.bashrc.