I am experimenting with passing arrays to a shell script, as described in this question. I wrote a little script that is simply designed to take in the name of an array and print out the array:
#!/bin/bash
echo "$1"
echo "${!1}"
arrayVar=("${!1}")
echo "${arrayVar[1]}"
I declare an array variable in line with running my script, like so:
array=(foo bar test) ./test.sh array[@]
Output:
|array[@] # the bars are only here to force the final blank line
|(foo bar test)
|
It seems that array
, instead of actually being an array, is simply the string (foo bar test)
Even if I modify my script to echo array
directly by name instead of indirectly through the positional parameter, I get the same result.
#!/bin/bash
echo "$1"
arrayVar=("${!1}")
echo $arrayVar
echo "${arrayVar[1]}"
echo $array
echo "${array[1]}"
Output:
|array[@] # the bars are only here to force the final blank line
|(foo bar test)
|
|(foo bar test)
|
Am I simply doing something wrong, or does bash not support array assignments before a command?