InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Exporting Variables in bash
Bash

Exporting Variables in bash

InfinityCoder February 14, 2017

You defined a variable in one script, but when you called another script it didn’t know about the variable.

Export variables that you want to pass on to other scripts:

1
2
export MYVAR
export NAME=value

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:

1
2
3
4
5
6
7
export FNAME
export SIZE
export MAX
...
MAX=2048
SIZE=64
FNAME=/tmp/scratch

and at other times you’ll see:

1
2
3
4
5
6
7
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, as in:

1
VAL=$(anotherscript)

in order to change the value of $VAL.

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).

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Rewrapping Paragraphs in bash
You have some text with lines that are too long …

Rewrapping Paragraphs in bash

Displaying Error Messages When Failures Occur in bash
You need your shell script to be verbose about failures. …

Displaying Error Messages When Failures Occur in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap