Bash scripting: Source and export

A key issue we encountered in batch scripting our analyses was how to make variables that are defined in one script available for another. 

Two ways of manipulating which scripts can access which variables are by changing from using 'sh' to 'source' when calling a script, or by using 'export' when defining a variable. 

The 'source' command can be used instead of the sh command to call scripts.  Sourcing a script essentially runs the text in the sourced script as if it had been copied and pasted into the calling script.  Variables that are defined within the sourced script are therefore available within the script that calls them.  Note that variables defined in this manner will not be available within the shell once your script has run.

In contrast, when variables are defined with the 'export' command, they are made available to all processes you run within that shell (terminal).

This is relevant for MRI analyses when you use a shell script to batch analyses. You may want to define images in one script (e.g., raw MPRage, T2, EPI runs etc.), then use and alter them (and define other variables) in subsequent scripts.  While this could be done using the 'export' command, once you begin batching multiple subjects you may end up with cases where data defined for a prior patient is carried to subsequent patient analyses.

This can be addressed by using the 'source' command (and not exporting variables), and controlling the availability of variables within nested 'job', 'setup' and 'task' scripts.

By running your subject's analysis in a separate process (job.sh) and not using export, you can contain all defined variables exclusively within that (job) process, and they are effectively cleared when job.sh is called for the next patient to be run.

# Script 1: batch.sh
# This script does not have access to variables within the job.sh script/process.
# Variables defined in this script are available, however, to the job.sh process.

for cases in SUB1 SUB2 SUB3 ; do
   sh job.sh

# Script 2: job.sh
# All variables defined in setup.sh are available to all other called scripts,
# but are effectively cleared once this script ends.
# This script must be called with 'sh' to start a new process.

source setup.sh.   #Script defining key variables (T1, T2, EPI runs etc.)
source task1.sh.    #Script running skull-stripping
source task2.sh.   #Script running analyses