tags:

views:

44

answers:

1

The "echo" below is failing and I am too retarded to figure out why. I am simply trying to echo all of the array members in my loop.

#!/bin/bash
foo=bar1,bar2
for i in ${foo//,/" "}
do
declare -a ${i}='(null null null null)'
echo ${i[*]}
done

Thanks for any help!

+1  A: 

Try eval "echo \${$i[*]}"

Why are you doing it this way? There is probably an easier and more direct way to do whatever your ultimate goal is. If you describe it, perhaps we can help you avoid the unnecessary complication.

Dennis Williamson
This isn't right. See my answer.
Daenyth
That didn't work. Basically I have a bash script that takes a single argument. That argument is representative of $1=foo,bar,baz. I am parsing the argument as you can see using "${foo//,/" "}". I am then looping through each parsed result and creating an array for each result that is storing multiple members that represent certain conditions that I am testing for throughout my script, these conditions may or may not change which means my array members will potentially be updated at various intervals.
spaghettiwestern
That did the trick Dennis, thanks. You original suggestion of 'echo ${!i[*]}' did not work.
spaghettiwestern
@spaghettiwestern: Bash doesn't multidimensional arrays or arrays of arrays (without some complex gyrations). Can you store your members in a string and convert them to arrays only when they're needed or iterate over them as "words"?
Dennis Williamson
Dennis,Thanks for you help it is much appreciated. After learning and using python over the last couple of months it makes it hard to go back to bash. I would use python if I could in this situation. I know I am pushing the limits with my bash script but I think I have gotten over most of the hurdles. I really only need a single dimensional array so in theory it shouldn't be too big of a deal. I could do the string method as you suggest, and I might just do that but I personally think using arrays will be much cleaner and easier to read if I can get it to work.
spaghettiwestern