I have an install script that sets some environment variables. To run the script I have to use the source command.

The script has to be run as sudo because the environment variables need to be set for the root account.

The problem is you can't execute "sudo source installScript". How do I execute "source installScript" as sudo, or root user?

I already tried "sudo /bin/bash ./installScript".

Dani AI

Generated

You are running into two separate issues. First, source (aka the . command) is a shell builtin, not an external executable, so sudo source ... fails because there is no standalone source binary to run. Second, even if you run a subshell as root, that subshell cannot modify the environment of your current shell; a child process inherits a copy of the parent’s environment, not the other way around. (man7.org)

The clean approach is what @John A and were steering you toward: get a root shell, then source the script inside that shell. If you need root’s login environment, prefer a login shell via sudo -i (this is the sudo equivalent of su -). Example:

sudo -i
. /path/to/installScript

Or in one line:

sudo -i bash -lc '. /path/to/installScript'

-i starts a login shell that reads root’s login RC files; -s would start a root shell without the login semantics and may preserve parts of the invoking environment. Use . instead of source for better POSIX portability. (linuxman7.org, man7.org)

If the variables must persist for future root sessions (not just during this install), place the relevant export VAR=value lines into root’s startup files so they are loaded automatically: login shells read /etc/profile and then ~/.bash_profile or ~/.profile; interactive non-login shells read ~/.bashrc. Avoid blindly sourcing an installer into these files; copy only the specific exports you actually need. (gnu.org)

Recommended Answers

All 4 Replies

In order to accomplish that, you'll need a root shell. If your root account is enabled, you can authenticate yourself with 'su', otherwise just run "sudo bash" (or whatever shell you prefer) and enter your password.

Once you've obtained the root shell, you can run the source command and proceed with the install.

One thing to remember about 'sudo su' is that it's not a 'login shell' by default. If you use 'sudo su -l' ('sudo su -' for short) then you'll inherit all of the root user's environment variables, like the path and such.

Just curious though, why would you need to source the install script? If you're only running the one script, you shouldn't need to export any variables to your shell. Especially if you're running it with sudo, because in that environment, you're root just for that installscript, and then you're out and anything that was sourced doesn't matter.

Thanks!
-G

Thanks got it working now.

marked as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.