# sudo source ./myscript



## waiting_for_OSX (Mar 16, 2008)

Anyone know why this error message happens? Or better yet, how do I source a file using sudo?

$ sudo source ./myscript
sudo: source: command not found


----------



## artov (Mar 17, 2008)

Do you know what source command does ?

In Unix command shells have two kinds of commands: internal and external commands.
Actually external commands are other programs, where as internal commands are part
of the command set that the command shell implements (ok, some of the external 
commands actually also does that); like cd that sets current working directory etc.

You can execute a sequence of the commands either by entering them to the command
shell by hand or entering them to a file and giving them to the shell. When
you give a command, if it is not internal, it is given to the Unix (in this case, OS X) kernel, which checks what kind of file it is. If it is a text file, it's two 
first characters are checked. If they are #!, the kernel assumes that the line 
contains interpreter path (most likely the command shell, but it could some 
other, like Perl). Then kernel starts the command shell and gives the script 
to it, so the shell can execute the commands. (If the file was not text, kernel 
assumes it is a normal program and loads it and starts it.) In both cases, a 
new process is started, having several important features separate
to the command shell where you entered the command (file name).

Several shells have a command that loads a text file and reads each line as if they
are entered by yourself, using current shells' features (like current working directory,
environment variables etc.). Bourne shells call the command "source" (and csh ".").

Sudo command starts a command and executes it using different user account. If
no command is given, it starts command shell. "Current user" is thus changed. Since
this is one of most important process features, what do you thing sudo source does?
Keeps all other features the same but changes the "current user" or starts a new 
command shell as the new user, and runs the script using he's features?

Also, as I said, source is internal to bash and bourne shells. Should sudo guess you 
like to run the ./myscript using bash? And if you type "sudo . ./myscript", you need
csh shell?

Anyhow, either

$ sudo
source ./myscript
exit

or
$ sudo /bin/bash ./myscript


----------

