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
Reusing Code with Includes and Sourcing in bash
Bash

Reusing Code with Includes and Sourcing in bash

InfinityCoder February 20, 2017

There are a set of shell variable assignments that you would like to have common across a set of scripts that you are writing.

You tried putting this configuration information in its own script.

But when you run that script from within another script, the values don’t stick; e.g., your configuration is running in another shell, and when that shell exits, so do your values.

Is there some way to run that configuration script within the current shell?

Use the bash shell’s source command or POSIX single period (.) to read in the contents of that configuration file.

The lines of that file will be processed as if encountered in the current script.
Here’s an example of some configuration data:

1
2
3
4
5
$ cat myprefs.cfg
SCRATCH_DIR=/var/tmp
IMG_FMT=png
SND_FMT=ogg
$

It is just a simple script consisting of three assignments. Here’s another script, one that will use these values:

1
2
3
4
5
6
7
#
# use the user prefs
#
source $HOME/myprefs.cfg
cd ${SCRATCH_DIR:-/tmp}
echo You prefer $IMG_FMT image files
echo You prefer $SND_FMT sound files

and so forth.

The script that is going to use the configuration file uses the source command to read in the file.

It can also use a dot (.) in place of the word source. A dot is easy and quick to type, but hard to notice in a script or screen shot:

1
. $HOME/myprefs.cfg

You wouldn’t be the first person to look right past the dot and think that the script was just being executed.
bash also has a third syntax, one that comes from the input processor readline, a topic we will not get into here.

We’ll just say that an equivalent action can occur with this syntax:

1
$include $HOME/myprefs.cfg

provided that the file is in your search path (or else specify an explicit path) and that the file has execute permissions and, of course, read permission, too.

That dollar sign is not the command prompt, but part of the directive $include.
Sourcing is both a powerful and a dangerous feature of bash scripting.

It gives you a way to create a configuration file and then share that file among several scripts.

With that mechanism, you can change your configuration by editing one file, not several scripts.
The contents of the configuration file are not limited to simple variable assignment, however.

Any valid shell command is legal syntax, because when you source a file like this, it is simply getting its input from a different source, but it is still the bash shell processing bash commands.

Regardless of what shell commands are in that sourced file, for example loops or invoking other commands, it is all legitimate shell input and will be run as if it were part of your script.
Here’s a modified configuration file:

1
2
3
4
5
6
7
8
9
10
11
$ cat myprefs.cfg
SCRATCH_DIR=/var/tmp
IMG_FMT=$(cat $HOME/myimage.pref)
if [ -e /media/mp3 ]
then
    SND_FMT=mp3
else
    SND_FMT=ogg
fi
echo config file loaded
$

This configuration file is hardly what one thinks of as a passive list of configured variables.

It can run other commands (e.g., cat) and use if statements to vary its choices. It even ends by echoing a message. Be careful when you source something, as it’s a wide open door into your script.
One of the best uses of sourcing scripts comes when you can define bash functions.

These functions can then be shared as a common library of functions among all the scripts that source the script of function definitions.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Customizing Your Prompt in bash
The default bash prompt is usually something uninformative that ends …

Customizing Your Prompt in bash

Getting bash for Windows in bash
You want to get bash for your Windows system, or …

Getting bash for Windows 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