You defined a variable in one script, but when you called another script it didn’t know about the variable.
Sometimes it’s a good thing that one script doesn’t know about the
other script’s variables. If you called a shell script from within a
for
loop in the first script, you
wouldn’t want the second script messing up the iterations of your
for
loop.
But sometimes you do want the information passed along. In those cases, you can export the variable so that its value is passed along to any other program that it invokes.
If you want to see a list of all the exported variables, just type
the built-in command env (or export -p
) for a list of each variable and its
value. All of these are available for your script when it runs. Many
have already been set up by the bash startup
scripts (see Chapter 16 for
more on configuring and customizing bash).
You can have the export statement just name the variable that will
be exported. Though the export statement can be put anywhere prior to
where you need the value to be exported, script writers often group
these export statements together like variable declarations at the front
of a script. You can also make the export
part of any variable assignment, though
that won’t work in old versions of the shell.
Once exported, you can assign repeatedly to the variable without exporting it each time. So, sometimes you’ll see statements like:
export FNAME export SIZE export MAX ... MAX=2048 SIZE=64 FNAME=/tmp/scratch
and at other times you’ll see:
export FNAME=/tmp/scratch export SIZE=64 export MAX=2048 ... FNAME=/tmp/scratch2 ... FNAME=/tmp/stillexported
One word of caution: the exported variables are, in effect, call by value. Changing the value of the exported value in the called script does not change that variable’s value back in the calling script.
This begs the question: “How would you pass back a changed value from the called script?” Answer: you can’t.
Is there a better answer? Unfortunately, there isn’t. You can only design your scripts so that they don’t need to do this. What mechanisms have people used to cope with this limitation?
One approach might be to have the called script echo its changed
value as output from the script, letting you read the output with the
resulting changed value. For example, suppose one script exports a
variable $VAL
and then calls another
script that modifies $VAL
. To get the
new value returned, you have to write the new value to standard out and
capture that value and assign it to $VAL
j, as in:
VAL=$(anotherscript)
in order to change the value of $VAL
(see Using Functions: Parameters and Return Values). You could
even change multiple values and echo them each in turn to standard out.
The calling program could then use a shell read
to capture each line of output one at a
time into the appropriate variables. This requires that the called
script produce no other output to standard out (at least not before or
among the variables), and sets up a very strong interdependency between
the scripts (not good from a maintenance standpoint).
help export
Chapter 16 for more information on configuring and customizing bash
3.15.14.98