Sample Header Ad - 728x90

Bash create parameter named array within function

0 votes
1 answer
113 views
I'm attempting to write a function that writes arrays with a name that's passed in. Given the following bash function: function writeToArray { local name="$1" echo "$name" declare -a "$name" ${name}="does this work?" } Running like this: writeToArray $("test") I get two errors: bash: declare: `': not a valid identifier =does this work?: command not found I am expecting to be able to do this: writeToArray $("test") for item in "${test[@]}"; do echo "item" echo "$item" done This should print: item does this work? How could I properly configure this to write the array (named test in the example, such that this array named test is readable outside the function)?
Asked by Lee (549 rep)
Mar 8, 2023, 03:31 PM
Last activity: Mar 8, 2023, 08:30 PM