Article 5KSS9 store the echo'ed values into an array in bash script

store the echo'ed values into an array in bash script

by
linxbee
from LinuxQuestions.org on (#5KSS9)
Hi,
I am trying to put some content (only numbers) into a variable using cat command and then using an echo statement trying to put into an array.

But, looks like all the values are stored in 0th index only, instead of storing each element in a different index.

Below is my code and am running it using following command:

Quote:
bash array_exer.sh content.txt
#My content.txt has following data

Quote:
[JUMBO]$ cat content.txt
9:d=1 hl=4 l= 311 cons: SEQUENCE
449:d=1 hl=4 l= 539 cons: SEQUENCE
1362:d=1 hl=4 l= 411 cons: SEQUENCE
1975:d=1 hl=4 l= 639 cons: SEQUENCE
[JUMBO]$
#shell script for extracting numbers into an array
Code:#!/bin/sh
declare -a list

out=$(cat $1)

list=$(echo "$out" | cut -f1 -d:)

for (( i=0; i<${#list[@]}; i++ )); do
echo ${list[i]};
done

echo $list[0]#I am getting the output as following

Quote:
[JUMBO]$ bash array_exer.sh content.txt
9 449 1362 1975
9 449 1362 1975[0]
[JUMBO]$
How to properly store echo'ed values into an Array?latest?d=yIl2AUoC8zA latest?i=rEjEycp3Ex4:5k0Mb7k2kY0:F7zBnMy latest?i=rEjEycp3Ex4:5k0Mb7k2kY0:V_sGLiP latest?d=qj6IDK7rITs latest?i=rEjEycp3Ex4:5k0Mb7k2kY0:gIN9vFwrEjEycp3Ex4
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments