1

We know that exporting arrays in a canonical way is impossible, but I am interested in finding a workaround. I have a following scenario: a list of variables is loaded from a file to an array during startup, and I need to have that array visible to several bash scripts that may or may not be executed in the parent environment (. example.sh or just example.sh). I tried many things, but something like this seems as most promising:

export j=1
export array$j=something

And then I tried to access the value using:

echo ${array[$j]}      #doesn't work in child script
echo $(echo \$array$j) #displays the actual '$array1' instead of 'something'

Any suggestions?

1 Answer 1

2

You can look up values through indirection using parameter expansion:

j=1
array1=something

name="array$j"
echo "The value of $name is ${!name}"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.