Skip to content

Instantly share code, notes, and snippets.

@jamiel
Created July 12, 2012 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiel/3096567 to your computer and use it in GitHub Desktop.
Save jamiel/3096567 to your computer and use it in GitHub Desktop.
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