'Export a variable from the function declare onto another script
So, I have the following variables (I am only showing a few of them as there are hundreds..)
> cat variables.txt
# Saved variables from the script (This file is only for illustration purposes)
br_int0_srxa = br-int0-xa
br_int1_srxa = br-int1-xa
br_int2_srxa = br-int2-xa
{...}
br_int0_srxb = br-int0-xb
{...}
br_wan3_srxd = br-wan3-xd
{...}
Each of those variables were set using the declare function declare br${H}=${br_name}
a.sh
Where:
# This is script a.sh
my_child_function ()
{
# i= Numbers from 0-7
# f= Alternating letters (a-d)
# br_name = the respective name given (This changes on each loop)
# THIS SCRIPT IS IN A LOOP, THAT HOW I GET DIFFERENT VARIABLES
# {START OF LOOP}
H=_int{$i}_srx{$f}
declare b{$H}=${br_name}
export b{$H}=${br_name} ## <<<<<<<<<<<<<< I tried using export to "export" the variable
# Saving the variable into the variables.txt file for illustration purposes
results= b{$H}=${br_name}
# Saving the result into variables.txt
echo $results >> variables.txt
# {END OF LOOP}
var_name=b{$H} # <<<<<<<<<<<<< Only renaming the declared variable
echo ${!var_name}
}
The previous script runs when I run main.sh:
main.sh
my_script ()
{
my_child_function
sleep 1
my_child_function2
}
. a.sh
. b.sh
b.sh
my_child_function2 ()
{
echo "This is script b.sh"
echo ${!var_name}
echo "End of script b.sh"
}
# Example of output when I run ./main.sh
br-int0-xa
This is script b.sh
End of script b.sh
As you can see I cannot export the variable that was saved using "declare" I tried adding the export function and nothing. I cannot use the variables stored from a.sh onto b.sh
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
