I think you are confused about the difference between sourcing and executing a script.
Executing a script means creating a new process, and running the program. The program can be a shell script, or any other type of program. As it is a sub process, any environmental variables changed in the program will not affect the shell.
Sourcing a script can only be used with a bash script (if you are running bash). It effectively types the commands in as if you did them. This is useful as it lets a script change environmental variables in the shell.
Running a script is simple, you just type in the path to the script. .
is the current directory. So ./script.sh
will execute the file script.sh
in the current directory. If the command is a single file (eg script.sh
), it will check all the folders in the PATH variable to find the script. Note that the current directory isn't in PATH, so you can't execute a file script.sh
in the current directory by running script.sh
, you need to run ./script.sh
(unless the current directory is in the PATH, eg you can run ls
while in the /bin
dir).
Sourcing a script doesn't use the PATH, and just searches for the path. Note that source
isn't a program - otherwise it wouldn't be able to change environmental variables in the current shell. It is actually a bash built in command. Search /bin
and /usr/bin
- you won't find a source
program there. So to source a file script.sh
in the current directory, you just use source script.sh
.
How does sudo interact with this? Well sudo takes a program, and executes it as root. Eg sudo ./script.sh
executes script.sh
in a sub process but running as root.
What does sudo source ./script.sh
do however? Remember source
isn't a program (rather a shell builtin)? Sudo expects a program name though, so it searches for a program named source
. It doesn't find one, and so fails. It isn't possible to source a file running as root, without creating a new subprocess, as you cannot change the runner of a program (in this case, bash) after it has started.
I'm not sure what you actually wanted, but hopefully this will clear it up for you.
Here is a concrete example. Make the file script.sh
in your current directory with the contents:
#!/bin/bash
export NEW_VAR="hello"
whoami
echo "Some text"
Make it executable with chmod +x script.sh
.
Now observe what happens with bash:
> ./script.sh
david
Some text
> echo $NEW_VAR
> sudo ./script.sh
root
Some text
> echo $NEW_VAR
> source script.sh
david
Some text
> echo $NEW_VAR
hello
> sudo source script.sh
sudo: source: command not found
setup.sh
does not have execute privileges, then you must explicitly interpret it with bash. This can be done asbash setup.sh
orsource setup.sh
or. setup.sh
. However, because the latter 2 are bash built-ins, only the 1st form can be used with sudo which requires an executable. You can usesudo which bash
andsudo which source
andsudo which .
to see whatsudo
will find. – Candescent./setup.sh
it would be a different story. – Outspansource
command (also a builtin). – Saadi