I am passing an empty array to a function and within the function the array has one element but the element is empty.
#!/bin/bash
#ytest
#==============================================================
function ShowArray
{
echo "in ShowArray"
AlocalArray=("${!1}")
#alternatively you could do it like that
#echo "${AlocalArray[@]}" #DEBUG
echo "Showing content of array"
local iMax
iMax=${#AlocalArray[*]}
echo "ARRAYCOUNT: $iMax"
for ((iItem=0; iItem < iMax ; iItem++))
do
echo "ITEM: ${AlocalArray[$iItem]}"
done
}
declare -a AARRAY
echo "${AARRAY[@]}" #DEBUG
iMax=${#AARRAY[*]}
echo "HERE ARRAYCOUNT: $iMax ITEMS in ARRAY"
ShowArray "$AARRAY"
From the body of the script I get:
HERE ARRAYCOUNT: 0 ITEMS in ARRAY
From within the function I get:
in ShowArray
Showing content of array
ARRAYCOUNT: 1
ITEM:
What's wrong with my code? Thanks in advance.
Disclaimer: this demo script does nothing useful and serves only the purpose to demonstrate the misbehaviour.