# Create array
arrayLong=(one two three four)
for element in "${arrayLong[@]}"
do
echo "$element"
done
echo "${#arrayLong[@]}"
Output:
one
two
three
four
4
Then:
# Make new array with only first half of values
arrayShort=("${arrayLong[@]:0:2}")
for element in "${arrayShort[@]}"
do
echo "$element"
done
echo "${#arrayShort[@]}"
The output of this is
one two
1
Why is my short array not actually an array? It's just one element. How can I split my array when the results are full on arrays?
My bash version is GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
arrayShort=(${arrayLong[@]:0:2}). Can you also post the output of:echo "${IFS@Q}"GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)declare -p arrayShortand see what that says (I getdeclare -a arrayShort='([0]="one" [1]="two")').arrayShortisdeclare -a arrayShort='([0]="one two")'. My suspicion is that hisIFSdoes not include a space.