8

I'm using Ubuntu Natty.

I have a shell script which I have saved to /etc/init.d/qstart. The shell script contains the following:

apt-get -y update
apt-get -y upgrade
apt-get -y install tofrodos gcc make nmap lsof expect sysstat
dbpass="mydbpassword"

However, after I execute the script, I want to test that dbpass is set and I enter echo $dbpass in the prompt. But it's empty.

How do I define a variable in my shell script so that I can access it outside of it?!

Thanks in advance.

3
  • Environments variables ? mcsr.olemiss.edu/unixhelp/environment/env3d.html Commented Nov 16, 2011 at 15:16
  • @jeromeG I tried that but that doesn't work, it came up empty when I did DBPASS="mydbpassword" and then echo $DBPASS. Commented Nov 16, 2011 at 18:18
  • An init script is hardly the place for any of this. You should make a package which depends on those other packages, and updates /etc/environment or something similar in its postinst script. Commented Nov 16, 2011 at 20:23

2 Answers 2

14

You can't set variables in parent process's environment. You can only set your current process's environment or prepare an environment for your process's children.

That said, you can instruct your shell to run commands from a script in the current shell process rather than forking a new shell. You can do it like this:

source your_script.sh

or

. your_script.sh

(note the space after the dot). Since here commands inside your_script.sh are run by the current shell the changes made to the environment inside the script are retained.

However, if the shell running your script is not an ancestor of the shell in which you wish to use the environment variable then there is no way to achieve your goal using environment variables at all. For example, if you script is run at initialization by some childless shell all environment settings done there are irreversibly lost forever. In this case, use some other mechanism like a file (perhaps somewhere under /var).

If you want all instances of a given shell to have certain variables set in their environment you can use initialization scripts that most shells use. Usually, they have a system-wide and per-user initialization scripts. For example, bash uses /etc/profile as system-wide initialization script for interactive login shell and $HOME/.bash_profile (also $HOME/.bash_login and $HOME/.profile) as per-user initialization script. See this reference for bash-specific details. If you use a different shell, try its respective manual.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok. I was just trying to use the script to define passwords for a MySQL installation that is down the line in the overall script. I guess I'm going to have to find another way.
If you set a var in the invoking script, then the invoked script should see it. Make sure you use export.
A parent process could indeed set its children's environment, but init scripts are not parents of users' login shells.
2

You can't set environment variable for parent process in the child process. What you can do is source script.sh or . script.sh instead of executing it, in this case it's executed in the same bash process and all the changes are preserved.

Also, if you want to have the same variable set in your init script and elsewhere you may put it into /etc/default/something, which is more common. And finally you can source this file into bashrc or /etc/profile.d/something file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.