I am trying to summarize multiple shell scripts within one large shell script, such as this:#!/bin/shbash script1.shbash script2.shbash script3.shAll scripts share the same variables var and var2. I wanted to delete the variable definitions from the individual shell scripts and put it into the large shell script instead, so that when I change the value of the variables I only have to do that once.However, when I do#!/bin/shvar1=1var2=2bash script1.shbash script2.shbash script3.shthe variables are not recognised by the references $var1 and $var2 in the individual shell scripts. Apologies if the question has been asked before, I am new to scripting in general and didn't know what to search for.It would also be nice to know how I could loop over the scripts with multiple variable inputs, such asvar1=1 2 3 4 5var2=a b c d efor i in $var1;do for j in $var2;doscript1.shscript2.shdone;done
Sharing variables across multiple shell scripts
bash;shell script
When you run scripts 1-3 inside your main script, they are each run inside their own sub-shell, which is why they don't recognise the variables defined in their parent shell. Use export to make variables available to sub-shells:#!/bin/shexport var1=1export var2=2bash script1.shbash script2.shbash script3.shAn alternative (relevant for your second question) would be to pass the variables to the scripts as positional parameters:#!/bin/shvar1=1var2=2bash script1.sh $var1 $var2bash script2.sh $var1 $var2bash script3.sh $var1 $var2Inside scripts 1-3, these variables would then be available as $1 and $2 respectively.