Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
$ source /etc/environment 

$ sudo source /etc/environment 
[sudo] password for t: 
sudo: source: command not found

It seems that a different shell than bash is run to execute source /etc/environment and that shell doesn't have source as builtin.

But my and the root's default shells are both bash.

$ echo $SHELL
/bin/bash

If sudo indeeds uses a different shell, why is it? I saw slm's reply, but don't understand in my case.

share|improve this question
3  
source is a shell builtin..you can not use sudo to run a shell builtin like an external command.. –  heemayl yesterday
    
Is the issue that you cannot read the file, or that you want the environment defined in the file to apply to subsequent sudo commands? –  Random832 19 hours ago
    
@Random832: want the environment defined in the file to apply to subsequent sudo commands –  Tim 11 hours ago

3 Answers 3

source is a shell builtin, so it cannot be executed without the shell. However, by default, sudo do not run shell. From sudo

Process model

When sudo runs a command, it calls fork(2), sets up the execution environment as described above, and calls the execve system call in the child process

If you want to explicitly execute shell, use -s option:

# sudo -s source /etc/environment

Which is still useless because after shell is exited, environment changes are lost.

share|improve this answer

In the realm of solving the problem rather than answering the question, here's the most obvious (to me) way to source a file which only root can read:

source <(sudo cat /etc/environment)

This uses process substitution. It takes the output of the cat command and turns it into a pseudo-file, which you can pass to source. source then runs the commands in the current shell.

Note that on most systems, /etc/environment is world-readable, so you ought to be able to just run this:

source /etc/environment
share|improve this answer

sudo expects a command but you are giving a shell builtin so it cannot find the command. If you write type source, you can see the output: source is a shell builtin and the output of which source is empty.

For example sudo strace will work and which strace will give output because strace is a command.

Edit: Also, you can see sudo su;sudo source /etc/environment works nicely so different shell is not used.

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.