Skip to content

Instantly share code, notes, and snippets.

@jamiel jamiel/gist:3096567
Created Jul 12, 2012

Embed
What would you like to do?
Bash - Testing re-assigning of arrays
#!/bin/bash
#
# Test script to test bashes handling of re-assigned arrays
FOO[0]='foo';
FOO[1]='bar';
BAR=$FOO
echo "Loop main array"
for value in "${FOO[@]}"; do
echo $value
done
# Just returns first element
echo "Loop re-assigned array"
for value in "${BAR[@]}"; do
echo $value
done
# Doesn't work, prints first value
BAR=${FOO[@]}
echo "Loop assigned values array"
for value in "${BAR[@]}"; do
echo $value
done
# Long winded, but works
echo "Loop loop assigned array"
for key in ${!FOO[@]}; do
BAR[$key]=${FOO[$key]}
done
for value in "${BAR[@]}"; do
echo $value
done
# Would expect this to work but bad substitution error
BAR=FOO
echo "Loop variable variable"
for value in ${$BAR[@]}; do
echo $value
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.