Home > OS >  Unable to access Associative from config file
Unable to access Associative from config file

Time:02-01

I am using 2 different config file which I want to load in my script. Currently I can access the variables declared in one file(file.config) but not able to access the associative array declared in another file(file1.config). How can I configure so that I can access the other file too.

file.config & file1.config are present in the same directory as script

Script.sh

#Base Directory
base_dir=`pwd`

Funct () {
   . $base_dir/file.config
}

Funct1 () {
   . $base_dir/file1.config
}

#Calling function to load config file(file.config)
Funct
echo $var1

#Calling function to load config file(file1.config)
Funct1
echo ${assArray1[flower]}

file.config

var1=val1
var2=val2

file1.config

declare -A assArray1
assArray1[fruit]=Mango
assArray1[flower]=Rose

Output : val1

Problem : Cannot print Associative array element - echo ${assArray1[flower]}

CodePudding user response:

declare called within a function (Funct1) makes the respective variable local to that function. Therefore it's not accessible outside.

You can simply omit the declare in file1.config – or use declare -g as suggested by dan to declare a global variable.

  •  Tags:  
  • Related