0

I have the following script named run.sh:

#This is run.sh

prerequisite(){

config_file=./../config/config.sh
source $config_file

    echo "VAR1 inside prerequisite: $VAR1"
    echo "country currency inside prerequisite: $country_currency[@]"
}

LIST_OF_COUNTRIES=`cat country_list.txt`

transform() {
    local country="$@"
    prerequisite
    echo "VAR1 inside transform: $VAR1"
    echo "country currency inside transform: $country_currency[@]"
}

transform ${LIST_OF_COUNTRIES[@]} &
transformpid=$!

wait $transformpid

echo "Complete"

The content of my config.sh file is:

VAR1="value1"
declare -a country_currency=( "INR" "USD" "JPY" )

When I run my run.sh file, I get the following output:

VAR1 inside prerequisite: value1
country currency inside prerequisite: INR USD JPY

VAR1 inside transform: value1
country currency inside transform:

As I see from the output, the value of all the variables that I am sourcing from my config .sh file is getting fetching into the prerequisite function. Whereas the values in the array are not getting fetched into the transform function. This is just a small snippet of code. I am doing multiple calls to the prerequisite function to dynamically source the values in my actual code. What am I doing wrong over here? Isn't the scope of the variable and the array that I have declared, supposed to be the same?

Djeah
  • 320
  • 8
  • 21
  • Source the file outside both functions - right above the call to transform – Timir May 10 '18 at 02:34
  • 3
    Possible duplicate of [Bash variable scope](https://stackoverflow.com/q/124167/608639), [Defining common variables across multiple scripts?](https://stackoverflow.com/q/27226650/608639), [How to export a variable in bash](https://stackoverflow.com/q/307120/608639), [How do I define a shell script variable to have scope outside of the script](https://stackoverflow.com/q/8153923/608639), etc. – jww May 10 '18 at 02:35
  • How are you getting output from the prerequisite functions when your script never calls it? – that other guy May 10 '18 at 02:47

0 Answers0